Beispiel #1
0
 function updateProductStatus($items_id, $new_status, $updater = '', $cancelby = 'J', $add_to_prod_target = true)
 {
     /*==============================================================
       * PURPOSE  :   TO CHANGE PROCESS STATUS ORDER OF PRODUCTS
       * PARAMS   :   $items_id (NUMBER) >> e.g: 1234
       *              $new_status (NUMBER) >> e.g: 1
       *              $updater (STRING) "OPTIONAL" >> e.g: 'mbosse'
       *              $cancelby (STRING) "OPTIONAL" >> e.g: 'S'
       ===============================================================*/
     use_class('production_target');
     $pt = new production_target();
     list($new_status, $sub_status) = explode('-', $new_status);
     $item = $this->retrieveProductDetail($items_id);
     $itemMode = modeName($item['mode']);
     $timestamp = date('Y-m-d H:i:s');
     $is_reviving = statusIsCancelled($item['status']) && !statusIsCancelled($new_status);
     //Revive Item should move to in HH directly if other items are already in HH
     if ($is_reviving) {
         $item_others = $this->retrieveProducts($item['orders_id']);
         $others_in_hh = false;
         //If there's other items are already in HH set new status directly to HH
         foreach ($item_others as $io) {
             if ($io['status'] == '8') {
                 $new_status = '8';
                 $others_in_hh = true;
                 break;
             }
         }
         //If there's no items are already in HH (Full MTO)
         if (!$others_in_hh) {
             use_class('depot_orders');
             $class_do = new depot_orders();
             $item_do = $class_do->retrieveDetailTransmigran('JG', $items_id);
             if (!is_null($item_do)) {
                 if ($item_do['status'] < 6) {
                     /*  Order status is before in Package, do below steps:
                            1) Set real status same with last status of DP
                            2) Consider also set sub production status "only" when main status is in production
                            3) Cancel "directly" DP order, means we not use doCancelOrder
                         */
                     $new_status = $item_do['status'];
                     $sub_status = $new_status == '4' ? $item_do['prod_status'] : '';
                     $class_do->updateStatus($item_do['depot_orders_id'], '12', 'auto-set');
                     $reviving_need_next_update = true;
                     $revived_stock_status = $item_do['stock_status'];
                     $revived_mode = 'MTO';
                 } else {
                     /* Order status is >= "in Package"
                           1) Set real status to New
                        */
                     $new_status = '1';
                 }
             }
         }
     }
     //Only "Sent" and "Complete" status can be updated to "Returned"
     if ($new_status == '14' && !($item['status'] == '9' || $item['status'] == '11')) {
         return false;
     }
     //KPI Logging
     if ($add_to_prod_target) {
         if ($new_status == '5') {
             $pt->addDataToField($timestamp, 'finish', $item['products_quantity']);
         }
         if ($new_status == '6') {
             $pt->addDataToField($timestamp, 'package', $item['products_quantity']);
         }
         if ($new_status == '7') {
             $pt->addDataToField($timestamp, 'sent', $item['products_quantity']);
         }
         if ($new_status == '16') {
             $pt->addDataToField($timestamp, 'log_return_to_bali', $item['products_quantity']);
         }
         if ($new_status == '19') {
             $pt->addDataToField($timestamp, 'log_return_missing', $item['products_quantity']);
         }
         if ($new_status == '20') {
             $pt->addDataToField($timestamp, 'log_return_to_bin_in', $item['products_quantity']);
         }
         if ($new_status == '9') {
             $pt->addDataToField($timestamp, 'log_products_sent_b2c', $item['products_quantity']);
             if ($itemMode == 'DSO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_full_dso', $item['products_quantity']);
             } elseif ($itemMode == 'MTO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_full_mto', $item['products_quantity']);
             } elseif ($itemMode == 'WMO' || $itemMode == 'MMO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_mo', $item['products_quantity']);
             }
         }
     }
     //Update Status and create Status History
     $sda_op = array('status' => $new_status);
     $sda_op['status'] = $new_status;
     if ($new_status == '10') {
         $sda_op['products_problem'] = $cancelby;
     } elseif ($this->productStatusIsCancelled($item['status']) && !$this->productStatusIsCancelled($new_status)) {
         $sda_op['products_problem'] = '0';
     }
     $sub_status = trim(strtoupper($sub_status));
     if ($new_status == '4' && $sub_status != '') {
         $sda_op['prod_status'] = $sub_status;
     }
     //PS: please don't set empty prod_status when it updated to status > 4,
     //since this will used for check whether order could transferred to DP or not
     //when real order taken over by HH when it from production
     tep_db_perform('orders_products', $sda_op, 'update', "orders_products_id={$items_id}");
     $save_main_status_history = true;
     if (in_array($item['status'], array('3', '4', '5')) && $new_status == '4' && ($sub_status != '' && trim(strtoupper($item['prod_status'])) != $sub_status) || $is_reviving && $new_status == '4') {
         $psh = array();
         $psh['orders_items_id'] = $items_id;
         $psh['type'] = 'JG';
         $psh['status'] = $sub_status;
         $psh['status_quantity'] = $item['products_quantity'];
         $psh['status_date'] = $timestamp;
         $psh['update_by'] = $updater;
         saveProductionStatusHistory($psh);
         if ($item['status'] == '4') {
             $save_main_status_history = false;
         }
     }
     if ($save_main_status_history) {
         $sda = array();
         $sda['orders_products_id'] = $items_id;
         $sda['status'] = $new_status;
         $sda['status_quantity'] = $item['products_quantity'];
         $sda['status_date'] = $timestamp;
         $sda['update_by'] = $updater;
         tep_db_perform('orders_products_status_history', $sda);
     }
     //Revive Additional Action
     if ($is_reviving) {
         //Revive Items from Cancel Request Status
         $this->recalcProductsCounter($item['orders_id']);
         if ($new_status == '8') {
             $this->printCountReset($items_id);
             $this->updateOrderItemMode($items_id, 'MMO');
             use_class('depot_orders');
             $class_do = new depot_orders();
             $item_do = $class_do->transferFromRealOrder(SEGMENT_ID_HAMBURG, 'JG', $items_id);
             if ($item_do['status'] > 7) {
                 $class_do->updateStatus($item_do['depot_orders_id'], '1');
                 $class_do->printCountReset($item_do['depot_orders_id']);
             }
         }
         if ($reviving_need_next_update) {
             $this->updateProductStockStatus($items_id, $revived_stock_status);
             if ($revived_mode != '') {
                 $this->updateOrderItemMode($items_id, $revived_mode);
             }
         }
         //Set need repackaging flag if needed
         use_class('products_minierp');
         $class_pm = new products_minierp();
         $p_data = $class_pm->retrieveDetail($item['products_id'], 'p');
         if ($p_data['p']['need_repackaging'] == 1) {
             $this->setNeedRepackaging($item['orders_id']);
         }
     }
     //Check other available products of this order which need repackage "nr" on cancel
     if ($new_status == '10') {
         use_class('products_minierp');
         $class_pm = new products_minierp();
         $need_repackaging = false;
         $nr_q = "SELECT products_id FROM orders_products WHERE orders_id = {$item['orders_id']} AND status NOT IN (10, 12)" . " AND orders_products_id != {$items_id}";
         $nr_dbq = tep_db_query($nr_q);
         while ($nr_r = tep_db_fetch_array($nr_dbq)) {
             $p_data = $class_pm->retrieveDetail($nr_r['products_id'], 'p');
             if ($p_data['p']['need_repackaging'] == 1) {
                 $need_repackaging = true;
                 break;
             }
         }
         if (!$need_repackaging) {
             $this->resetNeedRepackaging($item['orders_id']);
         }
     }
     //Recalculate Orders Total, Products Counter on cancellation and reviving
     if ($new_status == '10' || $is_reviving) {
         $this->recalcTotals($item['orders_id']);
         $this->recalcProductsCounter($item['orders_id']);
         $this->recalcVouchersAndShippingCost($item['orders_id'], false);
     }
     //Update Stock Status and Elements Stock if necessary
     if ($new_status == '2' && $item['stock_status'] != 'S') {
         $this->updateProductStockStatus($items_id, false);
         $this->prodStatusReset($items_id);
         $this->eanPrintedReset($items_id);
         //$this->printCountReset($items_id);    //now we don't need to reset print_count for reprint pi when move to ready again.
     } elseif ($new_status == '3' && $item['stock_status'] == 'S' || $new_status == '4' && $item['stock_status'] == 'W') {
         $eu = $this->countElementsUsage($item['products_id'], $item['products_articles_id']);
         $jng_warehouses_id = '1';
         use_class('elements_stock');
         $class_es = new elements_stock();
         foreach ($eu as $elements_id => $euqty) {
             $reduce_amount = $item['products_quantity'] * $euqty;
             $class_es->reduceStockByPieces($jng_warehouses_id, $elements_id, $reduce_amount, 'RED JG-' . $items_id);
         }
         $this->updateProductStockStatus($items_id, 'R');
     } elseif ($new_status == '1') {
         //IF THERES ORDERS NEEDS RESET BACK TO NEW
         $this->printCountReset($items_id);
         $this->printConfirmReset($items_id);
         $this->prodStatusReset($items_id);
         $this->eanPrintedReset($items_id);
         $this->updateProductStockStatus($items_id, 0);
         $this->updateOrderItemMode($items_id, 'UNSET');
     }
     //Production Target Flag Counter
     if ($item['status'] == '1' && ($new_status == '2' || $new_status == '3')) {
         $this->productionTargetIn($timestamp, $item);
     } elseif ($new_status == '6') {
         $this->productionTargetOut($timestamp, $item);
     } elseif ($new_status == '10') {
         $this->productionTargetInReset($item);
     }
     //Auto Print PI rules
     if ($new_status == '3' || $new_status == '12') {
         $orders_id = $item['orders_id'];
         //$q_pi_status = tep_db_query("SELECT COUNT(1)=order_item_total AS pi_ready_to_print FROM orders_products WHERE orders_id = $orders_id AND status = 3 AND print_count = 0 GROUP BY orders_id");
         //ALSO CONSIDER GIFT/VOUCHER THAT DIRECTLY SET AS "SENT TO CUSTOMER" BUT print_count still zero
         $q_pi_status = tep_db_query("SELECT COUNT(1)=order_item_total AS pi_ready_to_print FROM orders_products WHERE orders_id = {$orders_id} AND status IN (3, 9) AND print_count = 0 GROUP BY orders_id");
         $pi_status = tep_db_fetch_array($q_pi_status);
         if ($pi_status['pi_ready_to_print'] == 1) {
             //PI PRINTED FOR THE FIRST TIME
             /* HANDLED BY BATCH PRINTING CRON cron/print-pi.php
                use_class('production_instruction_pdf');
                $class_pi = new production_instruction_pdf();
                $class_pi->addOrder('JG', $orders_id);
                $class_pi->createPDF(false, 'F', true);
                 */
         } else {
             if ($item['print_count'] > 0 && $item['print_confirmed'] > 0) {
                 //SECOND OR MORE PI PRINTED, could happen when there product by stock_status is "OK" but in real stock is "0" so this will set back to "NS". And when ready this will reprinted by using this block
                 $this->printConfirmReset($item['orders_products_id']);
                 $this->printCountReset($item['orders_products_id']);
                 /*
                                         use_class('production_instruction_pdf');
                                         $class_pi = new production_instruction_pdf();
                                         $class_pi->addItem('JG', $item['orders_products_id']);
                                         $class_pi->createPDF(false, 'F', false);    //not add to daily statistic, we consider this as not "NEW"
                 */
             }
         }
     }
     $q = tep_db_query("SELECT o.orders_id, o.orders_status, o.payment_method FROM orders_products op LEFT JOIN orders o ON o.orders_id=op.orders_id WHERE op.orders_products_id={$items_id}");
     $order = tep_db_fetch_array($q);
     if ($new_status == '12' || $item['products_problem'] == '0') {
         $status_to_update_orders = array('2' => '6', '4' => '5', '13' => '6', '5' => '7', '7' => '8', '9' => '10', '12' => '12');
         //cancelled
         if (array_key_exists($new_status, $status_to_update_orders)) {
             $changeorderstatus = true;
             if ($new_status == '12') {
                 //dont update order status if not all products are cancelled
                 if (!$this->isAllProductsCancelled($order['orders_id'])) {
                     $changeorderstatus = false;
                 }
             }
             if ($changeorderstatus) {
                 $order_new_status = $status_to_update_orders[$new_status];
                 if ($order['orders_status'] < $order_new_status) {
                     $this->updateOrderStatus($order['orders_id'], $order_new_status);
                 }
             }
         }
         if ($new_status == '12' && (strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_BANK_TRANSFER) || strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_PAYPAL))) {
             /*----------------------------------------------------------------------------------------
               * MAKE RETURN ENTRY FOR "PREPAYMENT ONLY" ON "CANCEL CONFIRM", SINCE FOR NON-PREPAYMENT, 
               * PAYONE INVOICE WILL CREATED WHEN PRODUCTS IS SENT, 
               * SO NO NEED TO ADJUST IT OR CREATE RETURN CONTAINER WHEN CANCEL CONFIRM PROCESS
               -----------------------------------------------------------------------------------------*/
             use_class('payone');
             use_class('payone_invoice');
             $class_payone = new payone();
             $payi = new payone_invoice('invoice_order_id', $order['orders_id'], "invoice_order_type = 'J'");
             if ($payi->isUncapturedPrePayment()) {
                 //UNCAPTURED PREAUTH VORKASSE NEED TO RECALCULATE INVOICE
                 $payi->recalculate();
                 //SET AS PAID WHEN OPEN BALANCE LEAVE SMALL AMOUNT OR THIS CANCEL PROCESS MAKES UNDERPAID BALANCE BECOMES PAID/OVERPAID
                 if ($payi->getBalance() * 100 <= PAYONE_MAX_RETURNED_OPEN_PAYMENT) {
                     $this->updatePaymentStatus($item['orders_id'], '1', 'auto-set');
                 }
             } else {
                 //AUTH VORKASSE, CAPTURED PREAUTH VORKASSE, PAYPAL ORDER WILL ADD TO RETURN CONTAINER
                 $class_payone->createReturnContainer('NULL', $item['products_quantity'], $items_id);
             }
         }
     }
     //NEED TO RECALCULATE INVOICE WHEN REVIVING UNCAPTURED PREAUTH VORKASSE
     if ($is_reviving && strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_PREPAYMENT)) {
         use_class('payone_invoice');
         $payi = new payone_invoice('invoice_order_id', $order['jng_sp_orders_id'], "invoice_order_type = 'S'");
         if ($payi->isUncapturedPrePayment()) {
             $payi->recalculate();
         }
     }
     //SET READY TO CAPTURED FOR PREPAYMENT PREAUTH THAT ALREADY SENT
     if ($new_status == '9' && strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_BANK_TRANSFER)) {
         use_class('payone_invoice');
         $payi = new payone_invoice('invoice_order_id', $item['orders_id'], "invoice_order_type = 'J'");
         $payi->setIsCaptured('2');
     }
     return $new_status;
 }
 function updateItemStatus($items_id, $new_status, $updater = '', $cancelby = 'J', $add_to_prod_target = true)
 {
     use_class('production_target');
     $pt = new production_target();
     list($new_status, $sub_status) = explode('-', $new_status);
     $item = $this->retrieveItemDetail($items_id);
     $itemMode = modeName($item['mode']);
     $timestamp = date('Y-m-d H:i:s');
     $is_reviving = statusIsCancelled($item['status']) && !statusIsCancelled($new_status);
     $reviving_need_next_update = false;
     //Revive Item should move to in HH directly if other items are already in HH
     if ($is_reviving) {
         $item_others = $this->retrieveItems($item['jng_sp_orders_id']);
         $others_in_hh = false;
         //If there's other items are already in HH set new status directly to HH
         foreach ($item_others as $io) {
             if ($io['status'] == '8') {
                 $new_status = '8';
                 $others_in_hh = true;
                 break;
             }
         }
         //If there's no items are already in HH (Full MTO)
         if (!$others_in_hh) {
             use_class('depot_orders');
             $class_do = new depot_orders();
             $item_do = $class_do->retrieveDetailTransmigran('SP', $items_id);
             if (!is_null($item_do)) {
                 if ($item_do['status'] < 6) {
                     /*  Order status is before in Package, do below steps:
                            1) Set real status same with last status of DP
                            2) Consider also set sub production status "only" when main status is in production
                            3) Cancel "directly" DP order, means we not use doCancelOrder
                         */
                     $new_status = $item_do['status'];
                     $sub_status = $new_status == '4' ? $item_do['prod_status'] : '';
                     $class_do->updateStatus($item_do['depot_orders_id'], '12', 'auto-set');
                     $reviving_need_next_update = true;
                     $revived_stock_status = $item_do['stock_status'];
                     $revived_mode = 'MTO';
                 } else {
                     /* Order status is >= "in Package"
                           1) Set real status to New
                        */
                     $new_status = '1';
                 }
             }
         }
     }
     //Only "Sent" and "Complete" status can be updated to "Returned"
     if ($new_status == '14' && !($item['status'] == '9' || $item['status'] == '11')) {
         return false;
     }
     //KPI Logging
     if ($add_to_prod_target) {
         if ($new_status == '5') {
             $pt->addDataToField($timestamp, 'finish', $item['order_quantity']);
         }
         if ($new_status == '6') {
             $pt->addDataToField($timestamp, 'package', $item['order_quantity']);
         }
         if ($new_status == '7') {
             $pt->addDataToField($timestamp, 'sent', $item['order_quantity']);
         }
         if ($new_status == '16') {
             $pt->addDataToField($timestamp, 'log_return_to_bali', $item['order_quantity']);
         }
         if ($new_status == '19') {
             $pt->addDataToField($timestamp, 'log_return_missing', $item['order_quantity']);
         }
         if ($new_status == '20') {
             $pt->addDataToField($timestamp, 'log_return_to_bin_in', $item['order_quantity']);
         }
         if ($new_status == '9') {
             use_class('jng_sp');
             $class_jsp = new jng_sp();
             //Get Sales Partner B2B
             $sp_b2b = array_keys($class_jsp->retrieveList("sp_type = 1"));
             $item_orders = $this->retrieveDetail($item['jng_sp_orders_id']);
             if (in_array($item_orders['jng_sp_id'], $sp_b2b)) {
                 $pt->addDataToField($timestamp, 'log_products_sent_b2b', $item['order_quantity']);
             } else {
                 $pt->addDataToField($timestamp, 'log_products_sent_b2c', $item['order_quantity']);
             }
             if ($itemMode == 'DSO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_full_dso', $item['order_quantity']);
             } elseif ($itemMode == 'MTO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_full_mto', $item['order_quantity']);
             } elseif ($itemMode == 'WMO' || $itemMode == 'MMO') {
                 $pt->addDataToField($timestamp, 'log_products_sent_mo', $item['order_quantity']);
             }
         }
     }
     $sub_status = trim(strtoupper($sub_status));
     if ($updater != 'new-import') {
         $setcancelation = $item['status'] == '10' && $new_status != '12' ? ", cancel_status=NULL" : '';
         if ($new_status == '10') {
             $setcancelation = ", cancel_status='{$cancelby}'";
         }
         $set_prod_status = $new_status == '4' && $sub_status != '' ? ", prod_status = '" . $sub_status . "'" : '';
         //PS: please don't set empty prod_status when it updated to status > 4,
         //since this will used for check whether order could transferred to DP or not
         //when real order taken over by HH when it from production
         tep_db_query("UPDATE jng_sp_orders_items SET status='{$new_status}' {$setcancelation} {$set_prod_status} WHERE jng_sp_orders_items_id={$items_id}");
     }
     $save_main_status_history = true;
     if (in_array($item['status'], array('3', '4', '5')) && $new_status == '4' && ($sub_status != '' && trim(strtoupper($item['prod_status'])) != $sub_status) || $is_reviving && $new_status == '4') {
         $psh = array();
         $psh['orders_items_id'] = $items_id;
         $psh['type'] = 'SP';
         $psh['status'] = $sub_status;
         $psh['status_quantity'] = $item['order_quantity'];
         $psh['status_date'] = $timestamp;
         $psh['update_by'] = $updater;
         saveProductionStatusHistory($psh);
         if ($item['status'] == '4') {
             $save_main_status_history = false;
         }
     }
     if ($save_main_status_history) {
         $sda = array();
         $sda['jng_sp_orders_items_id'] = $items_id;
         $sda['status'] = $new_status;
         $sda['status_quantity'] = $item['order_quantity'];
         $sda['status_date'] = $timestamp;
         $sda['update_by'] = $updater;
         tep_db_perform('jng_sp_orders_items_status_history', $sda);
     }
     if ($is_reviving) {
         //Revive Items from Cancel Request Status
         $this->recalculateOrderItemCount(null, $items_id);
         if ($new_status == '8') {
             $this->printCountReset($items_id);
             $this->updateOrderItemMode($items_id, 'MMO');
             use_class('depot_orders');
             $class_do = new depot_orders();
             $item_do = $class_do->transferFromRealOrder(SEGMENT_ID_HAMBURG, 'SP', $items_id);
             $do_close_status = explode(',', depot_orders::FILTER_STATUS_CLOSE);
             if (in_array($item_do['status'], $do_close_status)) {
                 $class_do->updateStatus($item_do['depot_orders_id'], '1');
                 $class_do->printCountReset($item_do['depot_orders_id']);
             }
         }
         if ($reviving_need_next_update) {
             $this->updateItemStockStatus($items_id, $revived_stock_status);
             if ($revived_mode != '') {
                 $this->updateOrderItemMode($items_id, $revived_mode);
             }
         }
         //Set need repackaging flag if needed
         $class_pm = new products_minierp();
         $p_data = $class_pm->retrieveDetail($item['products_id'], 'p');
         if ($p_data['p']['need_repackaging'] == 1) {
             $this->setNeedRepackaging($item['jng_sp_orders_id']);
         }
     }
     //Check other available products of this order which need repackage "nr" on cancel
     if ($new_status == '10') {
         $class_pm = new products_minierp();
         $need_repackaging = false;
         $nr_q = "SELECT products_id FROM jng_sp_orders_items WHERE jng_sp_orders_id = {$item['jng_sp_orders_id']} AND status NOT IN (10, 12)" . " AND jng_sp_orders_items_id != {$items_id}";
         $nr_dbq = tep_db_query($nr_q);
         while ($nr_r = tep_db_fetch_array($nr_dbq)) {
             $p_data = $class_pm->retrieveDetail($nr_r['products_id'], 'p');
             if ($p_data['p']['need_repackaging'] == 1) {
                 $need_repackaging = true;
                 break;
             }
         }
         if (!$need_repackaging) {
             $this->resetNeedRepackaging($item['jng_sp_orders_id']);
         }
     }
     //Update Stock Status and Elements Stock if necessary
     if ($new_status == '2' && $item['stock_status'] != 'S' && $item['stock_status'] != 'W') {
         //Set stock status to need sourcing if item is move to sourcing tab
         $this->updateItemStockStatus($items_id, false);
         $this->prodStatusReset($items_id);
         $this->eanPrintedReset($items_id);
         //$this->printCountReset($items_id);    //now we don't need to reset print_count for reprint pi when move to ready again.
         //TODO: Ceck if above statement is still true with new batch pi printing
     } elseif ($new_status == '3' && $item['stock_status'] == 'S' || $new_status == '4' && $item['stock_status'] == 'W') {
         //Reduce elements stock when products are forced to move to ready/production before approved by elements stock check
         $eu = $this->countElementsUsage($item['products_id'], $item['products_articles_id']);
         if (count($eu) > 0) {
             $jng_warehouses_id = '1';
             use_class('elements_stock');
             $class_es = new elements_stock();
             foreach ($eu as $elements_id => $euqty) {
                 $reduce_amount = $item['order_quantity'] * $euqty;
                 $class_es->reduceStockByPieces($jng_warehouses_id, $elements_id, $reduce_amount, 'RED SP-' . $items_id);
             }
         }
         $this->updateItemStockStatus($items_id, 'R');
     } elseif ($new_status == '1') {
         //IF THERES ORDERS NEEDS RESET BACK TO NEW
         $this->printCountReset($items_id);
         $this->printConfirmReset($items_id);
         $this->prodStatusReset($items_id);
         $this->eanPrintedReset($items_id);
         $this->updateItemStockStatus($items_id, 0);
         $this->updateOrderItemMode($items_id, 'UNSET');
     }
     //Production Target Flag Counter
     if ($item['status'] == '1' && ($new_status == '2' || $new_status == '3')) {
         //TODO: Move TARGET IN TO WHEN PI IS CONFIRMED BY SCANNING IT FOR EAN LABEL
         $this->productionTargetIn($timestamp, $item);
     } elseif ($new_status == '6') {
         $this->productionTargetOut($timestamp, $item);
     } elseif ($new_status == '10') {
         $this->productionTargetInReset($item);
     }
     //Auto Print PI rules
     if ($new_status == '3' || $new_status == '12') {
         /* e.g. :
                    5 items ordered > 4 to ready, 1 to sourcing ( PI not printed yet )
                    so this block is for handling PI not printed when "1 sourcing" is cancelled, while print_count flag is still "0"
             * !!!ABOVE SAMPLE PROCESS ISN'T RELIABLE AGAIN, SINCE WE NOW CHANGE PRINT PI DIRECTLY WHEN IT MOVES TO READY TAB VIA CRON cron/print-pi.php!!!
            */
         $jng_sp_orders_id = $item['jng_sp_orders_id'];
         $q_pi_status = tep_db_query("SELECT COUNT(1)=order_item_total AS pi_ready_to_print FROM jng_sp_orders_items WHERE jng_sp_orders_id = {$jng_sp_orders_id} AND status = 3 AND print_count = 0 GROUP BY jng_sp_orders_id");
         $pi_status = tep_db_fetch_array($q_pi_status);
         if ($pi_status['pi_ready_to_print'] == 1) {
             //PI PRINTED FOR THE FIRST TIME
             /* HANDLED BY BATCH PRINTING CRON cron/print-pi.php
                use_class('production_instruction_pdf');
                $class_pi = new production_instruction_pdf();
                $class_pi->addOrder('SP', $jng_sp_orders_id);
                $class_pi->createPDF(false, 'F', true);
                 */
         } else {
             if ($item['print_count'] > 0 && $item['print_confirmed'] > 0) {
                 //SECOND OR MORE PI PRINTED, could happen when there product by stock_status is "OK" but in real stock is "0" so this will set back to "NS". And when ready this will reprinted by using this block
                 //reset print_count and print_confirmed to include this order again on next print-pi scheduler
                 $this->printConfirmReset($item['jng_sp_orders_items_id']);
                 $this->printCountReset($item['jng_sp_orders_items_id']);
                 /*
                                     use_class('production_instruction_pdf');
                                     $class_pi = new production_instruction_pdf();
                                     $class_pi->addItem('SP', $item['jng_sp_orders_items_id']);
                                     $class_pi->createPDF(false, 'F', false);    //not add to daily statistic, we consider this as not "NEW"
                 */
             }
         }
     }
     $q = tep_db_query("SELECT jo.jng_sp_orders_id, UPPER(jo.payment_method) AS payment_method FROM jng_sp_orders_items joi LEFT JOIN jng_sp_orders jo ON jo.jng_sp_orders_id=joi.jng_sp_orders_id WHERE joi.jng_sp_orders_items_id={$items_id}");
     $order = tep_db_fetch_array($q);
     if ($new_status == '12' && strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_PREPAYMENT)) {
         /*----------------------------------------------------------------------------------------
           * MAKE RETURN ENTRY FOR "PREPAYMENT ONLY" ON "CANCEL CONFIRM", SINCE FOR NON-PREPAYMENT, 
           * PAYONE INVOICE WILL CREATED WHEN PRODUCTS IS SENT, 
           * SO NO NEED TO ADJUST IT OR CREATE RETURN CONTAINER WHEN CANCEL CONFIRM PROCESS
           -----------------------------------------------------------------------------------------*/
         use_class('payone');
         use_class('payone_invoice');
         $class_payone = new payone();
         $payi = new payone_invoice('invoice_order_id', $order['jng_sp_orders_id'], "invoice_order_type = 'S'");
         if ($payi->isUncapturedPrePayment()) {
             //UNCAPTURED PREAUTH PREPAYMENT NEED TO RECALCULATE INVOICE
             $payi->recalculate();
             //SET AS PAID WHEN OPEN BALANCE LEAVE SMALL AMOUNT OR THIS CANCEL PROCESS MAKES UNDERPAID BALANCE BECOMES PAID/OVERPAID
             if ($payi->getBalance() * 100 <= PAYONE_MAX_RETURNED_OPEN_PAYMENT) {
                 $this->updatePaymentStatus($item['jng_sp_orders_id'], '1', 'auto-set');
             }
         } else {
             //AUTH PREPAYMENT, CAPTURED PREAUTH PREPAYMENT (ADD TO RETURN CONTAINER)
             $class_payone->createReturnContainerSP('NULL', $item['order_quantity'], $items_id);
         }
     }
     //NEED TO RECALCULATE INVOICE WHEN REVIVING UNCAPTURED PREAUTH PREPAYMENT
     if ($is_reviving && strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_PREPAYMENT)) {
         use_class('payone_invoice');
         $payi = new payone_invoice('invoice_order_id', $order['jng_sp_orders_id'], "invoice_order_type = 'S'");
         if ($payi->isUncapturedPrePayment()) {
             $payi->recalculate();
         }
     }
     //SET READY TO CAPTURED FOR PREPAYMENT PREAUTH THAT ALREADY SENT
     if ($new_status == '9' && strtoupper($order['payment_method']) == strtoupper(PAYMENT_METHOD_PREPAYMENT)) {
         use_class('payone_invoice');
         $payi = new payone_invoice('invoice_order_id', $item['jng_sp_orders_id'], "invoice_order_type = 'S'");
         $payi->setIsCaptured('2');
     }
     return $new_status;
 }