Merged changes form stable branch up to 2.3.13
[fa-stable.git] / purchasing / supplier_payment.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_SUPPLIERPAYMNT';
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 . "/purchasing/includes/purchasing_db.inc");
21 include_once($path_to_root . "/reporting/includes/reporting.inc");
22
23 $js = "";
24 if ($use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 if ($use_date_picker)
27         $js .= get_js_date_picker();
28
29 add_js_file('payalloc.js');
30
31 page(_($help_context = "Supplier Payment Entry"), false, false, "", $js);
32
33 if (isset($_GET['supplier_id']))
34 {
35         $_POST['supplier_id'] = $_GET['supplier_id'];
36 }
37
38 //----------------------------------------------------------------------------------------
39
40 check_db_has_suppliers(_("There are no suppliers defined in the system."));
41
42 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
43
44 //----------------------------------------------------------------------------------------
45
46 if (!isset($_POST['supplier_id']))
47         $_POST['supplier_id'] = get_global_supplier(false);
48
49 if (!isset($_POST['DatePaid']))
50 {
51         $_POST['DatePaid'] = new_doc_date();
52         if (!is_date_in_fiscalyear($_POST['DatePaid']))
53                 $_POST['DatePaid'] = end_fiscalyear();
54 }
55
56 if (isset($_POST['_DatePaid_changed'])) {
57   $Ajax->activate('_ex_rate');
58 }
59
60 //----------------------------------------------------------------------------------------
61
62 if (!isset($_POST['bank_account'])) { // first page call
63           $_SESSION['alloc'] = new allocation(ST_SUPPAYMENT, 0);
64
65         if (isset($_GET['PInvoice'])) {
66                 //  get date and supplier
67                 $inv = get_supp_trans($_GET['PInvoice'], ST_SUPPINVOICE);
68                 if($inv) {
69                         $_POST['supplier_id'] = $inv['supplier_id'];
70                         $_POST['DatePaid'] = sql2date($inv['tran_date']);
71 //                      $_POST['discount'] = price_format(0);
72 //              $_POST['bank_account'], $_POST['ref']
73                         $_POST['memo_'] = $inv['supp_reference'];
74                         foreach($_SESSION['alloc']->allocs as $line => $trans) {
75                                 if ($trans->type == ST_SUPPINVOICE && $trans->type_no == $_GET['PInvoice']) {
76                                         $_POST['amount'] =
77                                                 $_SESSION['alloc']->amount = price_format($_SESSION['alloc']->allocs[$line]->amount);
78                                         $_SESSION['alloc']->allocs[$line]->current_allocated =
79                                                 $_SESSION['alloc']->allocs[$line]->amount;
80                                         break;
81                                 }
82                         }
83                         unset($inv);
84                 } else
85                         display_error(_("Invalid purchase invoice number."));
86         }
87 }
88 if (isset($_GET['AddedID'])) {
89         $payment_id = $_GET['AddedID'];
90
91         display_notification_centered( _("Payment has been sucessfully entered"));
92
93         submenu_print(_("&Print This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, 'prtopt');
94         submenu_print(_("&Email This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, null, 1);
95
96     display_note(get_gl_view_str(ST_SUPPAYMENT, $payment_id, _("View the GL &Journal Entries for this Payment")));
97
98 //    hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
99
100         hyperlink_params($_SERVER['PHP_SELF'], _("Enter another supplier &payment"), "supplier_id=" . $_POST['supplier_id']);
101
102         display_footer_exit();
103 }
104
105 //----------------------------------------------------------------------------------------
106
107 function check_inputs()
108 {
109         global $Refs;
110
111         if (!get_post('supplier_id')) 
112         {
113                 display_error(_("There is no supplier selected."));
114                 set_focus('supplier_id');
115                 return false;
116         } 
117         
118         if ($_POST['amount'] == "") 
119         {
120                 $_POST['amount'] = price_format(0);
121         }
122
123         if (!check_num('amount', 0))
124         {
125                 display_error(_("The entered amount is invalid or less than zero."));
126                 set_focus('amount');
127                 return false;
128         }
129
130         if (isset($_POST['charge']) && !check_num('charge', 0)) {
131                 display_error(_("The entered amount is invalid or less than zero."));
132                 set_focus('charge');
133                 return false;
134         }
135
136         if (isset($_POST['charge']) && input_num('charge') > 0) {
137                 $charge_acct = get_bank_charge_account($_POST['bank_account']);
138                 if (get_gl_account($charge_acct) == false) {
139                         display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
140                         set_focus('charge');
141                         return false;
142                 }       
143         }
144
145         if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
146         {
147                 display_error(_("The exchange rate must be numeric and greater than zero."));
148                 set_focus('_ex_rate');
149                 return false;
150         }
151
152         if ($_POST['discount'] == "") 
153         {
154                 $_POST['discount'] = 0;
155         }
156
157         if (!check_num('discount', 0))
158         {
159                 display_error(_("The entered discount is invalid or less than zero."));
160                 set_focus('amount');
161                 return false;
162         }
163
164         //if (input_num('amount') - input_num('discount') <= 0) 
165         if (input_num('amount') <= 0) 
166         {
167                 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
168                 set_focus('amount');
169                 return false;
170         }
171
172         if (!is_date($_POST['DatePaid']))
173         {
174                 display_error(_("The entered date is invalid."));
175                 set_focus('DatePaid');
176                 return false;
177         } 
178         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
179         {
180                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
181                 set_focus('DatePaid');
182                 return false;
183         }
184
185         $limit = get_bank_account_limit($_POST['bank_account'], $_POST['DatePaid']);
186
187         if ($limit != null && ($limit < input_num('amount')))
188         {
189                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
190                 set_focus('amount');
191                 return false;
192         }
193
194     if (!$Refs->is_valid($_POST['ref'])) 
195     {
196                 display_error(_("You must enter a reference."));
197                 set_focus('ref');
198                 return false;
199         }
200
201         if (!is_new_reference($_POST['ref'], ST_SUPPAYMENT)) 
202         {
203                 display_error(_("The entered reference is already in use."));
204                 set_focus('ref');
205                 return false;
206         }
207
208         if (!db_has_currency_rates(get_supplier_currency($_POST['supplier_id']), $_POST['DatePaid'], true))
209                 return false;
210         $_SESSION['alloc']->amount = -input_num('amount');
211
212         if (isset($_POST["TotalNumberOfAllocs"]))
213                 return check_allocations();
214         else
215                 return true;
216 }
217
218 //----------------------------------------------------------------------------------------
219
220 function handle_add_payment()
221 {
222         $supp_currency = get_supplier_currency($_POST['supplier_id']);
223         $bank_currency = get_bank_account_currency($_POST['bank_account']);
224         $comp_currency = get_company_currency();
225         if ($comp_currency != $bank_currency && $bank_currency != $supp_currency)
226                 $rate = 0;
227         else
228                 $rate = input_num('_ex_rate');
229
230         $payment_id = add_supp_payment($_POST['supplier_id'], $_POST['DatePaid'],
231                 $_POST['bank_account'], input_num('amount'), input_num('discount'), 
232                 $_POST['ref'], $_POST['memo_'], $rate, input_num('charge'));
233         new_doc_date($_POST['DatePaid']);
234
235         $_SESSION['alloc']->trans_no = $payment_id;
236         $_SESSION['alloc']->write();
237         //unset($_POST['supplier_id']);
238         unset($_POST['bank_account']);
239         unset($_POST['DatePaid']);
240         unset($_POST['currency']);
241         unset($_POST['memo_']);
242         unset($_POST['amount']);
243         unset($_POST['discount']);
244         unset($_POST['ProcessSuppPayment']);
245
246         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
247 }
248
249 //----------------------------------------------------------------------------------------
250
251 if (isset($_POST['ProcessSuppPayment']))
252 {
253          /*First off  check for valid inputs */
254     if (check_inputs() == true) 
255     {
256         handle_add_payment();
257         end_page();
258         exit;
259     }
260 }
261
262 //----------------------------------------------------------------------------------------
263
264 start_form();
265
266         start_outer_table(TABLESTYLE2, "width=60%", 5);
267
268         table_section(1);
269
270     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
271
272         if (list_updated('supplier_id') || list_updated('bank_account')) {
273           $_SESSION['alloc']->read();
274           $_POST['memo_'] = $_POST['amount'] = '';
275           $Ajax->activate('alloc_tbl');
276         }
277
278         set_global_supplier($_POST['supplier_id']);
279
280         if (!list_updated('bank_account'))
281                 $_POST['bank_account'] = get_default_supplier_bank_account($_POST['supplier_id']);              
282
283     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
284
285         bank_balance_row($_POST['bank_account']);
286
287         table_section(2);
288
289     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));
290
291     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
292
293         table_section(3);
294
295         $supplier_currency = get_supplier_currency($_POST['supplier_id']);
296         $bank_currency = get_bank_account_currency($_POST['bank_account']);
297         if ($bank_currency != $supplier_currency) 
298         {
299                 exchange_rate_display($bank_currency, $supplier_currency, $_POST['DatePaid'], true);
300         }
301
302         amount_row(_("Bank Charge:"), 'charge');
303
304
305         end_outer_table(1); // outer table
306
307         if ($bank_currency == $supplier_currency) {
308                 div_start('alloc_tbl');
309                 show_allocatable(false);
310                 div_end();
311         }
312
313         start_table(TABLESTYLE, "width=60%");
314         amount_row(_("Amount of Discount:"), 'discount');
315         amount_row(_("Amount of Payment:"), 'amount');
316         textarea_row(_("Memo:"), 'memo_', null, 22, 4);
317         end_table(1);
318         
319         if ($bank_currency != $supplier_currency) 
320         {
321                 display_note(_("The amount and discount are in the bank account's currency."), 0, 1);
322         }
323
324         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
325
326 end_form();
327
328 end_page();
329 ?>