global $Ajax;
$Ajax->activate('items_table');
+ $Ajax->activate('footer');
set_focus('_code_id_edit');
}
display_footer_exit();
}
-if (isset($_POST['_date__changed'])) {
- $Ajax->activate('_ex_rate');
-}
//--------------------------------------------------------------------------------------------------
function create_cart($type, $trans_no)
if (isset($_POST['Process']))
{
begin_transaction();
-
+
$_SESSION['pay_items'] = &$_SESSION['pay_items'];
$new = $_SESSION['pay_items']->order_id == 0;
$_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, $_POST['bank_account'],
$_SESSION['pay_items'], $_POST['date_'],
$_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
- $_POST['ref'], $_POST['memo_'], true);
+ $_POST['ref'], $_POST['memo_'], true, input_num('settled_amount', null));
+ add_new_exchange_rate(get_bank_account_currency(get_post('bank_account')), get_post('date_'), input_num('_ex_rate'));
$trans_type = $trans[0];
$trans_no = $trans[1];
new_doc_date($_POST['date_']);
$_SESSION['pay_items']->clear_items();
unset($_SESSION['pay_items']);
-
+
commit_transaction();
-
+
if ($new)
meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
"AddedID=$trans_no" : "AddedDep=$trans_no");
set_focus('amount');
return false;
}
+ if (isset($_POST['_ex_rate']) && input_num('_ex_rate') <= 0)
+ {
+ display_error( _("The exchange rate cannot be zero or a negative number."));
+ set_focus('_ex_rate');
+ return false;
+ }
return true;
}
echo "<td>";
display_gl_items($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
_("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
-gl_options_controls();
+gl_options_controls($_SESSION['pay_items']);
echo "</td>";
end_row();
end_table(1);
// FIXME -revise code for update case
//
function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $date_,
- $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
+ $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true, $settled_amount=null)
{
global $Refs, $SysPrefs;
$args = func_get_args(); if (count($args) < 11) $args[] = true;
$args = (object)array_combine(array('trans_type', 'trans_no', 'from_account', 'items', 'date_',
- 'person_type_id', 'person_id', 'person_detail_id', 'ref', 'memo_', 'use_transaction'),
+ 'person_type_id', 'person_id', 'person_detail_id', 'ref', 'memo_', 'use_transaction', 'settled_amount'),
$args);
hook_db_prewrite($args, $trans_type);
$aid = 0;
// we need to add a customer transaction record
// convert to customer currency
- $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
+ if (!isset($settled_amount)) // leaved for backward/ext compatibility
+ $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
+ else
+ $cust_amount = $settled_amount;
// we need to negate it too
$cust_amount = -$cust_amount;
{
// we need to add a supplier transaction record
// convert to supp currency
- $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
+ if (!isset($settled_amount)) // leaved for for backward/ext compatibility
+ $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
+ else
+ $supp_amount = $settled_amount;
// we need to negate it too
$supp_amount = -$supp_amount;
//---------------------------------------------------------------------------------------------
+function add_new_exchange_rate($curr_code, $date_, $ex_rate)
+{
+ if (is_company_currency($curr_code) || !$ex_rate)
+ return;
+
+ if (!get_date_exchange_rate($curr_code, $date_))
+ add_exchange_rate($curr_code, $date_, 1.0/$ex_rate, 1.0/$ex_rate);
+}
+
+//---------------------------------------------------------------------------------------------
+
function delete_exchange_rate($rate_id)
{
$sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id);
{
global $path_to_root;
+ if ($date != Today()) // no historical rates available
+ return 0;
+
$curr_a = get_company_pref('curr_default');
if ($provider == 'ECB')
{
} else {
$contents = url_get_contents("http://".$site.$filename);
}
- if (!$contents) {
- display_warning(sprintf(_("Cannot retrieve currency rate from %s page. Please set the rate manually."), $provider));
- }
if ($provider == 'ECB')
{
$contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
$Ajax->activate('code_id');
$Ajax->activate('pagehelp');
$Ajax->activate('editors');
+ $Ajax->activate('footer');
}
payment_person_types_list_row( $payment ? _("Pay To:"):_("From:"),
'PayType', $_POST['PayType'], true);
$qid = get_quick_entry(get_post('person_id'));
if (list_updated('person_id')) {
unset($_POST['totamount']); // enable default
+ $Ajax->activate('footer');
$Ajax->activate('totamount');
}
amount_row($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']),
table_section(3, "33%");
- if (!$order->order_id && !list_updated('bank_account'))
+ if (!$order->order_id && !get_post('bank_account'))
{
if ($_POST['PayType'] == PT_CUSTOMER)
$_POST['bank_account'] = get_default_customer_bank_account($_POST['person_id']);
bank_accounts_list_row( $payment ? _("From:") : _("Into:"), 'bank_account', null, true);
if ($payment)
bank_balance_row($_POST['bank_account']);
-
- $person_currency = payment_person_currency($_POST['PayType'], $_POST['person_id']);
+
$bank_currency = get_bank_account_currency($_POST['bank_account']);
- exchange_rate_display($bank_currency, $person_currency, $_POST['date_']);
+ exchange_rate_display($bank_currency, get_company_currency(), $_POST['date_']);
end_outer_table(1); // outer table
//---------------------------------------------------------------------------------
-function gl_options_controls()
+function gl_options_controls($order)
{
+ div_start('footer');
echo "<br><table align='center'>";
+ $type = get_post('PayType');
+ $bank_curr = get_bank_account_currency(get_post('bank_account'));
+ $person_curr = $type == PT_CUSTOMER ? get_customer_currency(get_post('person_id'))
+ : ($type == PT_SUPPLIER ? get_supplier_currency(get_post('person_id')) : $bank_curr);
+
+ if ($person_curr != $bank_curr)
+ {
+ $_POST['settled_amount'] =
+ price_format($order->gl_items_total() / get_exchange_rate_from_to($bank_curr, $person_curr, get_post('date_')));
+ amount_row($type == PT_CUSTOMER ? _("Settled AR Amount:") : _("Settled AP Amount:"),
+ 'settled_amount', null, null, $person_curr, user_price_dec());
+ }
textarea_row(_("Memo"), 'memo_', null, 50, 3);
echo "</table>";
+ div_end();
}
return null;
}
+/*
+ Helper for ui drawing functions.
+ Checks whether any of input function parameters has changed or page is called with GET method (first page display).
+ $name - context
+*/
+function check_ui_refresh($name=null)
+{
+ $bt = debug_backtrace();
+ if (!$name)
+ $name = $bt[1]['function'];
+ $old = @$_SESSION['ui_context'][$name];
+ $new = $_SESSION['ui_context'][$name] = $bt[1]['args'];
+ return ($new != $old) || ($_SERVER['REQUEST_METHOD'] == 'GET');
+}
+
//--------------------------------------------------------------------------------------
// Displays currency exchange rate for given date.
// When there is no exrate for today,
// gets it form ECB and stores in local database.
//
-function exchange_rate_display($from_currency, $to_currency, $date_, $edit_rate=false)
+function exchange_rate_display($from_currency, $to_currency, $date_, $force_edit=false)
{
global $Ajax;
+ $readonly = false;
+
if ($from_currency != $to_currency)
{
- $comp_currency = get_company_currency();
- if ($from_currency == $comp_currency)
- $currency = $to_currency;
- else
- $currency = $from_currency;
- $rate = 0;
- if ($date_ == Today()) {
- $rate = get_date_exchange_rate($currency, $date_);
- if (!$rate) {
+ $rate = get_post('_ex_rate');
+ if (!$rate || check_ui_refresh()) { // readonly or ui context changed
+ $comp_currency = get_company_currency();
+ if ($from_currency == $comp_currency)
+ $currency = $to_currency;
+ else
+ $currency = $from_currency;
+
+ $rate = get_date_exchange_rate($currency, $date_); // try local
+ if ($rate)
+ $readonly = true;
+ if (!$rate) { // retry from remote service
$row = get_currency($currency);
- if ($row['auto_update']) {
+ if ($row['auto_update'])
$rate = retrieve_exrate($currency, $date_);
- if ($rate)
- add_exchange_rate($currency, $date_, $rate, $rate);
+ if ($rate)
+ add_new_exchange_rate($currency, $date_, $rate);
+ else
+ {
+ display_warning(sprintf(_("Cannot retrieve exchange rate for currency %s. Please adjust approximate rate if needed."), $currency));
+ $readonly = false;
}
}
+ if (!$rate) { // get and edit latest available
+ $readonly = false;
+ $rate = get_exchange_rate_from_home_currency($currency, $date_);
+ }
+ if ($from_currency != $comp_currency)
+ $rate = 1 / ($rate / get_exchange_rate_from_home_currency($to_currency, $date_));
+
+ $Ajax->activate('_ex_rate_span');
}
- if (!$rate)
- $rate = get_exchange_rate_from_home_currency($currency, $date_);
- if ($from_currency != $comp_currency)
- $rate = 1 / ($rate / get_exchange_rate_from_home_currency($to_currency, $date_));
$rate = number_format2($rate, user_exrate_dec());
- if ($edit_rate)
- text_row(_("Exchange Rate:"), '_ex_rate', $rate, 8, 8, null, "", " $from_currency = 1 $to_currency");
- else
- label_row(_("Exchange Rate:"),"<span style='vertical-align:top;' id='_ex_rate'>$rate</span> $from_currency = 1 $to_currency" );
- $Ajax->addUpdate('_ex_rate','_ex_rate', $rate);
+
+ if ($force_edit || !$readonly)
+ $ctrl = "<input type=\"text\" name=\"_ex_rate\" size=\"8\" maxlength=\"8\" value=\"$rate\">";
+ else
+ $ctrl = "<span id=\"_ex_rate\">$rate</span>";
+
+ label_row(_("Exchange Rate:"), $span = "<span style='vertical-align:top;' id='_ex_rate_span'>$ctrl $from_currency = 1 $to_currency</span>" );
+
+ $Ajax->addUpdate('_ex_rate_span', '_ex_rate_span', $span);
}
}