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