Mix location and order
[order_line_extra.git] / haxe / ItemScheduler.hx
index 2ce0fa0783c3874237c4dd99f1e31383c02a2aa1..ea59cd19bb8d46e50d00748f1174360c5c92eb6a 100644 (file)
@@ -15,56 +15,158 @@ class QueryIterator<T> {
        }
 
        private function fetch() {
-               var next = untyped __call__('db_fetch', this.result);
-               this.nextValue = if(next) Just(next) else Nothing;
+               var next : Dynamic = untyped __call__('db_fetch', this.result);
+               nextValue = if(next) Just(next) else Nothing;
        }
 
        public function  hasNext() : Bool {
-               return this.next == Nothing;
+               return this.nextValue != Nothing;
        }
 
        public function next() : T {
                switch(this.nextValue) {
                case Nothing : throw 'Iterator exhausted';
-               case Just(v) : return v;
+               case Just(v) :  {
+                       this.fetch();
+                       return v;
+               };
                }
        }
 }
 class FA {
        static public function query(sql: String) {
-               var result = untyped __call__('db_query', sql);
-               return QueryIterator(result);
+               var result = untyped __call__('db_query', sql, sql);
+               return new QueryIterator(result);
+       }
+
+       static public function tb() : String {
+               return untyped __php__('TB_PREF');
        }
 }
 class ItemScheduler {
        var stock_id:String;
-       function new(stock_id: String) {
+       var startLocation:String;
+       var qoh: Int;
+       function new(stock_id: String, startLocation) {
                this.stock_id = stock_id;
+               this.startLocation = startLocation;
+               qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
        }
 
        function tableHeader() {
-               return ["A", "B", "C"];
+               return ["Order", "Customer", "Quantity", "Left", "On Hand", "Loc", "Required Date"];
        }
 
-       function generateTable(): Void {
-               for(schedule in this.schedules()) {
-                       this.formatRow(schedule);
+       function generateTablex(): Void {
+               for(location in locations()) {
+                       formatLocation(location);
+               }
+               for(order in this.orders()) {
+                       this.formatRow(order);
                }
        }
 
+       function generateTable(): Void {
+                       var schedules  = [];
+                       var startDate = Date.fromTime(0);
+                       for(order in orders()) {
+                                       var obj = php.Lib.objectOfAssociativeArray(order);
+                                       schedules.push({date: Date.fromString(obj.delivery_date), order:order, location:null});
+                       }       
+                       for(location in locations()) {
+                               schedules.push({date:  if(location.code == startLocation) startDate else location.delivery, order:null, location:location});
+                       }
+                       
+                       schedules.sort(function(a,b ) { return cast(a.date.getTime() - b.date.getTime(), Int) ; });
+                       for(schedule in schedules) {
+                               if(schedule.order != null) formatRow(schedule.order);
+                               if(schedule.location != null) formatLocation(schedule.location);
+                       }
+       }
+
        function formatRow(row) {
-               php.Lib.print('<tr><td>helli</td></tr>');
+               var array = php.Lib.hashOfAssociativeArray(row);
+               var quantity_before : Int = array.get('quantity_before');
+               var quantity_available = qoh - quantity_before;
+               var quantity: Int = array.get('quantity');
+
+               var status : String = if(quantity_available < quantity) 'overduebg';
+
+               var cells : Array<Dynamic> = [
+                       array.get('order_id')
+                       ,array.get('deliver_to')
+                       ,quantity
+                       ,quantity_available-quantity
+                       ,quantity_available
+                       ,array.get('from_stk_loc')
+                       ,array.get('delivery_date')
+               ];
+
+               php.Lib.print('<tr class="'+status+'">');
+               for(cell in cells) {
+                       php.Lib.print('<td>');
+                       php.Lib.print(cell);
+                       php.Lib.print('</td>');
+               }
+               php.Lib.print('</tr>');
+               
                
        }
 
+       function formatLocation(location : Location) {
+               var cells = [
+                       location.code
+                       ,location.name
+                       ,location.quantityOnHand(stock_id, null)
+                       ,location.delivery
+               ];
+               var status = 'header';
+               php.Lib.print('<tr class="'+status+'">');
+               for(cell in cells) {
+                       php.Lib.print('<td>');
+                       php.Lib.print(cell);
+                       php.Lib.print('</td>');
+               }
+               php.Lib.print('</tr>');
+       }
+
+/*
        function schedules() {
+               //return orders()+locations();
+               //return  orders();
+               return cast(locations(), Array<Dynamic>);
+
+       }
+*/
+
+       function orders() {
                var tb : String =  untyped __php__('TB_PREF');
                var sql : String = "SELECT *  
                                                FROM "+tb+"denorm_order_details_queue 
                                                JOIN  "+tb+"sales_orders ON (order_no = order_id)
-                                               WHERE stock_id = '"+this.stock_id+"'";
+                                               WHERE stock_id = '"+this.stock_id+"'
+                                               ORDER by quantity_before";
+       
                return FA.query(sql);
        }
+
+       function locations() {
+               var TB = FA.tb();
+               var sql = 'SELECT * 
+                                                       FROM '+TB+'locations';
+               var _locs = [];
+               for(row in FA.query(sql)) {
+                       _locs.push(new Location(row));
+               }
+
+               return _locs;
+               
+       }
+
+
+       function purcharseOrders()  {
+       }
+
 }