Fixed layout for amount_ex() fields with label.
[fa-stable.git] / includes / banking.inc
index 93fcb294333f7ed728924f6cc4727eeaf559feb9..39b904c8b43b3c63515e11b886c478532f57e30c 100644 (file)
@@ -3,13 +3,22 @@
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------
-
+//     Check if given account is used by any bank_account. 
+//     Returns id of first bank_account using account_code, null otherwise.
+//
+//     Keep in mind that direct posting to bank account is depreciated
+//     because we have no way to select right bank account if 
+//     there is more than one using given gl account.
+//
 function is_bank_account($account_code)
 {
-       $sql= "SELECT account_code FROM ".TB_PREF."bank_accounts WHERE account_code='$account_code'";
-       $result = db_query($sql, "retreive bank account currency");
-
-       return (db_num_rows($result) > 0);
+       $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code='$account_code'";
+       $result = db_query($sql, "checking account is bank account");
+       if (db_num_rows($result) > 0) {
+               $acct = db_fetch($result);
+               return $acct['id'];
+       } else
+               return false;
 }
 
 //----------------------------------------------------------------------------------
@@ -35,9 +44,9 @@ function get_company_currency()
 
 //----------------------------------------------------------------------------------
 
-function get_bank_account_currency($bankAccount)
+function get_bank_account_currency($id)
 {
-       $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE account_code='$bankAccount'";
+       $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE id='$id'";
        $result = db_query($sql, "retreive bank account currency");
 
        $myrow = db_fetch_row($result);
@@ -149,6 +158,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'];
@@ -156,6 +166,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'];
@@ -163,9 +174,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;
@@ -173,6 +183,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);