}
            $total_products[$jng_warehouses_id][$products_id][$articles_id] += $row['order_quantity'];
        }
    }
}
foreach ($total_products as $jng_warehouses_id => $products) {
    foreach ($products as $products_id => $articles) {
        foreach ($articles as $aid => $add_amount) {
            $class_pm->stockAdd($jng_warehouses_id, $products_id, $add_amount, $aid);
            $class_ms->products_state($jng_warehouses_id, $products_id, $period, true);
        }
    }
}
//ELEMENTS
$jng_warehouses_id = 1;
$stocks = $class_es->retrieveAll($jng_warehouses_id);
foreach ($stocks as $eid => $stock) {
    $class_ms->elements_state($jng_warehouses_id, $eid, $period, true);
}
//FG Bali
$jng_warehouses_id = 7;
$stocks = $class_pm->stockRetrieveList($jng_warehouses_id);
foreach ($stocks as $s) {
    if ($s['stock'] > 0) {
        var_dump($s);
        echo '<br />';
        $class_ms->products_state($jng_warehouses_id, $s['products_id'], $period, true);
    }
}
echo 'Data is Processed!';
tep_db_close();
//REMOVE ANY OPEN EXISTING SUGGESTIONS BEFORE MAKING NEW ONES
elements_preorder::clearAllExistingOpenedSuggestion();
$logger->write('Existing Opened Suggestion is cleared!');
elements_preorder::clearPreordersTemp();
$logger->write('Existing Preorders temporary data is cleared!');
//Grab all existing elements, let's grab the data per element as an object
$r = tep_db_query("SELECT e.elements_id FROM element e " . "WHERE e.is_finalized='1' ORDER BY elements_id");
$elements = array();
while ($row = tep_db_fetch_array($r)) {
    $elements[] = $row['elements_id'];
}
$total_elements = count($elements);
$logger->write("Found {$total_elements} element(s) to process");
//COLLECTIVE QUERY
//Grab all data that can be retrieved collectively for the whole elements
$elements_stock = $class_es->retrieveAll(WAREHOUSE_ID_ELEMENTS);
$elements_open_po = elements_order::openOrdersQuantityAllElements();
$elements_need_in_sourcing_tab = elements_preorder::totalNeededForSourcingTabAllElements();
//Clean all existing opened suggested
//Loop through each element
$elements_with_missing_qty = array();
foreach ($elements as $eid) {
    $e = new element($eid);
    //Grab all needed data
    $target_stock = $e->getDIOHstockTarget();
    $need_in_sourcing_tab = $elements_need_in_sourcing_tab[$eid];
    $current_stock = intval($elements_stock[$eid]);
    $already_ordered = intval($elements_open_po[$eid]);
    //ANY EXISTING SUGGESTIONS/PREORDERS IS DELETED IN PREVIOUS SCRIPT
    $already_preordered = 0;
    //Check if new preorder is needed (calc missing quantity)
 $q .= " ORDER BY a.leadtime_start_date DESC";
 $result = tep_db_query($q);
 $el_id = array();
 $el = array();
 $element_name_max_chars = 30;
 while ($row = tep_db_fetch_array($result)) {
     $products_id = $row['products_id'];
     $articles_id = $row['articles_id'];
     $status = $row['stock_status'];
     $qty = $row['quantity'];
     if ($products_id != '') {
         $product = $class_pm->retrieveDetail($products_id);
         $elements = $class_pm->retrieveElementsUsage($products_id, $articles_id);
     }
     if (is_array($elements) && count($elements) > 0) {
         $elements_stock = $class_es->retrieveAll($whid);
         foreach ($elements as $eid => $usage) {
             $e = new element($eid);
             $stock = $elements_stock[$eid];
             $elusage = $qty * $usage['quantity'];
             if (isset($status) && $status == 'S') {
                 if ($stock < $elusage && !in_array($e->attributes['category']['id'], $STOCKLESS_ELEMENTS_CATEGORY)) {
                     $el['id1'] = $eid;
                     $el['tar q'] = $elusage;
                     $el_id[] = $el;
                     break;
                 }
             }
         }
     }
 }
                 $htp = array();
                 $htp['sort'] = $hcount;
                 $htp['date_add'] = date('d.m.y H:i:s', $tsproductionstatus);
                 $htp['status'] = 'Production: ' . statusNameProduction($hp['status']);
                 $htp['login'] = $hp['update_by'];
                 $htable[] = $htp;
                 unset($history_prod[$hpk]);
             }
         }
     }
 }
 if ($do['status'] == '2') {
     use_class('element');
     use_class('elements_stock');
     $class_es = new elements_stock();
     $estock = $class_es->retrieveAll($whid_material);
     $sourcing_table = array();
     $eused = $class_pm->retrieveElementsUsage($do['products_id'], $do['articles_id']);
     $sourcing_info = '<div style="margin-top:20px;">';
     $sourcing_info .= '<h3>Sourcing Information</h3>';
     if (count($eused) > 0) {
         $sourcing_table = array();
         $st = array();
         $st['d'] = 'Image';
         $st['id1'] = 'Element ID';
         $st['a'] = 'Qty Required';
         $st['o'] = 'Stock Available';
         $sourcing_table[] = $st;
         foreach ($eused as $eid => $eu) {
             $colclass = '';
             if ($eu['quantity'] > $estock[$eid]) {