Changes related to rewrite and optimalzation of taz register.
[fa-stable.git] / gl / includes / db / gl_db_banking.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 //      Add bank tranfer to database.
14 //
15 //      $from_account - source bank account id
16 //      $to_account   - target bank account id
17 //      
18 function add_bank_transfer($from_account, $to_account, $date_,
19         $amount, $ref, $memo_)
20 {
21         begin_transaction();
22
23         $trans_type = systypes::bank_transfer();
24
25         $currency = get_bank_account_currency($from_account);
26
27         $trans_no = get_next_trans_no($trans_type);
28
29         $from_gl_account = get_bank_gl_account($from_account);
30         $to_gl_account = get_bank_gl_account($to_account);
31
32         $total = 0;
33         // do the source account postings
34     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
35                 -$amount, $currency);
36
37     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
38                 $date_, -$amount,
39                 payment_person_types::misc(), "", $currency,
40                 "Cannot insert a source bank transaction");
41
42         // do the destination account postings
43         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
44                 $amount, $currency);
45         /*Post a balance post if $total != 0 */
46         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
47         
48         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
49                 $date_, $amount, payment_person_types::misc(), "",
50                 $currency, "Cannot insert a destination bank transaction");
51
52         add_comments($trans_type, $trans_no, $date_, $memo_);
53
54         references::save_last($ref, $trans_type);
55
56         commit_transaction();
57
58         return $trans_no;
59 }
60
61 //----------------------------------------------------------------------------------
62 //      Add bank payment or deposit to database.
63 //
64 //      $from_account - bank account id
65 //  $item - transaction cart (line item's amounts in bank account's currency)
66 //  $person_type_id - defines type of $person_id identifiers
67 //  $person_id  - supplier/customer/other id
68 // $person_detail_id - customer branch id or not used
69 //
70 // returns an array of (inserted trans type, trans no)
71
72 function add_bank_transaction($trans_type, $from_account, $items, $date_,
73         $person_type_id, $person_id, $person_detail_id, $ref, $memo_)
74 {
75         // we can only handle type 1 (payment)and type 2 (deposit)
76         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
77                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
78
79         begin_transaction();
80
81         $currency = get_bank_account_currency($from_account);
82         $bank_gl_account = get_bank_gl_account($from_account);
83
84         // the gl items are already inversed/negated for type 2 (deposit)
85         $total_amount = $items->gl_items_total();
86
87     if ($person_type_id == payment_person_types::customer())
88     {
89         // we need to add a customer transaction record
90
91                 // convert to customer currency
92                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
93                 // we need to negate it too
94                 $cust_amount = -$cust_amount;
95
96                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
97                 $ref, $cust_amount);
98
99     }
100     elseif ($person_type_id == payment_person_types::supplier())
101     {
102         // we need to add a supplier transaction record
103                 // convert to supp currency
104                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
105
106                 // we need to negate it too
107                 $supp_amount = -$supp_amount;
108
109                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
110                 $ref, "", $supp_amount, 0, 0);
111
112     }
113     else
114     {
115         $trans_no = get_next_trans_no($trans_type);
116     }
117
118         // do the source account postings
119
120     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
121         $date_, -$total_amount,
122         $person_type_id, $person_id,
123         $currency,
124         "Cannot insert a source bank transaction");
125         $total = 0;
126         foreach ($items->gl_items as $gl_item)
127         {
128                 $is_bank_to = is_bank_account($gl_item->code_id);
129
130                 if ($trans_type == 1 AND $is_bank_to)
131                 {
132                         // we don't allow payments to go to a bank account. use transfer for this !
133                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
134                 }
135
136         // do the destination account postings
137         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
138                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
139                 $gl_item->amount, $currency, $person_type_id, $person_id);
140
141         if ($is_bank_to)
142         {
143                 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
144                         $date_, $gl_item->amount, $person_type_id, $person_id, 
145                                 $currency, "Cannot insert a destination bank transaction");
146         }
147                 // store tax details if the gl account is a tax account
148
149                 $amount = $gl_item->amount;
150                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, $amount,
151                         $date_, $memo);
152         }
153
154         // do the source account postings
155     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
156         -$total, null, $person_type_id, $person_id);
157
158         add_comments($trans_type, $trans_no, $date_, $memo_);
159
160         references::save_last($ref, $trans_type);
161
162         commit_transaction();
163
164         return array($trans_type, $trans_no);
165 }
166
167 //----------------------------------------------------------------------------------------
168
169 ?>