2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //----------------------------------------------------------------------------------
14 // add a bank transaction
15 // $amount is in $currency
16 // $date_ is display date (non-sql)
18 function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_,
19 $amount, $person_type_id, $person_id, $currency="", $err_msg="", $rate=0)
21 $sqlDate = date2sql($date_);
23 // convert $amount to the bank's currency
26 $bank_account_currency = get_bank_account_currency($bank_act);
28 $to_bank_currency = get_exchange_rate_from_to($currency, $bank_account_currency, $date_);
30 $to_bank_currency = 1 / $rate;
31 $amount_bank = ($amount / $to_bank_currency);
34 $amount_bank = $amount;
37 // Also store the rate to the home
38 //$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
40 $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
41 trans_date, amount, person_type_id, person_id) ";
43 $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
44 ".db_escape($amount_bank).", ".db_escape($person_type_id)
45 .", ". db_escape($person_id).")";
48 $err_msg = "The bank transaction could not be inserted";
50 db_query($sql, $err_msg);
53 //----------------------------------------------------------------------------------------
55 function exists_bank_trans($type, $type_no)
57 $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=".db_escape($type)
58 ." AND trans_no=".db_escape($type_no);
59 $result = db_query($sql, "Cannot retreive a bank transaction");
61 return (db_num_rows($result) > 0);
64 //----------------------------------------------------------------------------------------
66 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
68 $sql = "SELECT *, bank_account_name, account_code, bank_curr_code
69 FROM ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts
70 WHERE ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act ";
72 $sql .= " AND type=".db_escape($type);
73 if ($trans_no != null)
74 $sql .= " AND ".TB_PREF."bank_trans.trans_no = ".db_escape($trans_no);
75 if ($person_type_id != null)
76 $sql .= " AND ".TB_PREF."bank_trans.person_type_id = ".db_escape($person_type_id);
77 if ($person_id != null)
78 $sql .= " AND ".TB_PREF."bank_trans.person_id = ".db_escape($person_id);
79 $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id";
81 return db_query($sql, "query for bank transaction");
84 //----------------------------------------------------------------------------------------
86 function get_gl_trans_value($account, $type, $trans_no)
88 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account="
89 .db_escape($account)." AND type=".db_escape($type)
90 ." AND type_no=".db_escape($trans_no);
92 $result = db_query($sql, "query for gl trans value");
94 $row = db_fetch_row($result);
98 //----------------------------------------------------------------------------------------
100 function void_bank_trans($type, $type_no, $nested=false)
105 $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
106 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
108 $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
110 void_gl_trans($type, $type_no, true);
112 // in case it's a customer trans - probably better to check first
113 void_cust_allocations($type, $type_no);
114 void_customer_trans($type, $type_no);
116 // in case it's a supplier trans - probably better to check first
117 void_supp_allocations($type, $type_no);
118 void_supp_trans($type, $type_no);
120 void_trans_tax_details($type, $type_no);
123 commit_transaction();
126 //----------------------------------------------------------------------------------