Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[fa-stable.git] / sales / customer_payments.php
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 $page_security = 'SA_SALESPAYMNT';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/allocation_cart.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/includes/ui.inc");
18 include_once($path_to_root . "/includes/banking.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20 include_once($path_to_root . "/sales/includes/sales_db.inc");
21 include_once($path_to_root . "/reporting/includes/reporting.inc");
22
23 $js = "";
24 if ($SysPrefs->use_popup_windows) {
25         $js .= get_js_open_window(900, 500);
26 }
27 if (user_use_date_picker()) {
28         $js .= get_js_date_picker();
29 }
30 add_js_file('payalloc.js');
31
32 page(_($help_context = "Customer Payment Entry"), false, false, "", $js);
33
34 //----------------------------------------------------------------------------------------------
35
36 check_db_has_customers(_("There are no customers defined in the system."));
37
38 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
39
40 //----------------------------------------------------------------------------------------
41 if (isset($_GET['customer_id']))
42 {
43         $_POST['customer_id'] = $_GET['customer_id'];
44 }
45
46 if (!isset($_POST['bank_account'])) { // first page call
47         $_SESSION['alloc'] = new allocation(ST_CUSTPAYMENT, 0, get_post('customer_id'));
48
49         if (isset($_GET['SInvoice'])) {
50                 //  get date and supplier
51                 $inv = get_customer_trans($_GET['SInvoice'], ST_SALESINVOICE);
52                 $dflt_act = get_default_bank_account($inv['curr_code']);
53                 $_POST['bank_account'] = $dflt_act['id'];
54                 if ($inv) {
55                         $_POST['customer_id'] = $inv['debtor_no'];
56                         $_SESSION['alloc']->set_person($inv['debtor_no'], PT_CUSTOMER);
57                         $_SESSION['alloc']->read();
58                         $_POST['BranchID'] = $inv['branch_code'];
59                         $_POST['DateBanked'] = sql2date($inv['tran_date']);
60                         foreach($_SESSION['alloc']->allocs as $line => $trans) {
61                                 if ($trans->type == ST_SALESINVOICE && $trans->type_no == $_GET['SInvoice']) {
62                                         $un_allocated = $trans->amount - $trans->amount_allocated;
63                                         if ($un_allocated){
64                                                 $_SESSION['alloc']->allocs[$line]->current_allocated = $un_allocated;
65                                                 $_POST['amount'] = $_POST['amount'.$line] = price_format($un_allocated);
66                                         }
67                                         break;
68                                 }
69                         }
70                         unset($inv);
71                 } else
72                         display_error(_("Invalid sales invoice number."));
73         }
74 }
75
76 if (list_updated('BranchID')) {
77         // when branch is selected via external editor also customer can change
78         $br = get_branch(get_post('BranchID'));
79         $_POST['customer_id'] = $br['debtor_no'];
80         $_SESSION['alloc']->person_id = $br['debtor_no'];
81         $Ajax->activate('customer_id');
82 }
83
84 if (!isset($_POST['customer_id'])) {
85         $_POST['customer_id'] = get_global_customer(false);
86         $_SESSION['alloc']->set_person($_POST['customer_id'], PT_CUSTOMER);
87         $_SESSION['alloc']->read();
88         $dflt_act = get_default_bank_account($_SESSION['alloc']->person_curr);
89         $_POST['bank_account'] = $dflt_act['id'];
90 }
91 if (!isset($_POST['DateBanked'])) {
92         $_POST['DateBanked'] = new_doc_date();
93         if (!is_date_in_fiscalyear($_POST['DateBanked'])) {
94                 $_POST['DateBanked'] = end_fiscalyear();
95         }
96 }
97
98
99 if (isset($_GET['AddedID'])) {
100         $payment_no = $_GET['AddedID'];
101
102         display_notification_centered(_("The customer payment has been successfully entered."));
103
104         submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');
105         submenu_print(_("&Email This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, null, 1);
106
107         submenu_view(_("&View this Customer Payment"), ST_CUSTPAYMENT, $payment_no);
108
109         submenu_option(_("Enter Another &Customer Payment"), "/sales/customer_payments.php");
110         submenu_option(_("Enter Other &Deposit"), "/gl/gl_bank.php?NewDeposit=Yes");
111         submenu_option(_("Enter Payment to &Supplier"), "/purchasing/supplier_payment.php");
112         submenu_option(_("Enter Other &Payment"), "/gl/gl_bank.php?NewPayment=Yes");
113         submenu_option(_("Bank Account &Transfer"), "/gl/bank_transfer.php");
114
115         display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));
116
117         display_footer_exit();
118 }
119 elseif (isset($_GET['UpdatedID'])) {
120         $payment_no = $_GET['UpdatedID'];
121
122         display_notification_centered(_("The customer payment has been successfully updated."));
123
124         submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');
125
126         display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));
127
128 //      hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");
129
130         hyperlink_no_params($path_to_root . "/sales/inquiry/customer_inquiry.php?", _("Select Another Customer Payment for &Edition"));
131
132         hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
133
134         display_footer_exit();
135 }
136
137 //----------------------------------------------------------------------------------------------
138
139 function can_process()
140 {
141         global $Refs;
142
143         if (!get_post('customer_id'))
144         {
145                 display_error(_("There is no customer selected."));
146                 set_focus('customer_id');
147                 return false;
148         } 
149         
150         if (!get_post('BranchID'))
151         {
152                 display_error(_("This customer has no branch defined."));
153                 set_focus('BranchID');
154                 return false;
155         } 
156         
157         if (!isset($_POST['DateBanked']) || !is_date($_POST['DateBanked'])) {
158                 display_error(_("The entered date is invalid. Please enter a valid date for the payment."));
159                 set_focus('DateBanked');
160                 return false;
161         } elseif (!is_date_in_fiscalyear($_POST['DateBanked'])) {
162                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
163                 set_focus('DateBanked');
164                 return false;
165         }
166
167         if (!check_reference($_POST['ref'], ST_CUSTPAYMENT, @$_POST['trans_no'])) {
168                 set_focus('ref');
169                 return false;
170         }
171
172         if (!check_num('amount', 0)) {
173                 display_error(_("The entered amount is invalid or negative and cannot be processed."));
174                 set_focus('amount');
175                 return false;
176         }
177
178         if (isset($_POST['charge']) && (!check_num('charge', 0) || $_POST['charge'] == $_POST['amount'])) {
179                 display_error(_("The entered amount is invalid or negative and cannot be processed."));
180                 set_focus('charge');
181                 return false;
182         }
183         if (isset($_POST['charge']) && input_num('charge') > 0) {
184                 $charge_acct = get_bank_charge_account($_POST['bank_account']);
185                 if (get_gl_account($charge_acct) == false) {
186                         display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
187                         set_focus('charge');
188                         return false;
189                 }       
190         }
191
192         if (@$_POST['discount'] == "") 
193         {
194                 $_POST['discount'] = 0;
195         }
196
197         if (!check_num('discount')) {
198                 display_error(_("The entered discount is not a valid number."));
199                 set_focus('discount');
200                 return false;
201         }
202
203         if (input_num('amount') <= 0) {
204                 display_error(_("The balance of the amount and discount is zero or negative. Please enter valid amounts."));
205                 set_focus('discount');
206                 return false;
207         }
208
209         if (isset($_POST['bank_amount']) && input_num('bank_amount')<=0)
210         {
211                 display_error(_("The entered payment amount is zero or negative."));
212                 set_focus('bank_amount');
213                 return false;
214         }
215
216         if (!db_has_currency_rates(get_customer_currency($_POST['customer_id']), $_POST['DateBanked'], true))
217                 return false;
218
219         $_SESSION['alloc']->amount = input_num('amount');
220
221         if (isset($_POST["TotalNumberOfAllocs"]))
222                 return check_allocations();
223         else
224                 return true;
225 }
226
227 //----------------------------------------------------------------------------------------------
228
229 if (isset($_POST['_customer_id_button'])) {
230 //      unset($_POST['branch_id']);
231         $Ajax->activate('BranchID');
232 }
233
234 //----------------------------------------------------------------------------------------------
235
236 if (get_post('AddPaymentItem') && can_process()) {
237
238         new_doc_date($_POST['DateBanked']);
239
240         $new_pmt = !$_SESSION['alloc']->trans_no;
241
242         $payment_no =  save_cust_payment($_SESSION['alloc'], get_post('customer_id'), get_post('BranchID'), get_post('bank_account'),
243                  get_post('DateBanked'), get_post('ref'), input_num('amount'), input_num('discount'), get_post('memo_'),
244                  input_num('charge'), input_num('bank_amount', input_num('amount')));
245
246         unset($_SESSION['alloc']);
247         meta_forward($_SERVER['PHP_SELF'], $new_pmt ? "AddedID=$payment_no" : "UpdatedID=$payment_no");
248 }
249
250 //----------------------------------------------------------------------------------------------
251
252 function read_customer_data()
253 {
254         global $Refs;
255
256         $myrow = get_customer_habit($_POST['customer_id']);
257
258         $_POST['HoldAccount'] = $myrow["dissallow_invoices"];
259         $_POST['pymt_discount'] = $myrow["pymt_discount"];
260         // To support Edit feature
261         // If page is called first time and New entry fetch the nex reference number
262         if (!$_SESSION['alloc']->trans_no && !isset($_POST['charge'])) 
263                 $_POST['ref'] = $Refs->get_next(ST_CUSTPAYMENT, null, array(
264                         'customer' => get_post('customer_id'), 'date' => get_post('DateBanked')));
265 }
266
267 //----------------------------------------------------------------------------------------------
268 $new = 1;
269
270 // To support Edit feature
271 if (isset($_GET['trans_no']) && $_GET['trans_no'] > 0 )
272 {
273         $_POST['trans_no'] = $_GET['trans_no'];
274
275         $new = 0;
276         $myrow = get_customer_trans($_POST['trans_no'], ST_CUSTPAYMENT);
277         $_POST['customer_id'] = $myrow["debtor_no"];
278         $_POST['customer_name'] = $myrow["DebtorName"];
279         $_POST['BranchID'] = $myrow["branch_code"];
280         $_POST['bank_account'] = $myrow["bank_act"];
281         $_POST['ref'] =  $myrow["reference"];
282         $charge = get_cust_bank_charge(ST_CUSTPAYMENT, $_POST['trans_no']);
283         $_POST['charge'] =  price_format($charge);
284         $_POST['DateBanked'] =  sql2date($myrow['tran_date']);
285         $_POST["amount"] = price_format($myrow['Total'] - $myrow['ov_discount']);
286         $_POST["bank_amount"] = price_format($myrow['bank_amount']+$charge);
287         $_POST["discount"] = price_format($myrow['ov_discount']);
288         $_POST["memo_"] = get_comments_string(ST_CUSTPAYMENT,$_POST['trans_no']);
289
290         //Prepare allocation cart 
291         if (isset($_POST['trans_no']) && $_POST['trans_no'] > 0 )
292                 $_SESSION['alloc'] = new allocation(ST_CUSTPAYMENT,$_POST['trans_no']);
293         else
294         {
295                 $_SESSION['alloc'] = new allocation(ST_CUSTPAYMENT, $_POST['trans_no']);
296                 $Ajax->activate('alloc_tbl');
297         }
298 }
299
300 //----------------------------------------------------------------------------------------------
301 $new = !$_SESSION['alloc']->trans_no;
302 start_form();
303
304 hidden('trans_no');
305
306 start_outer_table(TABLESTYLE2, "width='60%'", 5);
307
308 table_section(1);
309
310 if ($new)
311         customer_list_row(_("From Customer:"), 'customer_id', null, false, true);
312 else {
313         label_cells(_("From Customer:"), $_SESSION['alloc']->person_name, "class='label'");
314         hidden('customer_id', $_POST['customer_id']);
315 }
316
317 if (db_customer_has_branches($_POST['customer_id'])) {
318         customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'BranchID', null, false, true, true);
319 } else {
320         hidden('BranchID', ANY_NUMERIC);
321 }
322
323 if (list_updated('customer_id') || ($new && list_updated('bank_account'))) {
324         $_SESSION['alloc']->set_person($_POST['customer_id'], PT_CUSTOMER);
325         $_SESSION['alloc']->read();
326         $_POST['memo_'] = $_POST['amount'] = $_POST['discount'] = '';
327         if (list_updated('customer_id')) {
328                 $dflt_act = get_default_bank_account($_SESSION['alloc']->person_curr);
329                 $_POST['bank_account'] = $dflt_act['id'];
330         }
331         $Ajax->activate('_page_body');
332 }
333
334 bank_accounts_list_row(_("Into Bank Account:"), 'bank_account', null, true);
335
336 read_customer_data();
337
338 set_global_customer($_POST['customer_id']);
339 if (isset($_POST['HoldAccount']) && $_POST['HoldAccount'] != 0) 
340         display_warning(_("This customer account is on hold."));
341 $display_discount_percent = percent_format($_POST['pymt_discount']*100) . "%";
342
343 table_section(2);
344
345 date_row(_("Date of Deposit:"), 'DateBanked', '', true, 0, 0, 0, null, true);
346
347 ref_row(_("Reference:"), 'ref','' , null, '', ST_CUSTPAYMENT);
348
349 table_section(3);
350
351 $comp_currency = get_company_currency();
352 $cust_currency = $_SESSION['alloc']->set_person($_POST['customer_id'], PT_CUSTOMER);
353 if (!$cust_currency)
354         $cust_currency = $comp_currency;
355 $_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);
356
357 if ($cust_currency != $bank_currency)
358 {
359         amount_row(_("Payment Amount:"), 'bank_amount', null, '', $bank_currency);
360 }
361
362 amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);
363
364 end_outer_table(1);
365
366 div_start('alloc_tbl');
367 show_allocatable(false);
368 div_end();
369
370 start_table(TABLESTYLE, "width='60%'");
371
372 label_row(_("Customer prompt payment discount :"), $display_discount_percent);
373
374 amount_row(_("Amount of Discount:"), 'discount', null, '', $cust_currency);
375
376 amount_row(_("Amount:"), 'amount', null, '', $cust_currency);
377
378 textarea_row(_("Memo:"), 'memo_', null, 22, 4);
379 end_table(1);
380
381 if ($new)
382         submit_center('AddPaymentItem', _("Add Payment"), true, '', 'default');
383 else
384         submit_center('AddPaymentItem', _("Update Payment"), true, '', 'default');
385
386 br();
387
388 end_form();
389 end_page();