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

include($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/ui/allocation_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
include_once($path_to_root . "/purchasing/includes/ui/supplier_payment_ui.inc");

include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/purchasing/includes/ui/supplier_allocation_ui.inc");
//include_once($path_to_root . "/petty_cash/includes/db/petty_cash_payments_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_db.inc");


        
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

add_js_file('payalloc.js');

page(_($help_context = "Supplier Payment Entry"), false, false, "", $js);

if (isset($_GET['supplier_id']))
{
	$_POST['supplier_id'] = $_GET['supplier_id'];
}

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

check_db_has_suppliers(_("There are no suppliers defined in the system."));

check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));

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

if (!isset($_POST['supplier_id']))
	$_POST['supplier_id'] = get_global_supplier(false);

if (!isset($_POST['DatePaid']))
{
	$_POST['DatePaid'] = new_doc_date();
	if (!is_date_in_fiscalyear($_POST['DatePaid']))
		$_POST['DatePaid'] = end_fiscalyear();
}

if (isset($_POST['_DatePaid_changed'])) {
  $Ajax->activate('_ex_rate');
}

if (list_updated('supplier_id')){
	$_POST['amount'] = price_format(0);
	$_SESSION['alloc']->person_id = get_post('supplier_id');
	$Ajax->activate('amount');
} elseif (list_updated('bank_account'))
	$Ajax->activate('alloc_tbl');

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

if (!isset($_POST['bank_account'])) { // first page call
	allocate_suppliet_payment();
}


if (isset($_GET['AddedID'])) {
	 display_supplier_payment_added_id_links($_GET['AddedID']);
}

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





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

if (isset($_POST['ProcessSuppPayment']))
{
	 /*First off  check for valid inputs */
    if (check_suppliet_payment_inputs() == true) 
    {
    	handle_add_supplier_payment();
    	end_page();
     	exit;
    }
}

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

start_form();

	start_outer_table(TABLESTYLE2, "width=60%", 5);

	table_section(1);

    supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);

	if (list_updated('supplier_id') || list_updated('bank_account')) {
	  $_SESSION['alloc']->read();
	  $_POST['memo_'] = $_POST['amount'] = '';
	  $Ajax->activate('alloc_tbl');
	}

	set_global_supplier($_POST['supplier_id']);

	if (!list_updated('bank_account') && !get_post('__ex_rate_changed'))
		$_POST['bank_account'] = get_default_supplier_bank_account($_POST['supplier_id']);
	else
		$_POST['amount'] = price_format(0);

    bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);

	bank_balance_row($_POST['bank_account']);

	table_section(2);

    date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);

    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));

	table_section(3);

	$comp_currency = get_company_currency();
	$supplier_currency = $_SESSION['alloc']->set_person($_POST['supplier_id'], PT_SUPPLIER);
	if (!$supplier_currency)
			$supplier_currency = $comp_currency;
	$_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);

	if ($bank_currency != $supplier_currency) 
	{
		amount_row(_("Bank Amount:"), 'bank_amount', null, '', $bank_currency, 2);
	}

	amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);


	end_outer_table(2);

//        display_heading("Credit Notes");
//        display_unallocated_suppler_payments_and_credit_notes($_POST['supplier_id']);
    
//        br(2);
        display_heading("Transactions to be Allocated");
	div_start('alloc_tbl');
	show_allocatable(false);
	div_end();

        br(2);
	start_table(TABLESTYLE, "width=60%");
	amount_row(_("Amount of Discount:"), 'discount', null, '', $supplier_currency);
	amount_row(_("Amount of Payment:"), 'amount', null, '', $supplier_currency);
        
//        supplier_purchase_orders_list_row("Purchase Order No (for Advance Payments):", 'po_no', $_POST['supplier_id'], null, "Select a purchase order");
//	petty_cash_floats_list_row("Float:", 'float_id', null, '', false);
//        label_row("Float:", 'Supplier payments from floats facility has been removed in the program. Use GL > Cash Payments instead.');
        $_POST['float_id'] = 0;
        hidden('float_id', 0);
        
        textarea_row(_("Memo:"), 'memo_', null, 22, 4);
	end_table(1);

        br(2);
	submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
        
        br(2);

end_form();

end_page();



?>
