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", "Left", "On Hand", "Loc", "Required Date"];
60 function generateTablex(): Void {
61 for(location in locations()) {
62 formatLocation(location, null);
64 for(order in this.orders()) {
65 this.formatRow(order);
69 function generateTable(): Void {
70 var startDate = Date.fromTime(0);
71 for(order in orders()) {
72 var obj = php.Lib.objectOfAssociativeArray(order);
76 // Sort location by datae
77 var locations = this.locations();
78 locations.sort(function(a, b) {
79 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
82 // Get the start location, it should be the first one
83 var locationIter = locations.iterator();
84 var location = locationIter.next();
85 var qoh : Int = location.quantityOnHand(stock_id, null);
87 formatLocation(location, left);
89 // We display the order ordered by priority
90 // But insert the location when needed (meaning
91 // when we run out of item available
92 for(orderRow in orders()) {
93 var order = php.Lib.objectOfAssociativeArray(orderRow);
94 var quantity : Int = Std.parseInt(order.quantity);
96 while(quantity > left && locationIter.hasNext()) {
97 location = locationIter.next();
98 var quantityForLocation : Int = location.quantityOnHand(stock_id, null);
99 if(quantityForLocation == null || quantityForLocation == 0) continue;
100 left += quantityForLocation;
101 trace(quantityForLocation);
102 formatLocation(location, left);
106 formatOrder(order, left);
112 function printRow(tds : Array<Dynamic>, attributes : Array<String>) {
113 php.Lib.print('<tr "'+attributes.join(' ')+'">');
115 php.Lib.print('<td>');
117 php.Lib.print('</td>');
119 php.Lib.print('</tr>');
122 function formatOrder(order : Dynamic, left : Int) {
124 var cells : Array <Dynamic> = [
133 printRow(cells, attributes);
137 function formatRow(row) {
138 var array = php.Lib.hashOfAssociativeArray(row);
139 var quantity_before : Int = array.get('quantity_before');
140 var quantity_available = qoh - quantity_before;
141 var quantity: Int = array.get('quantity');
143 var status : String = if(quantity_available < quantity) 'overduebg';
145 var cells : Array<Dynamic> = [
146 array.get('order_id')
147 ,array.get('deliver_to')
149 ,quantity_available-quantity
151 ,array.get('from_stk_loc')
152 ,array.get('delivery_date')
155 php.Lib.print('<tr class="'+status+'">');
157 php.Lib.print('<td>');
159 php.Lib.print('</td>');
161 php.Lib.print('</tr>');
166 function formatLocation(location : Location, left : Int) {
170 ,location.quantityOnHand(stock_id, null)
174 var status = 'header';
175 php.Lib.print('<tr class="'+status+'">');
177 php.Lib.print('<td>');
179 php.Lib.print('</td>');
181 php.Lib.print('</tr>');
185 function schedules() {
186 //return orders()+locations();
188 return cast(locations(), Array<Dynamic>);
194 var tb : String = untyped __php__('TB_PREF');
195 var sql : String = "SELECT *
196 FROM "+tb+"denorm_order_details_queue
197 JOIN "+tb+"sales_orders ON (order_no = order_id)
198 WHERE stock_id = '"+this.stock_id+"'
199 ORDER by quantity_before";
201 return FA.query(sql);
204 function locations() {
207 FROM '+TB+'locations';
209 for(row in FA.query(sql)) {
210 var location = new Location(row);
211 if(location.code == startLocation) {
212 location.delivery = Date.fromTime(0);
214 _locs.push(location);
222 function purcharseOrders() {