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

$page_security = 'SA_SALESORDER';
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 . "/payroll/includes/ui/salary_advance_payments_ui.inc"); 
include_once($path_to_root . "/payroll/includes/db/salary_advances_db.inc"); 
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_float_adjustments_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_db.inc"); 
include_once($path_to_root . "/includes/db/approval_functions_db.inc"); 

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

global $systypes_array;

$type = ST_SALARY_ADVANCE_PAYMENT;

if (isset($_GET['sad_trans_no']) || isset($_POST['sad_trans_no'])) {
    $_POST['sad_trans_no'] = isset($_GET['sad_trans_no']) ? $_GET['sad_trans_no'] : $_POST['sad_trans_no'];
    $sad_trans_no = $_POST['sad_trans_no'];
    
    $pos_inv_row = get_salary_advance_details($sad_trans_no);
        if (! (isset($pos_inv_row['id']) && $pos_inv_row['id'] > 0)) {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Unknown ".$systypes_array[ST_SALARY_ADVANCE]." trans # $sad_trans_no."));
            display_footer_exit();
        }
    $page_title = _($systypes_array[$type]); 
    
} elseif (isset($_GET['ApprovedID'])) {
    $page_title = _($systypes_array[$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'];
    $type_name = $systypes_array[$type];

    display_notification_centered("Salary advance payment trans #$trans_no has been processed.");
    hyperlink_params("$path_to_root/payroll/inquiry/salary_advances_inquiry.php", "Process another $type_name", "trans_status=A");
    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$type&trans_no=$trans_no");
    display_footer_exit();
}

if (isset($_POST['PAY_SALARY_ADVANCE'])) {
    $msg = save_salary_advance_payment($sad_trans_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

global $Ajax;

start_form();

    if (isset($_POST['sad_trans_no'])) {
        if (isset($sad_trans_no) && $sad_trans_no > 0) {
            hidden('sad_trans_no', $sad_trans_no);
        } else {
            hidden('sad_trans_no', $_POST['sad_trans_no']);
            $sad_trans_no = $_POST['sad_trans_no'];
        }
    } 
    
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        display_salary_advance_details($sad_trans_no);
    
            submit_center_first('PAY_SALARY_ADVANCE', _("Pay"), 'Pay', 'default');    

            submit_center_last('Refresh', _("Refresh"), _("Refresh"), 'default');  

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

?>