$oid = tep_db_prepare_input($_GET['id']);
$eo = new elements_order($oid);
$po_number = $eo->detail['po_number'] == '' ? $oid : $eo->detail['po_number'];
if (isset($_POST['me_action'])) {
    if ($_POST['me_action'] == 'CONFIRMPRICE') {
        $is_saved_to_elsup = tep_db_prepare_input($_POST['is_saved_to_elsup']) == 'true';
        $eoi_id = tep_db_prepare_input($_POST['eoi_id']);
        $price = tep_db_prepare_input($_POST['price']);
        $currency = tep_db_prepare_input($_POST['currency']);
        $eo->confirmPrice($eoi_id, $price, $currency);
        $eoi = $eo->items[$eoi_id];
        if ($is_saved_to_elsup) {
            $e = new element($eoi['elements_id']);
            $e->getSuppliers();
            foreach ($e->suppliers as $etesid => $etes) {
                $el_cur_info = elements_suppliers::formatPreviousInfo($etes['elements_price'], $etes['price_currency'], $etes['price_quantity_type'], $etes['pieces_per_qty_type'], $etes['resourcing_day'], $session_userinfo['username']);
                if ($el_cur_info != '') {
                    $prev_infos = explode('||', $el_cur_info);
                    $prev_price = $prev_infos[1];
                    $new_el_price = $currency == 'IDR' ? number_format($price, 0, '.', '') : number_format($price, 2, '.', '');
                    $price_changed = "{$currency}|{$new_el_price}" != $prev_price;
                }
                if (!$price_changed) {
                    $el_cur_info = null;
                }
                if ($etes['elements_suppliers_id'] == $eo->suppliers_id && $etes['item_number'] == $eoi['item_number']) {
                    $e->updateSupplier($etesid, $etes['elements_suppliers_id'], $price, $etes['price_quantity_type'], $etes['item_number'], $currency, $etes['discount'], $etes['pieces_per_qty_type'], $etes['is_default'], $etes['resourcing_day'], $el_cur_info);
                }
            }
        }
        $result = array();
function getElementSuppliers($elements_id)
{
    global $s_suppliers, $s_elements, $accessed, $designs_id, $session_userinfo;
    $element = new element($elements_id);
    if (is_null($element->id)) {
        $result = '<span class="red">Can not find Element ' . $msg_filter_type . ' <strong>' . $elements_id . '</strong>';
    } else {
        $suppliers = $s_suppliers->retrieveListCombo(SERVICECLIENTCODE);
        $el_suppliers = $element->getSuppliers();
        $sup_exist = array();
        if (count($el_suppliers) == 0) {
            $suppliers_table = '<span class="red">There is no suppliers info for this element yet</span>';
            $suppliers_button = '';
        } else {
            $unit_types = array('', 'strand', 'bottle', 'box', 'meter', 'gram', 'pieces', 'set', 'dozen', 'sheet', 'pack', 'roll', 'pair');
            $currencies = array('', 'IDR', 'USD', 'EUR');
            $sup_table = array();
            /*
                        $st_header_is_default = 'Default';
                        $st_header_suppplier_name = 'Supplier Name';
                        $st_header_element_code = 'Supplier Code';
                        $st_header_net_price = 'Net Price';
                        $st_header_discount = 'Discount';
                        $st_header_unit = 'Buying<br />Unit';
                        $st_header_qty_per_unit = 'Qty per<br />Buying Unit';
                        $st_header_safety_del_time = '<abbr title="Safety Delivery Time">SDT</abbr><br />in days';
                        $st_header_remove = 'Remove';
            */
            $st_header = elements_suppliers::getListTableHeader();
            $st_header_is_default = $st_header[0];
            $st_header_suppplier_name = $st_header[1];
            $st_header_element_code = $st_header[2];
            $st_header_net_price = $st_header[3];
            $st_header_discount = $st_header[4];
            $st_header_unit = $st_header[5];
            $st_header_qty_per_unit = $st_header[6];
            $st_header_safety_del_time = $st_header[7];
            $st_header_remove = $st_header[8];
            $st_header = array();
            $st_header['a w080 tac'] = $st_header_is_default;
            $st_header['b w200 tac'] = $st_header_suppplier_name;
            $st_header['c w300 tac'] = $st_header_element_code;
            $st_header['d w150 tac'] = $st_header_net_price;
            $st_header['e w080 tac'] = $st_header_discount;
            $st_header['f w080 tac'] = $st_header_unit;
            $st_header['g w080 tac'] = $st_header_qty_per_unit;
            $st_header['h w080 tac'] = $st_header_safety_del_time;
            $st_header['i w080 tac'] = $st_header_remove;
            $sup_table[] = $st_header;
            foreach ($el_suppliers as $supplier) {
                $st = array();
                $obj_elsup = new elements_suppliers($supplier['elements_suppliers_id']);
                //$supid = $supplier['elements_suppliers_id'];
                $supid = $supplier['etes_id'];
                $sup_exist[] = $supid;
                foreach ($st_header as $sth_k => $sth_v) {
                    switch ($sth_v) {
                        case $st_header_is_default:
                            $def_checked = $supplier['is_default'] == '1' ? 'checked="checked"' : '';
                            $radio_def = '<input type="radio" name="is_default" value="' . $supid . '" ' . $def_checked . ' />';
                            $value = $radio_def;
                            break;
                        case $st_header_suppplier_name:
                            $value = $supplier['suppliers_name'] . '<input type="hidden" name="elsups[' . $supid . '][elements_suppliers_id]" value="' . $supplier['elements_suppliers_id'] . '" />';
                            break;
                        case $st_header_element_code:
                            $sup_code = $obj_elsup->autoGenerateSupplierCode($supplier['elements_id']);
                            $value = $sup_code != '' ? '<input type="text" class="w300" name="elsups[' . $supid . '][item_number]" value="' . $sup_code . '" readonly="readonly" />' : '<input type="text" class="w300" name="elsups[' . $supid . '][item_number]" value="' . $supplier['item_number'] . '" />';
                            break;
                        case $st_header_net_price:
                            $curr_combo = '<select class="mandatory" name="elsups[' . $supid . '][price_currency]" style="width:50px;">';
                            foreach ($currencies as $cur) {
                                $curr_sel = $cur == $supplier['price_currency'] ? 'selected="selected"' : '';
                                $curr_combo .= '<option ' . $curr_sel . '>' . $cur . '</option>';
                            }
                            $curr_combo .= '</select>';
                            //$price  = '<input type="text" name="elsups['.$supid.'][price_currency]" value="'.$supplier['price_currency'].'" style="width:50px;" />';
                            $price_value = $supplier['price_currency'] == 'IDR' ? number_format($supplier['elements_price'], 0, '.', '') : number_format($supplier['elements_price'], 2, '.', '');
                            //This below current info will saved when there data changes
                            $el_cur_info = elements_suppliers::formatPreviousInfo($supplier['elements_price'], $supplier['price_currency'], $supplier['price_quantity_type'], $supplier['pieces_per_qty_type'], $supplier['resourcing_day'], $session_userinfo['username']);
                            //GET PREVIOUS INFO OF ELEMENT
                            $title_prev_info = elements_suppliers::tooltipDataPreviousInfo($supplier['elements_previous_info']);
                            $value = $curr_combo;
                            $value .= '<input type="text" name="elsups[' . $supid . '][price]" value="' . $price_value . '" style="width:70px;" title="' . $title_prev_info . '" class="mandatory help"/>';
                            $value .= '<input type="hidden" name="elsups[' . $supid . '][cur_price]" value="' . $price_value . '"/>';
                            $value .= '<input type="hidden" name="elsups[' . $supid . '][cur_currency]" value="' . $supplier['price_currency'] . '"/>';
                            $value .= '<input type="hidden" name="elsups[' . $supid . '][cur_info]" value="' . $el_cur_info . '"/>';
                            break;
                        case $st_header_discount:
                            $value = '<input type="text" name="elsups[' . $supid . '][discount]" value="' . number_format($supplier['discount'], 2, '.', '') . '" style="width:50px;" />';
                            break;
                        case $st_header_unit:
                            $value = '<select class="es-pqt mandatory" name="elsups[' . $supid . '][price_qty_type]" style="width:70px;">';
                            foreach ($unit_types as $ut) {
                                $unit_sel = $ut == $supplier['price_quantity_type'] ? 'selected="selected"' : '';
                                $value .= '<option ' . $unit_sel . '>' . $ut . '</option>';
                            }
                            $value .= '</select>';
                            break;
                        case $st_header_qty_per_unit:
                            $value = '<input type="text" class="es-ppqt mandatory" name="elsups[' . $supid . '][pieces_per_qty_type]" value="' . $supplier['pieces_per_qty_type'] . '" style="width:50px;text-align:center;" />';
                            break;
                        case $st_header_safety_del_time:
                            $value = '<input type="text" name="elsups[' . $supid . '][resourcing_day]" value="' . $supplier['resourcing_day'] . '" readonly="readonly" style="width:30px;text-align:center;" title="Please change safety delivery time in supplier level" />';
                            break;
                        case $st_header_remove:
                            $value = '<input type="checkbox" name="remove[]" value="' . $supid . '" class="cbrem" />';
                            break;
                    }
                    $st[$sth_k] = $value;
                }
                $sup_table[] = $st;
            }
            $suppliers_table = tep_draw_table('', $sup_table);
            $suppliers_button = '<div style="margin-top:10px;"><input type="submit" name="submit" class="button" value="Save Changes" /></div>';
        }
        $suppliers_combo = '<select name="new_supplier" class="iwbutton input">';
        $suppliers_combo .= '<option value="">Select a Supplier...</option>';
        foreach ($suppliers as $sup) {
            //if(!in_array($sup['cb_value'], $sup_exist)) //open option for multiple supplier per element
            $suppliers_combo .= '<option value="' . $sup['cb_value'] . '">' . $sup['cb_option'] . '</option>';
        }
        $suppliers_combo .= '</select>';
        $result = '';
        $result .= '<div style="float:left;margin-right:10px;">' . webImage($element->image, '130', '130', $element->id, 'img-border') . '</div>';
        $result .= '<h3>Element ' . $elements_id . '</h3>';
        //$result .= '<h2>'.$element->detail['name'].'</h2>';
        $result .= '<h2>' . $element->detail['description'] . '</h2>';
        $result .= '<div>Weight ' . $element->detail['weight'] . ' gram/qty</div>';
        $result .= '<form name="new-el-sup" action="?open=elements-suppliers" method="post">';
        $result .= '<input type="hidden" name="me_action" value="ADDELEMENTSSUPPLIERS" />';
        $result .= '<input type="hidden" name="elements_id" value="' . $element->id . '" />';
        $result .= '<div style="margin-top:55px;">' . $suppliers_combo . '<input type="submit" name="submit" value="Add New Supplier" /></div>';
        $result .= '</form>';
        $result .= '<div style="clear:both;">&nbsp;</div>';
        $result .= '<div class="red" style="margin-bottom:2px;"><strong>Safety Delivery Time (SDT)</strong> is now set in Supplier level</div>';
        $add_param = $accessed == CONST_ACCESS_DESIGNS_MANAGEMENT ? '&accessed=' . CONST_ACCESS_DESIGNS_MANAGEMENT . '&designs_id=' . $designs_id : '';
        $result .= '<form id="upd-el-sup" action="?open=elements-suppliers' . $add_param . '" method="post">';
        $result .= '<input type="hidden" name="me_action" value="UPDATEELEMENTSSUPPLIERS" />';
        $result .= '<input type="hidden" name="elements_id" value="' . $element->id . '" />';
        $result .= $suppliers_table;
        $result .= $suppliers_button;
        $result .= '</form>';
    }
    return $result;
}