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;
35 $amount_bank = round2($amount_bank, user_price_dec());
37 $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
38 trans_date, amount, person_type_id, person_id) ";
40 $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
41 ".db_escape($amount_bank).", ".db_escape($person_type_id)
42 .", ". db_escape($person_id).")";
45 $err_msg = "The bank transaction could not be inserted";
47 db_query($sql, $err_msg);
50 //----------------------------------------------------------------------------------------
52 function exists_bank_trans($type, $type_no)
54 $sql = "SELECT trans_no
55 FROM ".TB_PREF."bank_trans
56 WHERE type=".db_escape($type)
57 ." AND trans_no=".db_escape($type_no);
58 $result = db_query($sql, "Cannot retreive a bank transaction");
60 return (db_num_rows($result) > 0);
63 //----------------------------------------------------------------------------------------
65 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
67 $sql = "SELECT bt.*, act.*,
68 IFNULL(abs(dt.ov_amount), IFNULL(ABS(st.ov_amount), bt.amount)) settled_amount,
69 IFNULL(abs(dt.ov_amount/bt.amount), IFNULL(ABS(st.ov_amount/bt.amount), 1)) settle_rate,
70 IFNULL(debtor.curr_code, IFNULL(supplier.curr_code, act.bank_curr_code)) settle_curr
72 FROM ".TB_PREF."bank_trans bt
73 LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
74 LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
75 LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
76 LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id,
77 ".TB_PREF."bank_accounts act
78 WHERE act.id=bt.bank_act ";
80 $sql .= " AND bt.type=".db_escape($type);
81 if ($trans_no != null)
82 $sql .= " AND bt.trans_no = ".db_escape($trans_no);
83 if ($person_type_id != null)
84 $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
85 if ($person_id != null)
86 $sql .= " AND bt.person_id = ".db_escape($person_id);
87 $sql .= " ORDER BY trans_date, bt.id";
89 return db_query($sql, "query for bank transaction");
92 //----------------------------------------------------------------------------------------
94 function get_bank_trans_for_bank_account($bank_account, $from, $to)
96 $from = date2sql($from);
99 FROM ".TB_PREF."bank_trans t
100 LEFT JOIN ".TB_PREF."voided v ON t.type=v.type AND t.trans_no=v.id
101 WHERE t.bank_act = ".db_escape($bank_account) . "
103 AND trans_date >= '$from'
104 AND trans_date <= '$to'
106 ORDER BY trans_date, t.id";
108 return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved");
111 //----------------------------------------------------------------------------------------
113 function get_balance_before_for_bank_account($bank_account, $from)
115 $from = date2sql($from);
116 $sql = "SELECT SUM(amount)
117 FROM ".TB_PREF."bank_trans
118 WHERE bank_act=".db_escape($bank_account) . "
119 AND trans_date < '$from'";
120 $before_qty = db_query($sql, "The starting balance on hand could not be calculated");
121 $bfw_row = db_fetch_row($before_qty);
124 //----------------------------------------------------------------------------------------
126 function get_gl_trans_value($account, $type, $trans_no)
128 $sql = "SELECT SUM(amount)
129 FROM ".TB_PREF."gl_trans
130 WHERE account=".db_escape($account)
131 ." AND type=".db_escape($type)
132 ." AND type_no=".db_escape($trans_no);
134 $result = db_query($sql, "query for gl trans value");
136 $row = db_fetch_row($result);
140 //----------------------------------------------------------------------------------------
142 function void_bank_trans($type, $type_no, $nested=false)
148 $sql = "UPDATE ".TB_PREF."bank_trans
150 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
152 db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
154 void_gl_trans($type, $type_no, true);
156 // in case it's a customer trans - probably better to check first
157 void_cust_allocations($type, $type_no);
158 void_customer_trans($type, $type_no);
160 // in case it's a supplier trans - probably better to check first
161 void_supp_allocations($type, $type_no);
162 void_supp_trans($type, $type_no);
164 void_trans_tax_details($type, $type_no);
167 commit_transaction();
171 * Check account history to find transaction which would exceed authorized overdraft for given account.
172 * Returns null or transaction in conflict. Running balance is checked on daily basis only, to enable ID change after edition.
173 * $delta_amount - tested change in bank balance at $date.
175 function check_bank_account_history($delta_amount, $bank_account, $date=null, $user=null, $balance_offset = 0)
177 if ($delta_amount >= 0 && isset($date))
178 return null; // amount increase is always safe
180 $balance = $date ? get_bank_account_limit($bank_account, $date, $user) : 0;
182 if (!isset($balance) && isset($date))
183 return null; // unlimited account
185 $balance += $balance_offset;
186 if (floatcmp($balance, -$delta_amount) < 0)
187 return array('amount' => $balance + $delta_amount, 'trans_date'=> date2sql($date));
189 $balance += $delta_amount;
191 $sql = "SELECT sum(amount) as amount, trans_date
192 FROM ".TB_PREF."bank_trans
193 WHERE bank_act=".db_escape($bank_account);
196 $date = date2sql($date);
197 $sql .= " AND trans_date > '$date'";
199 $sql .= " GROUP BY trans_date ORDER BY trans_date ASC";
201 $history = db_query($sql, "cannot retrieve cash account history");
203 while ($trans = db_fetch($history)) {
204 $balance += $trans['amount'];
207 $trans['amount'] = $balance;
216 * Check bank transfer, deposit or customer deposit before voiding.
218 function check_void_bank_trans($type, $type_no)
220 $moves = get_bank_trans($type, $type_no);
221 while ($trans = db_fetch($moves)) {
222 if ($trans['amount'] > 0) { // skip transfer input part
223 return check_bank_account_history(-$trans['amount'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;