Merged stable branch up to 2.3.10
[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 *, 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 ";
71         if ($type != null)
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";
80
81         return db_query($sql, "query for bank transaction");
82 }
83
84 //----------------------------------------------------------------------------------------
85
86 function get_bank_trans_for_bank_account($bank_account, $from, $to)
87 {
88         $from = date2sql($from);
89         $to = date2sql($to);
90         $sql = "SELECT t.* FROM "
91                 .TB_PREF."bank_trans t LEFT JOIN ".TB_PREF."voided v ON t.type=v.type AND t.trans_no=v.id
92                 WHERE t.bank_act = ".db_escape($bank_account) . "
93                 AND ISNULL(v.date_)
94                 AND trans_date >= '$from'
95                 AND trans_date <= '$to'
96                 ORDER BY trans_date, t.id";
97
98         return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved");
99 }
100
101 //----------------------------------------------------------------------------------------
102
103 function get_balance_before_for_bank_account($bank_account, $from)
104 {
105         $from = date2sql($from);
106         $sql = "SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE bank_act="
107                 .db_escape($bank_account) . "
108                 AND trans_date < '$from'";
109         $before_qty = db_query($sql, "The starting balance on hand could not be calculated");
110         $bfw_row = db_fetch_row($before_qty);
111         return $bfw_row[0];
112 }
113 //----------------------------------------------------------------------------------------
114
115 function get_gl_trans_value($account, $type, $trans_no)
116 {
117         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account="
118         .db_escape($account)." AND type=".db_escape($type)
119         ." AND type_no=".db_escape($trans_no);
120
121         $result = db_query($sql, "query for gl trans value");
122
123         $row = db_fetch_row($result);
124         return $row[0];
125 }
126
127 //----------------------------------------------------------------------------------------
128
129 function void_bank_trans($type, $type_no, $nested=false)
130 {
131
132         if (!$nested)
133                 begin_transaction();
134
135         $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
136                 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
137
138         $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
139
140         void_gl_trans($type, $type_no, true);
141
142         // in case it's a customer trans - probably better to check first
143         void_cust_allocations($type, $type_no);
144         void_customer_trans($type, $type_no);
145
146         // in case it's a supplier trans - probably better to check first
147         void_supp_allocations($type, $type_no);
148         void_supp_trans($type, $type_no);
149
150         void_trans_tax_details($type, $type_no);
151
152         if (!$nested)
153                 commit_transaction();
154 }
155
156 /**
157 *       Check account history to find transaction which would exceed authorized overdraft for given account.
158 *       Returns null or transaction in conflict. Running balance is checked on daily basis only, to enable ID change after edition.
159 *       $delta_amount - tested change in bank balance at $date.
160 **/
161 function check_bank_account_history($delta_amount, $bank_account, $date, $user=null)
162 {
163         if ($delta_amount >= 0)
164                  return null;   // amount increese is always safe
165
166         $balance = get_bank_account_limit($bank_account, $date, $user);
167
168         if (!isset($balance))
169                 return null;    // unlimited account
170
171         $date = date2sql($date);
172         $balance+= $delta_amount;
173
174         $sql = "SELECT sum(amount) as amount FROM ".TB_PREF."bank_trans WHERE bank_act=".db_escape($bank_account)
175                 ." AND trans_date >= '$date' GROUP BY trans_date ORDER BY trans_date ASC";
176
177         $history = db_query($sql, "cannot retrieve cash account history");
178
179         while ($trans = db_fetch($history)) {
180                 $balance += $trans['amount'];
181                 if ($balance < 0)
182                         return $trans;
183         }
184
185         return null;
186 }
187
188 /**
189 *       Check bank transfer, deposit or customer deposit before voiding.
190 **/
191 function check_void_bank_trans($type, $type_no)
192 {
193         $moves = get_bank_trans($type, $type_no);
194         while ($trans = db_fetch($moves)) {
195                 if ($trans['amount'] > 0) { // skip transfer input part
196                         return check_bank_account_history(-$trans['amount'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;
197                 }
198         }
199         return true;
200 }
201
202 ?>