From 654b2cb832f5ad2313cfbbd98832681fae05943a Mon Sep 17 00:00:00 2001 From: Joe Hunt Date: Sun, 26 Oct 2008 23:18:12 +0000 Subject: [PATCH] [0000078] Opened Exchange rate for editing (without storing) in sales/purchasing module payment --- gl/includes/db/gl_db_trans.inc | 9 +++++-- includes/banking.inc | 8 +++--- includes/ui/ui_view.inc | 17 ++++++------ purchasing/includes/db/supp_payment_db.inc | 23 +++++++++++----- purchasing/includes/db/supp_trans_db.inc | 6 +++-- purchasing/includes/purchasing_db.inc | 4 +-- purchasing/supplier_payment.php | 21 ++++++++++++--- reporting/rep101.php | 2 +- reporting/rep106.php | 2 +- reporting/rep201.php | 3 ++- reporting/rep203.php | 31 ++++++++++++++-------- reporting/rep209.php | 2 +- sales/customer_payments.php | 25 +++++++++++++++-- sales/includes/db/cust_trans_db.inc | 5 ++-- sales/includes/db/payment_db.inc | 12 ++++----- sales/includes/sales_db.inc | 4 +-- 16 files changed, 120 insertions(+), 54 deletions(-) diff --git a/gl/includes/db/gl_db_trans.inc b/gl/includes/db/gl_db_trans.inc index 79b03d80..96970678 100644 --- a/gl/includes/db/gl_db_trans.inc +++ b/gl/includes/db/gl_db_trans.inc @@ -8,13 +8,18 @@ // if $currency is not set, then defaults to no conversion function add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_, - $amount, $currency=null, $person_type_id=null, $person_id=null, $err_msg="") + $amount, $currency=null, $person_type_id=null, $person_id=null, $err_msg="", $rate=0) { global $use_audit_trail; $date = date2sql($date_); if ($currency != null) - $amount_in_home_currency = to_home_currency($amount, $currency, $date_); + { + if ($rate == 0) + $amount_in_home_currency = to_home_currency($amount, $currency, $date_); + else + $amount_in_home_currency = round($amount * $rate, user_price_dec()); + } else $amount_in_home_currency = $amount; if ($dimension == null || $dimension < 0) diff --git a/includes/banking.inc b/includes/banking.inc index 2c0fa945..d26bbbbd 100644 --- a/includes/banking.inc +++ b/includes/banking.inc @@ -147,6 +147,7 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am if ($person_type == payment_person_types::customer()) { $trans = get_customer_trans($trans_no, $type); + $pyt_trans = get_customer_trans($pyt_no, $pyt_type); $ar_ap_act = $trans['receivables_account']; $person_id = $trans['debtor_no']; $curr = $trans['curr_code']; @@ -154,6 +155,7 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am else { $trans = get_supp_trans($trans_no, $type); + $pyt_trans = get_supp_trans($pyt_no, $pyt_type); $supp_accs = get_supplier_accounts($trans['supplier_id']); $ar_ap_act = $supp_accs['payable_account']; $person_id = $trans['supplier_id']; @@ -161,9 +163,8 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am } if (is_company_currency($curr)) return; - $exc_var_act = get_company_pref('exchange_diff_act'); - $inv_amt = to_home_currency($amount, $curr, sql2date($trans['tran_date'])); - $pay_amt = to_home_currency($amount, $curr, $pyt_date); + $inv_amt = round($amount * $trans['rate'], user_price_dec()); + $pay_amt = round($amount * $pyt_trans['rate'], user_price_dec()); if ($inv_amt != $pay_amt) { $diff = $inv_amt - $pay_amt; @@ -171,6 +172,7 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am $diff = -$diff; if ($neg) $diff = -$diff; + $exc_var_act = get_company_pref('exchange_diff_act'); $memo = systypes::name($type)." ".$trans_no; add_gl_trans($pyt_type, $pyt_no, $pyt_date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id); add_gl_trans($pyt_type, $pyt_no, $pyt_date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id); diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index 5925579b..51ea641d 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -275,7 +275,7 @@ function get_trans_view_str($type, $trans_no, $label="") // 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_) +function exchange_rate_display($from_currency, $to_currency, $date_, $edit_rate=false) { global $Ajax; @@ -296,15 +296,16 @@ function exchange_rate_display($from_currency, $to_currency, $date_) } } if (!$rate) - $rate = get_exchange_rate_from_to($to_currency, $from_currency, $date_); - - if ($from_currency == $comp_currency) - $rate = 1 / $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()); - label_row(_("Exchange Rate:"),"1 " . $from_currency - . " = " . $rate . " " . $to_currency ); - $Ajax->addUpdate('_ex_rate','_ex_rate', $rate); + 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); } } diff --git a/purchasing/includes/db/supp_payment_db.inc b/purchasing/includes/db/supp_payment_db.inc index 38b1d440..5e5b400d 100644 --- a/purchasing/includes/db/supp_payment_db.inc +++ b/purchasing/includes/db/supp_payment_db.inc @@ -1,41 +1,50 @@ TextCol(3, 4, sql2date($trans['due_date'])); $item[0] = $item[1] = 0.0; if ($convert) - $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date); + $rate = $trans['rate']; else $rate = 1.0; if ($trans['type'] == 11 || $trans['type'] == 12 || $trans['type'] == 2) diff --git a/reporting/rep106.php b/reporting/rep106.php index 00e40461..ad26d7dd 100644 --- a/reporting/rep106.php +++ b/reporting/rep106.php @@ -127,7 +127,7 @@ function print_salesman_list() $subprov = 0; } $date = sql2date($myrow['tran_date']); - $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date); + $rate = $myrow['rate']; $amt = $myrow['InvoiceTotal'] * $rate; if ($subprov > $myrow['break_pt'] && $myrow['provision2'] != 0) $prov = $myrow['provision2'] * $amt / 100; diff --git a/reporting/rep201.php b/reporting/rep201.php index 92e85c04..89169ce0 100644 --- a/reporting/rep201.php +++ b/reporting/rep201.php @@ -118,7 +118,8 @@ function print_supplier_balances() $rep->TextCol(3, 4, sql2date($trans['due_date'])); $item[0] = $item[1] = 0.0; if ($convert) - $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date); + //$rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date); + $rate = $trans['rate']; else $rate = 1.0; if ($trans['TotalAmount'] > 0.0) diff --git a/reporting/rep203.php b/reporting/rep203.php index 625e3ce1..d69bf519 100644 --- a/reporting/rep203.php +++ b/reporting/rep203.php @@ -25,17 +25,19 @@ function getTransactions($supplier, $date) $sql = "SELECT ".TB_PREF."sys_types.type_name, ".TB_PREF."supp_trans.supp_reference, + ".TB_PREF."supp_trans.tran_date, ".TB_PREF."supp_trans.due_date, ".TB_PREF."supp_trans.trans_no, ".TB_PREF."supp_trans.type, - (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst - ".TB_PREF."supp_trans.alloc) AS Balance, - (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst ) AS TranTotal + ".TB_PREF."supp_trans.rate, + (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc) AS Balance, + (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) ) AS TranTotal FROM ".TB_PREF."supp_trans, ".TB_PREF."sys_types WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type AND ".TB_PREF."supp_trans.supplier_id = '" . $supplier . "' - AND ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst - ".TB_PREF."supp_trans.alloc != 0 - AND ".TB_PREF."supp_trans.due_date <='" . $date . "' + AND ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc != 0 + AND ".TB_PREF."supp_trans.tran_date <='" . $date . "' ORDER BY ".TB_PREF."supp_trans.type, ".TB_PREF."supp_trans.trans_no"; @@ -106,12 +108,7 @@ function print_payment_report() $rep->fontSize += 2; $rep->TextCol(0, 6, $myrow['name'] . " - " . $myrow['terms']); if ($convert) - { - $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to); $rep->TextCol(6, 7, $myrow['curr_code']); - } - else - $rate = 1.0; $rep->fontSize -= 2; $rep->NewLine(1, 2); $res = getTransactions($myrow['supplier_id'], $to); @@ -121,11 +118,23 @@ function print_payment_report() $total[0] = $total[1] = 0.0; while ($trans=db_fetch($res)) { + if ($convert) + $rate = $trans['rate']; + else + $rate = 1.0; $rep->NewLine(1, 2); $rep->TextCol(0, 1, $trans['type_name']); $rep->TextCol(1, 2, $trans['supp_reference']); - $rep->TextCol(2, 3, sql2date($trans['due_date'])); - $item[0] = Abs($trans['TranTotal']) * $rate; + if ($trans['type'] == 20) + $rep->TextCol(2, 3, sql2date($trans['due_date'])); + else + $rep->TextCol(2, 3, sql2date($trans['tran_date'])); + if ($trans['type'] != 20) + { + $trans['TranTotal'] = -$trans['TranTotal']; + $trans['Balance'] = -$trans['Balance']; + } + $item[0] = $trans['TranTotal'] * $rate; $rep->TextCol(6, 7, number_format2($item[0], $dec)); $item[1] = $trans['Balance'] * $rate; $rep->TextCol(7, 8, number_format2($item[1], $dec)); diff --git a/reporting/rep209.php b/reporting/rep209.php index 6ae25830..c6e0240f 100644 --- a/reporting/rep209.php +++ b/reporting/rep209.php @@ -111,7 +111,7 @@ function print_po() $DisplayNet = number_format2($Net,$dec); //$rep->TextCol(0, 1, $myrow2['item_code'], -2); $rep->TextCol(0, 2, $myrow2['description'], -2); - $rep->TextCol(2, 3, $myrow2['delivery_date'], -2); + $rep->TextCol(2, 3, sql2date($myrow2['delivery_date']), -2); $rep->TextCol(3, 4, $DisplayQty, -2); $rep->TextCol(4, 5, $myrow2['units'], -2); $rep->TextCol(5, 6, $DisplayPrice, -2); diff --git a/sales/customer_payments.php b/sales/customer_payments.php index 1ab43ce5..debd1f3a 100644 --- a/sales/customer_payments.php +++ b/sales/customer_payments.php @@ -86,6 +86,18 @@ function can_process() return false; } + if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001)) + { + display_error(_("The exchange rate must be numeric and greater than zero.")); + set_focus('_ex_rate'); + return false; + } + + if ($_POST['discount'] == "") + { + $_POST['discount'] = 0; + } + if (!check_num('discount')) { display_error(_("The entered discount is not a valid number.")); set_focus('discount'); @@ -120,9 +132,18 @@ if (isset($_POST['_DateBanked_changed'])) { //---------------------------------------------------------------------------------------------- if (isset($_POST['AddPaymentItem'])) { + + $cust_currency = get_customer_currency($_POST['customer_id']); + $bank_currency = get_bank_account_currency($_POST['bank_account']); + $comp_currency = get_company_currency(); + if ($comp_currency != $bank_currency && $bank_currency != $cust_currency) + $rate = 0; + else + $rate = input_num('_ex_rate'); + $payment_no = write_customer_payment(0, $_POST['customer_id'], $_POST['BranchID'], $_POST['bank_account'], $_POST['DateBanked'], $_POST['ReceiptType'], $_POST['ref'], - input_num('amount'), input_num('discount'), $_POST['memo_']); + input_num('amount'), input_num('discount'), $_POST['memo_'], $rate); meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_no"); } @@ -198,7 +219,7 @@ function display_item_form() $bank_currency = get_bank_account_currency($_POST['bank_account']); if ($cust_currency != $bank_currency) { - exchange_rate_display($cust_currency, $bank_currency, $_POST['DateBanked']); + exchange_rate_display($bank_currency, $cust_currency, $_POST['DateBanked'], true); } bank_trans_types_list_row(_("Type:"), 'ReceiptType', null); diff --git a/sales/includes/db/cust_trans_db.inc b/sales/includes/db/cust_trans_db.inc index b91a2795..afa814d4 100644 --- a/sales/includes/db/cust_trans_db.inc +++ b/sales/includes/db/cust_trans_db.inc @@ -79,11 +79,12 @@ function get_customer_trans_version($type, $trans_no) { function write_customer_trans($trans_type, $trans_no, $debtor_no, $BranchNo, $date_, $reference, $Total, $discount=0, $Tax=0, $Freight=0, $FreightTax=0, $sales_type=0, $order_no=0, $trans_link=0, $ship_via=0, $due_date="", - $AllocAmt=0) + $AllocAmt=0, $rate=0) { $curr = get_customer_currency($debtor_no); - $rate = get_exchange_rate_from_home_currency($curr, $date_); + if ($rate == 0) + $rate = get_exchange_rate_from_home_currency($curr, $date_); $SQLDate = date2sql($date_); if ($due_date == "") diff --git a/sales/includes/db/payment_db.inc b/sales/includes/db/payment_db.inc index 74054b06..14d358b8 100644 --- a/sales/includes/db/payment_db.inc +++ b/sales/includes/db/payment_db.inc @@ -3,14 +3,14 @@ Write/update customer payment. */ function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_account, - $date_, $receipt_type, $ref, $amount, $discount, $memo_) + $date_, $receipt_type, $ref, $amount, $discount, $memo_, $rate=0) { begin_transaction(); $company_record = get_company_prefs(); - $payment_no = write_customer_trans(12, $trans_no, - $customer_id, $branch_id, $date_, $ref, $amount, $discount); + $payment_no = write_customer_trans(12, $trans_no, $customer_id, $branch_id, + $date_, $ref, $amount, $discount, 0, 0, 0, 0, 0, 0, 0, "", 0, $rate); if ($trans_no != 0) { delete_comments(12, $trans_no); @@ -22,7 +22,7 @@ function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_accou /* Bank account entry first */ add_gl_trans_customer(12, $payment_no, $date_, $bank_account, 0, 0, $amount, $customer_id, - "Cannot insert a GL transaction for the bank account debit"); + "Cannot insert a GL transaction for the bank account debit", $rate); if ($branch_id != reserved_words::get_any_numeric()) { @@ -40,14 +40,14 @@ function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_accou /* Now Credit Debtors account with receipts + discounts */ add_gl_trans_customer(12, $payment_no, $date_, $debtors_account, 0, 0, -($discount + $amount), $customer_id, - "Cannot insert a GL transaction for the debtors account credit"); + "Cannot insert a GL transaction for the debtors account credit", $rate); } if ($discount != 0) { /* Now Debit discount account with discounts allowed*/ add_gl_trans_customer(12, $payment_no, $date_, $discount_account, 0, 0, $discount, $customer_id, - "Cannot insert a GL transaction for the payment discount debit"); + "Cannot insert a GL transaction for the payment discount debit", $rate); } /*now enter the bank_trans entry */ diff --git a/sales/includes/sales_db.inc b/sales/includes/sales_db.inc index 5c4fffdb..1c6f67f6 100644 --- a/sales/includes/sales_db.inc +++ b/sales/includes/sales_db.inc @@ -35,14 +35,14 @@ function add_stock_move_customer($type, $stock_id, $trans_id, $location, $date_, // $amount is in CUSTOMER'S currency function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $dimension2, - $amount, $customer_id, $err_msg="") + $amount, $customer_id, $err_msg="", $rate) { if ($err_msg == "") $err_msg = "The customer GL transaction could not be inserted"; return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, "", $amount, get_customer_currency($customer_id), - payment_person_types::customer(), $customer_id, $err_msg); + payment_person_types::customer(), $customer_id, $err_msg, $rate); } //---------------------------------------------------------------------------------------- -- 2.30.2