Add header
[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 class ItemScheduler {
43         var stock_id:String;
44         var qoh: Int;
45         function new(stock_id: String) {
46                 this.stock_id = stock_id;
47                 qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
48         }
49
50         function tableHeader() {
51                 return ["Order", "Customer", "Quantity", "Left", "On Hand", "Loc", "Required Date"];
52         }
53
54         function generateTable(): Void {
55                 for(schedule in this.schedules()) {
56                         this.formatRow(schedule);
57                 }
58         }
59
60         function formatRow(row) {
61                 var array = php.Lib.hashOfAssociativeArray(row);
62                 var quantity_before : Int = array.get('quantity_before');
63                 var quantity_available = qoh - quantity_before;
64                 var quantity: Int = array.get('quantity');
65
66
67                 var status : String = if(quantity_available < quantity) 'overduebg';
68
69                 var cells : Array<Dynamic> = [
70                         array.get('order_id')
71                         ,array.get('deliver_to')
72                         ,quantity
73                         ,quantity_available-quantity
74                         ,quantity_available
75                         ,array.get('from_stk_loc')
76                         ,array.get('delivery_date')
77                 ];
78
79                 php.Lib.print('<tr class="'+status+'">');
80                 for(cell in cells) {
81                         php.Lib.print('<td>');
82                         php.Lib.print(cell);
83                         php.Lib.print('</td>');
84                 }
85                 php.Lib.print('</tr>');
86                 
87                 
88         }
89
90         function schedules() {
91                 var tb : String =  untyped __php__('TB_PREF');
92                 var sql : String = "SELECT *  
93                                                 FROM "+tb+"denorm_order_details_queue 
94                                                 JOIN  "+tb+"sales_orders ON (order_no = order_id)
95                                                 WHERE stock_id = '"+this.stock_id+"'
96                                                 ORDER by quantity_before";
97         
98                 return FA.query(sql);
99         }
100 }
101
102