3 //----------------------------------------------------------------------------------
5 function add_bank_transfer($from_account, $to_account, $date_,
6 $amount, $payment_type, $ref, $memo_)
10 $trans_type = systypes::bank_transfer();
12 $currency = get_bank_account_currency($from_account);
14 $trans_no = get_next_trans_no($trans_type);
17 // do the source account postings
18 $total += add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
21 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
22 $date_, $payment_type, -$amount,
23 payment_person_types::misc(), "", $currency,
24 "Cannot insert a source bank transaction");
26 // do the destination account postings
27 add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
30 add_bank_trans($trans_type, $trans_no, $to_account, $ref,
31 $date_, $payment_type, $amount,
32 payment_person_types::misc(), "",
34 "Cannot insert a destination bank transaction");
36 add_comments($trans_type, $trans_no, $date_, $memo_);
38 references::save_last($ref, $trans_type);
45 //----------------------------------------------------------------------------------
47 // $amount is in $from_account's currency
49 // returns an array of (inserted trans type, trans no)
51 function add_bank_transaction($trans_type, $from_account, $items, $date_,
52 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
54 // we can only handle type 1 (payment)and type 2 (deposit)
55 if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
56 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
60 $currency = get_bank_account_currency($from_account);
62 // the gl items are already inversed/negated for type 2 (deposit)
63 $total_amount = $items->gl_items_total();
65 if ($person_type_id == payment_person_types::customer())
67 // we need to add a customer transaction record
69 // convert to customer currency
70 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
71 // we need to negate it too
72 $cust_amount = -$cust_amount;
74 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
78 elseif ($person_type_id == payment_person_types::supplier())
80 // we need to add a supplier transaction record
81 // convert to supp currency
82 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
84 // we need to negate it too
85 $supp_amount = -$supp_amount;
87 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
88 $ref, "", $supp_amount, 0, 0);
93 $trans_no = get_next_trans_no($trans_type);
96 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
97 $date_, $type, -$total_amount,
98 $person_type_id, $person_id,
100 "Cannot insert a source bank transaction");
102 foreach ($items->gl_items as $gl_item)
104 $is_bank_to = is_bank_account($gl_item->code_id);
106 if ($trans_type == 1 AND $is_bank_to)
108 // we don't allow payments to go to a bank account. use transfer for this !
109 display_db_error("invalid payment entered. Cannot pay to another bank account", "");
112 // do the destination account postings
113 $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
114 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
115 $gl_item->amount, $currency, $person_type_id, $person_id);
119 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
120 $date_, $type, $gl_item->amount,
121 $person_type_id, $person_id, $currency,
122 "Cannot insert a destination bank transaction");
126 // do the source account postings
127 add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
128 -$total, null, $person_type_id, $person_id);
130 add_comments($trans_type, $trans_no, $date_, $memo_);
132 references::save_last($ref, $trans_type);
134 commit_transaction();
136 return array($trans_type, $trans_no);
139 //----------------------------------------------------------------------------------------