2244eeea5a004832f302eab55e32f2e7e5fb48b8
[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 qoh: Int;
49         function new(stock_id: String) {
50                 this.stock_id = stock_id;
51                 qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
52         }
53
54         function tableHeader() {
55                 return ["Order", "Customer", "Quantity", "Left", "On Hand", "Loc", "Required Date"];
56         }
57
58         function generateTable(): Void {
59                 for(location in locations()) {
60                         formatLocation(location);
61                 }
62                 for(order in this.orders()) {
63                         this.formatRow(order);
64                 }
65         }
66
67         function formatRow(row) {
68                 var array = php.Lib.hashOfAssociativeArray(row);
69                 var quantity_before : Int = array.get('quantity_before');
70                 var quantity_available = qoh - quantity_before;
71                 var quantity: Int = array.get('quantity');
72
73
74                 var status : String = if(quantity_available < quantity) 'overduebg';
75
76                 var cells : Array<Dynamic> = [
77                         array.get('order_id')
78                         ,array.get('deliver_to')
79                         ,quantity
80                         ,quantity_available-quantity
81                         ,quantity_available
82                         ,array.get('from_stk_loc')
83                         ,array.get('delivery_date')
84                 ];
85
86                 php.Lib.print('<tr class="'+status+'">');
87                 for(cell in cells) {
88                         php.Lib.print('<td>');
89                         php.Lib.print(cell);
90                         php.Lib.print('</td>');
91                 }
92                 php.Lib.print('</tr>');
93                 
94                 
95         }
96
97         function formatLocation(location : Location) {
98                 var cells = [
99                         location.code
100                         ,location.name
101                 ];
102                 var status = 'header';
103                 php.Lib.print('<tr class="'+status+'">');
104                 for(cell in cells) {
105                         php.Lib.print('<td>');
106                         php.Lib.print(cell);
107                         php.Lib.print('</td>');
108                 }
109                 php.Lib.print('</tr>');
110         }
111
112         function schedules() {
113                 //return orders()+locations();
114                 //return  orders();
115                 return cast(locations(), Array<Dynamic>);
116
117         }
118
119         function orders() {
120                 var tb : String =  untyped __php__('TB_PREF');
121                 var sql : String = "SELECT *  
122                                                 FROM "+tb+"denorm_order_details_queue 
123                                                 JOIN  "+tb+"sales_orders ON (order_no = order_id)
124                                                 WHERE stock_id = '"+this.stock_id+"'
125                                                 ORDER by quantity_before";
126         
127                 return FA.query(sql);
128         }
129
130         function locations() {
131                 var TB = FA.tb();
132                 var sql = 'SELECT * 
133                                                         FROM '+TB+'locations';
134                 var _locs = [];
135                 for(row in FA.query(sql)) {
136                         _locs.push(new Location(row));
137                 }
138
139                 return _locs;
140                 
141         }
142
143
144         function purcharseOrders()  {
145         }
146
147 }
148
149