Added missing field for bank charges in bank_trans, rewritten bank transaction views...
[fa-stable.git] / gl / includes / db / gl_db_bank_trans.inc
1 <?php
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 //----------------------------------------------------------------------------------
13
14 // add a bank transaction
15 // $amount is in $currency
16 // $date_ is display date (non-sql)
17
18 function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_,
19         $amount, $charge=0, $person_type_id=0, $person_id=0, $currency="", $rate=0)
20 {
21         $sqlDate = date2sql($date_);
22
23         // convert $amount to the bank's currency
24         if ($currency != "")
25         {
26         $bank_account_currency = get_bank_account_currency($bank_act);
27         if ($rate == 0)
28                 $to_bank_currency = get_exchange_rate_from_to($currency, $bank_account_currency, $date_);
29         else
30                 $to_bank_currency = 1 / $rate;
31         $amount_bank = ($amount / $to_bank_currency);
32         }
33         else
34                 $amount_bank = $amount;
35         $amount_bank = round2($amount_bank, user_price_dec());  
36
37         $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
38                 trans_date, amount, charge, person_type_id, person_id) ";
39
40         $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
41                 ".db_escape($amount_bank).", ".db_escape($charge).", ".db_escape($person_type_id)
42                 .", ". db_escape($person_id).")";
43
44         db_query($sql, "The bank transaction could not be inserted");
45 }
46
47 //----------------------------------------------------------------------------------------
48
49 function exists_bank_trans($type, $type_no)
50 {
51         $sql = "SELECT trans_no
52                 FROM ".TB_PREF."bank_trans
53                 WHERE type=".db_escape($type)
54                 ." AND trans_no=".db_escape($type_no)
55                 ." AND (amount!=0 OR charge!=0)";
56         $result = db_query($sql, "Cannot retreive a bank transaction");
57
58     return (db_num_rows($result) > 0);
59 }
60
61 //----------------------------------------------------------------------------------------
62
63 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
64 {
65         $sql = "SELECT bt.*, act.*,
66                 IFNULL(abs(dt.ov_amount), IFNULL(ABS(st.ov_amount), bt.amount)) settled_amount,
67                 IFNULL(abs(dt.ov_amount/bt.amount), IFNULL(ABS(st.ov_amount/bt.amount), 1)) settle_rate,
68                 IFNULL(debtor.curr_code, IFNULL(supplier.curr_code, act.bank_curr_code)) settle_curr
69
70                 FROM ".TB_PREF."bank_trans bt
71                                  LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
72                                  LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
73                                  LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
74                                  LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id,
75                          ".TB_PREF."bank_accounts act
76                 WHERE act.id=bt.bank_act ";
77         if (isset($type))
78                 $sql .= " AND bt.type=".db_escape($type);
79         if (isset($trans_no))
80                 $sql .= " AND bt.trans_no = ".db_escape($trans_no);
81         if (isset($person_type_id))
82                 $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
83         if (isset($person_id))
84                 $sql .= " AND bt.person_id = ".db_escape($person_id);
85         $sql .= " ORDER BY trans_date, bt.id";
86
87         return db_query($sql, "query for bank transaction");
88 }
89
90 //----------------------------------------------------------------------------------------
91
92 function get_bank_trans_for_bank_account($bank_account, $from, $to)
93 {
94         $from = date2sql($from);
95         $to = date2sql($to);
96         $sql = "SELECT t.* 
97                 FROM ".TB_PREF."bank_trans t 
98                         LEFT JOIN ".TB_PREF."voided v ON t.type=v.type AND t.trans_no=v.id
99                 WHERE t.bank_act = ".db_escape($bank_account) . "
100                         AND ISNULL(v.date_)
101                         AND trans_date >= '$from'
102                         AND trans_date <= '$to'
103                         AND (amount != 0 OR charge != 0)
104                 ORDER BY trans_date, t.id";
105
106         return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved");
107 }
108
109 //----------------------------------------------------------------------------------------
110
111 function get_balance_before_for_bank_account($bank_account, $from)
112 {
113         $from = date2sql($from);
114         $sql = "SELECT SUM(amount+charge)
115                 FROM ".TB_PREF."bank_trans
116                 WHERE bank_act=".db_escape($bank_account) . "
117                         AND trans_date < '$from'";
118         $before_qty = db_query($sql, "The starting balance on hand could not be calculated");
119         $bfw_row = db_fetch_row($before_qty);
120         return $bfw_row[0];
121 }
122 //----------------------------------------------------------------------------------------
123
124 function get_gl_trans_value($account, $type, $trans_no)
125 {
126         $sql = "SELECT SUM(amount)
127                 FROM ".TB_PREF."gl_trans
128                 WHERE account=".db_escape($account)
129                         ." AND type=".db_escape($type)
130                         ." AND type_no=".db_escape($trans_no);
131
132         $result = db_query($sql, "query for gl trans value");
133
134         $row = db_fetch_row($result);
135         return $row[0];
136 }
137
138 //----------------------------------------------------------------------------------------
139
140 function void_bank_trans($type, $type_no, $nested=false)
141 {
142
143         if (!$nested)
144                 begin_transaction(__FUNCTION__, func_get_args());
145
146         $sql = "UPDATE ".TB_PREF."bank_trans 
147                         SET amount=0, charge=0
148                         WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
149
150         db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
151
152         void_gl_trans($type, $type_no, true);
153
154         // in case it's a customer trans - probably better to check first
155         void_cust_allocations($type, $type_no);
156         void_customer_trans($type, $type_no);
157
158         // in case it's a supplier trans - probably better to check first
159         void_supp_allocations($type, $type_no);
160         void_supp_trans($type, $type_no);
161
162         void_trans_tax_details($type, $type_no);
163
164         if (!$nested)
165                 commit_transaction();
166 }
167
168 /**
169 *       Check account history to find transaction which would exceed authorized overdraft for given account.
170 *       Returns null or transaction in conflict. Running balance is checked on daily basis only, to enable ID change after edition.
171 *       $delta_amount - tested change in bank balance at $date.
172 **/
173 function check_bank_account_history($delta_amount, $bank_account, $date=null, $user=null, $balance_offset = 0)
174 {
175         if ($delta_amount >= 0 && isset($date))
176                  return null;   // amount increase is always safe
177
178         $balance = $date ? get_bank_account_limit($bank_account, $date, $user) : 0;
179
180         if (!isset($balance) && isset($date))
181                 return null;    // unlimited account
182
183         $balance += $balance_offset;
184         if (floatcmp($balance, -$delta_amount) < 0)
185                 return array('amount' => $balance + $delta_amount, 'trans_date'=> date2sql($date));
186
187         $balance += $delta_amount;
188
189         $sql = "SELECT SUM(amount+charge) as amount, trans_date, trans_no, type
190                         FROM ".TB_PREF."bank_trans
191                         WHERE bank_act=".db_escape($bank_account);
192         if ($date)
193         {
194                 $date = date2sql($date);
195                 $sql .= " AND trans_date > '$date'";
196         }
197         $sql .= " GROUP BY trans_date ORDER BY trans_date ASC";
198
199         $history = db_query($sql, "cannot retrieve cash account history");
200
201         while ($trans = db_fetch($history)) {
202                 $balance += $trans['amount'];
203                 if (round2($balance, user_price_dec()) < 0)
204                 {
205                         $trans['amount'] = $balance;
206                         return $trans;
207                 }
208         }
209
210         return null;
211 }
212
213 /**
214 *       Check bank transfer, deposit or customer deposit before voiding.
215 **/
216 function check_void_bank_trans($type, $type_no)
217 {
218         $moves = get_bank_trans($type, $type_no);
219         while ($trans = db_fetch($moves)) {
220                 if ($trans['amount'] > 0) { // skip transfer input part
221                         return check_bank_account_history(-$trans['amount']-$trans['charge'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;
222                 }
223         }
224         return true;
225 }
226
227 function update_reconcile_date($type, $trans_no, $date=NULL)
228 {
229         $sql = "UPDATE ".TB_PREF."bank_trans SET reconciled=".($date ? "'".date2sql($date)."'" : 'NULL')
230                 ." WHERE type=" . db_escape($type). " AND trans_no=".db_escape($trans_no);
231
232         db_query($sql, "Can't change reconciliation status");
233 }