<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PURCHASE_REQUISITION_ALLOCATION';
$path_to_root = "..";

include($path_to_root . "/includes/session.inc");
add_access_extensions();

//page(_($help_context = "Requisition Alocations"));

//include_once($path_to_root . "/requisitions/includes/modules_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/ui/ui_lists.inc");
//include_once($path_to_root . "/purchasing/includes/ui/pr_allocation_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_new_db.inc");
include_once($path_to_root . "/inventory/includes/db/alternate_units_db.inc");
include_once($path_to_root . "/purchasing/includes/db/generate_po_db.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/purchasing/includes/ui/pr_allocation_new_ui.inc");

include_once($path_to_root . "/purchasing/includes/db/po_db.inc");


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

page(_($help_context = "Requisition Allocations"), @$_REQUEST['popup'], false, "", $js);

simple_page_mode(true);
//-----------------------------------------------------------------------------------
if(isset($_GET['po'])) {
	if($_GET['po'] == 'yes') {
		if (generate_po())
			display_notification(_("Purchase orders has been generated."));
		else
			display_error(_("Purchase orders generation failed."));
	}
}

$new_record_selection = false;
if(strlen($selected_id) > 10){
    $new_record_selection = true;
}

if(substr($selected_id, 0, 14) == "UpdatePrPoLine"){
    $selected_id = substr($selected_id, 14);
    $_POST["update_type"] = "update_pr_po_line";
}

if(substr($selected_id, 0, 7) == "NewLine"){
    $selected_id = substr($selected_id, 7);
    $_POST["update_type"] =  "new_line";
}

if(substr($selected_id, 0, 6) == "Delete"){
    $selected_id = substr($selected_id, 6);
    $_POST["update_type"] =  "delete_line";
}


function clear_data()
{
        unset($_POST['pr_no']);
        unset($_POST['pr_line_id']);
        unset($_POST['pr_po_line_id']);
        unset($_POST['editable_descri']);
        unset($_POST['pr_po_item_code']);
        unset($_POST['pr_po_item_description']);
        unset($_POST['project_id']);
        unset($_POST['project_description']);
        unset($_POST['task_description']);
        unset($_POST['item_code']);
        unset($_POST['requested_quantity']);
        unset($_POST['base_unit']);
        unset($_POST['estimate_price']);
        unset($_POST['description']);
        unset($_POST['line_comment']);
        unset($_POST['pr_location']);
        unset($_POST['requested_unit']);
        unset($_POST['requested_unit_qty']);
        unset($_POST['required_date']);
        unset($_POST['req_unit_conversion_factor']);
        unset($_POST['requested_quantity']);
        unset($_POST['project_site_id']);
        unset($_POST['project_site_name']);
        unset($_POST['pr_po_item_description']);
        unset($_POST['pr_po_item_description']);        
        unset($_POST['quantity_ordered']);
        unset($_POST['supplier_id']);
        unset($_POST['line_loc_code']);
        unset($_POST['pr_po_project_site_id']);
        unset($_POST['order_unit']);
        unset($_POST['order_unit_qty']);
        unset($_POST['order_line_required_date']);
        unset($_POST['order_unit_price']);
        unset($_POST['order_unit_price_discount']);
        unset($_POST['row_state']);
        unset($_POST['pr_line_id']);
        unset($_POST['row_state']);
}



function update_pr_allocation_line($selected_id)
{
    if(isset($_POST['order_unit']) && strlen($_POST['order_unit']) > 0){
        $order_unit = $_POST['order_unit'];
    }
    else{
        $item_edit_info = get_item_edit_info($_POST['item_code']);
        $order_unit = $item_edit_info['units'];
    }
    
    $order_unit_price_discount = input_num('order_unit_price_discount');
    
    get_convertion_factor2($_POST['item_code'], $_POST['order_unit'], $convesion_factor);
    $base_unit_order_qty = input_num('order_unit_qty') * $convesion_factor;
    $base_unit_order_price = (input_num('order_unit_price') * (1 - $order_unit_price_discount/100)) / $convesion_factor;
    
    $project_site_id = 0;
    if(get_post('pr_po_project_site_id') > 0){
        $project_site_id = get_post('pr_po_project_site_id');
    }
    
    $ura = update_requisition_allocation($selected_id, get_post('pr_line_id'), $base_unit_order_qty, 
                                    $base_unit_order_price, $_POST['order_unit'], input_num('order_unit_qty'), 
                                    input_num('order_unit_price'), $convesion_factor, $order_unit_price_discount, 
                                    $_POST['supplier_id'],  get_post('line_loc_code'), $_POST['delivery_required_date'], 
                                    input_num('minimum_price'), input_num('minimum_price_discount'), get_post('min_price_supplier_id'), 
                                    get_post('pr_po_item_description'), get_post('project_id'), $project_site_id, 
                                    get_post('row_state'));

    return $ura;
}


