e286bc0bc98277235b0cb498f0c7f5c54f99c50a
[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", "From",  "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(0 > 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, location.delivery);
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, date : Date) {
124                         var attributes = [];
125                         var classes = [];
126                         var before : Int = left + order.quantity;
127                         /* We have basically 3 different cases;
128                          * - the order can be fullfilled
129                          * - the order can be partially 
130                    * - not at all
131                          */
132                         if (before < 0 ) {
133                                         classes.push('soldout');
134                         }
135                         else if(left < 0) {
136                                         classes.push('partial');
137                         }
138                         else {
139                                         classes.push('full');
140                         }
141
142                         /* The order can also be late if we need
143                          * to wait for a delivery to get it
144                          */
145                         var cells : Array <Dynamic> = [
146                                 order.order_id
147                                 ,order.deliver_to
148                                 ,order.quantity
149                                 ,before
150                                 ,left
151                                 ,order.from_stk_loc
152                                 ,order.delivery_date
153                                 ,order.required_date 
154                                 ,order.comment
155                         ];
156
157                         attributes.push('class="'+classes.join(' ')+'"');
158                         printRow(cells, attributes);
159
160         }
161
162         function formatRow(row) {
163                 var array = php.Lib.hashOfAssociativeArray(row);
164                 var quantity_before : Int = array.get('quantity_before');
165                 var quantity_available = qoh - quantity_before;
166                 var quantity: Int = array.get('quantity');
167
168                 var status : String = if(quantity_available < quantity) 'overduebg';
169
170                 var cells : Array<Dynamic> = [
171                         array.get('order_id')
172                         ,array.get('deliver_to')
173                         ,quantity
174                         ,quantity_available-quantity
175                         ,quantity_available
176                         ,array.get('from_stk_loc')
177                         ,array.get('delivery_date')
178                 ];
179
180                 php.Lib.print('<tr class="'+status+'">');
181                 for(cell in cells) {
182                         php.Lib.print('<td>');
183                         php.Lib.print(cell);
184                         php.Lib.print('</td>');
185                 }
186                 php.Lib.print('</tr>');
187                 
188                 
189         }
190
191         function formatLocation(location : Location, type: String,  left : Int) {
192                 var cells = [
193                         type
194                         ,location.name
195                         ,location.quantityOnHand(stock_id, null)
196                         ,left-location.quantityOnHand(stock_id, null)
197                         ,left
198                         ,location.code
199                         ,location.delivery
200                 ];
201
202                 printRow(cells, ['class = "tableheader location"']);
203         }
204
205 /*
206         function schedules() {
207                 //return orders()+locations();
208                 //return  orders();
209                 return cast(locations(), Array<Dynamic>);
210
211         }
212 */
213
214         function orders() {
215                 var tb : String =  untyped __php__('TB_PREF');
216                 var sql : String = "SELECT *  
217                                                 FROM "+tb+"denorm_order_details_queue  d
218                                                 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
219                                                 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
220                                                 WHERE stock_id = '"+this.stock_id+"'
221                                                 AND od.trans_type = 30
222                                                 ORDER by quantity_before";
223         
224                 return FA.query(sql);
225         }
226
227         function locations() {
228                 var TB = FA.tb();
229                 var sql = 'SELECT * 
230                                                         FROM '+TB+'locations';
231                 var _locs = [];
232                 for(row in FA.query(sql)) {
233                         var location = new Location(row);
234                         if(location.code == startLocation) {
235                          location.delivery =  Date.fromTime(0);
236                         }
237                         _locs.push(location);
238                 }
239
240                 return _locs;
241                 
242         }
243
244
245         function purcharseOrders()  {
246         }
247
248 }
249
250