<?php



function display_header(){
    
//        $th = array(_('PO Line#'), 
//                    _("Freight"), _("Insurance"), _("Total CIF Price"), _("Total In Rs"), 
//                    _("Custom Duty Value/%"),_("Custom Duty"), _("Custom Duty Value"), _("Surcharge %"), _("Surcharge Value"), _("PAL Value"), 
//                    _("CESS Value/%"), _("CESS"), _("CESS Value"), _("VAT"), _("SRL %"), _("SRL Value"), _("Unit Cost"), _("Landed Unit Cost Rs"));    
        
        $th = array(_('PO Line#'), 
                    _("Description"), _("Calculation Method"), _("Percentage/Value"), _("Value"), 
//                    _("Custom Duty Value/%"),_("Custom Duty"), _("Custom Duty Value"), _("Surcharge %"), _("Surcharge Value"), _("PAL Value"), 
//                    _("CESS Value/%"), _("CESS"), _("CESS Value"), _("VAT"), _("SRL %"), _("SRL Value"), 
                    _("Unit Cost"), _("Landed Unit Cost Rs"));               

    table_header($th);
}

function get_array_value($array, $post_name){
    if(isset($array[$post_name]) && $array[$post_name] > 0){
        $value = $array[$post_name];
    }else{
        $value = 0;
    }
    return $value;
}
  
