17 class ScheduleParameters {
19 var rowDetails: Hash<Detail>;
21 function new(rawData : Dynamic) {
22 var data = php.Lib.hashOfAssociativeArray(rawData);
23 row_id = data.get('row_id');
24 var raw_order : Dynamic = data.get('row_order');
26 var row_ids = php.Lib.toHaxeArray(raw_order);
28 rowDetails = new Hash<Detail>();
31 var d : Dynamic = data.get(id);
33 var quantity : Int = null;
35 var o = php.Lib.objectOfAssociativeArray(d);
36 quantity = Std.parseInt(o.quantity);
50 public function position(id: String) : Null<Int> {
51 if(rowDetails == null) return null;
52 return rowDetails.get(id).position;
56 public function priority(order : {id: String, priority: Int}) : Int {
57 var orderId = ItemScheduler.orderId(order);
58 var orderPosition = position(orderId);
59 return orderPosition != null ? orderPosition : order.priority;
66 var startLocation:String;
67 var parameters:ScheduleParameters;
69 function new(stock_id: String, startLocation, parameters : Null<ScheduleParameters>) {
70 this.stock_id = stock_id;
71 this.startLocation = startLocation;
72 this.parameters = parameters;
73 qoh = untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
76 function tableHeader() {
77 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"];
80 function generateTable(): Void {
81 var startDate = Date.fromTime(0);
83 // Sort location by date
84 var locations = this.locations();
85 locations.sort(function(a, b) {
86 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
89 // Get the start location, it should be the first one
90 var locationIter = locations.iterator();
91 var location = locationIter.next();
92 var qoh : Int = location.quantityOnHand(stock_id, null);
94 formatLocation(location, "Initial", left);
96 // We display the order ordered by priority
97 // But insert the location when needed (meaning
98 // when we run out of item available
99 for(order in orders()) {
100 var quantity : Int = Std.parseInt(order.quantity);
102 while(0 > left && locationIter.hasNext()) {
103 location = locationIter.next();
104 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
105 if(quantityForLocation == null || quantityForLocation == 0) continue;
106 left += quantityForLocation;
107 formatLocation(location, "Delivery", left);
111 formatOrder(order, left, location.delivery);
117 function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
118 php.Lib.print('<tr '+attributes.join(' ')+'>');
119 var position : Int = 1;
121 php.Lib.print('<td class="cell_'+position+'">');
122 if(td) php.Lib.print(td);
123 php.Lib.print('</td>');
126 php.Lib.print('</tr>');
129 static public function orderId(order) {
130 return 'order_'+order.id;
133 function formatOrder(order : Dynamic, left : Int, date : Date) {
134 var row_id = orderId(order);
135 var attributes = ['id = "'+row_id+'"'];
137 var before : Int = left + order.quantity;
138 /* We have basically 3 different cases;
139 * - the order can be fullfilled
140 * - the order can be partially
144 classes.push('soldout');
147 classes.push('partial');
150 classes.push('full');
153 /* The order can also be late if we need
154 * to wait for a delivery to get it
156 var required_by : Date = FA.sql2date(order.required_date);
157 if(required_by == null) required_by = FA.sql2date(order.delivery_date);
158 if(required_by.getTime() < date.getTime()) {
159 classes.push('late');
162 classes.push('on_time');
164 var cells : Array <Dynamic> = [
166 , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
167 ,'<input type="text" name="'+row_id+'[quantity]" value="'+order.quantity+'">'
176 attributes.push('class="'+classes.join(' ')+'"');
177 printRow(cells, attributes);
181 function formatLocation(location : Location, type: String, left : Int) {
185 ,location.quantityOnHand(stock_id, null)
186 ,left-location.quantityOnHand(stock_id, null)
194 printRow(cells, ['class = "tableheader location"', 'id = "loc_'+location.code+'"']);
198 function schedules() {
199 //return orders()+locations();
201 return cast(locations(), Array<Dynamic>);
206 private function loadOrders() {
207 var tb : String = untyped __php__('TB_PREF');
208 var sql : String = "SELECT *
209 FROM "+tb+"denorm_order_details_queue d
210 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
211 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
212 WHERE stock_id = '"+this.stock_id+"'
213 AND od.trans_type = 30
214 ORDER by d.priority";
216 return FA.query(sql);
219 function orders():Array<Order> {
220 var rows = loadOrders();
223 var order:Order = php.Lib.objectOfAssociativeArray(row);
224 orderList.push(order);
227 if(parameters != null) {
228 orderList.sort(function(a, b) { return parameters.priority(a)-parameters.priority(b); });
231 for(order in orderList) {
239 function locations() {
242 FROM '+TB+'locations';
244 for(row in FA.query(sql)) {
245 var location = new Location(row);
246 if(location.code == startLocation) {
247 location.delivery = Date.fromTime(0);
249 _locs.push(location);
257 function purcharseOrders() {