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);
64 for(order in this.orders()) {
65 this.formatRow(order);
69 function generateTable(): Void {
71 var startDate = Date.fromTime(0);
72 for(order in orders()) {
73 var obj = php.Lib.objectOfAssociativeArray(order);
74 schedules.push({date: Date.fromString(obj.delivery_date), order:order, location:null});
76 for(location in locations()) {
77 schedules.push({date: if(location.code == startLocation) startDate else location.delivery, order:null, location:location});
80 schedules.sort(function(a,b ) { return cast(a.date.getTime() - b.date.getTime(), Int) ; });
81 for(schedule in schedules) {
82 if(schedule.order != null) formatRow(schedule.order);
83 if(schedule.location != null) formatLocation(schedule.location);
87 function formatRow(row) {
88 var array = php.Lib.hashOfAssociativeArray(row);
89 var quantity_before : Int = array.get('quantity_before');
90 var quantity_available = qoh - quantity_before;
91 var quantity: Int = array.get('quantity');
93 var status : String = if(quantity_available < quantity) 'overduebg';
95 var cells : Array<Dynamic> = [
97 ,array.get('deliver_to')
99 ,quantity_available-quantity
101 ,array.get('from_stk_loc')
102 ,array.get('delivery_date')
105 php.Lib.print('<tr class="'+status+'">');
107 php.Lib.print('<td>');
109 php.Lib.print('</td>');
111 php.Lib.print('</tr>');
116 function formatLocation(location : Location) {
120 ,location.quantityOnHand(stock_id, null)
123 var status = 'header';
124 php.Lib.print('<tr class="'+status+'">');
126 php.Lib.print('<td>');
128 php.Lib.print('</td>');
130 php.Lib.print('</tr>');
134 function schedules() {
135 //return orders()+locations();
137 return cast(locations(), Array<Dynamic>);
143 var tb : String = untyped __php__('TB_PREF');
144 var sql : String = "SELECT *
145 FROM "+tb+"denorm_order_details_queue
146 JOIN "+tb+"sales_orders ON (order_no = order_id)
147 WHERE stock_id = '"+this.stock_id+"'
148 ORDER by quantity_before";
150 return FA.query(sql);
153 function locations() {
156 FROM '+TB+'locations';
158 for(row in FA.query(sql)) {
159 _locs.push(new Location(row));
167 function purcharseOrders() {