Bank Payment View, Bank Deposit View: fixed 'left to allocate' amount, added value...
[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, $person_type_id, $person_id, $currency="", $err_msg="", $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         // Also store the rate to the home
38         //$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
39
40         $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
41                 trans_date, amount, person_type_id, person_id) ";
42
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).")";
46
47         if ($err_msg == "")
48                 $err_msg = "The bank transaction could not be inserted";
49
50         db_query($sql, $err_msg);
51 }
52
53 //----------------------------------------------------------------------------------------
54
55 function exists_bank_trans($type, $type_no)
56 {
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");
60
61     return (db_num_rows($result) > 0);
62 }
63
64 //----------------------------------------------------------------------------------------
65
66 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
67 {
68         $sql = "SELECT bt.*, act.*,
69                 IFNULL(abs(dt.ov_amount), IFNULL (abs(st.ov_amount), bt.amount)) settled_amount,
70                 IFNULL(abs(dt.ov_amount/bt.amount), IFNULL (abs(st.ov_amount/bt.amount), 1)) settle_rate,
71                 IFNULL(debtor.curr_code, IFNULL (supplier.curr_code, act.bank_curr_code)) settle_curr
72
73                 FROM ".TB_PREF."bank_trans bt
74                                  LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
75                                  LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
76                                  LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
77                                  LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id,
78                          ".TB_PREF."bank_accounts act
79                 WHERE act.id=bt.bank_act ";
80         if ($type != null)
81                 $sql .= " AND bt.type=".db_escape($type);
82         if ($trans_no != null)
83                 $sql .= " AND bt.trans_no = ".db_escape($trans_no);
84         if ($person_type_id != null)
85                 $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
86         if ($person_id != null)
87                 $sql .= " AND bt.person_id = ".db_escape($person_id);
88         $sql .= " ORDER BY trans_date, bt.id";
89
90         return db_query($sql, "query for bank transaction");
91 }
92
93 //----------------------------------------------------------------------------------------
94
95 function get_bank_trans_for_bank_account($bank_account, $from, $to)
96 {
97         $from = date2sql($from);
98         $to = date2sql($to);
99         $sql = "SELECT t.* FROM "
100                 .TB_PREF."bank_trans t 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) . "
102                 AND ISNULL(v.date_)
103                 AND trans_date >= '$from'
104                 AND trans_date <= '$to'
105                 ORDER BY trans_date, t.id";
106
107         return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved");
108 }
109
110 //----------------------------------------------------------------------------------------
111
112 function get_balance_before_for_bank_account($bank_account, $from)
113 {
114         $from = date2sql($from);
115         $sql = "SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE bank_act="
116                 .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) FROM ".TB_PREF."gl_trans WHERE account="
127         .db_escape($account)." AND type=".db_escape($type)
128         ." AND type_no=".db_escape($trans_no);
129
130         $result = db_query($sql, "query for gl trans value");
131
132         $row = db_fetch_row($result);
133         return $row[0];
134 }
135
136 //----------------------------------------------------------------------------------------
137
138 function void_bank_trans($type, $type_no, $nested=false)
139 {
140
141         if (!$nested)
142                 begin_transaction();
143
144         $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
145                 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
146
147         $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
148
149         void_gl_trans($type, $type_no, true);
150
151         // in case it's a customer trans - probably better to check first
152         void_cust_allocations($type, $type_no);
153         void_customer_trans($type, $type_no);
154
155         // in case it's a supplier trans - probably better to check first
156         void_supp_allocations($type, $type_no);
157         void_supp_trans($type, $type_no);
158
159         void_trans_tax_details($type, $type_no);
160
161         if (!$nested)
162                 commit_transaction();
163 }
164
165 /**
166 *       Check account history to find transaction which would exceed authorized overdraft for given account.
167 *       Returns null or transaction in conflict. Running balance is checked on daily basis only, to enable ID change after edition.
168 *       $delta_amount - tested change in bank balance at $date.
169 **/
170 function check_bank_account_history($delta_amount, $bank_account, $date=null, $user=null)
171 {
172         if ($delta_amount >= 0 && isset($date))
173                  return null;   // amount increese is always safe
174
175         $balance = $date ? get_bank_account_limit($bank_account, $date, $user) : 0;
176
177         if (!isset($balance) && isset($date))
178                 return null;    // unlimited account
179
180         if ($balance < -$delta_amount)
181                 return array('amount' => $balance, 'trans_date'=> $date);
182
183         $balance += $delta_amount;
184
185         $sql = "SELECT sum(amount) as amount, trans_date FROM ".TB_PREF."bank_trans WHERE bank_act=".db_escape($bank_account);
186         if ($date)
187         {
188                 $date = date2sql($date);
189                 $sql .= " AND trans_date > '$date'";
190         }
191         $sql .= " GROUP BY trans_date ORDER BY trans_date ASC";
192
193         $history = db_query($sql, "cannot retrieve cash account history");
194
195         while ($trans = db_fetch($history)) {
196                 $balance += $trans['amount'];
197                 if ($balance < 0)
198                         return $trans;
199         }
200
201         return null;
202 }
203
204 /**
205 *       Check bank transfer, deposit or customer deposit before voiding.
206 **/
207 function check_void_bank_trans($type, $type_no)
208 {
209         $moves = get_bank_trans($type, $type_no);
210         while ($trans = db_fetch($moves)) {
211                 if ($trans['amount'] > 0) { // skip transfer input part
212                         return check_bank_account_history(-$trans['amount'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;
213                 }
214         }
215         return true;
216 }
217
218 ?>