8 class QueryIterator<T> {
10 var nextValue : Maybe<T>;
11 public function new(result) {
13 /* We fetch the first row , so we can answer hasNext */
17 private function fetch() {
18 var next : Dynamic = untyped __call__('db_fetch', this.result);
19 nextValue = if(next) Just(next) else Nothing;
22 public function hasNext() : Bool {
23 return this.nextValue != Nothing;
26 public function next() : T {
27 switch(this.nextValue) {
28 case Nothing : throw 'Iterator exhausted';
37 static public function query(sql: String) {
38 var result = untyped __call__('db_query', sql, sql);
39 return new QueryIterator(result);
42 static public function tb() : String {
43 return untyped __php__('TB_PREF');
48 var startLocation:String;
50 function new(stock_id: String, startLocation) {
51 this.stock_id = stock_id;
52 this.startLocation = startLocation;
53 qoh = untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
56 function tableHeader() {
57 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date"];
61 function generateTablex(): Void {
62 for(location in locations()) {
63 formatLocation(location, null);
65 for(order in this.orders()) {
66 this.formatRow(order);
71 function generateTable(): Void {
72 var startDate = Date.fromTime(0);
73 for(order in orders()) {
74 var obj = php.Lib.objectOfAssociativeArray(order);
78 // Sort location by datae
79 var locations = this.locations();
80 locations.sort(function(a, b) {
81 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
84 // Get the start location, it should be the first one
85 var locationIter = locations.iterator();
86 var location = locationIter.next();
87 var qoh : Int = location.quantityOnHand(stock_id, null);
89 formatLocation(location, "Initial", left);
91 // We display the order ordered by priority
92 // But insert the location when needed (meaning
93 // when we run out of item available
94 for(orderRow in orders()) {
95 var order = php.Lib.objectOfAssociativeArray(orderRow);
96 var quantity : Int = Std.parseInt(order.quantity);
98 while(0 > left && locationIter.hasNext()) {
99 location = locationIter.next();
100 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
101 if(quantityForLocation == null || quantityForLocation == 0) continue;
102 left += quantityForLocation;
103 formatLocation(location, "Delivery", left);
107 formatOrder(order, left, location.delivery);
113 function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
114 php.Lib.print('<tr '+attributes.join(' ')+'>');
116 php.Lib.print('<td>');
118 php.Lib.print('</td>');
120 php.Lib.print('</tr>');
123 function formatOrder(order : Dynamic, left : Int, date : Date) {
126 var before : Int = left + order.quantity;
127 /* We have basically 3 different cases;
128 * - the order can be fullfilled
129 * - the order can be partially
133 classes.push('soldout');
136 classes.push('partial');
139 classes.push('full');
142 /* The order can also be late if we need
143 * to wait for a delivery to get it
145 var cells : Array <Dynamic> = [
157 attributes.push('class="'+classes.join(' ')+'"');
158 printRow(cells, attributes);
162 function formatRow(row) {
163 var array = php.Lib.hashOfAssociativeArray(row);
164 var quantity_before : Int = array.get('quantity_before');
165 var quantity_available = qoh - quantity_before;
166 var quantity: Int = array.get('quantity');
168 var status : String = if(quantity_available < quantity) 'overduebg';
170 var cells : Array<Dynamic> = [
171 array.get('order_id')
172 ,array.get('deliver_to')
174 ,quantity_available-quantity
176 ,array.get('from_stk_loc')
177 ,array.get('delivery_date')
180 php.Lib.print('<tr class="'+status+'">');
182 php.Lib.print('<td>');
184 php.Lib.print('</td>');
186 php.Lib.print('</tr>');
191 function formatLocation(location : Location, type: String, left : Int) {
195 ,location.quantityOnHand(stock_id, null)
196 ,left-location.quantityOnHand(stock_id, null)
202 printRow(cells, ['class = "tableheader location"']);
206 function schedules() {
207 //return orders()+locations();
209 return cast(locations(), Array<Dynamic>);
215 var tb : String = untyped __php__('TB_PREF');
216 var sql : String = "SELECT *
217 FROM "+tb+"denorm_order_details_queue d
218 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
219 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
220 WHERE stock_id = '"+this.stock_id+"'
221 AND od.trans_type = 30
222 ORDER by quantity_before";
224 return FA.query(sql);
227 function locations() {
230 FROM '+TB+'locations';
232 for(row in FA.query(sql)) {
233 var location = new Location(row);
234 if(location.code == startLocation) {
235 location.delivery = Date.fromTime(0);
237 _locs.push(location);
245 function purcharseOrders() {