<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_DAILY_ITEM_ISSUE';
$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 . "/includes/ui.inc");
//include_once($path_to_root . "/includes/ui/ui_lists.inc");
//include_once($path_to_root . "/includes/ui/ui_input.inc");
//include_once($path_to_root . "/includes/ui/ui_msgs.inc");
include_once($path_to_root . "/inventory/includes/ui/batch_item_issue_transactions_ui.inc");
include_once($path_to_root . "/inventory/includes/db/batch_item_issue_transactions_db.inc");
include_once($path_to_root . "/inventory/includes/db/batch_item_issue_db.inc");

include_once($path_to_root . "/purchasing/includes/ui/po_ui.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/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();

if (isset($_GET['type_id']) && $_GET['type_id'] > 0)
{
    $_POST['source_type_id'] = $_GET['type_id'];   
}
if (isset($_POST['source_type_id']) && $_POST['source_type_id'] > 0)
{
    $source_type_id = $_POST['source_type_id'];
}

if (isset($_GET['line_id']) && $_GET['line_id'] > 0)
{
    $_POST['bii_line_id'] = $_GET['line_id'];    
}
if (isset($_POST['bii_line_id']) && $_POST['bii_line_id'] > 0)
{
    $bii_line_id = $_POST['bii_line_id'];
}

$help_context = "Batch Item Issue - Select ".$systypes_array[$source_type_id]."s";

page($help_context, true, false, "", $js);

simple_page_mode(true);


$from_date = INFINITE_START_DATE;
$to_date = INFINITE_END_DATE;

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';
//    }
//} 

$delete_id = find_submit("Delete"."_biit_");
if($delete_id > 0){
    
//    begin_transaction();
    $dpra = handle_delete_batch_item_issue_detail_transaction($delete_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['Save_BII_Transactions'])){     
    
    handle_save_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    
} 
//---------------------------------------------------------------------------------------------------------------------------

$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_batch_item_issue_line_details($bii_line_id);
    br(1);
    
    if($source_type_id == ST_PURCH_REQ){
        display_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    }
    elseif($source_type_id == ST_MATERIAL_REQ){
        display_batch_item_issue_mr_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    }
    elseif($source_type_id == ST_DAILY_ITEM_ISSUE){
        display_batch_item_issue_dii_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    }
    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('source_type_id', $_POST['source_type_id']);
    hidden('bii_line_id', $_POST['bii_line_id']);
//    hidden('row_state', $_POST['row_state']);
//    hidden('item_code', $_POST['item_code']);
//    hidden('item_description', $_POST['item_description']);

    submit_center_first('Save_BII_Transactions', _("Save"), 'default');
    submit_center_last('Calculate_BII_Transactions', _("Calculate"), '', 'default');
    
end_form();
//----------------------------------------------------------------------------------------------------------------------------

end_page(true, false, false, $source_type_id, $bii_line_id);

?>