if ($oi['status'] < 4) {
             $class_o->productionTargetInReset($oi);
         }
     }
 }
 //IF SOURCE ORDER STATUS IS (NEW/SOURCING) OR (READY AND PI IS NOT YET PRINTED)
 if (in_array($oi['status'], $oi_status_to_reset_stock_status) || $oi['status'] == '3' && $oi['print_count'] == 0) {
     //CHECK STOCK STATUS TO DECIDE WHAT STOCK SHOULD BE READDED
     if ($oi['stock_status'] == 'R') {
         //ADD BACK ELEMENTS STOCK AUTOMATICALLY
         $eu = countElementsUsage($oi);
         foreach ($eu as $e_id => $e_qty) {
             if (!in_array($elements_categories[$e_id], $STOCKLESS_ELEMENTS_CATEGORY)) {
                 $stock_id = $e_id;
                 $order_qty = $oi['order_quantity'] * $e_qty;
                 $class_es->addStockByPieces($whid_elements, $e_id, $order_qty, 'ADD-EL ' . $order_type . '-' . $oiid);
                 $elements_stock[$stock_id] += $order_qty;
             }
         }
     } elseif ($oi['stock_status'] == 'P') {
         //ADD BACK FG STOCK AUTOMATICALLY
         $stock_id = $oi['products_id'] . $oi['products_articles_id'];
         $order_qty = $oi['order_quantity'];
         $class_pm->stockAdd($whid_prod, $oi['products_id'], $oi['products_articles_id'], $order_qty, 'ADD-FG ' . $order_type . '-' . $oiid);
         $products_stock_bali[$stock_id] += $order_qty;
     }
 }
 $oi_trans = null;
 //PRODUCT IS ALREADY MOVED TO IN HH TAB (MMO) - Retrieve MMO details as $oi_trans
 if ($oi['status'] == $new_status) {
     $oi_trans = $class_do->retrieveDetailTransmigran($order_type, $oiid);
function cancelOrderStockHandler($type, $oiid, $stock_status, $status, $print_count, $print_confirmed, $products_id, $articles_id, $quantity, $is_promotion_order = false)
{
    global $STOCKLESS_ELEMENTS_CATEGORY;
    use_class('products_minierp');
    use_class('elements_stock');
    use_class('element');
    $class_pm = new products_minierp();
    $class_es = new elements_stock();
    $timestamp = date('Y-m-d H:i:s');
    if (in_array($status, array('1', '2', '3')) && $print_confirmed == 0 || $status == '8' && $print_count == 0) {
        switch ($stock_status) {
            case 'D':
                //DEPOT STOCK
                $class_pm->stockAdd(WAREHOUSE_ID_FOR_HANDLING_ORDERS, $products_id, $articles_id, $quantity, 'ADD CANCEL ' . $type . '-' . $oiid);
                break;
            case 'P':
                //FG STOCK
                $class_pm->stockAdd(WAREHOUSE_ID_PRODUCTS_BALI, $products_id, $articles_id, $quantity, 'ADD CANCEL ' . $type . '-' . $oiid);
                break;
            case 'R':
                //ELEMENTS STOCK
                $elements_used = array();
                $el_usage = $class_pm->retrieveElementsUsage($products_id, $articles_id);
                foreach ($el_usage as $e) {
                    $elements_used[$e['elements_id']] = $e['quantity'];
                }
                foreach ($elements_used as $eid => $el_qty) {
                    $el = new element($eid);
                    $el_cat_id = !is_null($el->attributes['category']['id']) ? $el->attributes['category']['id'] : '';
                    if (!in_array($el_cat_id, $STOCKLESS_ELEMENTS_CATEGORY)) {
                        $stock_qty = $quantity * $el_qty;
                        $current_stock = $class_es->addStockByPieces(WAREHOUSE_ID_ELEMENTS, $eid, $stock_qty, 'ADD CANCEL ' . $type . '-' . $oiid, $is_promotion_order);
                    }
                }
                break;
        }
    }
}
     exit;
 } elseif ($_POST['me_action'] == 'STOCKUPDATE') {
     $elstock_update = true;
     $jng_warehouses_id = tep_db_prepare_input($_POST['jng_warehouses_id']);
     $elements_id = tep_db_prepare_input($_POST['elements_id']);
     $qty_type = tep_db_prepare_input($_POST['qty_type']);
     $qty_unit = tep_db_prepare_input($_POST['qty_unit']);
     $ppqt = tep_db_prepare_input($_POST['pieces_per_qty_type']);
     $update_action = tep_db_prepare_input($_POST['update_action']);
     $stock_value = tep_db_prepare_input($_POST['stock_value']);
     $stock_value_pieces = $_POST['update_mode'] == 'unit' ? $stock_value * $ppqt : $stock_value;
     $qty_type_updated = $_POST['update_mode'] == 'unit' ? $qty_type : $qty_unit;
     $red_rea = $reduce_reason[$_POST['reduce_reason']];
     switch ($update_action) {
         case 'ADD':
             $class_es->addStockByPieces($jng_warehouses_id, $elements_id, $stock_value_pieces, "ADD by {$session_userinfo['username']}");
             $message = "{$stock_value} {$qty_type_updated} is added to stock";
             break;
         case 'ADJ':
             $message = "Stock is updated to {$stock_value} {$qty_type_updated}";
             $class_es->updateStockByPieces($jng_warehouses_id, $elements_id, $stock_value_pieces, "ADJUSTED  by {$session_userinfo['username']}");
             break;
         case 'RED':
             $message = "{$stock_value} {$qty_type_updated} is reduced from stock";
             $class_es->reduceStockByPieces($jng_warehouses_id, $elements_id, $stock_value_pieces, "REDUCE by {$session_userinfo['username']} ({$red_rea})");
             break;
     }
     $message = '<div class="messagebox green">' . $message . '</div>';
     $el_stock = $message . getElementStock($jng_warehouses_id, $elements_id);
     echo utf8_encode($el_stock);
     exit;