Formatting table
[order_line_extra.git] / haxe / ItemScheduler.hx
index 92d0212e40bf74181cec1a0fa11486888e0f7ffc..0446983596a2f12764dfdbf93fc2745addcc8557 100644 (file)
 import php.Lib;
+
+enum Maybe<T> {
+       Nothing;
+       Just(v : T);
+}
+
+class QueryIterator<T> {
+       var result : Dynamic;
+       var nextValue : Maybe<T>;
+       public function new(result) {
+               this.result= result;
+               /* We fetch the first row , so we can answer hasNext */
+               fetch();
+       }
+
+       private function fetch() {
+               var next : Dynamic = untyped __call__('db_fetch', this.result);
+               nextValue = if(next) Just(next) else Nothing;
+       }
+
+       public function  hasNext() : Bool {
+               return this.nextValue != Nothing;
+       }
+
+       public function next() : T {
+               switch(this.nextValue) {
+               case Nothing : throw 'Iterator exhausted';
+               case Just(v) :  {
+                       this.fetch();
+                       return v;
+               };
+               }
+       }
+}
+class FA {
+       static public function query(sql: String) {
+               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", "Before", "After", "Loc", "Required Date"];
+       }
+
+/*
+       function generateTablex(): Void {
+               for(location in locations()) {
+                       formatLocation(location, null);
+               }
+               for(order in this.orders()) {
+                       this.formatRow(order);
+               }
        }
+*/
 
        function generateTable(): Void {
-               for(schedule in this.schedules()) {
-                       this.formatRow(schedule);
+                       var startDate = Date.fromTime(0);
+                       for(order in orders()) {
+                                       var obj = php.Lib.objectOfAssociativeArray(order);
+
+                       }       
+
+                       // Sort location by datae
+                       var locations = this.locations();
+                       locations.sort(function(a, b) {
+                               return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
+                               });
+
+               // Get the start location, it should be the first one
+               var locationIter = locations.iterator();
+               var location  = locationIter.next();
+               var qoh : Int = location.quantityOnHand(stock_id, null);
+               var left = qoh;
+               formatLocation(location, "Initial", left);
+
+               // We display the order ordered by priority 
+               // But insert the location when needed (meaning
+               // when we run out of item available
+               for(orderRow in orders()) {
+                       var order = php.Lib.objectOfAssociativeArray(orderRow);
+                       var quantity : Int = Std.parseInt(order.quantity);
+
+                       while(quantity > left && locationIter.hasNext()) {
+                               location = locationIter.next();
+                               var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
+                               if(quantityForLocation == null || quantityForLocation == 0) continue;
+                               left += quantityForLocation;
+                               formatLocation(location, "Delivery", left);
+                       }
+                       left -= quantity;
+
+                       formatOrder(order, left);
+                       
                }
+                       
+       }
+
+       function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
+               php.Lib.print('<tr "'+attributes.join(' ')+'">');
+               for(td in tds) {
+                       php.Lib.print('<td>');
+                       php.Lib.print(td);
+                       php.Lib.print('</td>');
+               }
+               php.Lib.print('</tr>');
+       }
+
+       function formatOrder(order : Dynamic, left : Int) {
+                       var attributes = [];
+                       var cells : Array <Dynamic> = [
+                               order.order_id
+                               ,order.deliver_to
+                               ,order.quantity
+                               ,left
+                               ,left+order.quantity
+                               ,order.from_stk_code
+                               ,order.delivery_date
+                       ];
+                       printRow(cells, attributes);
+
        }
 
        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, type: String,  left : Int) {
+               var cells = [
+                       type
+                       ,location.name
+                       ,location.quantityOnHand(stock_id, null)
+                       ,left-location.quantityOnHand(stock_id, null)
+                       ,left
+                       ,location.code
+                       ,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 ["a", "b", "c"];
+               //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+"'
+                                               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)) {
+                       var location = new Location(row);
+                       if(location.code == startLocation) {
+                        location.delivery =  Date.fromTime(0);
+                       }
+                       _locs.push(location);
+               }
+
+               return _locs;
+               
+       }
+
+
+       function purcharseOrders()  {
+       }
+
 }