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);
11 public function action() {
12 if($this->parameters !== null && $this->parameters->mode == ScheduleMode::$Update) {
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();
22 $position = 0 - $priorities->length;
25 while($_g < $orders->length) {
26 $order = $orders[$_g];
28 $new_priority = DateTools::delta($p, 1000 * $position);
29 update_order_detail_priority($order->id, $new_priority->toString());
32 unset($order,$new_priority);
35 update_queue_quantity_for_item($this->stock_id);
37 public function needsUpdate() {
38 return $this->parameters !== null && $this->parameters->mode == ScheduleMode::$Move;
40 public function purcharseOrders() {
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);
45 public function locations() {
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()) {
53 $location = new Location($row);
54 if($location->code === $this->startLocation) {
55 $location->delivery = Date::fromTime(0);
57 if($location->delivery === null) {
61 $_locs->push($location);
66 public function orders() {
68 $rows = $this->loadOrders();
69 $orderList = new _hx_array(array());
71 while($»it->hasNext()) {
73 $a = php_Lib::objectOfAssociativeArray($row);
75 $order->priority = Date::fromString($a->pp);
76 $order->quantity = $a->qty;
77 $orderList->push($order);
80 if($this->parameters !== null) {
81 $orderList->sort(array(new _hx_lambda(array(&$_g, &$orderList, &$rows), "ItemScheduler_2"), 'execute'));
85 while($_g1 < $orderList->length) {
86 $order = $orderList[$_g1];
93 public function loadOrders() {
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);
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 . "\"")));
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;
108 $classes->push("soldout");
111 $classes->push("partial");
113 $classes->push("full");
116 $required_by = FA::sql2date($order->required_date);
117 if($required_by === null) {
118 $required_by = FA::sql2date($order->expiry_date);
120 if($required_by !== null && $required_by->getTime() < $date->getTime()) {
121 $classes->push("late");
123 $hold_until = FA::sql2date($order->hold_until_date);
124 php_Lib::hprint($hold_until);
125 php_Lib::hprint($date);
126 if($hold_until === null) {
127 $hold_until = FA::sql2date($order->delivery_date);
129 if($hold_until->getTime() > $date->getTime()) {
130 $classes->push("early");
132 $classes->push("on_time");
135 $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->hold_until_date, $order->required_date, $order->comment));
136 $attributes->push("class=\"" . $classes->join(" ") . "\"");
137 $this->printRow($cells, $attributes);
139 public function printRow($tds, $attributes) {
140 php_Lib::hprint("<tr " . $attributes->join(" ") . ">");
144 while($_g < $tds->length) {
147 php_Lib::hprint("<td class=\"cell_" . _hx_string_rec($position, "") . "\">");
149 php_Lib::hprint($td);
151 php_Lib::hprint("</td>");
156 php_Lib::hprint("</tr>");
158 public function generateTable() {
159 $startDate = Date::fromTime(0);
160 $locations = $this->locations();
161 $locations->sort(array(new _hx_lambda(array(&$locations, &$startDate), "ItemScheduler_3"), 'execute'));
162 $locationIter = $locations->iterator();
163 $location = $locationIter->next();
164 $qoh = $location->quantityOnHand($this->stock_id, null);
166 $this->formatLocation($location, "Initial", $left);
168 $_g = 0; $_g1 = $this->orders();
169 while($_g < $_g1->length) {
172 $quantity = Std::parseInt($order->quantity);
173 while(0 >= $left && $locationIter->hasNext()) {
174 $location = $locationIter->next();
175 $quantityForLocation = $location->quantityOnHand($this->stock_id, null) + $location->quantityOnOrder($this->stock_id);
176 if($quantityForLocation === null || $quantityForLocation === 0 || $location->delivery === null) {
179 $left += $quantityForLocation;
180 $this->formatLocation($location, "Delivery", $left);
181 unset($quantityForLocation);
185 $this->formatOrder($order, $left, ItemScheduler_4($this, $_g, $_g1, $left, $location, $locationIter, $locations, $now, $order, $qoh, $quantity, $startDate));
186 unset($quantity,$order,$now);
189 while(0 >= $left && $locationIter->hasNext()) {
190 $location = $locationIter->next();
191 $quantityForLocation = $location->quantityOnHand($this->stock_id, null);
192 if($quantityForLocation === null || $quantityForLocation === 0) {
195 $left += $quantityForLocation;
196 $this->formatLocation($location, "Delivery", $left);
197 unset($quantityForLocation);
200 public function tableHeader() {
201 return new _hx_array(array("Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"));
205 public $startLocation;
207 public function __call($m, $a) {
208 if(isset($this->$m) && is_callable($this->$m))
209 return call_user_func_array($this->$m, $a);
210 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
211 return call_user_func_array($this->»dynamics[$m], $a);
212 else if('toString' == $m)
213 return $this->__toString();
215 throw new HException('Unable to call «'.$m.'»');
217 static function orderId($order) {
218 return "order_" . $order->id;
220 function __toString() { return 'ItemScheduler'; }
222 function ItemScheduler_0(&$orders, $o) {
227 function ItemScheduler_1(&$orders, &$priorities, $a, $b) {
229 $as = $a->toString();
230 $bs = $b->toString();
240 function ItemScheduler_2(&$_g, &$orderList, &$rows, $a, $b) {
242 return $_g->parameters->priority($a) - $_g->parameters->priority($b);
245 function ItemScheduler_3(&$locations, &$startDate, $a, $b) {
247 $as = $a->delivery->getTime();
248 $bs = $b->delivery->getTime();
260 function ItemScheduler_4(&$»this, &$_g, &$_g1, &$left, &$location, &$locationIter, &$locations, &$now, &$order, &$qoh, &$quantity, &$startDate) {
261 if($now->getTime() > $location->delivery->getTime()) {
264 return $location->delivery;