<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_IMPORT_PURCHASE_ORDERS';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");
include_once($path_to_root . "/includes/ui.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.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");
include_once($path_to_root . "/imports/includes/ui/import_purch_order_costing_detail_ui.inc");
include_once($path_to_root . "/includes/db/connect_db.inc");



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

page(_($help_context = "View Import Purchase Order"), true, false, "", $js);

simple_page_mode(true);

if(isset($_GET['trans_no']) && $_GET['trans_no'] > 0){
    display_heading(_("Import Purchase Order") . " #" . $_GET['trans_no']);
    $order_no = $_POST['trans_no'] = $_GET['trans_no'];
}elseif(isset($_POST['trans_no']) && $_POST['trans_no'] > 0){
    display_heading(_("Import Purchase Order") . " #" . $_POST['trans_no']);
    $order_no = $_POST['trans_no'];
}else{
    display_heading(_("Import Purchase Order"));
    $order_no = 0;
}

$total_in_rs_custom_perentage = 10;
$_POST['total_in_rs_custom_perentage_0'] = $total_in_rs_custom_perentage;
hidden("total_in_rs_custom_perentage_0", $total_in_rs_custom_perentage);

start_form(); 

if(isset($_POST['trans_no']) && $_POST['trans_no'] > 0){
    hidden('trans_no', $_POST['trans_no']);
}

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

    //-------------------------------------------------------------------------------------------------------------------
    
    global $Ajax;
    $Ajax->activate('content');
    
    div_start('content');
    
    $po_line_ids_array = create_po_line_ids_array($order_no);
    if(isset($_POST['calculate'])){
        $array_wv = create_po_line_id_array_with_value($order_no, $po_line_ids_array);
        $array_with_value = calculate_array_with_screen_values($po_line_ids_array, $array_wv, $total_in_rs_custom_perentage);
        set_session_variable('array_with_value', $array_with_value);
    }else{
        $array_with_value = create_po_line_id_array_with_value($order_no, $po_line_ids_array);
    }
    
    if(isset($_POST['save'])){
        $array_wv = create_po_line_id_array_with_value($order_no, $po_line_ids_array);
        $array_with_value = calculate_array_with_screen_values($po_line_ids_array, $array_wv, $total_in_rs_custom_perentage);
        set_session_variable('array_with_value', $array_with_value);
        $array_with_value = get_session_variable('array_with_value');
        begin_transaction();
            $msg = save_import_purch_order_costing_details_from_array($order_no, $po_line_ids_array, $array_with_value);
            
            if($msg['success'] !== true){
                cancel_transaction();
                display_error($msg['message']);
            }else{
                commit_transaction();
                display_notification($msg['message']);
            }
//        save_import_purch_order_costing_details($order_no, $po_line_ids_array);
    }
    
    display_common_inputs($order_no , $array_with_value);
    
    br();
    start_table(TABLESTYLE, "width=60%");
//    display_header();
       
    display_import_purch_order_costing_detail_table_content($order_no, $po_line_ids_array, $array_with_value);   
       
    div_end();
    
    br(1);
    
    submit_center_first('calculate', "Calculate", '', 'default');	
    submit_center_last('save', "Save", '', 'default');	

//----------------------------------------------------------------------------------------------------
    
end_form();  

br(1);

if(isset($_GET['trans_no']) && $_GET['trans_no'] > 0){
    end_page(true, false, false, ST_IMPORT_PURCHORDER, $_GET['trans_no']);
}else{
    end_page(true, false, false, ST_IMPORT_PURCHORDER, $order_no);
}

?>