Sorting sorted.
authorMaxime Bourget <bmx007@gmail.com>
Sat, 1 Jun 2013 21:15:46 +0000 (22:15 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Sat, 1 Jun 2013 21:15:46 +0000 (22:15 +0100)
haxe/ItemScheduler.hx

index c767bda6fd592e2b43a65c19251f81cddbcbb40a..bc763e9618ef4b1b0e46a69a924efeedcaf3b90e 100644 (file)
@@ -6,6 +6,14 @@ typedef Detail = {
        position : Int,
 }
 
+typedef Order = {
+               id:String
+               ,quantity:String
+               ,priority:Int
+               ,delivery_date:String
+
+}
+
 class ScheduleParameters {
        var row_id : String;
        var rowDetails: Hash<Detail>;
@@ -21,9 +29,6 @@ class ScheduleParameters {
                        var position = 1;
                        for(id in row_ids) {
                                var d : Dynamic = data.get(id);
-                               //var o = php.Lib.objectOfAssociativeArray(d);
-                               php.Lib.dump(d);
-                               php.Lib.print('<br>');
 
                                var quantity : Int = null;
                                if(d != null) {
@@ -35,27 +40,36 @@ class ScheduleParameters {
                                rowDetails.set(id, {
                                        id: id
                                        ,quantity: quantity
-                                       ,position: position++
+                                       ,position: position
                                        });
+                               position+=1;
                        }
                }
-               //php.Lib.dump(rowDetails);
        }
 
-public function position(id: String) : Int {
-       return rowDetails == null ? rowDetails.get(id).position : null;
+       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 {
+               var orderId = ItemScheduler.orderId(order);
+               var orderPosition = position(orderId);
+               return orderPosition != null  ? orderPosition : order.priority;
+       }
 }
 
 
 class ItemScheduler {
        var stock_id:String;
        var startLocation:String;
+       var parameters:ScheduleParameters; 
        var qoh: Int;
        function new(stock_id: String, startLocation, parameters : Null<ScheduleParameters>) {
                this.stock_id = stock_id;
                this.startLocation = startLocation;
+               this.parameters = parameters;
                qoh =  untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
        }
 
@@ -66,7 +80,7 @@ class ItemScheduler {
        function generateTable(): Void {
                var startDate = Date.fromTime(0);
 
-               // Sort location by datae
+               // Sort location by date
                var locations = this.locations();
                locations.sort(function(a, b) {
                                return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
@@ -112,8 +126,12 @@ class ItemScheduler {
                php.Lib.print('</tr>');
        }
 
+       static public function orderId(order) {
+               return 'order_'+order.id;
+       }
+
        function formatOrder(order : Dynamic, left : Int, date : Date) {
-               var row_id = 'order_'+order.id;
+               var row_id = orderId(order);
                var attributes = ['id = "'+row_id+'"'];
                var classes = [];
                var before : Int = left + order.quantity;
@@ -198,14 +216,21 @@ private function loadOrders() {
        return FA.query(sql);
 }
 
-function orders()  {
+function orders():Array<Order>  {
        var rows = loadOrders();
        var orderList = [];
        for(row in rows) {
-               var order = php.Lib.objectOfAssociativeArray(row);
+               var order:Order = php.Lib.objectOfAssociativeArray(row);
                orderList.push(order);
        };
 
+       if(parameters != null) {
+               orderList.sort(function(a, b) { return parameters.priority(a)-parameters.priority(b); });
+
+       }
+               for(order in orderList) {
+               }
+
        return orderList;
 }