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->delivery_date);
120 if($required_by->getTime() < $date->getTime()) {
121 $classes->push("late");
123 $classes->push("on_time");
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);
129 public function printRow($tds, $attributes) {
130 php_Lib::hprint("<tr " . $attributes->join(" ") . ">");
134 while($_g < $tds->length) {
137 php_Lib::hprint("<td class=\"cell_" . _hx_string_rec($position, "") . "\">");
139 php_Lib::hprint($td);
141 php_Lib::hprint("</td>");
146 php_Lib::hprint("</tr>");
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);
156 $this->formatLocation($location, "Initial", $left);
158 $_g = 0; $_g1 = $this->orders();
159 while($_g < $_g1->length) {
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) {
169 $left += $quantityForLocation;
170 $this->formatLocation($location, "Delivery", $left);
171 unset($quantityForLocation);
174 $this->formatOrder($order, $left, $location->delivery);
175 unset($quantity,$order);
178 while(0 >= $left && $locationIter->hasNext()) {
179 $location = $locationIter->next();
180 $quantityForLocation = $location->quantityOnHand($this->stock_id, null);
181 if($quantityForLocation === null || $quantityForLocation === 0) {
184 $left += $quantityForLocation;
185 $this->formatLocation($location, "Delivery", $left);
186 unset($quantityForLocation);
189 public function tableHeader() {
190 return new _hx_array(array("Order", "Customer", "Quantity", "Before", "After", "Loc", "From", "Required Date", "Comment"));
194 public $startLocation;
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();
204 throw new HException('Unable to call «'.$m.'»');
206 static function orderId($order) {
207 return "order_" . $order->id;
209 function __toString() { return 'ItemScheduler'; }
211 function ItemScheduler_0(&$orders, $o) {
216 function ItemScheduler_1(&$orders, &$priorities, $a, $b) {
218 $as = $a->toString();
219 $bs = $b->toString();
229 function ItemScheduler_2(&$_g, &$orderList, &$rows, $a, $b) {
231 return $_g->parameters->priority($a) - $_g->parameters->priority($b);
234 function ItemScheduler_3(&$locations, &$startDate, $a, $b) {
236 $as = $a->delivery->getTime();
237 $bs = $b->delivery->getTime();