Merge branch 'add_jquery_sortable'
[order_line_extra.git] / haxe / ItemScheduler.hx
index ea59cd19bb8d46e50d00748f1174360c5c92eb6a..be596ac4d121b87f0f547ed3e59984c3c4dbf2fd 100644 (file)
@@ -42,6 +42,10 @@ class FA {
        static public function tb() : String {
                return untyped __php__('TB_PREF');
        }
+
+       static public function sql2date(sqlDate:Dynamic) : Null<Date> {
+               return sqlDate == null ? null : Date.fromString(sqlDate);
+       }
 }
 class ItemScheduler {
        var stock_id:String;
@@ -54,34 +58,120 @@ class ItemScheduler {
        }
 
        function tableHeader() {
-               return ["Order", "Customer", "Quantity", "Left", "On Hand", "Loc", "Required Date"];
+               return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From",  "Required Date", "Comment"];
        }
 
+/*
        function generateTablex(): Void {
                for(location in locations()) {
-                       formatLocation(location);
+                       formatLocation(location, null);
                }
                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});
+
+                       // 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(0 > 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, location.delivery);
+                       
+               }
                        
-                       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 printRow(tds : Array<Dynamic>, attributes : Array<String>) {
+               php.Lib.print('<tr '+attributes.join(' ')+'>');
+               var position : Int = 1;
+               for(td in tds) {
+                       php.Lib.print('<td class="cell_'+position+'">');
+                       if(td) php.Lib.print(td);
+                       php.Lib.print('</td>');
+                       position++;
+               }
+               php.Lib.print('</tr>');
+       }
+
+       function formatOrder(order : Dynamic, left : Int, date : Date) {
+                       var row_id = 'order_'+order.id;
+                       var attributes = ['id = "'+row_id+'"'];
+                       var classes = [];
+                       var before : Int = left + order.quantity;
+                       /* We have basically 3 different cases;
+                        * - the order can be fullfilled
+                        * - the order can be partially 
+                  * - not at all
+                        */
+                       if (before < 0 ) {
+                                       classes.push('soldout');
+                       }
+                       else if(left < 0) {
+                                       classes.push('partial');
+                       }
+                       else {
+                                       classes.push('full');
                        }
+
+                       /* The order can also be late if we need
+                        * to wait for a delivery to get it
+                        */
+               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()) {
+                       classes.push('late');
+               }
+               else {
+                       classes.push('on_time');
+               }
+                       var cells : Array <Dynamic> = [
+                               order.order_id
+                               , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
+                       ,'<input type="text" name="'+row_id+'[quantity]" value="'+order.quantity+'">'
+                               ,before
+                               ,left
+                               ,order.from_stk_loc
+                               ,order.delivery_date
+                               ,order.required_date 
+                               ,order.comment
+                       ];
+
+                       attributes.push('class="'+classes.join(' ')+'"');
+                       printRow(cells, attributes);
+
        }
 
        function formatRow(row) {
@@ -95,7 +185,7 @@ class ItemScheduler {
                var cells : Array<Dynamic> = [
                        array.get('order_id')
                        ,array.get('deliver_to')
-                       ,quantity
+                       ,'<input type="text" name="quantity">'+quantity+'</input>'
                        ,quantity_available-quantity
                        ,quantity_available
                        ,array.get('from_stk_loc')
@@ -113,21 +203,20 @@ class ItemScheduler {
                
        }
 
-       function formatLocation(location : Location) {
+       function formatLocation(location : Location, type: String,  left : Int) {
                var cells = [
-                       location.code
+                       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>');
+
+               printRow(cells, ['class = "tableheader location"', 'id = "loc_'+location.code+'"']);
        }
 
 /*
@@ -142,9 +231,11 @@ class ItemScheduler {
        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)
+                                               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)
                                                WHERE stock_id = '"+this.stock_id+"'
+                                               AND od.trans_type = 30
                                                ORDER by quantity_before";
        
                return FA.query(sql);
@@ -156,7 +247,11 @@ class ItemScheduler {
                                                        FROM '+TB+'locations';
                var _locs = [];
                for(row in FA.query(sql)) {
-                       _locs.push(new Location(row));
+                       var location = new Location(row);
+                       if(location.code == startLocation) {
+                        location.delivery =  Date.fromTime(0);
+                       }
+                       _locs.push(location);
                }
 
                return _locs;