function display_import_purch_order_costing_detail_table_content(&$order_no, &$po_line_ids_array, &$array_with_value){
    global $Ajax;
    
    
    $result = get_import_purch_order_costing_detail_table_content($order_no);    
    
        $k = 0; //row colour counter
        $total = $line_totals = $freight_totals = $insurance_totals = $total_cif_prices = $total_in_rss = $custom_duty_values = $pal_values = $vat_values = 
                 $srl_values = $nbt_values = $total_duty_without_vats = $total_cost_without_vats = $clearing_charges = $total_costs = $unit_costs = $landed_unit_costs = 0;
        
        while($myrow = db_fetch($result)){       
            display_header();
            $po_id = $myrow['po_detail_item'];
            $pot = get_purch_order_lines_total($order_no);
            $po_total = $pot['po_total'];
            alt_table_row_color($k);

                label_cell($po_id, 'align=center');
                if(strlen($myrow['description']) > 0){
                    $item_name = ($myrow['item_code'].'-'.$myrow['description']);
                }else{
                    $item_name = ($myrow['item_code'].'-'.$myrow['stock_master_description']);
                }                
                $unit_name = ($myrow['order_unit']);
                
                $quantity = ($myrow['order_unit_qty']);
                hidden('order_unit_qty_'.$po_id, $myrow['order_unit_qty']);
                
                $unit_price = ($myrow['order_unit_price']);
                hidden('order_unit_price_'.$po_id, $myrow['order_unit_price']);
             
                $Ajax->activate('line_total');
                div_start('line_total'); 
                    $line_total_name = 'line_total_'.$po_id;
                    $line_total_value = get_array_value($array_with_value, $line_total_name);
                        if(!$line_total_value > 0){
                            $line_total_value = round($myrow['order_unit_qty'] * $myrow['order_unit_price'], 2);
                        }

                        hidden($line_total_name, $line_total_value );                        
                        $_POST[$line_total_name] = $line_total_value;

//                    label_cell($_POST[$line_total_name], 'align=right');
                div_end();

                $item_detail_text = "Item: ".text_bold($item_name);
                $item_detail_text .= spaces(5)."Unit: ".text_bold($unit_name);
                $item_detail_text .= spaces(5)."Quantity: ".text_bold(number_format($quantity,3));
                $item_detail_text .= spaces(5)."Unit Price: ".text_bold(price_format($unit_price));
                $item_detail_text .= spaces(5)."Line Tiotal: ".text_bold(price_format($line_total_value));
                label_cell($item_detail_text, "colspan=6 style=color:DarkBlue align=left");
                
            end_row();
                
        //----------------------------------------------------------------------------------------------------        
                
            start_row();
                label_cell('');
                label_cell('Freight:', "colspan=3");
                
                $Ajax->activate('freight_total');
                div_start('freight_total');
                    $line_freight_total_name = 'line_freight_total_'.$po_id;
                    $line_freight_total_value = get_array_value($array_with_value, $line_freight_total_name);
                        if(!$line_freight_total_value > 0){
                            $line_freight_total_value = (($myrow['order_unit_qty'] * $myrow['order_unit_price']) / $po_total) * $myrow['freight_value'];
                        }
                        hidden($line_freight_total_name, $line_freight_total_value );                        
                        $_POST[$line_freight_total_name] = $line_freight_total_value;

                        amount_decimal_cell(number_format($_POST[$line_freight_total_name], 2));
                div_end();
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('Insurance:', "colspan=3");
                $Ajax->activate('insurance_total');
                div_start('insurance_total'); 
                    $line_insurance_total_name = 'line_insurance_total_'.$po_id;
                    $line_insurance_total_value = get_array_value($array_with_value, $line_insurance_total_name);
                        if(!$line_insurance_total_value > 0){
                            $line_insurance_total_value = round((($myrow['order_unit_qty'] * $myrow['order_unit_price']) / $po_total) * $myrow['insurance_value'], 2);
                        }
                        hidden($line_insurance_total_name, $line_insurance_total_value );                        
                        $_POST[$line_insurance_total_name] = $line_insurance_total_value;

                    amount_decimal_cell(round($_POST[$line_insurance_total_name], 2));
                div_end(); 
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('CIF:', "colspan=3");

                $Ajax->activate('total_cif_prices');
                div_start('total_cif_prices'); 
                    $line_total_cif_price_name = 'line_total_cif_price_'.$po_id;
                    $line_total_cif_price_value = get_array_value($array_with_value, $line_total_cif_price_name);
//                        if(!$line_total_cif_price_value > 0){
                            $line_total_cif_price_value = round($line_total_value + $line_freight_total_value + $line_insurance_total_value, 2);
//                        }
                        hidden($line_total_cif_price_name, $line_total_cif_price_value );                        
                        $_POST[$line_total_cif_price_name] = $line_total_cif_price_value;

                    amount_decimal_cell(round($_POST[$line_total_cif_price_name], 2));
                div_end();
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('Total:', "colspan=3");

                $Ajax->activate('total_in_rs');
                div_start('total_in_rs'); 
                    $line_total_in_rs_name = 'line_total_in_rs_'.$po_id;//$pod_id
                    $line_total_in_rs_value = get_array_value($array_with_value, $line_total_in_rs_name);
                    hidden($line_total_in_rs_name, $line_total_in_rs_value );                        
                    $_POST[$line_total_in_rs_name] = $line_total_in_rs_value;

                    amount_decimal_cell(round($_POST[$line_total_in_rs_name], 2));
                div_end(); 
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('Custom Duty:');
                
                $Ajax->activate('custom_duty_value_percentage');
                div_start('custom_duty_value_percentage'); 
                    $custom_duty_value_per_name = 'custom_duty_value_per_'.$po_id;
                    $custom_duty_value_per_value = get_array_value($array_with_value, $custom_duty_value_per_name);    
                    hidden($custom_duty_value_per_name, round($custom_duty_value_per_value));   
                    $_POST[$custom_duty_value_per_name] = round($custom_duty_value_per_value);

                    yesno_list_cells("", $custom_duty_value_per_name, null, 'Percentage', 'Value');
                div_end(); 
                
                $Ajax->activate('custom_duty_values');
                div_start('custom_duty_values'); 
                    $custom_duty_perc_name = 'custom_duty_'.$po_id;
                    $custom_duty_perc_value = get_array_value($array_with_value, $custom_duty_perc_name);    
                    hidden($custom_duty_perc_name, number_format($custom_duty_perc_value, 2));   
                    $_POST[$custom_duty_perc_name] = number_format($custom_duty_perc_value, 2);
                    
                    small_amount_cells('', $custom_duty_perc_name);
                    
                    $line_custom_duty_value_name = 'line_custom_duty_value_'.$po_id;
                    $line_custom_duty_value_value = get_array_value($array_with_value, $line_custom_duty_value_name);
                    hidden($line_custom_duty_value_name, $line_custom_duty_value_value );                        
                    $_POST[$line_custom_duty_value_name] = $line_custom_duty_value_value;

                    amount_decimal_cell(round($_POST[$line_custom_duty_value_name], 2));   
                div_end();
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('Surcharge:', "colspan=1");
                label_cell('(Percentage)');

                $Ajax->activate('surcharge_value');
                div_start('surcharge_value');  
                    $surcharge_perc_name = 'surchrge_'.$po_id;
                    $surcharge_perc_value = get_array_value($array_with_value, $surcharge_perc_name);     
                    hidden($surcharge_perc_name, $surcharge_perc_value );
                    $_POST[$surcharge_perc_name] = $surcharge_perc_value;
                    
                    small_amount_cells('', $surcharge_perc_name);

                    $line_surcharge_value_name = 'line_surcharge_value_'.$po_id;
                    $line_surcharge_value_value = get_array_value($array_with_value, $line_surcharge_value_name);
                    hidden($line_surcharge_value_name, $line_surcharge_value_value );                        
                    $_POST[$line_surcharge_value_name] = $line_surcharge_value_value;
                    
                    amount_decimal_cell(round($_POST[$line_surcharge_value_name], 2));   
                div_end();
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('PAL:', "colspan=3");

                $Ajax->activate('pal_values'); 
                div_start('pal_values');   
                    $line_pal_value_name = 'line_pal_value_'.$po_id;
                    $line_pal_value_value = get_array_value($array_with_value, $line_pal_value_name);
                    hidden($line_pal_value_name, $line_pal_value_value );                        
                    $_POST[$line_pal_value_name] = $line_pal_value_value;

                    amount_decimal_cell(round($_POST[$line_pal_value_name], 2)); 
                    hidden('line_freight_totals');
                div_end(); 
                label_cell('');label_cell('');
                end_row();
                
                start_row();
                label_cell('');
                label_cell('Cess:');
                
                $Ajax->activate('cess_value_percentage');
                div_start('cess_value_percentage'); 
                    $cess_value_per_name = 'cess_value_per_'.$po_id;
                    $cess_value_per_value = get_array_value($array_with_value, $cess_value_per_name);    
                    hidden($cess_value_per_name, round($cess_value_per_value));   
                    $_POST[$cess_value_per_name] = round($cess_value_per_value);

                    yesno_list_cells("", 'cess_value_per_'.$po_id, null, 'Percentage', 'Value');
                div_end(); 
                    
                $Ajax->activate('cess_values'); 
                div_start('cess_values');     
                    $cess_perc_name = 'cess_'.$po_id;
                    $cess_perc_value = get_array_value($array_with_value, $cess_perc_name);   
                    hidden($cess_perc_name, number_format($cess_perc_value, 2));  
                    $_POST[$cess_perc_name] = number_format($cess_perc_value, 2);

                    small_amount_cells('', $cess_perc_name);

                    $line_cess_value_name = 'line_cess_value_'.$po_id;
                    $line_cess_value_value = get_array_value($array_with_value, $line_cess_value_name); 
                    hidden($line_cess_value_name, $line_cess_value_value );  
                    $_POST[$line_cess_value_name] = $line_cess_value_value;

                    amount_decimal_cell(round($_POST[$line_cess_value_name], 2));  
                div_end(); 
                label_cell('');label_cell('');  
                end_row();
                
                start_row();
                label_cell('');
                label_cell('VAT:', "colspan=3");

                $Ajax->activate('vat_values');       
                div_start('vat_values'); 
                    $line_vat_value_name = 'line_vat_value_'.$po_id;
                    $line_vat_value_value = get_array_value($array_with_value, $line_vat_value_name);     
                    hidden($line_vat_value_name, $line_vat_value_value); 
                    $_POST[$line_vat_value_name] = $line_vat_value_value;

                    amount_decimal_cell(round($_POST[$line_vat_value_name], 2));  
                div_end(); 
                label_cell('');label_cell('');    
                end_row();
                
                start_row();
                label_cell('');
                label_cell('SRL:');
                label_cell('(Percentage)');

                $Ajax->activate('srl_values');       
                div_start('srl_values');   
                    $srl_perc_name = 'srl_'.$po_id;
                    $srl_perc_value = get_array_value($array_with_value, $srl_perc_name);   
                    hidden($srl_perc_name, $srl_perc_value); 
                    $_POST[$srl_perc_name] = $srl_perc_value;

                    small_amount_cells('', $srl_perc_name);

                    $line_srl_value_name = 'line_srl_value_'.$po_id;
                    $line_srl_value_value = get_array_value($array_with_value, $line_srl_value_name); 
                    hidden($line_srl_value_name, $line_srl_value_value);                     
                    $_POST[$line_srl_value_name] = $line_srl_value_value;

                    amount_decimal_cell(round($_POST[$line_srl_value_name], 2));  
                div_end();    
                label_cell('');label_cell('');

            end_row();
            
        //-----------------------------------------------------------------------------------------------------------
            
//        alt_table_row_color($k);
            $item_detail_text2 = null;
            
            start_row();
                label_cell('');
                label_cell('NBT:', 'colspan=3');
                
                $Ajax->activate('nbt_values');       
                div_start('nbt_values');  
                    $line_nbt_value_name = 'line_nbt_value_'.$po_id;
                    $line_nbt_value_value = get_array_value($array_with_value, $line_nbt_value_name);  
                    hidden($line_nbt_value_name, $line_nbt_value_value); 
                    $_POST[$line_nbt_value_name] = $line_nbt_value_value;

                    $item_detail_text2 .= "NBT: ".text_bold(number_format($_POST[$line_nbt_value_name], 2));
                    amount_decimal_cell(round($_POST[$line_nbt_value_name], 2));  
                div_end(); 
                label_cell('');label_cell('');
            end_row();
                
            start_row();
                label_cell('');
                label_cell('Total Duty Without VAT:', 'colspan=3');
                
                $Ajax->activate('total_duty_without_vats');       
                div_start('total_duty_without_vats');   
                    $line_total_duty_without_vat_name = 'line_total_duty_without_vat_'.$po_id;
                    $line_total_duty_without_vat_value = get_array_value($array_with_value, $line_total_duty_without_vat_name);   
                    hidden($line_total_duty_without_vat_name, $line_total_duty_without_vat_value); 
                    $_POST[$line_total_duty_without_vat_name] = $line_total_duty_without_vat_value;

                    $item_detail_text2 .= spaces(5)."Total Duty Without VAT: ".text_bold(number_format($_POST[$line_total_duty_without_vat_name], 2));
                    amount_decimal_cell(round($_POST[$line_total_duty_without_vat_name], 2));  
                div_end();  
                label_cell('');label_cell('');               
            end_row();
                
            start_row();
                label_cell('');
                label_cell('Total Cost Without VAT:', 'colspan=3');
                
               $Ajax->activate('total_cost_without_vats');       
                div_start('total_cost_without_vats');   
                    $line_total_cost_without_vat_name = 'line_total_cost_without_vat_'.$po_id;
                    $line_total_cost_without_vat_value = get_array_value($array_with_value, $line_total_cost_without_vat_name);   
                    hidden($line_total_cost_without_vat_name, $line_total_cost_without_vat_value); 
                    $_POST[$line_total_cost_without_vat_name] = $line_total_cost_without_vat_value;

                    $item_detail_text2 .= spaces(5)."Total Cost Without VAT: ".text_bold(number_format($_POST[$line_total_cost_without_vat_name], 2));
                    amount_decimal_cell(round($_POST[$line_total_cost_without_vat_name], 2));  
                div_end();  
                label_cell('');label_cell('');                
            end_row();
                
            start_row();
                label_cell('');
                label_cell('Clearing Charge:', 'colspan=3');
                
                $Ajax->activate('clearing_charges');       
                div_start('clearing_charges'); 
                    $line_clearing_charge_name = 'line_clearing_charge_'.$po_id;
                    $line_clearing_charge_value = get_array_value($array_with_value, $line_clearing_charge_name);  
                    hidden($line_clearing_charge_name, $line_clearing_charge_value); 
                    $_POST[$line_clearing_charge_name] = $line_clearing_charge_value;

                    $item_detail_text2 .= spaces(5)."Clearing Charge: ".text_bold(number_format($_POST[$line_clearing_charge_name], 2));
                    amount_decimal_cell(round($_POST[$line_clearing_charge_name], 2));  
                div_end();          
                label_cell('');label_cell('');       
            end_row();
                
            start_row();
                label_cell('');
                label_cell('Total Cost:', 'colspan=3');
                
                $Ajax->activate('total_costs');       
                div_start('total_costs');  
                    $line_total_cost_name = 'line_total_cost_'.$po_id;
                    $line_total_cost_value = get_array_value($array_with_value, $line_total_cost_name);    
                    hidden($line_total_cost_name, $line_total_cost_value); 
                    $_POST[$line_total_cost_name] = $line_total_cost_value;

                    $item_detail_text2 .= spaces(5)."Total Cost: ".text_bold(number_format($_POST[$line_total_cost_name], 2));
                    amount_decimal_cell(round($_POST[$line_total_cost_name], 2));  
                div_end();                           
                label_cell('');label_cell('');
            end_row();  
                
//                label_cell($item_detail_text2, "colspan=4");

                
                unset($item_detail_text2);
            
            alt_table_row_color($k);
                label_cell('', "colspan=5");
                
                $Ajax->activate('unit_costs');       
                div_start('unit_costs');   
                    $line_unit_cost_name = 'line_unit_cost_'.$po_id;
                    $line_unit_cost_value = get_array_value($array_with_value, $line_unit_cost_name);   
                    hidden($line_unit_cost_name, $line_unit_cost_value); 
                    $_POST[$line_unit_cost_name] = $line_unit_cost_value;

                    amount_decimal_cell(round($_POST[$line_unit_cost_name], 2)); 
                div_end();    

                $Ajax->activate('landed_unit_costs');       
                div_start('landed_unit_costs');     
                    $line_landed_unit_cost_name = 'line_landed_unit_cost_'.$po_id;
                    $line_landed_unit_cost_value = get_array_value($array_with_value, $line_landed_unit_cost_name);    
                    hidden($line_landed_unit_cost_name, $line_landed_unit_cost_value); 
                    $_POST[$line_landed_unit_cost_name] = $line_landed_unit_cost_value;

                    amount_decimal_cell(round($_POST[$line_landed_unit_cost_name], 2)); 
                div_end(); 

//            vertical_space("height=16");
        
        }
  
        vertical_space("height=16");
        dispaly_last_row($array_with_value);
        
    end_table();
}

