<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_IMPORT_PURCHASE_ORDERS';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/imports/includes/db/import_purch_order_costing_detail_db.inc");

$js = "";
if($use_popup_windows)
    $js .= get_js_open_window(900, 500);

page(_($help_context = "View Import Purchase Order #".$_GET['trans_no']." Costing Details"), true, false, "", $js);

if(!isset($_GET['trans_no'])){
    die ("<br>" . _("This page must be called with a purchase order number to review."));
}else{
    $order_no = $_GET['trans_no'];
}

display_heading(_($help_context) );
br();

function view_display_import_purchase_order_costing_details($order_no, $array_with_value){

    display_heading2(_("Import Purchase Order Line Details"));
    br();
    start_table(TABLESTYLE, "colspan=7 width=60%");

    global $Ajax;
    
    function display_header(){
        $th = array(_('PO Line#'), _("Description"), _("Calculation Method"), _("Percentage/Value"), _("Value"), _("Unit Cost"), _("Landed Unit Cost Rs")); 

        table_header($th);
    }
        
        $po_line_id = get_import_purch_order_costing_details_po_line_ids($order_no);
        $k = 0;
        
        $result = get_import_purch_order_costing_detail_table_content($order_no); 
            while($myrow = db_fetch($result)){  
                display_header();
                alt_table_row_color($k);
                    $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']);

                        $unit_price = ($myrow['order_unit_price']);

                        $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 = $myrow['order_unit_qty'] * $myrow['order_unit_price'];
                            }

                        $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=7 style=color:DarkBlue align=left");
                
                end_row();
                
                        //----------------------------------------------------------------------------------------------------        
                
                start_row();
                    label_cell('');
                    label_cell('Freight:', "colspan=3");

                        $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'];
                            }
                            amount_decimal_cell(round($line_freight_total_value, 2));
    
                    label_cell('');label_cell('');
                end_row();
                    
                start_row();
                    label_cell('');
                    label_cell('Insurance:', "colspan=3");
                
                        $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 = (($myrow['order_unit_qty'] * $myrow['order_unit_price']) / $po_total) * $myrow['insurance_value'];
                            }
                        amount_decimal_cell(round($line_insurance_total_value, 2));
                        
                    label_cell('');label_cell('');
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('CIF:', "colspan=3");
  
                        $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 = $line_total_value + $line_freight_total_value + $line_insurance_total_value;
                            }
                        amount_decimal_cell(round($line_total_cif_price_value, 2));
                        
                    label_cell('');label_cell('');
                end_row();

                start_row();
                    label_cell('');
                    label_cell('Total:', "colspan=3");
                        $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);
                        amount_decimal_cell(round($line_total_in_rs_value, 2));
                        
                    label_cell('');label_cell('');
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Custom Duty:');

                        $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);    
                        if($custom_duty_value_per_value == 1){
                            $custom_duty_value_per_yes_no = "Percentage";
                        }else{
                            $custom_duty_value_per_yes_no = "Value";
                        }
                        label_cell($custom_duty_value_per_yes_no, "align=center");

                        $custom_duty_perc_name = 'custom_duty_'.$po_id;
                        $custom_duty_perc_value = get_array_value($array_with_value, $custom_duty_perc_name);    
                        label_cell($custom_duty_perc_value, "align=right");

                        $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);

                        amount_decimal_cell(round($line_custom_duty_value_value, 2));   
  
                    label_cell('');label_cell('');
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Surcharge:', "colspan=1");
                    label_cell('(Percentage)', "align=center");
                
                        $surcharge_perc_name = 'surchrge_'.$po_id;
                        $surcharge_perc_value = get_array_value($array_with_value, $surcharge_perc_name);     
                        amount_decimal_cell(round($surcharge_perc_value, 2)); 

                        $line_surcharge_value_name = 'line_surcharge_value_'.$po_id;
                        $line_surcharge_value_value = get_array_value($array_with_value, $line_surcharge_value_name);
                        amount_decimal_cell(round($line_surcharge_value_value, 2));   
                        
                    label_cell('');label_cell('');
                end_row();
 
                start_row();
                    label_cell('');
                    label_cell('PAL:', "colspan=3");
                        $line_pal_value_name = 'line_pal_value_'.$po_id;
                        $line_pal_value_value = get_array_value($array_with_value, $line_pal_value_name);
                        amount_decimal_cell(round($line_pal_value_value, 2)); 
                        
                    label_cell('');label_cell('');
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Cess:');

                        $cess_value_per_name = 'cess_value_per_'.$po_id;
                        $cess_value_per_value = get_array_value($array_with_value, $cess_value_per_name);    
                        if($cess_value_per_value == 1){
                            $cess_value_per_value_yes_no = "Percentage";
                        }else{
                            $cess_value_per_value_yes_no = "Value";
                        }
                        label_cell($cess_value_per_value_yes_no, "align=center");
   
                        $cess_perc_name = 'cess_'.$po_id;
                        $cess_perc_value = get_array_value($array_with_value, $cess_perc_name);   
                        amount_decimal_cell(round($cess_perc_value, 2)); 

                        $line_cess_value_name = 'line_cess_value_'.$po_id;
                        $line_cess_value_value = get_array_value($array_with_value, $line_cess_value_name); 
                        amount_decimal_cell(round($line_cess_value_value, 2));  
                        
                    label_cell('');label_cell('');  
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('VAT:', "colspan=3");

                        $line_vat_value_name = 'line_vat_value_'.$po_id;
                        $line_vat_value_value = get_array_value($array_with_value, $line_vat_value_name);     
                        amount_decimal_cell(round($line_vat_value_value, 2));  
                        
                    label_cell('');label_cell('');    
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('SRL:');
                    label_cell('(Percentage)', "align=center");
  
                        $srl_perc_name = 'srl_'.$po_id;
                        $srl_perc_value = get_array_value($array_with_value, $srl_perc_name);   
                        amount_decimal_cell(round($srl_perc_value, 2)); 

                        $line_srl_value_name = 'line_srl_value_'.$po_id;
                        $line_srl_value_value = get_array_value($array_with_value, $line_srl_value_name); 
                        amount_decimal_cell(round($line_srl_value_value, 2));     

                    label_cell('');label_cell('');
                end_row();
                
                        //-------------------------------------------------------------------------------------------
                
