+ static public function orderId(order) {
+ return 'order_'+order.id;
+ }
+
+ function formatOrder(order : Dynamic, left : Int, date : Date) {
+ var row_id = orderId(order);
+ 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 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.getTime() == 0 ? '' : location.delivery.format("%F")
+ ,""
+ ,""
+ ];
+
+ printRow(cells, ['class = "tableheader location"', 'id = "loc_'+location.code+'"']);
+ }
+
+ /*
+ function schedules() {
+//return orders()+locations();
+//return orders();
+return cast(locations(), Array<Dynamic>);
+
+}
+ */
+
+private function loadOrders() {
+ var tb : String = untyped __php__('TB_PREF');
+ 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)
+ WHERE stock_id = '"+this.stock_id+"'
+ AND od.trans_type = 30
+ ORDER by d.priority";
+
+ return FA.query(sql);
+}
+
+function orders():Array<Order> {
+ var rows = loadOrders();
+ var orderList = [];
+ for(row in rows) {
+ // 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);
+ };
+
+ if(parameters != null) {
+ orderList.sort(function(a, b) { return parameters.priority(a)-parameters.priority(b); });
+
+ }
+ for(order in orderList) {
+ }
+
+ return orderList;
+}
+
+
+
+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);
+ }
+ else if(location.delivery == null) {
+ continue;
+ }
+ _locs.push(location);
+ }
+
+ return _locs;
+
+}
+
+
+ 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 {
+ 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);
+
+ }