function display_common_inputs($order_no, $array_with_value){          
    $exchange_rate = get_array_value($array_with_value, 'exchange_rate_0');
    $clearing_charge = get_array_value($array_with_value, 'clearing_charge_0');
    $landed_unit_cost = get_array_value($array_with_value, 'landed_unit_cost_0');
    $pal = get_array_value($array_with_value, 'pal_0');
    $vat = get_array_value($array_with_value, 'vat_0');
    $nbt = get_array_value($array_with_value, 'nbt_0');

    start_outer_table();
        start_row();
            label_cell("Exchange RATE:", "align=right");
            amount_cells('', 'exchange_rate', $exchange_rate, null, null, 4);
            label_cell("Clearing Charges:", "align=right");
            amount_cells('', 'clearing_charge', number_format($clearing_charge, 2));
            label_cell("Landed Unit Cost:", "align=right");
            amount_cells('', 'landed_unit_cost', number_format($landed_unit_cost, 2));
        end_row();  
        start_row();
            label_cell("PAL:", "align=right");
            amount_cells('', 'pal', number_format($pal, 2));
            label_cell("VAT:", "align=right");
            amount_cells('', 'vat', number_format($vat, 2));
            label_cell("NBT:", "align=right");
            amount_cells('', 'nbt', number_format($nbt, 2));
        end_row();
    end_outer_table();
}

