Example #1
0
 $q = "SELECT products_id, articles_id, SUM(quantity) AS total_qty" . ", GROUP_CONCAT(depot_orders_id) AS order_ids" . ", GROUP_CONCAT(status) AS order_status" . " FROM depot_orders" . " WHERE trans_type = 'AR' AND" . " trans_id = " . depot_orders::AUTO_REFILL_ID_DAILYCRON . " AND (status IN (1,2,3) OR (status = 4 AND prod_status = 'P'))" . " GROUP BY products_id, articles_id";
 " ORDER BY products_id, articles_id";
 $r = tep_db_query($q);
 $total_found = tep_db_num_rows($r);
 $logger->write("{$total_found} article(s) found");
 if ($total_found > 0) {
     $canceled_qty_total = 0;
     while ($row = tep_db_fetch_array($r)) {
         $pid = $row['products_id'];
         $aid = $row['articles_id'];
         $stock_id = $pid . '-' . $aid;
         if (!isset($p) || !is_object($p) || $p->id != $pid) {
             $p = new Product($pid);
         }
         //CURRENT STOCK TARGET AFTER LEVELLING
         $target_after_levelling = $p->getDIOHstockTargetAfterLevelling($aid);
         //CURRENT STOCK (ALREADY RETRIEVED ABOVE)
         $current_stock = intval($products_stock[$stock_id]);
         //CURRENT OPEN ORDERS
         $already_ordered = $class_do->refillOpenOrdersQuantity($segment_id, $pid, $aid);
         //Check if we have too much refill
         $over_refill = $current_stock + $already_ordered - $target_after_levelling;
         $logger->write("- article {$pid}-{$aid}, Total orders = {$row['total_qty']}" . ", Over refill = {$over_refill}");
         if ($over_refill > 0) {
             //cancel orders
             $order_ids = explode(',', $row['order_ids']);
             $order_status = explode(',', $row['order_status']);
             $canceled_qty = 0;
             foreach ($order_ids as $key => $order_id) {
                 //AR is always 1 qty, so cancel all orders
                 //1 by 1 until total over_refill is reached