Change color of partial After quantity.
[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         static public function sql2date(sqlDate:Dynamic) : Null<Date> {
47                 return sqlDate == null ? null : Date.fromString(sqlDate);
48         }
49 }
50 class ItemScheduler {
51         var stock_id:String;
52         var startLocation:String;
53         var qoh: Int;
54         function new(stock_id: String, startLocation) {
55                 this.stock_id = stock_id;
56                 this.startLocation = startLocation;
57                 qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
58         }
59
60         function tableHeader() {
61                 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From",  "Required Date", "Comment"];
62         }
63
64 /*
65         function generateTablex(): Void {
66                 for(location in locations()) {
67                         formatLocation(location, null);
68                 }
69                 for(order in this.orders()) {
70                         this.formatRow(order);
71                 }
72         }
73 */
74
75         function generateTable(): Void {
76                         var startDate = Date.fromTime(0);
77                         for(order in orders()) {
78                                         var obj = php.Lib.objectOfAssociativeArray(order);
79
80                         }       
81
82                         // Sort location by datae
83                         var locations = this.locations();
84                         locations.sort(function(a, b) {
85                                 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
86                                 });
87
88                 // Get the start location, it should be the first one
89                 var locationIter = locations.iterator();
90                 var location  = locationIter.next();
91                 var qoh : Int = location.quantityOnHand(stock_id, null);
92                 var left = qoh;
93                 formatLocation(location, "Initial", left);
94
95                 // We display the order ordered by priority 
96                 // But insert the location when needed (meaning
97                 // when we run out of item available
98                 for(orderRow in orders()) {
99                         var order = php.Lib.objectOfAssociativeArray(orderRow);
100                         var quantity : Int = Std.parseInt(order.quantity);
101
102                         while(0 > left && locationIter.hasNext()) {
103                                 location = locationIter.next();
104                                 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
105                                 if(quantityForLocation == null || quantityForLocation == 0) continue;
106                                 left += quantityForLocation;
107                                 formatLocation(location, "Delivery", left);
108                         }
109                         left -= quantity;
110
111                         formatOrder(order, left, location.delivery);
112                         
113                 }
114                         
115         }
116
117         function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
118                 php.Lib.print('<tr '+attributes.join(' ')+'>');
119                 for(td in tds) {
120                         php.Lib.print('<td>');
121                         if(td) php.Lib.print(td);
122                         php.Lib.print('</td>');
123                 }
124                 php.Lib.print('</tr>');
125         }
126
127         function formatOrder(order : Dynamic, left : Int, date : Date) {
128                         var attributes = [];
129                         var classes = [];
130                         var before : Int = left + order.quantity;
131                         /* We have basically 3 different cases;
132                          * - the order can be fullfilled
133                          * - the order can be partially 
134                    * - not at all
135                          */
136                         if (before < 0 ) {
137                                         classes.push('soldout');
138                         }
139                         else if(left < 0) {
140                                         classes.push('partial');
141                         }
142                         else {
143                                         classes.push('full');
144                         }
145
146                         /* The order can also be late if we need
147                          * to wait for a delivery to get it
148                          */
149                 var required_by : Date = FA.sql2date(order.required_date);
150                 if(required_by == null) required_by = FA.sql2date(order.delivery_date);
151                 if(required_by.getTime() < date.getTime()) {
152                         classes.push('late');
153                 }
154                 else {
155                         classes.push('on_time');
156                 }
157                         var cells : Array <Dynamic> = [
158                                 order.order_id
159                                 , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
160                                 ,order.quantity
161                                 ,before
162                                 ,left
163                                 ,order.from_stk_loc
164                                 ,order.delivery_date
165                                 ,order.required_date 
166                                 ,order.comment
167                         ];
168
169                         attributes.push('class="'+classes.join(' ')+'"');
170                         printRow(cells, attributes);
171
172         }
173
174         function formatRow(row) {
175                 var array = php.Lib.hashOfAssociativeArray(row);
176                 var quantity_before : Int = array.get('quantity_before');
177                 var quantity_available = qoh - quantity_before;
178                 var quantity: Int = array.get('quantity');
179
180                 var status : String = if(quantity_available < quantity) 'overduebg';
181
182                 var cells : Array<Dynamic> = [
183                         array.get('order_id')
184                         ,array.get('deliver_to')
185                         ,quantity
186                         ,quantity_available-quantity
187                         ,quantity_available
188                         ,array.get('from_stk_loc')
189                         ,array.get('delivery_date')
190                 ];
191
192                 php.Lib.print('<tr class="'+status+'">');
193                 for(cell in cells) {
194                         php.Lib.print('<td>');
195                         php.Lib.print(cell);
196                         php.Lib.print('</td>');
197                 }
198                 php.Lib.print('</tr>');
199                 
200                 
201         }
202
203         function formatLocation(location : Location, type: String,  left : Int) {
204                 var cells = [
205                         type
206                         ,location.name
207                         ,location.quantityOnHand(stock_id, null)
208                         ,left-location.quantityOnHand(stock_id, null)
209                         ,left
210                         ,location.code
211                         ,location.delivery
212                         ,""
213                         ,""
214                 ];
215
216                 printRow(cells, ['class = "tableheader location"']);
217         }
218
219 /*
220         function schedules() {
221                 //return orders()+locations();
222                 //return  orders();
223                 return cast(locations(), Array<Dynamic>);
224
225         }
226 */
227
228         function orders() {
229                 var tb : String =  untyped __php__('TB_PREF');
230                 var sql : String = "SELECT *  
231                                                 FROM "+tb+"denorm_order_details_queue  d
232                                                 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
233                                                 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
234                                                 WHERE stock_id = '"+this.stock_id+"'
235                                                 AND od.trans_type = 30
236                                                 ORDER by quantity_before";
237         
238                 return FA.query(sql);
239         }
240
241         function locations() {
242                 var TB = FA.tb();
243                 var sql = 'SELECT * 
244                                                         FROM '+TB+'locations';
245                 var _locs = [];
246                 for(row in FA.query(sql)) {
247                         var location = new Location(row);
248                         if(location.code == startLocation) {
249                          location.delivery =  Date.fromTime(0);
250                         }
251                         _locs.push(location);
252                 }
253
254                 return _locs;
255                 
256         }
257
258
259         function purcharseOrders()  {
260         }
261
262 }
263
264