function replace_array_with_new_value(&$array_with_value, $po_line_ids_array, $post_prefix){
    foreach($po_line_ids_array as $po_line_id){
        if($post_prefix.$po_line_id == 'custom_duty_value_per_'.$po_line_id){
            $array_with_value[$post_prefix.$po_line_id] = $_POST[$post_prefix.$po_line_id];
            continue;
        }
        if($post_prefix.$po_line_id == 'cess_value_per_'.$po_line_id){
            $array_with_value[$post_prefix.$po_line_id] = $_POST[$post_prefix.$po_line_id];
            continue;
        }
        $array_with_value[$post_prefix.$po_line_id] = input_num($post_prefix.$po_line_id);
    }
}
    
function calculate_array_with_screen_values($po_line_ids_array, &$array_with_value, &$total_in_rs_custom_perentage){
    $array_with_value['exchange_rate_0'] = $_POST['exchange_rate'];
    $v = input_num('clearing_charge');
    $array_with_value['clearing_charge_0'] = $v;
    $luc = input_num('landed_unit_cost');
    $array_with_value['landed_unit_cost_0'] = $luc;
    $array_with_value['pal_0'] = $_POST['pal'];
    $array_with_value['vat_0'] = $_POST['vat'];
    $array_with_value['nbt_0'] = $_POST['nbt'];
    $array_with_value['total_in_rs_custom_perentage_0'] = $_POST['total_in_rs_custom_perentage_0'];

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_');
    $line_totals = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_');
    $array_with_value['line_totalss_0'] = $line_totals;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_freight_total_');
    $freight_totals = calculate_line_total($po_line_ids_array, $array_with_value, 'line_freight_total_');
    $array_with_value['line_freight_totals_0'] = $freight_totals;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_insurance_total_');
    $insurance_totals = calculate_line_total($po_line_ids_array, $array_with_value, 'line_insurance_total_');
    $array_with_value['line_insurance_totals_0'] = $insurance_totals;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_cif_price_');
    $total_cif_prices = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_cif_price_');
    $array_with_value['line_cif_prices_totals_0'] = $total_cif_prices;

    replace_calculated_total_in_rs_value($array_with_value, $po_line_ids_array, 'exchange_rate', 'line_total_in_rs_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_in_rs_');
    $total_in_rss = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_in_rs_');
    $array_with_value['line_in_rs_totals_0'] = $total_in_rss;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'custom_duty_');

    replace_calculated_cstom_duty_value($array_with_value, $po_line_ids_array, 'custom_duty_', 'line_custom_duty_value_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_custom_duty_value_');
    $custom_duty_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_custom_duty_value_');
    $array_with_value['line_custom_duty_value_totals_0'] = $custom_duty_values;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'surchrge_');

    replace_calculated_surcharge_value($array_with_value, $po_line_ids_array, 'surchrge_', 'line_surcharge_value_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_surcharge_value_');

    replace_calculated_pal_value($array_with_value, $po_line_ids_array, 'pal', 'line_pal_value_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_pal_value_');
    $pal_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_pal_value_');
    $array_with_value['line_pal_value_totals_0'] = $pal_values;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'cess_');

    replace_calculated_cess_value($array_with_value, $po_line_ids_array, 'cess_', 'line_cess_value_', $total_in_rs_custom_perentage);
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_cess_value_');
    $line_cess_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_cess_value_');
    $array_with_value['line_cess_value_totals_0'] = $line_cess_values;

    replace_calculated_vat_value($array_with_value, $po_line_ids_array, 'vat', 'line_vat_value_', $total_in_rs_custom_perentage);
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_vat_value_');
    $vat_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_vat_value_');
    $array_with_value['line_vat_value_totals_0'] = $vat_values;

    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'srl_');

    replace_calculated_srl_value($array_with_value, $po_line_ids_array, 'srl_', 'line_srl_value_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_srl_value_');
    $srl_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_srl_value_');
    $array_with_value['line_srl_value_totals_0'] = $srl_values;

    replace_calculated_nbt_value($array_with_value, $po_line_ids_array, 'nbt', 'line_nbt_value_', 'cess_', $total_in_rs_custom_perentage);
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_nbt_value_');
    $nbt_values = calculate_line_total($po_line_ids_array, $array_with_value, 'line_nbt_value_');
    $array_with_value['line_nbt_value_totals_0'] = $nbt_values;

    replace_calculated_total_duty_without_vat_value($array_with_value, $po_line_ids_array, 'line_total_duty_without_vat_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_duty_without_vat_');
    $total_duty_without_vats = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_duty_without_vat_');
    $array_with_value['line_total_duty_without_vats_0'] = $total_duty_without_vats;

    replace_calculated_total_cost_without_vat_value($array_with_value, $po_line_ids_array, 'line_total_cost_without_vat_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_cost_without_vat_');
    $total_cost_without_vats = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_cost_without_vat_');
    $array_with_value['line_total_cost_without_vats_0'] = $total_cost_without_vats;

    replace_calculated_clearing_charge_value($array_with_value, $po_line_ids_array, 'clearing_charge', 'line_clearing_charge_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_clearing_charge_');
    $clearing_charges = calculate_line_total($po_line_ids_array, $array_with_value, 'line_clearing_charge_');
    $array_with_value['line_clearing_charge_totals_0'] = $clearing_charges;

    replace_calculated_total_cost_value($array_with_value, $po_line_ids_array, 'line_total_cost_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_total_cost_');
    $total_costs = calculate_line_total($po_line_ids_array, $array_with_value, 'line_total_cost_');
    $array_with_value['line_total_costs_0'] = $total_costs;

    replace_calculated_unit_cost_value($array_with_value, $po_line_ids_array, 'line_unit_cost_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_unit_cost_');
    $unit_costs = calculate_line_total($po_line_ids_array, $array_with_value, 'line_unit_cost_');
    $array_with_value['line_unit_costs_0'] = $unit_costs;

    replace_calculated_landed_unit_cost_value($array_with_value, $po_line_ids_array, 'landed_unit_cost', 'line_landed_unit_cost_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'line_landed_unit_cost_');
    $landed_unit_costs = calculate_line_total($po_line_ids_array, $array_with_value, 'line_landed_unit_cost_');
    $array_with_value['line_landed_unit_costs_0'] = $landed_unit_costs;
    
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'custom_duty_value_per_');
    replace_array_with_new_value($array_with_value, $po_line_ids_array, 'cess_value_per_');

    return $array_with_value;
}

