<?php
/**********************************************************************
***********************************************************************/
//-----------------------------------------------------------------------------
$page_security = 'SA_CUSTOMER_PAYMENT_RETURN';
$path_to_root = "..";

include_once($path_to_root . "/sales/includes/sales_payment_return_class.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/taxes/tax_calc.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_payment_return_db.inc");
include_once($path_to_root . "/sales/includes/ui/customer_payment_return_ui.inc");
include_once($path_to_root . "/includes/banking_custom.inc");

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

if(isset($_GET['ModifyDelivery'])) {
    $_SESSION['page_title'] = sprintf(_("Modifying Delivery Note # %d."), $_GET['ModifyDelivery']);
    $help_context = "Customer Payment Return";
    processing_start();
}elseif (isset($_GET['InvoiceNumber'])) {
    $_SESSION['page_title'] = _($help_context = "Customer Payment Return");
    processing_start();
}

page($_SESSION['page_title'], false, false, "", $js);

if(isset($_GET['AddedID'])){
    $dispatch_no = $_GET['AddedID'];

    display_notification_centered(sprintf(_("Customer Payment Return # %d has been created."),$dispatch_no));
    display_note(get_customer_trans_view_str(ST_CUSTOMER_PAYMENT_RETURN, $dispatch_no, _("&View this Customer Payment Return")), 0, 1);

    display_note(print_document_link($dispatch_no, _("&Print Customer Payment Return"), true, ST_CUSTOMER_PAYMENT_RETURN));

//    display_note(get_gl_view_str(13, $dispatch_no, _("View the GL Journal Entries for this Dispatch")),1);
//    hyperlink_params("$path_to_root/sales/customer_invoice.php", _("Invoice This Delivery"), "DeliveryNumber=$dispatch_no");
//    hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select Another Order For Dispatch"), "OutstandingOnly=1");

    display_footer_exit();

}

//-------------------------------------------------------------------------------------------------------------

if(isset($_GET['InvoiceNumber']) && $_GET['InvoiceNumber'] > 0){
    $ord = new Sales_Payment_Return_Cart(ST_SALESINVOICE, $_GET['InvoiceNumber'], true);

    $project_id = $ord->project_id;
    $user_id = $_SESSION['wa_current_user']->user;
    $cap = can_access_project($project_id, $user_id);
    if($cap['success'] !== true){
        display_error($cap["message"]);
        set_focus('project_id');
        display_footer_exit();
        exit;
    }   
    
    if($ord->count_items() == 0) {
//        hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php", _("Select a different sales order to delivery"), "OutstandingOnly=1");
        die ("<br><b>" . _("This invoice has nothing to return.") . "</b>");
    }
    
    $_SESSION['Items'] = $ord;
    copy_from_cart();
    
}elseif(isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0){
    $_SESSION['Items'] = new Cart(ST_CUSTOMER_PAYMENT_RETURN, $_GET['ModifyDelivery']);

    if($_SESSION['Items']->count_items() == 0){
        hyperlink_params($path_to_root . "/sales/inquiry/customer_inquiry.php", _("Select a different delivery"), "OutstandingOnly=1");
        echo "<br><center><b>" . _("This delivery has all items invoiced. There is nothing to modify.") ."</center></b>";
        display_footer_exit();
    }

    copy_from_cart();
	
}elseif(!processing_active()){
    /* This page can only be called with an order number for invoicing*/

    display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
    hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");

    end_page();
    exit;

}else{
    check_edit_conflicts();

//    $cq_msg = check_customer_payment_return_quantities();
//    if($cq_msg != ""){
//        display_error(_($cq_msg));
//    }else
        
    if(!check_num('ChargeFreightCost', 0)) {
        display_error(_("Freight cost cannot be less than zero"));
        set_focus('ChargeFreightCost');
    }
}

//------------------------------------------------------------------------------

if(isset($_POST['Calculate_payment_return'])){
       
    check_customer_payment_return_data();
    check_customer_payment_return_qoh();
}

if(isset($_POST['process_payment_return'])){
       
    if(check_customer_payment_return_data() && check_customer_payment_return_qoh()){
        process_customer_payment_return();
    }
}

if(isset($_POST['Update']) || isset($_POST['_Location_update']) || isset($_POST['qty'])){
    $Ajax->activate('Items');
}

//------------------------------------------------------------------------------

global $Ajax;

$Ajax->activate('Items');

start_form();
hidden('cart_id');

    display_customer_payment_return_header();
    display_customer_payment_return_details();

    submit_center_first('Calculate_payment_return', _("Calculate"), _('Refresh document page'), true);
    submit_center_last('process_payment_return', _("Process Payment Return"), _('Check entered data and save document'), 'default');

    br(1);
    if(isset($_POST['clear_quantity'])) {
        submit_center_first('reset_quantity', _("Reset quantity"), _('Refresh document page'), true);
    }else{
        submit_center_first('clear_quantity', _("Clear quantity"), _('Refresh document page'), true);
    }

end_form();

br(2);

end_page();

?>