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