function replace_calculated_total_in_rs_value($array_with_value, $po_line_ids_array, $exchange_rate, $line_total_in_rs){
    $calculated_total_in_rs_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_total_in_rs.$po_line_id){
                $line_total_cif_price = $array_with_value['line_total_cif_price_'.$po_line_id];

                $calculated_total_in_rs_value = $line_total_cif_price * $_POST[$exchange_rate];
                $_POST[$line_total_in_rs.$po_line_id] = round($calculated_total_in_rs_value, 2);
            }
        }
    }
}
  
function replace_calculated_cstom_duty_value($array_with_value, $po_line_ids_array, $custome_duty, $line_custom_duty_value){
    $calculated_cstom_duty_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_custom_duty_value.$po_line_id){
                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                $custom_duty = $array_with_value[$custome_duty.$po_line_id];
                
                if($_POST['custom_duty_value_per_'.$po_line_id] == 1){
                    $calculated_cstom_duty_value = $total_in_rs * $custom_duty / 100;
                }else{
                    $calculated_cstom_duty_value = $custom_duty;
                }

                $_POST[$line_custom_duty_value.$po_line_id] = round($calculated_cstom_duty_value, 2);
            }
        }
    }
}
    
function replace_calculated_surcharge_value($array_with_value, $po_line_ids_array, $surcharge, $line_surcharge_value){
    $calculated_surcharge_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_surcharge_value.$po_line_id){
                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_percentage= $array_with_value[$surcharge.$po_line_id];

                $calculated_surcharge_value = $custom_duty_value * $surcharge_percentage / 100;
                $_POST[$line_surcharge_value.$po_line_id] = round($calculated_surcharge_value, 2);
            }
        }
    }
}
    
function replace_calculated_pal_value($array_with_value, $po_line_ids_array, $pal, $line_pal_value){
    $calculated_pal_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_pal_value.$po_line_id){
                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];

                $calculated_pal_value = $total_in_rs * $_POST[$pal] / 100;
                $_POST[$line_pal_value.$po_line_id] = round($calculated_pal_value, 2);
            }
        }
    }
}
    
function replace_calculated_cess_value($array_with_value, $po_line_ids_array, $cess, $line_cess_value, $total_in_rs_custom_perentage){
    $calculated_cess_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_cess_value.$po_line_id){
                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                
                if($_POST['cess_value_per_'.$po_line_id] == 1){
                    $calculated_cess_value = ($total_in_rs + ($total_in_rs * $array_with_value['total_in_rs_custom_perentage_0'] / 100)) * $array_with_value[$cess.$po_line_id] / 100;
                }else{
                    $calculated_cess_value = $array_with_value[$cess.$po_line_id];
                }

                $_POST[$line_cess_value.$po_line_id] = round($calculated_cess_value, 2);
            }
        }
    }
}
    
