From 0add67eea2165764ad65bbb2dccbb687a7d20b77 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Thu, 23 May 2013 13:07:53 +0200 Subject: [PATCH] Fixed exchange rate ui to be editable for new rates only, bank payment/deposit use amount in supplier/customer currency instead of rate. --- gl/gl_bank.php | 21 +++++---- gl/includes/db/gl_db_banking.inc | 14 ++++-- gl/includes/db/gl_db_rates.inc | 17 +++++-- gl/includes/ui/gl_bank_ui.inc | 25 ++++++++--- includes/ui/ui_view.inc | 76 +++++++++++++++++++++++--------- 5 files changed, 111 insertions(+), 42 deletions(-) diff --git a/gl/gl_bank.php b/gl/gl_bank.php index a0a2d11c..23d0fa74 100644 --- a/gl/gl_bank.php +++ b/gl/gl_bank.php @@ -60,6 +60,7 @@ function line_start_focus() { global $Ajax; $Ajax->activate('items_table'); + $Ajax->activate('footer'); set_focus('_code_id_edit'); } @@ -130,9 +131,6 @@ if (isset($_GET['UpdatedDep'])) display_footer_exit(); } -if (isset($_POST['_date__changed'])) { - $Ajax->activate('_ex_rate'); -} //-------------------------------------------------------------------------------------------------- function create_cart($type, $trans_no) @@ -288,7 +286,7 @@ if (isset($_POST['Process'])) if (isset($_POST['Process'])) { begin_transaction(); - + $_SESSION['pay_items'] = &$_SESSION['pay_items']; $new = $_SESSION['pay_items']->order_id == 0; @@ -296,17 +294,18 @@ if (isset($_POST['Process'])) $_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"); @@ -326,6 +325,12 @@ function check_item_data() 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; } @@ -395,7 +400,7 @@ start_row(); echo ""; 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 ""; end_row(); end_table(1); diff --git a/gl/includes/db/gl_db_banking.inc b/gl/includes/db/gl_db_banking.inc index 4a88583a..c49ad674 100644 --- a/gl/includes/db/gl_db_banking.inc +++ b/gl/includes/db/gl_db_banking.inc @@ -288,7 +288,7 @@ function add_bank_transfer($from_account, $to_account, $date_, // 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; @@ -303,7 +303,7 @@ function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $ $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; @@ -324,7 +324,10 @@ function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $ // 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; @@ -336,7 +339,10 @@ function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $ { // 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; diff --git a/gl/includes/db/gl_db_rates.inc b/gl/includes/db/gl_db_rates.inc index f3237d55..1d358ec8 100644 --- a/gl/includes/db/gl_db_rates.inc +++ b/gl/includes/db/gl_db_rates.inc @@ -65,6 +65,17 @@ function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate) //--------------------------------------------------------------------------------------------- +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); @@ -90,6 +101,9 @@ function get_extern_rate($curr_b, $provider = 'ECB', $date) { global $path_to_root; + if ($date != Today()) // no historical rates available + return 0; + $curr_a = get_company_pref('curr_default'); if ($provider == 'ECB') { @@ -137,9 +151,6 @@ function get_extern_rate($curr_b, $provider = 'ECB', $date) } 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 (" 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); @@ -93,6 +94,7 @@ function display_bank_header(&$order) $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']), @@ -105,7 +107,7 @@ function display_bank_header(&$order) 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']); @@ -118,11 +120,10 @@ function display_bank_header(&$order) 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 @@ -291,13 +292,27 @@ function gl_edit_item_controls(&$order, $dim, $Index=null) //--------------------------------------------------------------------------------- -function gl_options_controls() +function gl_options_controls($order) { + div_start('footer'); echo "
"; + $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 "
"; + div_end(); } diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index 00684612..492bd486 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -250,45 +250,77 @@ function get_trans_view_str($type, $trans_no, $label="", $icon=false, 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:"),"$rate $from_currency = 1 $to_currency" ); - $Ajax->addUpdate('_ex_rate','_ex_rate', $rate); + + if ($force_edit || !$readonly) + $ctrl = ""; + else + $ctrl = "$rate"; + + label_row(_("Exchange Rate:"), $span = "$ctrl $from_currency = 1 $to_currency" ); + + $Ajax->addUpdate('_ex_rate_span', '_ex_rate_span', $span); } } -- 2.30.2