BulkUpdater work.
[order_line_extra.git] / haxe / ItemScheduler.hx
index 8ac7f6ced256c3605df2655bb810d71a92b59400..fb2cd4746a38d0a36a87f5010630288bf56f2019 100644 (file)
@@ -1,4 +1,5 @@
 import php.Lib;
+using DateTools;
 
 typedef Detail = {
        id : String,
@@ -89,7 +90,7 @@ class ItemScheduler {
                this.stock_id = stock_id;
                this.startLocation = startLocation;
                this.parameters = parameters;
-               qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
+               qoh =  untyped __call__('get_qoh_on_date', this.stock_id, startLocation);
        }
 
        function tableHeader() {
@@ -102,7 +103,13 @@ class ItemScheduler {
                // Sort location by date
                var locations = this.locations();
                locations.sort(function(a, b) {
-                               return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
+                       var as = a.delivery.getTime();
+                       var bs = b.delivery.getTime();
+                       if(as < bs) 
+                               return -1;
+                       else if(as > bs)
+                               return 1;
+                       else return 0;
                                });
 
                // Get the start location, it should be the first one
@@ -120,14 +127,16 @@ class ItemScheduler {
 
                        while(0 >= left && locationIter.hasNext()) {
                                location = locationIter.next();
-                               var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
-                               if(quantityForLocation == null || quantityForLocation == 0) continue;
+                               var quantityForLocation : Int = location.quantityOnHand(stock_id, null) + location.quantityOnOrder(stock_id);
+                               if(quantityForLocation == null || quantityForLocation == 0 || location.delivery == null) continue;
                                left += quantityForLocation;
                                formatLocation(location, "Delivery", left);
                        }
                        left -= quantity;
 
-                       formatOrder(order, left, location.delivery);
+                       var now = Date.now();
+                       //formatOrder(order, left, location.delivery); // now.getTime() > location.delivery ? now : location.delivery);
+                       formatOrder(order, left, now.getTime() > location.delivery.getTime() ? now : location.delivery);
 
                }
                // display the left locations
@@ -179,14 +188,24 @@ class ItemScheduler {
 
                /* The order can also be late if we need
                 * to wait for a delivery to get it
+                * or early if the item is on hold.
                 */
                var required_by : Date = FA.sql2date(order.required_date);
-               if(required_by == null) required_by = FA.sql2date(order.delivery_date);
-               if(required_by.getTime() < date.getTime()) {
+               if(required_by == null) required_by = FA.sql2date(order.expiry_date);
+               if(required_by != null && required_by.getTime() < date.getTime()) {
                        classes.push('late');
                }
                else {
-                       classes.push('on_time');
+                       var hold_until : Date = FA.sql2date(order.hold_until_date);
+                       php.Lib.print(hold_until);
+                       php.Lib.print(date);
+                       if(hold_until == null) hold_until = FA.sql2date(order.delivery_date);
+                       if(hold_until.getTime() > date.getTime()) {
+                               classes.push('early');
+                       }
+                       else {
+                               classes.push('on_time');
+                       }
                }
                var cells : Array <Dynamic> = [
                        order.order_id
@@ -195,7 +214,7 @@ class ItemScheduler {
                        ,before
                        ,left
                        ,order.from_stk_loc
-                       ,order.delivery_date
+                       ,order.hold_until_date
                        ,order.required_date 
                        ,order.comment
                        ];
@@ -213,7 +232,7 @@ class ItemScheduler {
                        ,left-location.quantityOnHand(stock_id, null)
                        ,left
                        ,location.code
-                       ,location.delivery
+                       ,location.delivery.getTime() == 0 ? '' : location.delivery.format("%F")
                        ,""
                        ,""
                        ];
@@ -232,7 +251,7 @@ return cast(locations(), Array<Dynamic>);
 
 private function loadOrders() {
        var tb : String =  untyped __php__('TB_PREF');
-       var sql : String = "SELECT *  , d.priority AS pp
+       var sql : String = "SELECT *  , d.quantity as qty,   d.priority AS pp
                FROM "+tb+"denorm_order_details_queue  d
                JOIN "+tb+"sales_order_details od ON (od.id = d.id)
                JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
@@ -252,6 +271,7 @@ function orders():Array<Order>  {
                var a:Dynamic = php.Lib.objectOfAssociativeArray(row);
                var order:Order = a;
                order.priority = Date.fromString(a.pp);
+               order.quantity = a.qty;
                orderList.push(order);
        };
 
@@ -277,6 +297,9 @@ function locations() {
                if(location.code == startLocation) {
                        location.delivery =  Date.fromTime(0);
                }
+               else if(location.delivery == null) {
+               continue;
+               }
                _locs.push(location);
        }
 
@@ -286,6 +309,17 @@ function locations() {
 
 
        function purcharseOrders()  {
+               var TB = FA.tb();
+               var sql = 'SELECT SUM(quantity_ordered - quantity_received) as quantity
+                       into_stock_location AS location
+                       FROM '+TB+'purch_order_details
+                       NATURAL JOIN '+TB+'purch_orders
+                       WHERE item_code = "'+this.stock_id+'"
+                       AND quantity_ordered > quantity_received
+                       GROUP by item_code,delivery_date, into_stock_location
+                       ORDER by delivery_date' ;
+
+               return  FA.query(sql);
        }
 
        public function needsUpdate():Bool {