Fix install hook returning error
[order_line_extra.git] / haxe / ItemScheduler.hx
index 75727a2091d62f179eaf39bf5ceebe9884e206b9..ab4e9ac83f78fd623696335ff6021d176cdb0b04 100644 (file)
@@ -9,19 +9,27 @@ typedef Detail = {
 typedef Order = {
                id:String
                ,quantity:String
-               ,priority:Int
+               ,priority:Date
                ,delivery_date:String
 
 }
 
+enum ScheduleMode {
+       Update;
+       Cancel;
+       Move;
+}
+
 class ScheduleParameters {
        var row_id : String;
        var rowDetails: Hash<Detail>;
+       public var mode:ScheduleMode;
 
        function new(rawData : Dynamic) {
                var data  = php.Lib.hashOfAssociativeArray(rawData);
                row_id = data.get('row_id');
                var raw_order : Dynamic = data.get('row_order');
+               mode = ScheduleMode.Move;
 
                var row_ids = php.Lib.toHaxeArray(raw_order);
                if (row_ids!= null) {
@@ -47,16 +55,27 @@ class ScheduleParameters {
                }
        }
 
+       public function setMode(action:String) {
+               mode = switch(action) {
+                       case "update" :
+                               ScheduleMode.Update;
+                       case "cancel" :
+                               ScheduleMode.Cancel;
+                       default:
+                               ScheduleMode.Move;
+               };
+       }
+
        public function position(id: String) : Null<Int> {
                if(rowDetails == null) return null;
                return rowDetails.get(id).position;
 
        }
 
-       public function priority(order : {id: String, priority: Int})  : Int {
+       public function priority(order : {id: String, priority: Date})  : Int {
                var orderId = ItemScheduler.orderId(order);
                var orderPosition = position(orderId);
-               return orderPosition != null  ? orderPosition : order.priority;
+               return orderPosition != null  ? orderPosition : cast(order.priority.getTime(), Int);
        }
 }
 
@@ -99,7 +118,7 @@ class ItemScheduler {
                for(order in orders()) {
                        var quantity : Int = Std.parseInt(order.quantity);
 
-                       while(0 > left && locationIter.hasNext()) {
+                       while(0 >= left && locationIter.hasNext()) {
                                location = locationIter.next();
                                var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
                                if(quantityForLocation == null || quantityForLocation == 0) continue;
@@ -112,7 +131,7 @@ class ItemScheduler {
 
                }
                // display the left locations
-                       while(0 > left && locationIter.hasNext()) {
+                       while(0 >= left && locationIter.hasNext()) {
                                location = locationIter.next();
                                var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
                                if(quantityForLocation == null || quantityForLocation == 0) continue;
@@ -213,7 +232,7 @@ return cast(locations(), Array<Dynamic>);
 
 private function loadOrders() {
        var tb : String =  untyped __php__('TB_PREF');
-       var sql : String = "SELECT *  
+       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)
@@ -228,7 +247,12 @@ function orders():Array<Order>  {
        var rows = loadOrders();
        var orderList = [];
        for(row in rows) {
-               var order:Order = php.Lib.objectOfAssociativeArray(row);
+               // for some reason, priority is null
+               // so we use the pp field
+               var a:Dynamic = php.Lib.objectOfAssociativeArray(row);
+               var order:Order = a;
+               order.priority = Date.fromString(a.pp);
+               order.quantity = a.qty;
                orderList.push(order);
        };
 
@@ -262,8 +286,43 @@ function locations() {
 }
 
 
-function purcharseOrders()  {
-}
+       function purcharseOrders()  {
+       }
+
+       public function needsUpdate():Bool {
+               return parameters != null && parameters.mode == ScheduleMode.Move;
+       }
+
+       function update() {
+               var orders = this.orders();
+               var priorities = Lambda.array(Lambda.map(orders, function(o) { return o.priority;}));
+               priorities.sort(function(a,b) {
+                       var as = a.toString();
+                       var bs = b.toString();
+                       if (as < bs) return -1;
+                       if( as > bs) return 1;
+                       return 0;
+                       });
+
+               var iter = priorities.iterator();
+               var p = iter.next();
+               var position:Int = 0-priorities.length;
+               for(order in orders) {
+                       var new_priority = DateTools.delta(p, 1000*position);
+                       untyped __call__ ('update_order_detail_priority', order.id, new_priority.toString());
+                       
+                       position +=1;
+                       p = iter.next();
+               }
+                       untyped __call__ ('update_queue_quantity_for_item', stock_id);
+               
+       }
+       
+       public function action() {
+               if(parameters != null && parameters.mode == ScheduleMode.Update) {
+                       update();
+               }
+       }
 
 
 }