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');
46 static public function sql2date(sqlDate:Dynamic) : Null<Date> {
47 return sqlDate == null ? null : Date.fromString(sqlDate);
52 var startLocation:String;
54 function new(stock_id: String, startLocation) {
55 this.stock_id = stock_id;
56 this.startLocation = startLocation;
57 qoh = untyped __call__('get_qoh_on_date', this.stock_id, 'DEF');
60 function tableHeader() {
61 return ["Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"];
65 function generateTablex(): Void {
66 for(location in locations()) {
67 formatLocation(location, null);
69 for(order in this.orders()) {
70 this.formatRow(order);
75 function generateTable(): Void {
76 var startDate = Date.fromTime(0);
77 for(order in orders()) {
78 var obj = php.Lib.objectOfAssociativeArray(order);
82 // Sort location by datae
83 var locations = this.locations();
84 locations.sort(function(a, b) {
85 return cast(a.delivery.getTime() - b.delivery.getTime(), Int );
88 // Get the start location, it should be the first one
89 var locationIter = locations.iterator();
90 var location = locationIter.next();
91 var qoh : Int = location.quantityOnHand(stock_id, null);
93 formatLocation(location, "Initial", left);
95 // We display the order ordered by priority
96 // But insert the location when needed (meaning
97 // when we run out of item available
98 for(orderRow in orders()) {
99 var order = php.Lib.objectOfAssociativeArray(orderRow);
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(' ')+'>');
120 php.Lib.print('<td>');
121 if(td) php.Lib.print(td);
122 php.Lib.print('</td>');
124 php.Lib.print('</tr>');
127 function formatOrder(order : Dynamic, left : Int, date : Date) {
130 var before : Int = left + order.quantity;
131 /* We have basically 3 different cases;
132 * - the order can be fullfilled
133 * - the order can be partially
137 classes.push('soldout');
140 classes.push('partial');
143 classes.push('full');
146 /* The order can also be late if we need
147 * to wait for a delivery to get it
149 var required_by : Date = FA.sql2date(order.required_date);
150 if(required_by == null) required_by = FA.sql2date(order.delivery_date);
151 if(required_by.getTime() < date.getTime()) {
152 classes.push('late');
155 classes.push('on_time');
157 var cells : Array <Dynamic> = [
159 , '<a href="/modules/order_line_extra/order_lines_view.php?customer_id='+Std.string(order.debtor_no)+'">'+order.deliver_to+'</a>'
169 attributes.push('class="'+classes.join(' ')+'"');
170 printRow(cells, attributes);
174 function formatRow(row) {
175 var array = php.Lib.hashOfAssociativeArray(row);
176 var quantity_before : Int = array.get('quantity_before');
177 var quantity_available = qoh - quantity_before;
178 var quantity: Int = array.get('quantity');
180 var status : String = if(quantity_available < quantity) 'overduebg';
182 var cells : Array<Dynamic> = [
183 array.get('order_id')
184 ,array.get('deliver_to')
186 ,quantity_available-quantity
188 ,array.get('from_stk_loc')
189 ,array.get('delivery_date')
192 php.Lib.print('<tr class="'+status+'">');
194 php.Lib.print('<td>');
196 php.Lib.print('</td>');
198 php.Lib.print('</tr>');
203 function formatLocation(location : Location, type: String, left : Int) {
207 ,location.quantityOnHand(stock_id, null)
208 ,left-location.quantityOnHand(stock_id, null)
216 printRow(cells, ['class = "tableheader location"']);
220 function schedules() {
221 //return orders()+locations();
223 return cast(locations(), Array<Dynamic>);
229 var tb : String = untyped __php__('TB_PREF');
230 var sql : String = "SELECT *
231 FROM "+tb+"denorm_order_details_queue d
232 JOIN "+tb+"sales_order_details od ON (od.id = d.id)
233 JOIN "+tb+"sales_orders so ON (so.order_no = d.order_id)
234 WHERE stock_id = '"+this.stock_id+"'
235 AND od.trans_type = 30
236 ORDER by quantity_before";
238 return FA.query(sql);
241 function locations() {
244 FROM '+TB+'locations';
246 for(row in FA.query(sql)) {
247 var location = new Location(row);
248 if(location.code == startLocation) {
249 location.delivery = Date.fromTime(0);
251 _locs.push(location);
259 function purcharseOrders() {