2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 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/gpl-3.0.html>.
11 ***********************************************************************/
13 Write/update customer payment.
15 Warning: $rate is leaved here for extensions compatibility reasons, will be removed in 2.4
16 since 2.3.17 is not used: use $bank_amount instead.
18 $amount - in customer currency (ex. discount)
19 $discount - in customer currency
20 $bank_amount - in bank currency (before charge)
21 $charge - in bank currency
23 function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_account,
24 $date_, $ref, $amount, $discount, $memo_, $rate=0, $charge=0, $bank_amount=0)
29 $args = func_get_args(); while (count($args) < 12) $args[] = 0;
30 $args = (object)array_combine(array('trans_no', 'customer_id', 'branch_id', 'bank_account',
31 'date_', 'ref', 'amount', 'discount', 'memo_','rate','charge', 'bank_amount'), $args);
32 hook_db_prewrite($args, ST_CUSTPAYMENT);
34 $company_record = get_company_prefs();
37 delete_comments(ST_CUSTPAYMENT, $trans_no);
38 void_bank_trans(ST_CUSTPAYMENT, $trans_no, true);
39 void_gl_trans(ST_CUSTPAYMENT, $trans_no, true);
40 void_cust_allocations(ST_CUSTPAYMENT, $trans_no, $date_);
43 $bank = get_bank_account($bank_account);
45 $rate = get_exchange_rate_from_home_currency($bank['bank_curr_code'], $date_ );
47 if (!$bank_amount) // backward compatibility workaround
49 $bank_amount = $amount/$rate;
52 $payment_no = write_customer_trans(ST_CUSTPAYMENT, $trans_no, $customer_id, $branch_id,
53 $date_, $ref, $amount, $discount, 0, 0, 0, 0, 0, 0, "", 0, $rate);
55 $bank_gl_account = get_bank_gl_account($bank_account);
59 /* Bank account entry first */
60 $total += add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_,
61 $bank_gl_account, 0, 0, '', ($bank_amount - $charge), $bank['bank_curr_code'], PT_CUSTOMER, $customer_id);
63 if ($branch_id != ANY_NUMERIC) {
65 $branch_data = get_branch_accounts($branch_id);
67 $debtors_account = $branch_data["receivables_account"];
68 $discount_account = $branch_data["payment_discount_account"];
71 $debtors_account = $company_record["debtors_act"];
72 $discount_account = $company_record["default_prompt_payment_act"];
75 if (($discount + $amount) != 0) {
76 /* Now Credit Debtors account with receipts + discounts */
77 $total += add_gl_trans_customer(ST_CUSTPAYMENT, $payment_no, $date_,
78 $debtors_account, 0, 0, -($discount + $amount), $customer_id,
79 "Cannot insert a GL transaction for the debtors account credit");
82 /* Now Debit discount account with discounts allowed*/
83 $total += add_gl_trans_customer(ST_CUSTPAYMENT, $payment_no, $date_,
84 $discount_account, 0, 0, $discount, $customer_id,
85 "Cannot insert a GL transaction for the payment discount debit");
89 /* Now Debit bank charge account with charges */
90 $charge_act = get_company_pref('bank_charge_act');
91 $total += add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_, $charge_act, 0, 0, '',
92 $charge, $bank['bank_curr_code'], PT_CUSTOMER, $customer_id);
96 /*Post a balance post if $total != 0 due to variance in AR and bank posted values*/
99 $variance_act = get_company_pref('exchange_diff_act');
100 add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_, $variance_act, 0, 0, '',
101 -$total, null, PT_CUSTOMER, $customer_id);
104 /*now enter the bank_trans entry */
105 add_bank_trans(ST_CUSTPAYMENT, $payment_no, $bank_account, $ref,
106 $date_, $bank_amount - $charge, PT_CUSTOMER, $customer_id);
108 add_comments(ST_CUSTPAYMENT, $payment_no, $date_, $memo_);
110 $Refs->save(ST_CUSTPAYMENT, $payment_no, $ref);
112 $args->trans_no = $payment_no;
113 hook_db_postwrite($args, ST_CUSTPAYMENT);
114 commit_transaction();
119 //-------------------------------------------------------------------------------------------------
121 function void_customer_payment($type, $type_no)
125 hook_db_prevoid($type, $type_no);
126 void_bank_trans($type, $type_no, true);
127 void_gl_trans($type, $type_no, true);
128 void_cust_allocations($type, $type_no);
129 void_customer_trans($type, $type_no);
131 commit_transaction();