7eb77dfb2dfcc42d161d692c3ba2f1845f230a3e
[fa-stable.git] / sales / includes / db / payment_db.inc
1 <?php
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 ***********************************************************************/
12 /*
13   Write/update customer payment.
14
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.
17
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
22 */
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)
25 {
26         global $Refs;
27
28         begin_transaction(__FUNCTION__, func_get_args());
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);
33
34         $company_record = get_company_prefs();
35
36         if ($trans_no != 0) {
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_);
41         }
42
43         $bank = get_bank_account($bank_account);
44
45         if (!$bank_amount)      // backward compatibility workaround
46         {
47                 if(!$rate)
48                         $rate = get_exchange_rate_from_to(get_customer_currency($customer_id),
49                                 $bank['bank_curr_code'], $date_ );
50
51                 $bank_amount = $amount/$rate;
52         }
53
54         // do not use $rate here: global rate stored in exrate table is always used
55         $payment_no = write_customer_trans(ST_CUSTPAYMENT, $trans_no, $customer_id, $branch_id, 
56                 $date_, $ref, $amount, $discount);
57
58         $bank_gl_account = get_bank_gl_account($bank_account);
59
60         $total = 0;
61
62         /* Bank account entry first */
63         $total += add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_,
64                 $bank_gl_account, 0, 0, '', ($bank_amount - $charge),  $bank['bank_curr_code'], PT_CUSTOMER, $customer_id);
65
66         if ($branch_id != ANY_NUMERIC) {
67
68                 $branch_data = get_branch_accounts($branch_id);
69
70                 $debtors_account = $branch_data["receivables_account"];
71                 $discount_account = $branch_data["payment_discount_account"];
72
73         } else {
74                 $debtors_account = $company_record["debtors_act"];
75                 $discount_account = $company_record["default_prompt_payment_act"];
76         }
77
78         if (($discount + $amount) != 0) {
79         /* Now Credit Debtors account with receipts + discounts */
80         $total += add_gl_trans_customer(ST_CUSTPAYMENT, $payment_no, $date_,
81                 $debtors_account, 0, 0, -($discount + $amount), $customer_id);
82         }
83         if ($discount != 0)     {
84                 /* Now Debit discount account with discounts allowed*/
85                 $total += add_gl_trans_customer(ST_CUSTPAYMENT, $payment_no, $date_,
86                         $discount_account, 0, 0, $discount, $customer_id);
87         }
88
89         if ($charge != 0)       {
90                 /* Now Debit bank charge account with charges */
91                 $charge_act = get_bank_charge_account($bank_account);
92                 $total += add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_,     $charge_act, 0, 0, '', 
93                         $charge, $bank['bank_curr_code'], PT_CUSTOMER,  $customer_id);
94         }
95
96
97         /*Post a balance post if $total != 0 due to variance in AR and bank posted values*/
98         if ($total != 0)
99         {
100                 $variance_act = get_company_pref('exchange_diff_act');
101                 add_gl_trans(ST_CUSTPAYMENT, $payment_no, $date_,       $variance_act, 0, 0, '',
102                         -$total, null, PT_CUSTOMER,  $customer_id);
103         }
104
105         /*now enter the bank_trans entry */
106         add_bank_trans(ST_CUSTPAYMENT, $payment_no, $bank_account, $ref,
107                 $date_, $bank_amount - $charge, PT_CUSTOMER, $customer_id);
108
109         add_comments(ST_CUSTPAYMENT, $payment_no, $date_, $memo_);
110
111         $Refs->save(ST_CUSTPAYMENT, $payment_no, $ref);
112
113         $args->trans_no = $payment_no;
114         hook_db_postwrite($args, ST_CUSTPAYMENT);
115         commit_transaction();
116
117         return $payment_no;
118 }
119
120 //-------------------------------------------------------------------------------------------------
121
122 function void_customer_payment($type, $type_no)
123 {
124         begin_transaction(__FUNCTION__, func_get_args());
125
126         hook_db_prevoid($type, $type_no);
127         void_bank_trans($type, $type_no, true);
128         void_gl_trans($type, $type_no, true);
129         void_cust_allocations($type, $type_no);
130         void_customer_trans($type, $type_no);
131
132         commit_transaction();
133 }
134
135 /*
136         Retrieve bank charge amount from GL postings for customer payment.
137         . Bank charge is not stored explicitly in database as of 2.3.xx
138         . Due to roundings the retrieved charge can differ from the original amount when bank_curr!=home_curr && bank_curr!=cust_curr
139 */
140 function get_cust_bank_charge($type, $trans_no)
141 {
142
143         // restore charge amount from amounts in bank currency if possible, otherwise calculate from GL posting with exchange rate used for amount posting
144         $sql = "SELECT  IF(act.bank_curr_code=home_curr.value, charge.amount,
145                                         IF(act.bank_curr_code=debtor.curr_code, -(trans.amount-ar.ov_amount+ar.ov_discount),
146                                         IFNULL(charge.amount*trans.amount/pmt.amount, 0)))
147                         FROM ".TB_PREF."bank_trans trans
148                                 LEFT JOIN ".TB_PREF."bank_accounts act ON trans.bank_act=act.id
149                                 LEFT JOIN ".TB_PREF."sys_prefs charge_act ON charge_act.name='bank_charge_act'
150                                 LEFT JOIN ".TB_PREF."sys_prefs home_curr ON home_curr.name='curr_default'
151                                 LEFT JOIN ".TB_PREF."gl_trans charge ON charge.type=trans.type AND charge.type_no=trans.trans_no AND charge.account=charge_act.value
152                                 LEFT JOIN ".TB_PREF."gl_trans pmt ON pmt.type=trans.type AND pmt.type_no=trans.trans_no AND pmt.account=act.account_code
153                                 LEFT JOIN ".TB_PREF."debtors_master debtor ON trans.person_id=debtor.debtor_no AND trans.person_type_id=".PT_CUSTOMER."
154                                 LEFT JOIN ".TB_PREF."debtor_trans ar ON trans.type=ar.type AND trans.trans_no=ar.trans_no
155                         WHERE pmt.amount!=0 AND charge.amount!=0 AND trans.amount!=0
156                                 AND trans.type=".db_escape($type)." AND trans.trans_no=".db_escape($trans_no);
157
158         $result = db_query($sql, "cannot retrieve bank charge");
159
160         if (!db_num_rows($result))
161                 return 0;
162
163         $myrow = db_fetch($result);
164         return $myrow['0'];
165 }
166