<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_DIRECT_PURCHASE_RECEIPT';
if (!@$_GET['popup'])
    $path_to_root = "..";
else	
    $path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/purchasing/includes/ui/direct_purchase_receipts_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/quality_checks_db.inc"); 
include_once($path_to_root . "/purchasing/includes/db/direct_purchase_requisitions_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc"); 
include_once($path_to_root . "/admin/db/transactions_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 
include_once($path_to_root . "/includes/project_cost_functions.inc"); 
include_once($path_to_root . "/includes/db/inventory_db.inc"); 
include_once($path_to_root . "/includes/approval_functions.inc"); 
include_once($path_to_root . "/inventory/includes/db/lot_allocations_db.inc");  
include_once($path_to_root . "/inventory/includes/ui/lot_ui.inc");  
include_once($path_to_root . "/inventory/includes/db/lots_db.inc");   

$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

global $systypes_array;

if (isset($_GET['ProcessedID'])) {
    page(_($help_context = $systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['ProcessedID'];
    $trans_type = $_GET['type'];

    $msg = get_session_variable('dp_rec_messages');
    clear_session_variable('dp_rec_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if (isset($_GET['BackedOrgID'])) {
    page(_($help_context = $systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['BackedOrgID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('dp_rec_bo_messages');
    clear_session_variable('dp_rec_bo_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if (isset($_GET['RejectedID'])) {
    page(_($help_context = $systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['RejectedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('dp_rcp_rj_messages');
    clear_session_variable('dp_rcp_rj_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if ((isset($_GET['qck_no']) && is_numeric($_GET['qck_no'])) || (isset($_POST['qck_no']) && is_numeric($_POST['qck_no']))) {
    $_POST['qck_no'] = isset($_GET['qck_no']) ? $_GET['qck_no'] : $_POST['qck_no'];
    
    $header = get_quality_check_header_record($_POST['qck_no']);
        if (is_array($header)) {
            if ($header['type'] != ST_QUALITY_CHECK) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_QUALITY_CHECK]." record #".$_POST['qck_no']));
            display_footer_exit();
        }
    
    $page_title = _('New '.$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]);
    
} elseif ((isset($_GET['rcp_no']) && is_numeric($_GET['rcp_no'])) || (isset($_POST['rcp_no']) && is_numeric($_POST['rcp_no']))) {
    $_POST['rcp_no'] = isset($_GET['rcp_no']) ? $_GET['rcp_no'] : $_POST['rcp_no'];
    
    $header = get_quality_check_header_record($_POST['rcp_no']);
        if (is_array($header)) {
            if ($header['type'] != ST_DIRECT_PURCHASE_RECEIPT) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]." record #".$_POST['rcp_no']));
            display_footer_exit();
        }
        
    $_POST['qck_no'] = $header['dpr_no'];
    $page_title = _('Modify '.$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]." #".$header['trans_no'] );
    
} else {
    page(_($help_context = ''), false, false, "", $js);
    display_error(("Could not find ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]));
    display_footer_exit();
}

page(_($help_context = $page_title), false, false, "", $js);

simple_page_mode(true);

if (isset($_POST['Process_RCP'])) {
    $msg = save_direct_purchase_receipts($_POST['qck_no'], $_POST['rcp_no'], true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['BACK_TO_ORIGINATOR_RCP'])) {
    $msg = back_to_originator_direct_purchase_receipt($_POST['qck_no'], $_POST['rcp_no']);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['REJECT_RCP'])) {
    $msg = reject_direct_purchase_receipt($_POST['qck_no'], $_POST['rcp_no']);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Create_RCP'])) {        
    $msg = save_direct_purchase_receipts($_POST['qck_no'], $_POST['rcp_no']);
        if ($msg['success'] !== true) {
            if (isset($msg["focus_to"])) {
                set_focus($msg["focus_to"]);            
            }
            $return_hd_row = get_quality_check_header_record($_POST['rcp_no']);
                if (! is_array($return_hd_row)) {
                    $rcp_no = $_POST['rcp_no'] = 0; 
                }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
        }
}

global $Ajax;

start_form();

    if (isset($_POST['qck_no']) && $_POST['qck_no'] > 0) {
        hidden('qck_no', $_POST['qck_no']);
    } else {
        $_POST['qck_no'] = 0;
    }   
        
    if (isset($_POST['rcp_no']) && $_POST['rcp_no'] > 0) {
        hidden('rcp_no', $_POST['rcp_no']);
    } else {
        $_POST['rcp_no'] = 0;
    }

    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $req_hd_row = display_quality_check_header_details($_POST['qck_no'], $_POST['rcp_no']);
        
        display_direct_purchase_receipt_header($_POST['rcp_no'], $req_hd_row);

        display_direct_purchase_receipt_details($_POST['qck_no'], $_POST['rcp_no']);
            
            $header = get_quality_check_header($_POST['rcp_no']);
            if (is_array($header)) {
                submit_center_first('Create_RCP', _("Save ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), '', 'default');    
                
                submit('Process_RCP', _("Process ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), true, _("Process"), false, ICON_SUBMIT);
            
                submit_center_last('Refresh', 'Refresh', 'Refresh', false, ICON_SUBMIT);                br(2);
                
                start_table();
                    submit_center_first('REJECT_RCP', _("Reject ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), '', true, ICON_DELETE);

                    submit('BACK_TO_ORIGINATOR_RCP', 'Back To Originator', true, 'Back To Originator', false, ICON_SUBMIT);
                end_table();
            } else {
                submit_center_first('Create_RCP', _("Create ".$systypes_array[ST_DIRECT_PURCHASE_RECEIPT]), '', 'default');    
                submit('Refresh', 'Refresh', true, false, false, ICON_SUBMIT);
            }
            
    div_end();
    
end_form(2);
end_page(@$_GET['popup'], false, false);        

?>