Fix order location display.
[order_line_extra.git] / haxe / ItemScheduler.hx
1 import php.Lib;
2
3 enum Maybe<T> {
4         Nothing;
5         Just(v : T);
6 }
7
8 class QueryIterator<T> {
9         var result : Dynamic;
10         var nextValue : Maybe<T>;
11         public function new(result) {
12                 this.result= result;
13                 /* We fetch the first row , so we can answer hasNext */
14                 fetch();
15         }
16
17         private function fetch() {
18                 var next : Dynamic = untyped __call__('db_fetch', this.result);
19                 nextValue = if(next) Just(next) else Nothing;
20         }
21
22         public function  hasNext() : Bool {
23                 return this.nextValue != Nothing;
24         }
25
26         public function next() : T {
27                 switch(this.nextValue) {
28                 case Nothing : throw 'Iterator exhausted';
29                 case Just(v) :  {
30                         this.fetch();
31                         return v;
32                 };
33                 }
34         }
35 }
36 class FA {
37         static public function query(sql: String) {
38                 var result = untyped __call__('db_query', sql, sql);
39                 return new QueryIterator(result);
40         }
41
42         static public function tb() : String {
43                 return untyped __php__('TB_PREF');
44         }
45 }
46 class ItemScheduler {
47         var stock_id:String;
48         var startLocation:String;
49         var qoh: Int;
50         function new(stock_id: String, startLocation) {
51                 this.stock_id = stock_id;
52                 this.startLocation = startLocation;
53                 qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
54         }
55
56         function tableHeader() {
57                 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "Required Date"];
58         }
59
60 /*
61         function generateTablex(): Void {
62                 for(location in locations()) {
63                         formatLocation(location, null);
64                 }
65                 for(order in this.orders()) {
66                         this.formatRow(order);
67                 }
68         }
69 */
70
71         function generateTable(): Void {
72                         var startDate = Date.fromTime(0);
73                         for(order in orders()) {
74                                         var obj = php.Lib.objectOfAssociativeArray(order);
75
76                         }       
77
78                         // Sort location by datae
79                         var locations = this.locations();
80                         locations.sort(function(a, b) {
81                                 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
82                                 });
83
84                 // Get the start location, it should be the first one
85                 var locationIter = locations.iterator();
86                 var location  = locationIter.next();
87                 var qoh : Int = location.quantityOnHand(stock_id, null);
88                 var left = qoh;
89                 formatLocation(location, "Initial", left);
90
91                 // We display the order ordered by priority 
92                 // But insert the location when needed (meaning
93                 // when we run out of item available
94                 for(orderRow in orders()) {
95                         var order = php.Lib.objectOfAssociativeArray(orderRow);
96                         var quantity : Int = Std.parseInt(order.quantity);
97
98                         while(quantity > left && locationIter.hasNext()) {
99                                 location = locationIter.next();
100                                 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
101                                 if(quantityForLocation == null || quantityForLocation == 0) continue;
102                                 left += quantityForLocation;
103                                 formatLocation(location, "Delivery", left);
104                         }
105                         left -= quantity;
106
107                         formatOrder(order, left);
108                         
109                 }
110                         
111         }
112
113         function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
114                 php.Lib.print('<tr "'+attributes.join(' ')+'">');
115                 for(td in tds) {
116                         php.Lib.print('<td>');
117                         php.Lib.print(td);
118                         php.Lib.print('</td>');
119                 }
120                 php.Lib.print('</tr>');
121         }
122
123         function formatOrder(order : Dynamic, left : Int) {
124                         var attributes = [];
125                         var cells : Array <Dynamic> = [
126                                 order.order_id
127                                 ,order.deliver_to
128                                 ,order.quantity
129                                 ,left
130                                 ,left+order.quantity
131                                 ,order.from_stk_loc
132                                 ,order.delivery_date
133                         ];
134                         printRow(cells, attributes);
135
136         }
137
138         function formatRow(row) {
139                 var array = php.Lib.hashOfAssociativeArray(row);
140                 var quantity_before : Int = array.get('quantity_before');
141                 var quantity_available = qoh - quantity_before;
142                 var quantity: Int = array.get('quantity');
143
144                 var status : String = if(quantity_available < quantity) 'overduebg';
145
146                 var cells : Array<Dynamic> = [
147                         array.get('order_id')
148                         ,array.get('deliver_to')
149                         ,quantity
150                         ,quantity_available-quantity
151                         ,quantity_available
152                         ,array.get('from_stk_loc')
153                         ,array.get('delivery_date')
154                 ];
155
156                 php.Lib.print('<tr class="'+status+'">');
157                 for(cell in cells) {
158                         php.Lib.print('<td>');
159                         php.Lib.print(cell);
160                         php.Lib.print('</td>');
161                 }
162                 php.Lib.print('</tr>');
163                 
164                 
165         }
166
167         function formatLocation(location : Location, type: String,  left : Int) {
168                 var cells = [
169                         type
170                         ,location.name
171                         ,location.quantityOnHand(stock_id, null)
172                         ,left-location.quantityOnHand(stock_id, null)
173                         ,left
174                         ,location.code
175                         ,location.delivery
176                 ];
177                 var status = 'header';
178                 php.Lib.print('<tr class="'+status+'">');
179                 for(cell in cells) {
180                         php.Lib.print('<td>');
181                         php.Lib.print(cell);
182                         php.Lib.print('</td>');
183                 }
184                 php.Lib.print('</tr>');
185         }
186
187 /*
188         function schedules() {
189                 //return orders()+locations();
190                 //return  orders();
191                 return cast(locations(), Array<Dynamic>);
192
193         }
194 */
195
196         function orders() {
197                 var tb : String =  untyped __php__('TB_PREF');
198                 var sql : String = "SELECT *  
199                                                 FROM "+tb+"denorm_order_details_queue 
200                                                 JOIN  "+tb+"sales_orders ON (order_no = order_id)
201                                                 WHERE stock_id = '"+this.stock_id+"'
202                                                 ORDER by quantity_before";
203         
204                 return FA.query(sql);
205         }
206
207         function locations() {
208                 var TB = FA.tb();
209                 var sql = 'SELECT * 
210                                                         FROM '+TB+'locations';
211                 var _locs = [];
212                 for(row in FA.query(sql)) {
213                         var location = new Location(row);
214                         if(location.code == startLocation) {
215                          location.delivery =  Date.fromTime(0);
216                         }
217                         _locs.push(location);
218                 }
219
220                 return _locs;
221                 
222         }
223
224
225         function purcharseOrders()  {
226         }
227
228 }
229
230