<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PURCHASE_REQUISITION_ALLOCATION';
$path_to_root = "../..";
//include($path_to_root . "/purchasing/includes/po_class.inc");

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_new_db.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/purchasing/includes/ui/purchase_requisition_allocation_details_edit_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisition_allocation_details_edit_db.inc");
include_once($path_to_root . "/includes/db/crm_contacts_db.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/includes/date_functions.inc");


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

if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "Manage Purchase Requition Allocations"), true, false, "", $js);

simple_page_mode(true);

if (isset($_GET['trans_no']))
{
    $_POST['trans_no'] = $_GET['trans_no'];
    
    $trans_no = $_POST['trans_no'];        
    $sep_pos = strpos($trans_no, "|");
    $pr_line_id = substr($trans_no, 0, $sep_pos);
    $selected_id = substr($trans_no, $sep_pos+1);
}

if (isset($_POST['trans_no']))
{
    $trans_no = $_POST['trans_no'];

    $sep_pos = strpos($trans_no, "|");
    $pr_line_id = substr($trans_no, 0, $sep_pos); 
}



function clear_data()
{
    global $selected_id;
    
    $selected_id = -1;
    unset($_POST['supplier_id']);
}

//if(isset($pr_po_line_id) && $pr_po_line_id >0 ){
//    $selected_id = $pr_po_line_id;
//}
//
//if(isset($pr_po_line_id) && $pr_po_line_id >0 ){
//    $selected_id = $pr_po_line_id;
//}


if(isset($_POST['CancelItemChanges'])){
    $Mode = 'RESET';
    $selected_id = -1;
}


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


if(isset($_POST['new_pr_po_line'])&& check_pr_allocation_line_data()){     
    
    $npra = new_pr_allocation_line($pr_line_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)){
    
    begin_transaction();
    $dpra = delete_pr_allocation_line($selected_id);
    if($dpra['success'] !== true){
        cancel_transaction();
        display_error($dpra['message']);
    }
    else{
        commit_transaction();
        $selected_id = -1;
        display_notification($dpra['message']);
        $Mode = 'RESET';
    }
}

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

$pr_po_line_id = $selected_id;

//$pr_line_id_no = substr($selected_id, 14);
//if(!($pr_line_id_no > 0)){
//    $pr_line_id_no = -1;
//}

start_form();



    global $Ajax;
    
    display_purchase_requisition_line($pr_line_id, $pr_po_line_id);
    br();
    
    $Ajax->activate('pr_pod_div');
    $Ajax->activate('supplier_id');
    
    div_start('pr_pod_div');
    display_pr_po_details($pr_line_id, $pr_po_line_id);
//    display_purchase_requisition_allocation_edit_controls($pr_line_id, $pr_po_line_id);
    div_end();

    
    
//    if($selected_id > 0){
//        start_row();
//        submit_center_first("update_pr_po_line", "Update PR Allocation Detail" );
//        submit_center_last("close_pr_line", "Close PR Line" );
//        end_row();
//    }
//    else{
//        start_row();
//        submit_center_first("new_pr_po_line", "Create New PR Allocation" );
//        submit_center_last("close_pr_line", "Close PR Line" );
//        end_row();
//    }
    
    br(2);

    display_pr_item_purchase_history($_POST['item_code'], get_post('item_description'));
    
    
    
//    if(isset($_POST["edit_pr"])){//18
//        $edit_pr = $_POST["edit_pr"];
//        $pr_line_id_no = substr($edit_pr, 18);
//        display_purchase_requisition_detail_edit($pr_line_id, $pr_line_id_no);
//    }


    hidden('selected_id', $selected_id);
    hidden('pr_line_id', $pr_line_id);
    hidden('trans_no', $_POST['trans_no']);
//    hidden('row_state', $_POST['row_state']);
    hidden('item_code', $_POST['item_code']);
    hidden('item_description', $_POST['item_description']);

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

end_page(true, false, false, ST_PURCHORDER, $trans_no);

?>