function replace_calculated_vat_value($array_with_value, $po_line_ids_array, $vat, $line_vat_value, $total_in_rs_custom_perentage){
    $calculated_vat_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_vat_value.$po_line_id){
                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_value = $array_with_value['line_surcharge_value_'.$po_line_id];
                $pal_value = $array_with_value['line_pal_value_'.$po_line_id];
                $cess_value = $array_with_value['line_cess_value_'.$po_line_id];

                $calculated_vat_value = (($total_in_rs + ($total_in_rs * $array_with_value['total_in_rs_custom_perentage_0'] / 100)) + $custom_duty_value + $surcharge_value + $pal_value + $cess_value) 
                                          * $_POST[$vat] / 100;
                $_POST[$line_vat_value.$po_line_id] = round($calculated_vat_value, 2);
            }
        }
    }
}
    
function replace_calculated_srl_value($array_with_value, $po_line_ids_array, $srl, $line_srl_value){
    $calculated_srl_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_srl_value.$po_line_id){

                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_value = $array_with_value['line_surcharge_value_'.$po_line_id];

                $calculated_srl_value = ($custom_duty_value + $surcharge_value) *  $array_with_value[$srl.$po_line_id] / 100;
                $_POST[$line_srl_value.$po_line_id] = round($calculated_srl_value, 2);
            }
        }
    }
}
    
function replace_calculated_nbt_value($array_with_value, $po_line_ids_array, $nbt, $line_nbt_value, $cess, $total_in_rs_custom_perentage){
    $calculated_nbt_value = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_nbt_value.$po_line_id){
                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_value = $array_with_value['line_surcharge_value_'.$po_line_id];
                $pal_value = $array_with_value['line_pal_value_'.$po_line_id];
                $cess_value = $array_with_value['line_cess_value_'.$po_line_id];

                $calculated_nbt_value = ($total_in_rs + ($total_in_rs * $array_with_value['total_in_rs_custom_perentage_0'] / 100) 
                                         + $custom_duty_value + $surcharge_value + $pal_value + $cess_value ) * $_POST[$nbt] / 100;

                $_POST[$line_nbt_value.$po_line_id] = round($calculated_nbt_value, 2);
            }
        }
    }
}
    
function replace_calculated_total_duty_without_vat_value($array_with_value, $po_line_ids_array, $line_total_duty_without_vat){
    $calculated_total_duty_without_vat = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_total_duty_without_vat.$po_line_id){

                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_value = $array_with_value['line_surcharge_value_'.$po_line_id];
                $pal_value = $array_with_value['line_pal_value_'.$po_line_id];
                $cess_value = $array_with_value['line_cess_value_'.$po_line_id];
                $srl_value = $array_with_value['line_srl_value_'.$po_line_id];
                $nbt_value = $array_with_value['line_nbt_value_'.$po_line_id];

                $calculated_total_duty_without_vat = $custom_duty_value + $surcharge_value + $pal_value + $cess_value + $srl_value + $nbt_value;
                $_POST[$line_total_duty_without_vat.$po_line_id] = round($calculated_total_duty_without_vat, 2);
            }
        }
    }
}
    
function replace_calculated_total_cost_without_vat_value($array_with_value, $po_line_ids_array, $line_total_cost_without_vat){
    $calculated_total_cost_without_vat = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_total_cost_without_vat.$po_line_id){

                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                $custom_duty_value = $array_with_value['line_custom_duty_value_'.$po_line_id];
                $surcharge_value = $array_with_value['line_surcharge_value_'.$po_line_id];
                $pal_value = $array_with_value['line_pal_value_'.$po_line_id];
                $cess_value = $array_with_value['line_cess_value_'.$po_line_id];
                $srl_value = $array_with_value['line_srl_value_'.$po_line_id];
                $nbt_value = $array_with_value['line_nbt_value_'.$po_line_id];

                $calculated_total_cost_without_vat = $total_in_rs + $custom_duty_value + $surcharge_value + $pal_value + $cess_value + $srl_value + $nbt_value;
                $_POST[$line_total_cost_without_vat.$po_line_id] = round($calculated_total_cost_without_vat, 2);
            }
        }
    }
}
    
function replace_calculated_clearing_charge_value($array_with_value, $po_line_ids_array, $clearing_charge, $line_clearing_charge_value){
    $calculated_clearing_charge = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_clearing_charge_value.$po_line_id){

                $total_in_rs = $array_with_value['line_total_in_rs_'.$po_line_id];
                $total_in_rs_totals = $array_with_value['line_in_rs_totals_0'];

                if($total_in_rs_totals > 0){
                    $calculated_clearing_charge = ($total_in_rs / $total_in_rs_totals) * input_num($clearing_charge);
                }else{
                    $calculated_clearing_charge = 0;
                }
                $_POST[$line_clearing_charge_value.$po_line_id] = round($calculated_clearing_charge, 2);
            }
        }
    }
}
    
function replace_calculated_total_cost_value($array_with_value, $po_line_ids_array, $line_total_cost_value){
    $calculated_total_cost = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_total_cost_value.$po_line_id){

                $line_total_cost_without_vat = $array_with_value['line_total_cost_without_vat_'.$po_line_id];
                $line_clearing_charge = $array_with_value['line_clearing_charge_'.$po_line_id];

                $calculated_total_cost = $line_total_cost_without_vat + $line_clearing_charge;
                $_POST[$line_total_cost_value.$po_line_id] = round($calculated_total_cost, 2);
            }
        }
    }
}
    
function replace_calculated_unit_cost_value($array_with_value, $po_line_ids_array, $line_unit_cost_value){
    $calculated_unit_cost = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_unit_cost_value.$po_line_id){

                $line_total_cost = $array_with_value['line_total_cost_'.$po_line_id];
                $order_unit_qty = $_POST['order_unit_qty_'.$po_line_id];

                if($order_unit_qty > 0){
                    $calculated_unit_cost = $line_total_cost / $order_unit_qty;
                }else{
                    $calculated_unit_cost = 0;
                }
                $_POST[$line_unit_cost_value.$po_line_id] = round($calculated_unit_cost, 2);
            }
        }
    }
}

