23 class ScheduleParameters {
25 var rowDetails: Hash<Detail>;
26 var mode:ScheduleMode;
28 function new(rawData : Dynamic) {
29 var data = php.Lib.hashOfAssociativeArray(rawData);
30 row_id = data.get('row_id');
31 var raw_order : Dynamic = data.get('row_order');
33 var row_ids = php.Lib.toHaxeArray(raw_order);
35 rowDetails = new Hash<Detail>();
38 var d : Dynamic = data.get(id);
40 var quantity : Int = null;
42 var o = php.Lib.objectOfAssociativeArray(d);
43 quantity = Std.parseInt(o.quantity);
57 public function setMode(action:String) {
58 mode = switch(action) {
68 public function position(id: String) : Null<Int> {
69 if(rowDetails == null) return null;
70 return rowDetails.get(id).position;
74 public function priority(order : {id: String, priority: Int}) : Int {
75 var orderId = ItemScheduler.orderId(order);
76 var orderPosition = position(orderId);
77 return orderPosition != null ? orderPosition : order.priority;
84 var startLocation:String;
85 var parameters:ScheduleParameters;
87 function new(stock_id: String, startLocation, parameters : Null<ScheduleParameters>) {
88 this.stock_id = stock_id;
89 this.startLocation = startLocation;
90 this.parameters = parameters;
91 qoh = untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
94 function tableHeader() {
95 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"];
98 function generateTable(): Void {
99 var startDate = Date.fromTime(0);
101 // Sort location by date
102 var locations = this.locations();
103 locations.sort(function(a, b) {
104 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
107 // Get the start location, it should be the first one
108 var locationIter = locations.iterator();
109 var location = locationIter.next();
110 var qoh : Int = location.quantityOnHand(stock_id, null);
112 formatLocation(location, "Initial", left);
114 // We display the order ordered by priority
115 // But insert the location when needed (meaning
116 // when we run out of item available
117 for(order in orders()) {
118 var quantity : Int = Std.parseInt(order.quantity);
120 while(0 > left && locationIter.hasNext()) {
121 location = locationIter.next();
122 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
123 if(quantityForLocation == null || quantityForLocation == 0) continue;
124 left += quantityForLocation;
125 formatLocation(location, "Delivery", left);
129 formatOrder(order, left, location.delivery);
132 // display the left locations
133 while(0 > left && locationIter.hasNext()) {
134 location = locationIter.next();
135 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
136 if(quantityForLocation == null || quantityForLocation == 0) continue;
137 left += quantityForLocation;
138 formatLocation(location, "Delivery", left);
143 function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
144 php.Lib.print('<tr '+attributes.join(' ')+'>');
145 var position : Int = 1;
147 php.Lib.print('<td class="cell_'+position+'">');
148 if(td) php.Lib.print(td);
149 php.Lib.print('</td>');
152 php.Lib.print('</tr>');
155 static public function orderId(order) {
156 return 'order_'+order.id;
159 function formatOrder(order : Dynamic, left : Int, date : Date) {
160 var row_id = orderId(order);
161 var attributes = ['id = "'+row_id+'"'];
163 var before : Int = left + order.quantity;
164 /* We have basically 3 different cases;
165 * - the order can be fullfilled
166 * - the order can be partially
170 classes.push('soldout');
173 classes.push('partial');
176 classes.push('full');
179 /* The order can also be late if we need
180 * to wait for a delivery to get it
182 var required_by : Date = FA.sql2date(order.required_date);
183 if(required_by == null) required_by = FA.sql2date(order.delivery_date);
184 if(required_by.getTime() < date.getTime()) {
185 classes.push('late');
188 classes.push('on_time');
190 var cells : Array <Dynamic> = [
192 , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
193 ,'<input type="text" name="'+row_id+'[quantity]" value="'+order.quantity+'">'
202 attributes.push('class="'+classes.join(' ')+'"');
203 printRow(cells, attributes);
207 function formatLocation(location : Location, type: String, left : Int) {
211 ,location.quantityOnHand(stock_id, null)
212 ,left-location.quantityOnHand(stock_id, null)
220 printRow(cells, ['class = "tableheader location"', 'id = "loc_'+location.code+'"']);
224 function schedules() {
225 //return orders()+locations();
227 return cast(locations(), Array<Dynamic>);
232 private function loadOrders() {
233 var tb : String = untyped __php__('TB_PREF');
234 var sql : String = "SELECT *
235 FROM "+tb+"denorm_order_details_queue d
236 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
237 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
238 WHERE stock_id = '"+this.stock_id+"'
239 AND od.trans_type = 30
240 ORDER by d.priority";
242 return FA.query(sql);
245 function orders():Array<Order> {
246 var rows = loadOrders();
249 var order:Order = php.Lib.objectOfAssociativeArray(row);
250 orderList.push(order);
253 if(parameters != null) {
254 orderList.sort(function(a, b) { return parameters.priority(a)-parameters.priority(b); });
257 for(order in orderList) {
265 function locations() {
268 FROM '+TB+'locations';
270 for(row in FA.query(sql)) {
271 var location = new Location(row);
272 if(location.code == startLocation) {
273 location.delivery = Date.fromTime(0);
275 _locs.push(location);
283 function purcharseOrders() {