c767bda6fd592e2b43a65c19251f81cddbcbb40a
[order_line_extra.git] / haxe / ItemScheduler.hx
1 import php.Lib;
2
3 typedef Detail = {
4         id : String,
5         quantity : Int,
6         position : Int,
7 }
8
9 class ScheduleParameters {
10         var row_id : String;
11         var rowDetails: Hash<Detail>;
12
13         function new(rawData : Dynamic) {
14                 var data  = php.Lib.hashOfAssociativeArray(rawData);
15                 row_id = data.get('row_id');
16                 var raw_order : Dynamic = data.get('row_order');
17
18                 var row_ids = php.Lib.toHaxeArray(raw_order);
19                 if (row_ids!= null) {
20                         rowDetails = new Hash<Detail>();
21                         var position = 1;
22                         for(id in row_ids) {
23                                 var d : Dynamic = data.get(id);
24                                 //var o = php.Lib.objectOfAssociativeArray(d);
25                                 php.Lib.dump(d);
26                                 php.Lib.print('<br>');
27
28                                 var quantity : Int = null;
29                                 if(d != null) {
30                                                 var o = php.Lib.objectOfAssociativeArray(d);
31                                                 quantity = Std.parseInt(o.quantity);
32                                 }
33                                 
34
35                                 rowDetails.set(id, {
36                                         id: id
37                                         ,quantity: quantity
38                                         ,position: position++
39                                         });
40                         }
41                 }
42                 //php.Lib.dump(rowDetails);
43         }
44
45 public function position(id: String) : Int {
46         return rowDetails == null ? rowDetails.get(id).position : null;
47
48 }
49 }
50
51
52 class ItemScheduler {
53         var stock_id:String;
54         var startLocation:String;
55         var qoh: Int;
56         function new(stock_id: String, startLocation, parameters : Null<ScheduleParameters>) {
57                 this.stock_id = stock_id;
58                 this.startLocation = startLocation;
59                 qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
60         }
61
62         function tableHeader() {
63                 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From",  "Required Date", "Comment"];
64         }
65
66         function generateTable(): Void {
67                 var startDate = Date.fromTime(0);
68
69                 // Sort location by datae
70                 var locations = this.locations();
71                 locations.sort(function(a, b) {
72                                 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
73                                 });
74
75                 // Get the start location, it should be the first one
76                 var locationIter = locations.iterator();
77                 var location  = locationIter.next();
78                 var qoh : Int = location.quantityOnHand(stock_id, null);
79                 var left = qoh;
80                 formatLocation(location, "Initial", left);
81
82                 // We display the order ordered by priority 
83                 // But insert the location when needed (meaning
84                 // when we run out of item available
85                 for(order in orders()) {
86                         var quantity : Int = Std.parseInt(order.quantity);
87
88                         while(0 > left && locationIter.hasNext()) {
89                                 location = locationIter.next();
90                                 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
91                                 if(quantityForLocation == null || quantityForLocation == 0) continue;
92                                 left += quantityForLocation;
93                                 formatLocation(location, "Delivery", left);
94                         }
95                         left -= quantity;
96
97                         formatOrder(order, left, location.delivery);
98
99                 }
100
101         }
102
103         function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
104                 php.Lib.print('<tr '+attributes.join(' ')+'>');
105                 var position : Int = 1;
106                 for(td in tds) {
107                         php.Lib.print('<td class="cell_'+position+'">');
108                         if(td) php.Lib.print(td);
109                         php.Lib.print('</td>');
110                         position++;
111                 }
112                 php.Lib.print('</tr>');
113         }
114
115         function formatOrder(order : Dynamic, left : Int, date : Date) {
116                 var row_id = 'order_'+order.id;
117                 var attributes = ['id = "'+row_id+'"'];
118                 var classes = [];
119                 var before : Int = left + order.quantity;
120                 /* We have basically 3 different cases;
121                  * - the order can be fullfilled
122                  * - the order can be partially 
123                  * - not at all
124                  */
125                 if (before < 0 ) {
126                         classes.push('soldout');
127                 }
128                 else if(left < 0) {
129                         classes.push('partial');
130                 }
131                 else {
132                         classes.push('full');
133                 }
134
135                 /* The order can also be late if we need
136                  * to wait for a delivery to get it
137                  */
138                 var required_by : Date = FA.sql2date(order.required_date);
139                 if(required_by == null) required_by = FA.sql2date(order.delivery_date);
140                 if(required_by.getTime() < date.getTime()) {
141                         classes.push('late');
142                 }
143                 else {
144                         classes.push('on_time');
145                 }
146                 var cells : Array <Dynamic> = [
147                         order.order_id
148                         , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
149                         ,'<input type="text" name="'+row_id+'[quantity]" value="'+order.quantity+'">'
150                         ,before
151                         ,left
152                         ,order.from_stk_loc
153                         ,order.delivery_date
154                         ,order.required_date 
155                         ,order.comment
156                         ];
157
158                 attributes.push('class="'+classes.join(' ')+'"');
159                 printRow(cells, attributes);
160
161         }
162
163         function formatLocation(location : Location, type: String,  left : Int) {
164                 var cells = [
165                         type
166                         ,location.name
167                         ,location.quantityOnHand(stock_id, null)
168                         ,left-location.quantityOnHand(stock_id, null)
169                         ,left
170                         ,location.code
171                         ,location.delivery
172                         ,""
173                         ,""
174                         ];
175
176                 printRow(cells, ['class = "tableheader location"', 'id = "loc_'+location.code+'"']);
177         }
178
179         /*
180                  function schedules() {
181 //return orders()+locations();
182 //return  orders();
183 return cast(locations(), Array<Dynamic>);
184
185 }
186          */
187
188 private function loadOrders() {
189         var tb : String =  untyped __php__('TB_PREF');
190         var sql : String = "SELECT *  
191                 FROM "+tb+"denorm_order_details_queue  d
192                 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
193                 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
194                 WHERE stock_id = '"+this.stock_id+"'
195                 AND od.trans_type = 30
196                 ORDER by d.priority";
197
198         return FA.query(sql);
199 }
200
201 function orders()  {
202         var rows = loadOrders();
203         var orderList = [];
204         for(row in rows) {
205                 var order = php.Lib.objectOfAssociativeArray(row);
206                 orderList.push(order);
207         };
208
209         return orderList;
210 }
211
212
213
214 function locations() {
215         var TB = FA.tb();
216         var sql = 'SELECT * 
217                 FROM '+TB+'locations';
218         var _locs = [];
219         for(row in FA.query(sql)) {
220                 var location = new Location(row);
221                 if(location.code == startLocation) {
222                         location.delivery =  Date.fromTime(0);
223                 }
224                 _locs.push(location);
225         }
226
227         return _locs;
228
229 }
230
231
232 function purcharseOrders()  {
233 }
234
235
236 }
237
238