function replace_calculated_landed_unit_cost_value($array_with_value, $po_line_ids_array, $landed_unit_cost, $line_anded_unit_cost_value){
    $calculated_unit_cost = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $line_anded_unit_cost_value.$po_line_id){

                $line_unit_cost_value = $array_with_value['line_unit_cost_'.$po_line_id];

                $calculated_unit_cost = $line_unit_cost_value + ($line_unit_cost_value * $_POST[$landed_unit_cost] / 100);
                $_POST[$line_anded_unit_cost_value.$po_line_id] = round($calculated_unit_cost, 2);
            }
        }
    }
}
    
function dispaly_last_row($array_with_value){
    
//    _("Freight"), _("Insurance"), _("Total CIF Price"), _("Total In Rs"), 
//                    _("Custom Duty Value/%"),_("Custom Duty"), _("Custom Duty Value"), _("Surcharge %"), _("Surcharge Value"), _("PAL Value"), 
//                    _("CESS Value/%"), _("CESS"), _("CESS Value"), _("VAT"), _("SRL %"), _("SRL Value"), _("Unit Cost"), _("Landed Unit Cost Rs"));    
        
    start_row();
        label_cell('');label_cell('Total PO:', 'colspan=3');
        amount_cell(round($array_with_value['line_totalss_0'], 2), "font-weight=bold");
        label_cell('');label_cell('');
    end_row();    
    start_row();
        label_cell('');label_cell('Total Freight:', 'colspan=3');
        amount_cell(round($array_with_value['line_freight_totals_0'], 2), "font-weight=bold");
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total Insuarance:', 'colspan=3');
        amount_cell(round($array_with_value['line_insurance_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total CIF:', 'colspan=3');        
        amount_cell(round($array_with_value['line_cif_prices_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total in Rs:', 'colspan=3');
        amount_cell(round($array_with_value['line_in_rs_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total Custom Duty Value:', 'colspan=3');
        amount_cell(round($array_with_value['line_custom_duty_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total PAL Value:', 'colspan=3');
        amount_cell(round($array_with_value['line_pal_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total CESS Value:', 'colspan=3');
        amount_cell(round($array_with_value['line_cess_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    start_row();    
        label_cell('');label_cell('Total VAT Value:', 'colspan=3');
        amount_cell(round($array_with_value['line_vat_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();    
    
    start_row();
        label_cell('');label_cell('Total NBT:', 'colspan=3');
        amount_cell(round($array_with_value['line_nbt_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();
    
    start_row();    
        label_cell('');label_cell('Total SRL Value:', 'colspan=3');
        amount_cell(round($array_with_value['line_srl_value_totals_0'], 2), true);
        label_cell('');label_cell('');
    end_row();

    start_row();
        label_cell('');label_cell('Total Duty Without VAT:', 'colspan=3');
        amount_cell(round($array_with_value['line_total_duty_without_vats_0'], 2), true);
        label_cell('');label_cell('');
    end_row();
    
    start_row();
        label_cell('');label_cell('Total Cost Without VAT:', 'colspan=3');
        amount_cell(round($array_with_value['line_total_cost_without_vats_0'], 2), true);
        label_cell('');label_cell('');
    end_row();
    
    start_row();
        label_cell('');label_cell('Total Clearring Charge:', 'colspan=3');
        amount_cell(round($array_with_value['line_clearing_charge_totals_0']), true);
        label_cell('');label_cell('');
    end_row();

    start_row();
        label_cell('');label_cell('Total Cost:', 'colspan=3');
        amount_cell(round($array_with_value['line_total_costs_0'], 2), true);
        label_cell('');label_cell('');
    end_row();
    
        $item_detail_text3 = null;

        $item_detail_text3 .= "Lines Total NBT Value: ".text_bold(number_format($array_with_value['line_nbt_value_totals_0'],2));
        $item_detail_text3 .= spaces(5)."Lines Total Duty Without VAT: ".text_bold(number_format($array_with_value['line_total_duty_without_vats_0'],2));
        $item_detail_text3 .= spaces(5)."Lines Total Clearring Charge: ".text_bold(number_format(round($array_with_value['line_clearing_charge_totals_0']), 0));
        $item_detail_text3 .= spaces(5)."Lines Total Cost: ".text_bold(number_format($array_with_value['line_total_costs_0'],2));
          
    end_row();
}
      
function create_po_line_ids_array($order_no){
    $po_line_ids_array = array();
    $result = get_po_line_ids($order_no);
    while($row = db_fetch($result)){
        $po_line_ids_array[] = $row['po_detail_item'];
    }
    return $po_line_ids_array;
}
    
function calculate_line_total($po_line_ids_array, $array_with_value, $post_prefix){
    $calculated_line_total = 0;
    foreach ($array_with_value as $key_id => $value) {
        foreach ($po_line_ids_array as $po_line_id) {
            if($key_id == $post_prefix.$po_line_id){
                $calculated_line_total += $_POST[$post_prefix.$po_line_id];
            }
        }
    }
    return $calculated_line_total;
}
    
function create_po_line_id_array_with_value($order_no, $po_line_ids_array){
    $po_line_ids_array_with_value = array();
    $result = get_po_line_id_with_value($order_no);
    $num_of_rows = db_num_rows($result);

    $po_costing_details_array = get_import_purch_order_costing_details_po_line_ids($order_no);

        if($num_of_rows > 0 && count($po_line_ids_array) == count($po_costing_details_array)){
            while($row = db_fetch($result)){
                $po_line_ids_array_with_value[$row['value_name']] = $row['value'];
            }
        }elseif($num_of_rows > 0 && count($po_line_ids_array) > count($po_costing_details_array)){
            while($row = db_fetch($result)){
                $po_line_ids_array_with_value[$row['value_name']] = $row['value'];
            }

            $array_diff_result = array_diff($po_line_ids_array, $po_costing_details_array);
            $po_line_ids_array_with_value = po_line_ids_with_zero_values($po_line_ids_array_with_value, $array_diff_result);

        }else{
            $po_line_ids_array_with_value = po_line_ids_with_zero_values($po_line_ids_array_with_value, $po_line_ids_array);
            $po_line_ids_array_with_value = po_line_rows_totals_with_zero_values($po_line_ids_array_with_value);           
        }
    return $po_line_ids_array_with_value;
}
    
function po_line_ids_with_zero_values($po_line_ids_array_with_value, $po_array_with_zero_value){
    foreach ($po_array_with_zero_value as $po_line_id) {
        $po_line_ids_array_with_value['line_total_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_freight_total_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_insurance_total_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_total_cif_price_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_total_in_rs_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['custom_duty_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_custom_duty_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['surchrge_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_surcharge_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_pal_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['cess_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_cess_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_vat_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['srl_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_srl_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_nbt_value_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_total_duty_without_vat_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_total_cost_without_vat_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_clearing_charge_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_total_cost_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_unit_cost_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['line_landed_unit_cost_'.$po_line_id] = 0;
        $po_line_ids_array_with_value['custom_duty_value_per_'.$po_line_id] = "";
        $po_line_ids_array_with_value['cess_value_per_'.$po_line_id] = "";
    }
    return $po_line_ids_array_with_value;
}

function po_line_rows_totals_with_zero_values($po_line_ids_array_with_value){
    $po_line_ids_array_with_value['line_totalss_0'] = 0;
    $po_line_ids_array_with_value['line_freight_totals_0'] = 0;
    $po_line_ids_array_with_value['line_insurance_totals_0'] = 0;
    $po_line_ids_array_with_value['line_cif_prices_totals_0'] = 0;
    $po_line_ids_array_with_value['line_in_rs_totals_0'] = 0;
    $po_line_ids_array_with_value['line_custom_duty_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_pal_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_cess_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_vat_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_srl_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_nbt_value_totals_0'] = 0;
    $po_line_ids_array_with_value['line_total_duty_without_vats_0'] = 0;
    $po_line_ids_array_with_value['line_total_cost_without_vats_0'] = 0;
    $po_line_ids_array_with_value['line_clearing_charge_totals_0'] = 0;
    $po_line_ids_array_with_value['line_total_costs_0'] = 0;
    $po_line_ids_array_with_value['line_unit_costs_0'] = 0;
    $po_line_ids_array_with_value['line_landed_unit_costs_0'] = 0;  
    $po_line_ids_array_with_value['total_in_rs_custom_perentage_0'] = 0;  

    return $po_line_ids_array_with_value;
}

function save_import_purch_order_costing_details_from_array($order_no, $po_line_ids_array, $array_with_value){
    $msg = delete_import_purch_order_costing_detail_lines($order_no);
    if($msg['success'] !== true){ return $msg; }

    $msg = add_import_purch_order_costing_detail($order_no, 0, 'exchange_rate', $array_with_value['exchange_rate_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'clearing_charge', $array_with_value['clearing_charge_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'landed_unit_cost', $array_with_value['landed_unit_cost_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'pal', $array_with_value['pal_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'vat', $array_with_value['vat_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'nbt', $array_with_value['nbt_0']);
    if($msg['success'] !== true){ return $msg; }

    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_totalss', $array_with_value['line_totalss_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_freight_totals', $array_with_value['line_freight_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_insurance_totals', $array_with_value['line_insurance_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_cif_prices_totals', $array_with_value['line_cif_prices_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_in_rs_totals', $array_with_value['line_in_rs_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_custom_duty_value_totals', $array_with_value['line_custom_duty_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_pal_value_totals', $array_with_value['line_pal_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_cess_value_totals', $array_with_value['line_cess_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_vat_value_totals', $array_with_value['line_vat_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_srl_value_totals', $array_with_value['line_srl_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_nbt_value_totals', $array_with_value['line_nbt_value_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_total_duty_without_vats', $array_with_value['line_total_duty_without_vats_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_total_cost_without_vats', $array_with_value['line_total_cost_without_vats_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_clearing_charge_totals', $array_with_value['line_clearing_charge_totals_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_total_costs', $array_with_value['line_total_costs_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_unit_costs', $array_with_value['line_unit_costs_0']);
    if($msg['success'] !== true){ return $msg; }
    $msg = add_import_purch_order_costing_detail($order_no, 0, 'line_landed_unit_costs', $array_with_value['line_landed_unit_costs_0']);
    if($msg['success'] !== true){ return $msg; }

    foreach ($array_with_value as $value_name => $value)  {

//        strripos("I love php, I love php too!","PHP");
        foreach ($po_line_ids_array as $po_line_id){
            if($value_name == 'exchange_rate_0' || $value_name == 'clearing_charge_0' || $value_name == 'landed_unit_cost_0'
               || $value_name == 'pal_0' || $value_name == 'vat_0' || $value_name == 'nbt_0' || $value_name == 'line_totalss_0'    
               || $value_name == 'line_freight_totals_0'|| $value_name == 'line_insurance_totals_0'|| $value_name == 'line_cif_prices_totals_0' 
               || $value_name == 'line_in_rs_totals_0'|| $value_name == 'line_custom_duty_value_totals_0'|| $value_name == 'line_pal_value_totals_0'
               || $value_name == 'line_cess_value_totals_0' || $value_name == 'line_vat_value_totals_0'|| $value_name == 'line_srl_value_totals_0' 
               || $value_name == 'line_nbt_value_totals_0' || $value_name == 'line_total_duty_without_vats_0'|| $value_name == 'line_total_cost_without_vats_0'
               || $value_name == 'line_clearing_charge_totals_0' || $value_name == 'line_total_costs_0'|| $value_name == 'line_unit_costs_0'
               || $value_name == 'line_landed_unit_costs_0' || $value_name == 'total_in_rs_custom_perentage_0' ){

            }else{
                $real_value_name = str_replace("_".$po_line_id,"",$value_name);
                $current_id = $real_value_name.'_'.$po_line_id;
                    if($current_id == $value_name){
                        $msg= add_import_purch_order_costing_detail($order_no, $po_line_id, $real_value_name, $value);
                        if($msg['success'] !== true){ return $msg; }
                    }
            }
        }
    }
    
    return array("success" => true, "message" => "");
}
