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", "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(quantity > 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);
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) {
125 var cells : Array <Dynamic> = [
134 printRow(cells, attributes);
138 function formatRow(row) {
139 var array = php.Lib.hashOfAssociativeArray(row);
140 var quantity_before : Int = array.get('quantity_before');
141 var quantity_available = qoh - quantity_before;
142 var quantity: Int = array.get('quantity');
144 var status : String = if(quantity_available < quantity) 'overduebg';
146 var cells : Array<Dynamic> = [
147 array.get('order_id')
148 ,array.get('deliver_to')
150 ,quantity_available-quantity
152 ,array.get('from_stk_loc')
153 ,array.get('delivery_date')
156 php.Lib.print('<tr class="'+status+'">');
158 php.Lib.print('<td>');
160 php.Lib.print('</td>');
162 php.Lib.print('</tr>');
167 function formatLocation(location : Location, type: String, left : Int) {
171 ,location.quantityOnHand(stock_id, null)
172 ,left-location.quantityOnHand(stock_id, null)
177 var status = 'header';
178 php.Lib.print('<tr class="'+status+'">');
180 php.Lib.print('<td>');
182 php.Lib.print('</td>');
184 php.Lib.print('</tr>');
188 function schedules() {
189 //return orders()+locations();
191 return cast(locations(), Array<Dynamic>);
197 var tb : String = untyped __php__('TB_PREF');
198 var sql : String = "SELECT *
199 FROM "+tb+"denorm_order_details_queue
200 JOIN "+tb+"sales_orders ON (order_no = order_id)
201 WHERE stock_id = '"+this.stock_id+"'
202 ORDER by quantity_before";
204 return FA.query(sql);
207 function locations() {
210 FROM '+TB+'locations';
212 for(row in FA.query(sql)) {
213 var location = new Location(row);
214 if(location.code == startLocation) {
215 location.delivery = Date.fromTime(0);
217 _locs.push(location);
225 function purcharseOrders() {