Splitter Test written. Doesn't pass.
[order_line_extra.git] / hincludes / lib / ItemScheduler.class.php
1 <?php
2
3 class ItemScheduler {
4         public function __construct($stock_id, $startLocation, $parameters) {
5                 if(!php_Boot::$skip_constructor) {
6                 $this->stock_id = $stock_id;
7                 $this->startLocation = $startLocation;
8                 $this->parameters = $parameters;
9                 $this->qoh = get_qoh_on_date($this->stock_id, $startLocation);
10         }}
11         public function action() {
12                 if($this->parameters !== null && $this->parameters->mode == ScheduleMode::$Update) {
13                         $this->update();
14                 }
15         }
16         public function update() {
17                 $orders = $this->orders();
18                 $priorities = Lambda::harray(Lambda::map($orders, array(new _hx_lambda(array(&$orders), "ItemScheduler_0"), 'execute')));
19                 $priorities->sort(array(new _hx_lambda(array(&$orders, &$priorities), "ItemScheduler_1"), 'execute'));
20                 $iter = $priorities->iterator();
21                 $p = $iter->next();
22                 $position = 0 - $priorities->length;
23                 {
24                         $_g = 0;
25                         while($_g < $orders->length) {
26                                 $order = $orders[$_g];
27                                 ++$_g;
28                                 $new_priority = DateTools::delta($p, 1000 * $position);
29                                 update_order_detail_priority($order->id, $new_priority->toString());
30                                 $position += 1;
31                                 $p = $iter->next();
32                                 unset($order,$new_priority);
33                         }
34                 }
35                 update_queue_quantity_for_item($this->stock_id);
36         }
37         public function needsUpdate() {
38                 return $this->parameters !== null && $this->parameters->mode == ScheduleMode::$Move;
39         }
40         public function purcharseOrders() {
41                 $TB = FA::tb();
42                 $sql = "SELECT SUM(quantity_ordered - quantity_received) as quantity\x0A\x09\x09\x09into_stock_location AS location\x0A\x09\x09\x09FROM " . $TB . "purch_order_details\x0A\x09\x09\x09NATURAL JOIN " . $TB . "purch_orders\x0A\x09\x09\x09WHERE item_code = \"" . $this->stock_id . "\"\x0A\x09\x09\x09AND quantity_ordered > quantity_received\x0A\x09\x09\x09GROUP by item_code,delivery_date, into_stock_location\x0A\x09\x09\x09ORDER by delivery_date";
43                 return FA::query($sql);
44         }
45         public function locations() {
46                 $TB = FA::tb();
47                 $sql = "SELECT * \x0A\x09\x09FROM " . $TB . "locations";
48                 $_locs = new _hx_array(array());
49                 if(null == _hx_qtype("FA")) throw new HException('null iterable');
50                 $»it = FA::query($sql);
51                 while($»it->hasNext()) {
52                         $row = $»it->next();
53                         $location = new Location($row);
54                         if($location->code === $this->startLocation) {
55                                 $location->delivery = Date::fromTime(0);
56                         } else {
57                                 if($location->delivery === null) {
58                                         continue;
59                                 }
60                         }
61                         $_locs->push($location);
62                         unset($location);
63                 }
64                 return $_locs;
65         }
66         public function orders() {
67                 $_g = $this;
68                 $rows = $this->loadOrders();
69                 $orderList = new _hx_array(array());
70                 $»it = $rows;
71                 while($»it->hasNext()) {
72                         $row = $»it->next();
73                         $a = php_Lib::objectOfAssociativeArray($row);
74                         $order = $a;
75                         $order->priority = Date::fromString($a->pp);
76                         $order->quantity = $a->qty;
77                         $orderList->push($order);
78                         unset($order,$a);
79                 }
80                 if($this->parameters !== null) {
81                         $orderList->sort(array(new _hx_lambda(array(&$_g, &$orderList, &$rows), "ItemScheduler_2"), 'execute'));
82                 }
83                 {
84                         $_g1 = 0;
85                         while($_g1 < $orderList->length) {
86                                 $order = $orderList[$_g1];
87                                 ++$_g1;
88                                 unset($order);
89                         }
90                 }
91                 return $orderList;
92         }
93         public function loadOrders() {
94                 $tb = TB_PREF;
95                 $sql = "SELECT *  , d.quantity as qty,   d.priority AS pp\x0A\x09\x09FROM " . $tb . "denorm_order_details_queue  d\x0A\x09\x09JOIN " . $tb . "sales_order_details od ON (od.id = d.id)\x0A\x09\x09JOIN " . $tb . "sales_orders so ON (so.order_no = d.order_id)\x0A\x09\x09WHERE stock_id = '" . $this->stock_id . "'\x0A\x09\x09AND od.trans_type = 30\x0A\x09\x09ORDER by d.priority";
96                 return FA::query($sql);
97         }
98         public function formatLocation($location, $type, $left) {
99                 $cells = new _hx_array(array($type, $location->name, $location->quantityOnHand($this->stock_id, null), $left - $location->quantityOnHand($this->stock_id, null), $left, $location->code, ((_hx_equal($location->delivery->getTime(), 0)) ? "" : DateTools::format($location->delivery, "%F")), "", ""));
100                 $this->printRow($cells, new _hx_array(array("class = \"tableheader location\"", "id = \"loc_" . $location->code . "\"")));
101         }
102         public function formatOrder($order, $left, $date) {
103                 $row_id = ItemScheduler::orderId($order);
104                 $attributes = new _hx_array(array("id = \"" . $row_id . "\""));
105                 $classes = new _hx_array(array());
106                 $before = $left + $order->quantity;
107                 if($before < 0) {
108                         $classes->push("soldout");
109                 } else {
110                         if($left < 0) {
111                                 $classes->push("partial");
112                         } else {
113                                 $classes->push("full");
114                         }
115                 }
116                 $required_by = FA::sql2date($order->required_date);
117                 if($required_by === null) {
118                         $required_by = FA::sql2date($order->delivery_date);
119                 }
120                 if($required_by->getTime() < $date->getTime()) {
121                         $classes->push("late");
122                 } else {
123                         $classes->push("on_time");
124                 }
125                 $cells = new _hx_array(array($order->order_id, "<a href=\"/modules/order_line_extra/order_lines_view.php?customer_id=" . Std::string($order->debtor_no) . "\">" . Std::string($order->deliver_to) . "</a>", "<input type=\"text\" name=\"" . $row_id . "[quantity]\" value=\"" . Std::string($order->quantity) . "\">", $before, $left, $order->from_stk_loc, $order->delivery_date, $order->required_date, $order->comment));
126                 $attributes->push("class=\"" . $classes->join(" ") . "\"");
127                 $this->printRow($cells, $attributes);
128         }
129         public function printRow($tds, $attributes) {
130                 php_Lib::hprint("<tr " . $attributes->join(" ") . ">");
131                 $position = 1;
132                 {
133                         $_g = 0;
134                         while($_g < $tds->length) {
135                                 $td = $tds[$_g];
136                                 ++$_g;
137                                 php_Lib::hprint("<td class=\"cell_" . _hx_string_rec($position, "") . "\">");
138                                 if($td) {
139                                         php_Lib::hprint($td);
140                                 }
141                                 php_Lib::hprint("</td>");
142                                 $position++;
143                                 unset($td);
144                         }
145                 }
146                 php_Lib::hprint("</tr>");
147         }
148         public function generateTable() {
149                 $startDate = Date::fromTime(0);
150                 $locations = $this->locations();
151                 $locations->sort(array(new _hx_lambda(array(&$locations, &$startDate), "ItemScheduler_3"), 'execute'));
152                 $locationIter = $locations->iterator();
153                 $location = $locationIter->next();
154                 $qoh = $location->quantityOnHand($this->stock_id, null);
155                 $left = $qoh;
156                 $this->formatLocation($location, "Initial", $left);
157                 {
158                         $_g = 0; $_g1 = $this->orders();
159                         while($_g < $_g1->length) {
160                                 $order = $_g1[$_g];
161                                 ++$_g;
162                                 $quantity = Std::parseInt($order->quantity);
163                                 while(0 >= $left && $locationIter->hasNext()) {
164                                         $location = $locationIter->next();
165                                         $quantityForLocation = $location->quantityOnHand($this->stock_id, null) + $location->quantityOnOrder($this->stock_id);
166                                         if($quantityForLocation === null || $quantityForLocation === 0 || $location->delivery === null) {
167                                                 continue;
168                                         }
169                                         $left += $quantityForLocation;
170                                         $this->formatLocation($location, "Delivery", $left);
171                                         unset($quantityForLocation);
172                                 }
173                                 $left -= $quantity;
174                                 $this->formatOrder($order, $left, $location->delivery);
175                                 unset($quantity,$order);
176                         }
177                 }
178                 while(0 >= $left && $locationIter->hasNext()) {
179                         $location = $locationIter->next();
180                         $quantityForLocation = $location->quantityOnHand($this->stock_id, null);
181                         if($quantityForLocation === null || $quantityForLocation === 0) {
182                                 continue;
183                         }
184                         $left += $quantityForLocation;
185                         $this->formatLocation($location, "Delivery", $left);
186                         unset($quantityForLocation);
187                 }
188         }
189         public function tableHeader() {
190                 return new _hx_array(array("Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"));
191         }
192         public $qoh;
193         public $parameters;
194         public $startLocation;
195         public $stock_id;
196         public function __call($m, $a) {
197                 if(isset($this->$m) && is_callable($this->$m))
198                         return call_user_func_array($this->$m, $a);
199                 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
200                         return call_user_func_array($this->»dynamics[$m], $a);
201                 else if('toString' == $m)
202                         return $this->__toString();
203                 else
204                         throw new HException('Unable to call «'.$m.'»');
205         }
206         static function orderId($order) {
207                 return "order_" . $order->id;
208         }
209         function __toString() { return 'ItemScheduler'; }
210 }
211 function ItemScheduler_0(&$orders, $o) {
212         {
213                 return $o->priority;
214         }
215 }
216 function ItemScheduler_1(&$orders, &$priorities, $a, $b) {
217         {
218                 $as = $a->toString();
219                 $bs = $b->toString();
220                 if($as < $bs) {
221                         return -1;
222                 }
223                 if($as > $bs) {
224                         return 1;
225                 }
226                 return 0;
227         }
228 }
229 function ItemScheduler_2(&$_g, &$orderList, &$rows, $a, $b) {
230         {
231                 return $_g->parameters->priority($a) - $_g->parameters->priority($b);
232         }
233 }
234 function ItemScheduler_3(&$locations, &$startDate, $a, $b) {
235         {
236                 $as = $a->delivery->getTime();
237                 $bs = $b->delivery->getTime();
238                 if($as < $bs) {
239                         return -1;
240                 } else {
241                         if($as > $bs) {
242                                 return 1;
243                         } else {
244                                 return 0;
245                         }
246                 }
247         }
248 }