3 //----------------------------------------------------------------------------------
4 // Add bank tranfer to database.
6 // $from_account - source bank account id
7 // $to_account - target bank account id
9 function add_bank_transfer($from_account, $to_account, $date_,
10 $amount, $ref, $memo_)
14 $trans_type = systypes::bank_transfer();
16 $currency = get_bank_account_currency($from_account);
18 $trans_no = get_next_trans_no($trans_type);
20 $from_gl_account = get_bank_gl_account($from_account);
21 $to_gl_account = get_bank_gl_account($to_account);
24 // do the source account postings
25 $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
28 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
30 payment_person_types::misc(), "", $currency,
31 "Cannot insert a source bank transaction");
33 // do the destination account postings
34 $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
36 /*Post a balance post if $total != 0 */
37 add_gl_balance($trans_type, $trans_no, $date_, -$total);
39 add_bank_trans($trans_type, $trans_no, $to_account, $ref,
40 $date_, $amount, payment_person_types::misc(), "",
41 $currency, "Cannot insert a destination bank transaction");
43 add_comments($trans_type, $trans_no, $date_, $memo_);
45 references::save_last($ref, $trans_type);
52 //----------------------------------------------------------------------------------
53 // Add bank payment or deposit to database.
55 // $from_account - bank account id
56 // $item - transaction cart (line item's amounts in bank account's currency)
57 // $person_type_id - defines type of $person_id identifiers
58 // $person_id - supplier/customer/other id
59 // $person_detail_id - customer branch id or not used
61 // returns an array of (inserted trans type, trans no)
63 function add_bank_transaction($trans_type, $from_account, $items, $date_,
64 $person_type_id, $person_id, $person_detail_id, $ref, $memo_)
66 // we can only handle type 1 (payment)and type 2 (deposit)
67 if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
68 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
72 $currency = get_bank_account_currency($from_account);
73 $bank_gl_account = get_bank_gl_account($from_account);
75 // the gl items are already inversed/negated for type 2 (deposit)
76 $total_amount = $items->gl_items_total();
78 if ($person_type_id == payment_person_types::customer())
80 // we need to add a customer transaction record
82 // convert to customer currency
83 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
84 // we need to negate it too
85 $cust_amount = -$cust_amount;
87 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
91 elseif ($person_type_id == payment_person_types::supplier())
93 // we need to add a supplier transaction record
94 // convert to supp currency
95 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
97 // we need to negate it too
98 $supp_amount = -$supp_amount;
100 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
101 $ref, "", $supp_amount, 0, 0);
106 $trans_no = get_next_trans_no($trans_type);
109 // do the source account postings
111 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
112 $date_, -$total_amount,
113 $person_type_id, $person_id,
115 "Cannot insert a source bank transaction");
117 foreach ($items->gl_items as $gl_item)
119 $is_bank_to = is_bank_account($gl_item->code_id);
121 if ($trans_type == 1 AND $is_bank_to)
123 // we don't allow payments to go to a bank account. use transfer for this !
124 display_db_error("invalid payment entered. Cannot pay to another bank account", "");
127 // do the destination account postings
128 $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
129 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
130 $gl_item->amount, $currency, $person_type_id, $person_id);
134 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
135 $date_, $gl_item->amount, $person_type_id, $person_id,
136 $currency, "Cannot insert a destination bank transaction");
140 // do the source account postings
141 add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, "",
142 -$total, null, $person_type_id, $person_id);
144 add_comments($trans_type, $trans_no, $date_, $memo_);
146 references::save_last($ref, $trans_type);
148 commit_transaction();
150 return array($trans_type, $trans_no);
153 //----------------------------------------------------------------------------------------