<?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 . "/sales/includes/ui/pos_customer_payments_ui.inc"); 
include_once($path_to_root . "/sales/includes/db/pos_customer_payments_db.inc");
include_once($path_to_root . "/sales/includes/db/pos_invoice_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_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;

$pos_cus_pay_id = 0;

if (isset($_GET['inv_trans_no']) || isset($_POST['inv_trans_no'])) {
    $_POST['inv_trans_no'] = isset($_GET['inv_trans_no']) ? $_GET['inv_trans_no'] : $_POST['inv_trans_no'];
    $inv_trans_no = $_POST['inv_trans_no'];
    
    $pos_inv_row = get_pos_invoice_header_record_type_trans_no(ST_POS_INVOICE, $inv_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_POS_INVOICE]." trans # $inv_trans_no."));
            display_footer_exit();
        }
        
    if (isset($_POST['pos_cus_pay_id']) && $_POST['pos_cus_pay_id'] > 0) {
        $pos_cus_pay_id = $_POST['pos_cus_pay_id'];
    } 
    
    $page_title = _($systypes_array[ST_POS_CUSTOMER_PAYMENT]); 
    
} elseif (isset($_GET['Modify_pos_cus_pay_id']) || isset($_POST['Modify_pos_cus_pay_id'])) {
    $_POST['Modify_pos_cus_pay_id'] = isset($_GET['Modify_pos_cus_pay_id']) ? $_GET['Modify_pos_cus_pay_id'] : $_POST['Modify_pos_cus_pay_id'];
    $pos_cus_pay_id = $_POST['Modify_pos_cus_pay_id'];
    
    $header = get_pos_customer_payment_record($pos_cus_pay_id);
        if (is_array($header)) {
            if ($header['type'] != ST_POS_CUSTOMER_PAYMENT) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
            
            $inv_trans_no = $header['source_trans_no'];
            $trans_no = $header['trans_no'];
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Invalid transaction ID."));
            display_footer_exit();
        }
    
    $page_title = _('Modify '.$systypes_array[ST_POS_CUSTOMER_PAYMENT]." #$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 has been processed.");
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    hyperlink_params("$path_to_root/sales/inquiry/pos_customer_payments_inquiry.php", "Process 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_POS_CUS_PAY'])) {
    $msg = save_pos_customer_payment($inv_trans_no, $pos_cus_pay_id);
        if ($msg['success'] !== true) {
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
            $pos_cus_pay_id = $msg['pos_cus_pay_id'];
        }
}

if (isset($_POST['Approve_POS_CUS_PAY'])) {
    $msg = save_pos_customer_payment($inv_trans_no, $pos_cus_pay_id, true);
        if ($msg['success'] !== true) {
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            display_error($msg['message']);
        } 
}

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

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

global $Ajax;

start_form();

    if (isset($_POST['inv_trans_no'])) {
        if (isset($inv_trans_no) && $inv_trans_no > 0) {
            hidden('inv_trans_no', $inv_trans_no);
        } else {
            hidden('inv_trans_no', $_POST['inv_trans_no']);
            $inv_trans_no = $_POST['inv_trans_no'];
        }
    } elseif(isset($_POST['Modify_pos_cus_pay_id'])) {
        hidden('Modify_pos_cus_pay_id', $_POST['Modify_pos_cus_pay_id']);
    } 
    
    if (isset($pos_cus_pay_id) && $pos_cus_pay_id > 0) {
        hidden('pos_cus_pay_id', $pos_cus_pay_id);
    } elseif (isset($_POST['pos_cus_pay_id']) && $_POST['pos_cus_pay_id'] > 0) {
        hidden('pos_cus_pay_id', $_POST['pos_cus_pay_id']);
        $pos_cus_pay_id = $_POST['pos_cus_pay_id'];
    } else {
        $pos_cus_pay_id = 0;
    }

    $Ajax->activate('pos_cus_pay_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        display_pos_invoice_header_details($inv_trans_no);
    
        $pi_header = display_pos_customer_payment($inv_trans_no, $pos_cus_pay_id);

            submit_center_first('Save_POS_CUS_PAY', _("Save"), 'Save', 'default');    

            if ($pos_cus_pay_id > 0) {
                submit_center_last('Approve_POS_CUS_PAY', _("Process"), _("Process"), 'default'); br();
                submit_center('Refresh', _("Refresh"), true, _("Refresh"), false, ICON_SUBMIT);  
            } else {
                submit_center_last('Refresh', _("Refresh"), _("Refresh"), 'default');  
            }

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

?>