Clone record support and fixed defult focus on some controls.
[fa-stable.git] / includes / banking.inc
index 54345259c889ffb9c573a96e2dbfd3ecb69431f1..cf532ea05ec95548da9b870edbe694c8fc4e1ec4 100644 (file)
@@ -1,5 +1,14 @@
 <?php
-
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------
@@ -116,7 +125,7 @@ function get_exchange_rate_to_home_currency($currency_code, $date_)
 function to_home_currency($amount, $currency_code, $date_)
 {
        $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
-       return round($amount / $ex_rate,  user_price_dec());
+       return round2($amount / $ex_rate,  user_price_dec());
 }
 
 //----------------------------------------------------------------------------------
@@ -158,23 +167,26 @@ 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'];
+               $date = sql2date($trans['tran_date']);
        }
        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'];
                $curr = $trans['SupplierCurrCode'];
+               $date = sql2date($trans['tran_date']);
        }
        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 = round2($amount * $trans['rate'], user_price_dec()); 
+       $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec());
        if ($inv_amt != $pay_amt)
        {
                $diff = $inv_amt - $pay_amt;
@@ -182,9 +194,19 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am
                        $diff = -$diff;
                if ($neg)
                        $diff = -$diff;
-               $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);
+               $exc_var_act = get_company_pref('exchange_diff_act');
+               if (date1_greater_date2($date, $pyt_date))
+               {
+                       $memo = systypes::name($pyt_type)." ".$pyt_no;
+                       add_gl_trans($type, $trans_no, $date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
+                       add_gl_trans($type, $trans_no, $date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
+               }
+               else
+               {
+                       $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);
+               }
        }
 }