Mix location and 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);
63                 }
64                 for(order in this.orders()) {
65                         this.formatRow(order);
66                 }
67         }
68
69         function generateTable(): Void {
70                         var schedules  = [];
71                         var startDate = Date.fromTime(0);
72                         for(order in orders()) {
73                                         var obj = php.Lib.objectOfAssociativeArray(order);
74                                         schedules.push({date: Date.fromString(obj.delivery_date), order:order, location:null});
75                         }       
76                         for(location in locations()) {
77                                 schedules.push({date:  if(location.code == startLocation) startDate else location.delivery, order:null, location:location});
78                         }
79                         
80                         schedules.sort(function(a,b ) { return cast(a.date.getTime() - b.date.getTime(), Int) ; });
81                         for(schedule in schedules) {
82                                 if(schedule.order != null) formatRow(schedule.order);
83                                 if(schedule.location != null) formatLocation(schedule.location);
84                         }
85         }
86
87         function formatRow(row) {
88                 var array = php.Lib.hashOfAssociativeArray(row);
89                 var quantity_before : Int = array.get('quantity_before');
90                 var quantity_available = qoh - quantity_before;
91                 var quantity: Int = array.get('quantity');
92
93                 var status : String = if(quantity_available < quantity) 'overduebg';
94
95                 var cells : Array<Dynamic> = [
96                         array.get('order_id')
97                         ,array.get('deliver_to')
98                         ,quantity
99                         ,quantity_available-quantity
100                         ,quantity_available
101                         ,array.get('from_stk_loc')
102                         ,array.get('delivery_date')
103                 ];
104
105                 php.Lib.print('<tr class="'+status+'">');
106                 for(cell in cells) {
107                         php.Lib.print('<td>');
108                         php.Lib.print(cell);
109                         php.Lib.print('</td>');
110                 }
111                 php.Lib.print('</tr>');
112                 
113                 
114         }
115
116         function formatLocation(location : Location) {
117                 var cells = [
118                         location.code
119                         ,location.name
120                         ,location.quantityOnHand(stock_id, null)
121                         ,location.delivery
122                 ];
123                 var status = 'header';
124                 php.Lib.print('<tr class="'+status+'">');
125                 for(cell in cells) {
126                         php.Lib.print('<td>');
127                         php.Lib.print(cell);
128                         php.Lib.print('</td>');
129                 }
130                 php.Lib.print('</tr>');
131         }
132
133 /*
134         function schedules() {
135                 //return orders()+locations();
136                 //return  orders();
137                 return cast(locations(), Array<Dynamic>);
138
139         }
140 */
141
142         function orders() {
143                 var tb : String =  untyped __php__('TB_PREF');
144                 var sql : String = "SELECT *  
145                                                 FROM "+tb+"denorm_order_details_queue 
146                                                 JOIN  "+tb+"sales_orders ON (order_no = order_id)
147                                                 WHERE stock_id = '"+this.stock_id+"'
148                                                 ORDER by quantity_before";
149         
150                 return FA.query(sql);
151         }
152
153         function locations() {
154                 var TB = FA.tb();
155                 var sql = 'SELECT * 
156                                                         FROM '+TB+'locations';
157                 var _locs = [];
158                 for(row in FA.query(sql)) {
159                         _locs.push(new Location(row));
160                 }
161
162                 return _locs;
163                 
164         }
165
166
167         function purcharseOrders()  {
168         }
169
170 }
171
172