<?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 . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/sales/includes/ui/pos_credit_note_ui.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 . "/inventory/includes/db/items_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc"); 
include_once($path_to_root . "/includes/db/inventory_db.inc"); 
include_once($path_to_root . "/includes/project_cost_functions.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/inventory/includes/db/daily_item_issue_db.inc"); 

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

global $systypes_array;

if (isset($_GET['NewCreditNote']) || isset($_POST['NewCreditNote'])) {
    $_POST['NewCreditNote'] = isset($_GET['NewCreditNote']) ? $_GET['NewCreditNote'] : $_POST['NewCreditNote'];
    $dii_no = $_POST['NewCreditNote'];
    
    $page_title = _($systypes_array[ST_POS_CREDIT_NOTE]); 
    
} elseif (isset($_GET['ModifyCreditNote']) || isset($_POST['ModifyCreditNote'])) {
    $_POST['ModifyCreditNote'] = isset($_GET['ModifyCreditNote']) ? $_GET['ModifyCreditNote'] : $_POST['ModifyCreditNote'];
    $dii_no = $_POST['ModifyCreditNote'];
    
    $header = get_pos_invoice_header($dii_no);
        if (is_array($header)) {
            if ($header['type'] != ST_POS_CREDIT_NOTE) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
            
            $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_CREDIT_NOTE]." #$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/sales/inquiry/pos_invoices_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_DIS_Header'])) {
    $msg = save_pos_credit_note_header_only($dii_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['Approve_DIS'])) {
    $msg = save_pos_credit_note_header_only($dii_no, true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Add_DIS']) || isset($_POST['Update_DIS'])) {        
    $msg = save_pos_credit_note($dii_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            $dii_no = $msg['trans_no'];
            display_notification($msg['message']);
            clear_pos_credit_note_details();
            $Mode = 'RESET';
        }
}

if ($Mode == 'Delete') {
    $msg = delete_pos_credit_note_line($dii_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_pos_credit_note_details();
            $Mode = 'RESET';
        }
}

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

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

global $Ajax;

start_form();

    if (isset($_POST['NewCreditNote'])) {
        if (isset($dii_no) && $dii_no > 0) {
            hidden('NewCreditNote', $dii_no);
        } else {
            hidden('NewCreditNote', $_POST['NewCreditNote']);
            $dii_no = $_POST['NewCreditNote'];
        }
    } elseif(isset($_POST['ModifyCreditNote'])) {
        hidden('ModifyCreditNote', $_POST['ModifyCreditNote']);
    } 

    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $pi_header = display_pos_credit_note_header($dii_no);

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

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

            display_pos_credit_note_details($dii_no);

        $Ajax->activate("control_items");
        display_pos_credit_note_details_edit_controls($selected_id, $Mode);

            if ($selected_id > 0 && $Mode != 'Delete') {
                submit_center_first('Update_DIS', _("Update"), '', 'default');
                submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
                hidden('selected_id', $selected_id);

            } else {        
                submit_center_first('Add_DIS', _("ADD"), '', 'default');
                submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
            }
        
    div_end();
    
end_form(2);
end_page(@$_GET['popup'], false, false);        

?>