Display order and location in good order.
[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", "Left", "On Hand", "Loc", "Required Date"];
58         }
59
60         function generateTablex(): Void {
61                 for(location in locations()) {
62                         formatLocation(location, null);
63                 }
64                 for(order in this.orders()) {
65                         this.formatRow(order);
66                 }
67         }
68
69         function generateTable(): Void {
70                         var startDate = Date.fromTime(0);
71                         for(order in orders()) {
72                                         var obj = php.Lib.objectOfAssociativeArray(order);
73
74                         }       
75
76                         // Sort location by datae
77                         var locations = this.locations();
78                         locations.sort(function(a, b) {
79                                 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
80                                 });
81
82                 // Get the start location, it should be the first one
83                 var locationIter = locations.iterator();
84                 var location  = locationIter.next();
85                 var qoh : Int = location.quantityOnHand(stock_id, null);
86                 var left = qoh;
87                 formatLocation(location, left);
88
89                 // We display the order ordered by priority 
90                 // But insert the location when needed (meaning
91                 // when we run out of item available
92                 for(orderRow in orders()) {
93                         var order = php.Lib.objectOfAssociativeArray(orderRow);
94                         var quantity : Int = Std.parseInt(order.quantity);
95
96                         while(quantity > left && locationIter.hasNext()) {
97                                 location = locationIter.next();
98                                 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
99                                 if(quantityForLocation == null || quantityForLocation == 0) continue;
100                                 left += quantityForLocation;
101                                 trace(quantityForLocation);
102                                 formatLocation(location, left);
103                         }
104                         left -= quantity;
105
106                         formatOrder(order, left);
107                         
108                 }
109                         
110         }
111
112         function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
113                 php.Lib.print('<tr "'+attributes.join(' ')+'">');
114                 for(td in tds) {
115                         php.Lib.print('<td>');
116                         php.Lib.print(td);
117                         php.Lib.print('</td>');
118                 }
119                 php.Lib.print('</tr>');
120         }
121
122         function formatOrder(order : Dynamic, left : Int) {
123                         var attributes = [];
124                         var cells : Array <Dynamic> = [
125                                 order.order_id
126                                 ,order.deliver_to
127                                 ,order.quantity
128                                 ,left
129                                 ,left+order.quantity
130                                 ,order.from_stk_code
131                                 ,order.delivery_date
132                         ];
133                         printRow(cells, attributes);
134
135         }
136
137         function formatRow(row) {
138                 var array = php.Lib.hashOfAssociativeArray(row);
139                 var quantity_before : Int = array.get('quantity_before');
140                 var quantity_available = qoh - quantity_before;
141                 var quantity: Int = array.get('quantity');
142
143                 var status : String = if(quantity_available < quantity) 'overduebg';
144
145                 var cells : Array<Dynamic> = [
146                         array.get('order_id')
147                         ,array.get('deliver_to')
148                         ,quantity
149                         ,quantity_available-quantity
150                         ,quantity_available
151                         ,array.get('from_stk_loc')
152                         ,array.get('delivery_date')
153                 ];
154
155                 php.Lib.print('<tr class="'+status+'">');
156                 for(cell in cells) {
157                         php.Lib.print('<td>');
158                         php.Lib.print(cell);
159                         php.Lib.print('</td>');
160                 }
161                 php.Lib.print('</tr>');
162                 
163                 
164         }
165
166         function formatLocation(location : Location, left : Int) {
167                 var cells = [
168                         location.code
169                         ,location.name
170                         ,location.quantityOnHand(stock_id, null)
171                         ,left
172                         ,location.delivery
173                 ];
174                 var status = 'header';
175                 php.Lib.print('<tr class="'+status+'">');
176                 for(cell in cells) {
177                         php.Lib.print('<td>');
178                         php.Lib.print(cell);
179                         php.Lib.print('</td>');
180                 }
181                 php.Lib.print('</tr>');
182         }
183
184 /*
185         function schedules() {
186                 //return orders()+locations();
187                 //return  orders();
188                 return cast(locations(), Array<Dynamic>);
189
190         }
191 */
192
193         function orders() {
194                 var tb : String =  untyped __php__('TB_PREF');
195                 var sql : String = "SELECT *  
196                                                 FROM "+tb+"denorm_order_details_queue 
197                                                 JOIN  "+tb+"sales_orders ON (order_no = order_id)
198                                                 WHERE stock_id = '"+this.stock_id+"'
199                                                 ORDER by quantity_before";
200         
201                 return FA.query(sql);
202         }
203
204         function locations() {
205                 var TB = FA.tb();
206                 var sql = 'SELECT * 
207                                                         FROM '+TB+'locations';
208                 var _locs = [];
209                 for(row in FA.query(sql)) {
210                         var location = new Location(row);
211                         if(location.code == startLocation) {
212                          location.delivery =  Date.fromTime(0);
213                         }
214                         _locs.push(location);
215                 }
216
217                 return _locs;
218                 
219         }
220
221
222         function purcharseOrders()  {
223         }
224
225 }
226
227