Automatic exchange rate update after document date change
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sun, 21 Sep 2008 14:52:11 +0000 (14:52 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sun, 21 Sep 2008 14:52:11 +0000 (14:52 +0000)
gl/bank_transfer.php
gl/gl_bank.php
gl/includes/db/gl_db_rates.inc
gl/includes/ui/gl_bank_ui.inc
includes/banking.inc
purchasing/supplier_payment.php
sales/customer_payments.php
sales/includes/cart_class.inc

index d40436d924975fa525a755b1d94866508f8c662e..78089f5a4db6f59b56a0d5a128042c4bf1ff48c8 100644 (file)
@@ -37,6 +37,10 @@ if (isset($_GET['AddedID']))
        safeExit();
 }
 
+if (isset($_POST['_DatePaid_changed'])) {
+       $Ajax->activate('_ex_rate');
+}
+
 //----------------------------------------------------------------------------------------
 
 function gl_payment_controls()
@@ -54,7 +58,7 @@ function gl_payment_controls()
 
     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
 
-    date_row(_("Transfer Date:"), 'DatePaid');
+    date_row(_("Transfer Date:"), 'DatePaid', '', null, 0, 0, 0, null, true);
 
        $from_currency = get_bank_account_currency($_POST['FromBankAccount']);
        $to_currency = get_bank_account_currency($_POST['ToBankAccount']);
index 3bb0e9e4d66f56452b0763765fd9fa0079d09fc4..ce8c40fd233e17d3773b946e12e0f6afd744afed 100644 (file)
@@ -98,6 +98,9 @@ if (isset($_GET['AddedDep']))
 
        display_footer_exit();
 }
+if (isset($_POST['_date__changed'])) {
+       $Ajax->activate('_ex_rate');
+}
 //--------------------------------------------------------------------------------------------------
 
 function handle_new_order($type)
index 0a2c2276df6370560cca64c82453fc94e579f57e..cba2e5afe2bcaef7ca7244521d86fd9b42c8b220 100644 (file)
@@ -18,14 +18,18 @@ function get_exchange_rate($rate_id)
        return db_fetch($result);
 }
 
+// Retrieves buy exchange rate for given currency/date, zero if no result
 function get_date_exchange_rate($curr_code, $date_)
 {
        $date = date2sql($date_);
-       $sql = "SELECT date_ FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code' 
+       $sql = "SELECT rate_buy FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code' 
                AND date_='$date'";
        $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");        
 
-       return (db_num_rows($result) == 1);
+       if(db_num_rows($result) == 0) 
+               return 0;
+       $row = db_fetch($result);
+               return $row[0];
 }
 
 //---------------------------------------------------------------------------------------------
@@ -100,11 +104,11 @@ function get_ecb_rate($curr_b)
        $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
        $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
        preg_match ( $from_mask, $contents, $out );
-       $val_a = $out[1];
+       $val_a = isset($out[1]) ? $out[1] : 0;
        $val_a = str_replace ( ',', '', $val_a );
        $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
        preg_match ( $to_mask, $contents, $out );
-       $val_b = $out[1];
+       $val_b = isset($out[1]) ? $out[1] : 0;
        $val_b = str_replace ( ',', '', $val_b );
        if ($val_b) 
        {
index 3ea9b9d95229553ea4658ac1967353c0e930c7a0..61c5250ab1d2e1d9d62cc55428cc10dcff2062eb 100644 (file)
@@ -13,7 +13,7 @@ function display_bank_header(&$order)
 
     bank_accounts_list_row( $payment ? _("From:") : _("To:"), 'bank_account', null, true);
 
-    date_row(_("Date:"), 'date_');
+    date_row(_("Date:"), 'date_', '', null, 0, 0, 0, null, true);
 
        echo "</table>"; // inner table
 
index e3232a768a26bc14e84504ce26ff509df490857e..2c0fa94573c09cf2a0bcb3656741f73808f64d63 100644 (file)
@@ -85,7 +85,7 @@ function get_exchange_rate_from_home_currency($currency_code, $date_)
        if (db_num_rows($result) == 0)
        {
                // no stored exchange rate, just return 1
-               display_error(_("Cannot get exchange rate for this currency. Please add exchange rate manually on Exchange Rates page.") );
+               display_error(_("Cannot retrieve currency exchange rate for this date. Please add exchange rate manually on Exchange Rates page.") );
                return 1.000;
        }
 
index c731c491493581e7ed2eef3d114e1c0472d749ae..28e16c6f3229aad6c1229f0f2cd93aba3ebf60ef 100644 (file)
@@ -41,6 +41,9 @@ if (isset($_POST['_supplier_id_editor'])) {
                array( 'supplier_id', 'bank_account', 'DatePaid', 
                        'PaymentType', 'ref', 'amount', 'discount', 'memo_') );
 }
+if (isset($_POST['_DatePaid_changed'])) {
+  $Ajax->activate('_ex_rate');
+}
 //----------------------------------------------------------------------------------------
 
 if (isset($_GET['AddedID'])) 
@@ -83,7 +86,7 @@ function display_controls()
        amount_row(_("Amount of Payment:"), 'amount');
        amount_row(_("Amount of Discount:"), 'discount');
 
-    date_row(_("Date Paid") . ":", 'DatePaid');
+    date_row(_("Date Paid") . ":", 'DatePaid', '', null, 0, 0, 0, null, true);
 
        echo "</table>";
        echo "</td><td valign=top class='tableseparator'>"; // outer table
index 42cb552d8a39bd10a1c61d2df344194b28933fdd..1ab43ce52a0cc320eb617400f00d3f789f441f95 100644 (file)
@@ -114,6 +114,9 @@ if (isset($_POST['_customer_id_button'])) {
 //     unset($_POST['branch_id']);
        $Ajax->activate('BranchID');
 }
+if (isset($_POST['_DateBanked_changed'])) {
+  $Ajax->activate('_ex_rate');
+}
 //----------------------------------------------------------------------------------------------
 
 if (isset($_POST['AddPaymentItem'])) {
@@ -171,7 +174,6 @@ function display_item_form()
        read_customer_data();
 
        set_global_customer($_POST['customer_id']);
-
        if (isset($_POST['HoldAccount']) && $_POST['HoldAccount'] != 0) {
                echo "</table></table>";
                display_note(_("This customer account is on hold."), 0, 0, "class='redfb'");
@@ -184,7 +186,7 @@ function display_item_form()
 
                label_row(_("Customer prompt payment discount :"), $display_discount_percent);
 
-               date_row(_("Date of Deposit:"), 'DateBanked');
+               date_row(_("Date of Deposit:"), 'DateBanked','',null, 0, 0, 0, null, true);
 
                echo "</table>";
                echo "</td><td valign=top class='tableseparator'>"; // outer table
index 46427af4cdc6492269df93ab7f7f61f22ace203e..83c9e28f9d25896e9fdf33188dd28fa225f13ab8 100644 (file)
@@ -138,7 +138,7 @@ class cart
                                        get_invoice_duedate($this->customer_id, $this->document_date);
                                else
                                  $this->due_date =
-                                       add_days($this->document_date, 10); // FIX this should be in company prefs
+                                       add_days($this->document_date, sys_prefs::default_delivery_required_by());
                        }
        }