function new_pr_allocation_line($selected_id)
{
    if(isset($_POST['order_unit']) && strlen($_POST['order_unit']) > 0){
        $order_unit = $_POST['order_unit'];
    }
    else{
        $item_edit_info = get_item_edit_info($_POST['item_code']);
        $order_unit = $item_edit_info['units'];
    }
    
    $order_unit_price_discount = input_num('order_unit_price_discount');
    
    get_convertion_factor2($_POST['item_code'], $_POST['order_unit'], $convesion_factor);
    $base_unit_order_qty = input_num('order_unit_qty') * $convesion_factor;
    $base_unit_order_price = (input_num('order_unit_price') * (1 - $order_unit_price_discount/100)) / $convesion_factor;
    
    $project_site_id = 0;
    if(get_post('pr_po_project_site_id') > 0){
        $project_site_id = get_post('pr_po_project_site_id');
    }
    
    $ara = add_requisition_allocation($selected_id, $base_unit_order_qty, $base_unit_order_price,
                                    $_POST['order_unit'], input_num('order_unit_qty'), input_num('order_unit_price'), $convesion_factor,
                                    $order_unit_price_discount, $_POST['supplier_id'], get_post('line_loc_code'), 
                                    $_POST['delivery_required_date'], input_num('minimum_price'), input_num('minimum_price_discount'), 
                                    get_post('min_price_supplier_id'), get_post('item_code'), get_post('pr_po_item_description'),
                                    get_post('project_id'), $project_site_id);

    return $ara;
}






if(isset($_POST['update_pr_po_line']) && check_pr_allocation_line_data()){
    
    $upra = update_pr_allocation_line($selected_id);
    if($upra['success'] !== true){
        display_error($upra['message']);
    }
    else{
        display_notification($upra['message']);
        $Mode = 'RESET';
    }
}


if(isset($_POST['new_line'])&& check_pr_allocation_line_data()){     
    
    $npra = new_pr_allocation_line($selected_id);	
    if($npra['success'] !== true){
        display_error($npra['message']);
    }
    else{
        display_notification($npra['message']);
        $Mode = 'RESET';
    }
} 

if(isset($_POST['Delete'.$selected_id]) && can_delete_pr_allocation_line($selected_id)){
    
    $dpra = delete_pr_allocation_line($selected_id);
    if($dpra['success'] !== true){
        display_error($dpra['message']);
    }
    else{
        display_notification($dpra['message']);
        $Mode = 'RESET';
    }
}

if(isset($_POST['close_pr_line'])){     
    
    $cpra = close_pr_line($selected_id);
    if($cpra['success'] !== true){
        display_error($cpra['message']);
    }
    else{
        display_notification($cpra['message']);
        $Mode = 'RESET';
    }
} 
    
//-----------------------------------------------------------------------------------

if ($Mode == 'RESET')
{
	$selected_id = -1;
    clear_data();
    unset($_POST['update_type']);

}
//-----------------------------------------------------------------------------------

global $Ajax;

start_form();

start_table(TABLESTYLE_INQUIRY_PARAMETERES);
start_row();
ref_cells(text_bold("PR No."), 'purchase_requisition_no', null, null, null, true);
value_list_cells("Department:", "department_id", "DEPARTMENTS", null, "", true);

project_list_cells("Project:", 'project_id', null, "", true);

system_users_list_cells("Requester", 'requester_id', null, '', true);


end_row();
end_table();

br(1);

$Ajax->activate('disp_open_prs');

div_start('disp_open_prs');
display_open_purchase_requisitions(get_post('department_id'), get_post('project_id'), get_post('requester_id'), get_post('purchase_requisition_no'));
div_end();

hidden('update_type',get_post("update_type"));
//-----------------------------------------------------------------------------------

//start_table(TABLESTYLE2);
//start_outer_table(TABLESTYLE2); // outer table
//
//$pr_row = array();
//load_pr_line_details($selected_id, $pr_row);


//table_section(1);
//table_section_title("PR Line");
//display_pr_line_details($selected_id, $pr_row);
//
//
//table_section(2);
//table_section_title("PR Allocation Line");
//display_pr_allocation_line_details($selected_id, $pr_row, $new_record_selection);
//
//end_table(1);
//end_outer_table(1); // outer table

//submit_add_or_update_center($selected_id == -1, '', 'both');
//submit_add_or_update(false);

if(get_post('update_type') == "update_pr_po_line"){
    submit_center_first("update_pr_po_line", "Update PR Allocation Detail" );
    br(2);
    submit_center_first("close_pr_line", "Close PR Line" );
}
elseif(get_post('update_type') == "new_line"){
    submit_center_first("new_line", "Create New PR Allocation" );
    br(2);
    submit_center_first("close_pr_line", "Close PR Line" );
}

br(2);

end_form();

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

end_page();

?>









