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

$page_security = 'SA_TOOL_REQUEST';
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 . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/manufacturing/includes/ui/raw_material_issue_ui.inc");
include_once($path_to_root . "/manufacturing/includes/db/raw_material_issue_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_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 . "/includes/project_cost_functions.inc"); 

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

global $systypes_array;

$type = ST_RAW_MATERIAL_ISSUE;

if (isset($_GET['NewIssue']) || isset($_POST['NewIssue'])) {
    $_POST['NewIssue'] = isset($_GET['NewIssue']) ? $_GET['NewIssue'] : $_POST['NewIssue'];
    $trans_no = $_POST['NewIssue'];
    
    $page_title = _($systypes_array[$type]); 
    
} elseif (isset($_GET['ModifyIssue']) || isset($_POST['ModifyIssue'])) {
    $_POST['ModifyIssue'] = isset($_GET['ModifyIssue']) ? $_GET['ModifyIssue'] : $_POST['ModifyIssue'];
    $trans_no = $_POST['ModifyIssue'];
    
    $header = get_raw_material_issue_header_record_type_trans_no($type, $trans_no);
        if (is_array($header)) {
            if ($header['type'] != $type) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Invalid transaction ID."));
            display_footer_exit();
        }
    
    $page_title = _('Modify '.$systypes_array[$type]." #$trans_no" );
    
} elseif (isset($_GET['type']) && isset($_GET['ApprovedID'])) {
    $page_title = _($systypes_array[$_GET['type']]);
} else {
    page(_($help_context = ''), false, false, "", $js);
    display_error(("Invalid transaction type."));
    display_footer_exit();
}

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

simple_page_mode(true);

if (isset($_GET['ApprovedID'])) {
    global $systypes_array;
    
    $trans_no = $_GET['ApprovedID'];
    $trans_type = $_GET['type'];

    $type_name = $systypes_array[$trans_type];

    display_notification_centered("$type_name trans #$trans_no approved.");
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    hyperlink_params("$path_to_root/manufacturing/inquiry/raw_material_issue_inquiry.php", "Approve another $type_name", "type=$trans_type");
    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
    display_footer_exit();
}

if (isset($_POST['Save_DTI_Header'])) {
    $msg = save_raw_material_issue_header_only($trans_no, $type);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_raw_material_issue_details();
            $Mode = 'RESET';
        }
}

if (isset($_POST['Approve_DTI'])) {
    $msg = save_raw_material_issue_header_only($trans_no, $type, true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Add_DTI']) || isset($_POST['Update_DTI'])) {        
    $msg = save_raw_material_issue($trans_no, $type, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            $trans_no = $msg['trans_no'];
            display_notification($msg['message']);
            clear_raw_material_issue_details();
            $Mode = 'RESET';
        }
}

if ($Mode == 'Delete') {
    $msg = delete_raw_material_issue_line($trans_no, $type, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_raw_material_issue_details();
            $Mode = 'RESET';
        }
}

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

if ($Mode == 'RESET') {
    $selected_id = -1;
}

global $Ajax;

start_form();

    if (isset($_POST['NewIssue'])) {
        if (isset($trans_no) && $trans_no > 0) {
            hidden('NewIssue', $trans_no);
        } else {
            hidden('NewIssue', $_POST['NewIssue']);
            $trans_no = $_POST['NewIssue'];
        }
    } elseif(isset($_POST['ModifyIssue'])) {
        hidden('ModifyIssue', $_POST['ModifyIssue']);
    } 

    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $pi_header = display_raw_material_issue_header($trans_no, $type);

            if ($trans_no > 0) {
                submit_center_first('Save_DTI_Header', _("Save Header"), '', 'default');    

                submit_center_last('Approve_DTI', _("Approve"), _("Approve"), 'default');   br(2);
            }

        display_raw_material_issue_details($trans_no, $type, $Mode, $selected_id);

    div_end();
    
end_form(4);
end_page(@$_GET['popup'], false, false);        

?>