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.
15 // $from_account - source bank account id
16 // $to_account - target bank account id
18 function add_bank_transfer($from_account, $to_account, $date_,
19 $amount, $ref, $memo_)
23 $trans_type = systypes::bank_transfer();
25 $currency = get_bank_account_currency($from_account);
27 $trans_no = get_next_trans_no($trans_type);
29 $from_gl_account = get_bank_gl_account($from_account);
30 $to_gl_account = get_bank_gl_account($to_account);
33 // do the source account postings
34 $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
37 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
39 payment_person_types::misc(), "", $currency,
40 "Cannot insert a source bank transaction");
42 // do the destination account postings
43 $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
45 /*Post a balance post if $total != 0 */
46 add_gl_balance($trans_type, $trans_no, $date_, -$total);
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");
52 add_comments($trans_type, $trans_no, $date_, $memo_);
54 references::save_last($ref, $trans_type);
61 //----------------------------------------------------------------------------------
62 // Add bank payment or deposit to database.
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
70 // returns an array of (inserted trans type, trans no)
72 function add_bank_transaction($trans_type, $from_account, $items, $date_,
73 $person_type_id, $person_id, $person_detail_id, $ref, $memo_)
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");
81 $currency = get_bank_account_currency($from_account);
82 $bank_gl_account = get_bank_gl_account($from_account);
84 // the gl items are already inversed/negated for type 2 (deposit)
85 $total_amount = $items->gl_items_total();
87 if ($person_type_id == payment_person_types::customer())
89 // we need to add a customer transaction record
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;
96 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
100 elseif ($person_type_id == payment_person_types::supplier())
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_);
106 // we need to negate it too
107 $supp_amount = -$supp_amount;
109 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
110 $ref, "", $supp_amount, 0, 0);
115 $trans_no = get_next_trans_no($trans_type);
118 // do the source account postings
120 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
121 $date_, -$total_amount,
122 $person_type_id, $person_id,
124 "Cannot insert a source bank transaction");
126 foreach ($items->gl_items as $gl_item)
128 $is_bank_to = is_bank_account($gl_item->code_id);
130 if ($trans_type == 1 AND $is_bank_to)
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", "");
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);
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");
147 // store tax details if the gl account is a tax account
149 $amount = $gl_item->amount;
150 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, $amount,
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);
158 add_comments($trans_type, $trans_no, $date_, $memo_);
160 references::save_last($ref, $trans_type);
162 commit_transaction();
164 return array($trans_type, $trans_no);
167 //----------------------------------------------------------------------------------------