6e440080ebef97c3b047d0ba240d37a2a101d334
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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="")
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         $to_bank_currency = get_exchange_rate_from_to($currency, $bank_account_currency, $date_);
28         $amount_bank = ($amount / $to_bank_currency);
29         }
30         else
31                 $amount_bank = $amount;
32
33
34         // Also store the rate to the home
35         //$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
36
37         $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
38                 trans_date, amount, person_type_id, person_id) ";
39
40         $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
41                 $amount_bank, $person_type_id, ". db_escape($person_id).")";
42
43         if ($err_msg == "")
44                 $err_msg = "The bank transaction could not be inserted";
45
46         db_query($sql, $err_msg);
47 }
48
49 //----------------------------------------------------------------------------------------
50
51 function exists_bank_trans($type, $type_no)
52 {
53         $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=$type
54                 AND trans_no=$type_no";
55         $result = db_query($sql, "Cannot retreive a bank transaction");
56
57     return (db_num_rows($result) > 0);
58 }
59
60 //----------------------------------------------------------------------------------------
61
62 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
63 {
64         $sql = "SELECT *, bank_account_name, account_code, bank_curr_code
65                 FROM ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts
66                 WHERE ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act ";
67         if ($type != null)
68                 $sql .= " AND type=$type ";
69         if ($trans_no != null)
70                 $sql .= " AND ".TB_PREF."bank_trans.trans_no = $trans_no ";
71         if ($person_type_id != null)
72                 $sql .= " AND ".TB_PREF."bank_trans.person_type_id = $person_type_id ";
73         if ($person_id != null)
74                 $sql .= " AND ".TB_PREF."bank_trans.person_id = '$person_id'";
75         $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id";
76
77         return db_query($sql, "query for bank transaction");
78 }
79
80 //----------------------------------------------------------------------------------------
81
82 function get_gl_trans_value($account, $type, $trans_no)
83 {
84         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account='$account' AND type=$type AND type_no=$trans_no";
85
86         $result = db_query($sql, "query for gl trans value");
87
88         $row = db_fetch_row($result);
89         return $row[0];
90 }
91
92 //----------------------------------------------------------------------------------------
93
94 function void_bank_trans($type, $type_no, $nested=false)
95 {
96         if (!$nested)
97                 begin_transaction();
98
99         $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
100                 WHERE type=$type AND trans_no=$type_no";
101
102         $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
103
104         void_gl_trans($type, $type_no, true);
105
106         // in case it's a customer trans - probably better to check first
107         void_cust_allocations($type, $type_no);
108         void_customer_trans_tax_details($type, $type_no);
109         void_customer_trans($type, $type_no);
110
111         // in case it's a supplier trans - probably better to check first
112         void_supp_allocations($type, $type_no);
113         void_supp_invoice_tax_items($type, $type_no);
114         void_supp_trans($type, $type_no);
115
116         if (!$nested)
117                 commit_transaction();
118 }
119
120 //----------------------------------------------------------------------------------
121
122 ?>