//                alt_table_row_color($k);
                $item_detail_text2 = null;
                start_row();
                    label_cell('');
                    label_cell('NBT:', 'colspan=3');
                        $line_nbt_value_name = 'line_nbt_value_'.$po_id;
                        $line_nbt_value_value = get_array_value($array_with_value, $line_nbt_value_name);  
                        amount_decimal_cell(round($line_nbt_value_value, 2));     
                    label_cell('');label_cell('');
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Total Duty Without VAT:', 'colspan=3'); 
                        $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);   
                        amount_decimal_cell(round($line_total_duty_without_vat_value, 2));  
                    label_cell('');label_cell('');               
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Total Cost Without VAT:', 'colspan=3');
                        $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);   
                        amount_decimal_cell(round($line_total_cost_without_vat_value, 2));  
                    label_cell('');label_cell('');                
                end_row();
               
                start_row();
                    label_cell('');
                    label_cell('Clearing Charge:', 'colspan=3');
                        $line_clearing_charge_name = 'line_clearing_charge_'.$po_id;
                        $line_clearing_charge_value = get_array_value($array_with_value, $line_clearing_charge_name);  
                        amount_decimal_cell(round($line_clearing_charge_value, 2));  
                    div_end(); 
                
                    label_cell('');label_cell('');       
                end_row();
                
                start_row();
                    label_cell('');
                    label_cell('Total Cost:', 'colspan=3');
                        $line_total_cost_name = 'line_total_cost_'.$po_id;
                        $line_total_cost_value = get_array_value($array_with_value, $line_total_cost_name);    
                        amount_decimal_cell(round($line_total_cost_value, 2));  
                    div_end();
                
                    label_cell('');label_cell('');
                end_row();
                
//                label_cell($item_detail_text2, "colspan=16");
                
                unset($item_detail_text2);
                
                alt_table_row_color($k);
                    label_cell('', "colspan=5");

                    $line_unit_cost_name = 'line_unit_cost_'.$po_id;
                    $line_unit_cost_value = get_array_value($array_with_value, $line_unit_cost_name);   
                    amount_decimal_cell(round($line_unit_cost_value, 2)); 
  
                    $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);    
                    amount_decimal_cell(round($line_landed_unit_cost_value, 2)); 
            }
            
            vertical_space("height=16");
        dispaly_last_row($array_with_value);

    end_table();
}
    
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 create_po_line_id_array_with_value($order_no){
    $po_line_ids_array_with_value = array();
    $result = get_po_line_id_with_value($order_no);
    $num_of_rows = db_num_rows($result);

    if($num_of_rows > 0){
        while($row = db_fetch($result)){
            $po_line_ids_array_with_value[$row['value_name']] = $row['value'];
        }
    }
    return $po_line_ids_array_with_value;
}

function view_display_common_inputs($array_with_value){         
    start_outer_table(TABLESTYLE2, "width=35%"); // outer table
        start_table(TABLESTYLE2);  
            table_section(1, "40%");
                start_row();
                    label_row("Exchange RATE:", text_bold(get_array_value($array_with_value, 'exchange_rate_0')), "class='tableheader2'");
                    label_row("Clearing Charges:", text_bold(number_format(get_array_value($array_with_value, 'clearing_charge_0'), 2)), "class='tableheader2'");
                    label_row("Landed Unit Cost:", text_bold(number_format(get_array_value($array_with_value, 'landed_unit_cost_0'), 2)), "class='tableheader2'");
                end_row();  
            
            table_section(2, '40%');
                start_row();
                    label_row("PAL:", text_bold(number_format(get_array_value($array_with_value, 'pal_0'), 2)), "class='tableheader2'"); 
                    label_row("VAT:", text_bold(number_format(get_array_value($array_with_value, 'vat_0'), 2)), "class='tableheader2'");
                    label_row("NBT:", text_bold(number_format(get_array_value($array_with_value, 'nbt_0'), 2)), "class='tableheader2'");
                end_row();
        end_table();
    end_outer_table(2);
}

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 dispaly_last_row($array_with_value){        
    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();
}
    
$po_line_ids_array = create_po_line_ids_array($order_no);
$array_with_value = create_po_line_id_array_with_value($order_no);
view_display_common_inputs($array_with_value);

view_display_import_purchase_order_costing_details($order_no, $array_with_value);

//----------------------------------------------------------------------------------------------------

end_page(true, false, false, ST_PURCHORDER, $_GET['trans_no']);

?>