Supplier Payment: small fix in fixed ui translation.
[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 if (list_updated('supplier_id')) {
61         $_POST['amount'] = price_format(0);
62         $_SESSION['alloc']->person_id = get_post('supplier_id');
63         $Ajax->activate('amount');
64 } elseif (list_updated('bank_account'))
65         $Ajax->activate('alloc_tbl');
66
67 //----------------------------------------------------------------------------------------
68
69 if (!isset($_POST['bank_account'])) { // first page call
70         $_SESSION['alloc'] = new allocation(ST_SUPPAYMENT, 0, get_post('supplier_id'));
71
72         if (isset($_GET['PInvoice'])) {
73                 //  get date and supplier
74                 $inv = get_supp_trans($_GET['PInvoice'], ST_SUPPINVOICE);
75                 if($inv) {
76                         $_POST['supplier_id'] = $inv['supplier_id'];
77                         $_POST['DatePaid'] = sql2date($inv['tran_date']);
78                         $_POST['memo_'] = $inv['supp_reference'];
79                         foreach($_SESSION['alloc']->allocs as $line => $trans) {
80                                 if ($trans->type == ST_SUPPINVOICE && $trans->type_no == $_GET['PInvoice']) {
81                                         $un_allocated = abs($trans->amount) - $trans->amount_allocated;
82                                         $_POST['amount'] = $_SESSION['alloc']->amount =
83                                                 $_SESSION['alloc']->allocs[$line]->current_allocated = price_format($un_allocated);
84                                         break;
85                                 }
86                         }
87                         unset($inv);
88                 } else
89                         display_error(_("Invalid purchase invoice number."));
90         }
91 }
92 if (isset($_GET['AddedID'])) {
93         $payment_id = $_GET['AddedID'];
94
95         display_notification_centered( _("Payment has been sucessfully entered"));
96
97         submenu_print(_("&Print This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, 'prtopt');
98         submenu_print(_("&Email This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, null, 1);
99
100         submenu_view(_("View this Payment"), ST_SUPPAYMENT, $payment_id);
101     display_note(get_gl_view_str(ST_SUPPAYMENT, $payment_id, _("View the GL &Journal Entries for this Payment")), 0, 1);
102
103         submenu_option(_("Enter another supplier &payment"), "/purchasing/supplier_payment.php?supplier_id=".$_POST['supplier_id']);
104         submenu_option(_("Enter Other &Payment"), "/gl/gl_bank.php?NewPayment=Yes");
105         submenu_option(_("Enter &Customer Payment"), "/sales/customer_payments.php");
106         submenu_option(_("Enter Other &Deposit"), "/gl/gl_bank.php?NewDeposit=Yes");
107         submenu_option(_("Bank Account &Transfer"), "/gl/bank_transfer.php");
108
109         display_footer_exit();
110 }
111
112 //----------------------------------------------------------------------------------------
113
114 function check_inputs()
115 {
116         global $Refs;
117
118         if (!get_post('supplier_id')) 
119         {
120                 display_error(_("There is no supplier selected."));
121                 set_focus('supplier_id');
122                 return false;
123         } 
124         
125         if (@$_POST['amount'] == "") 
126         {
127                 $_POST['amount'] = price_format(0);
128         }
129
130         if (!check_num('amount', 0))
131         {
132                 display_error(_("The entered amount is invalid or less than zero."));
133                 set_focus('amount');
134                 return false;
135         }
136
137         if (isset($_POST['charge']) && !check_num('charge', 0)) {
138                 display_error(_("The entered amount is invalid or less than zero."));
139                 set_focus('charge');
140                 return false;
141         }
142
143         if (isset($_POST['charge']) && input_num('charge') > 0) {
144                 $charge_acct = get_company_pref('bank_charge_act');
145                 if (get_gl_account($charge_acct) == false) {
146                         display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
147                         set_focus('charge');
148                         return false;
149                 }       
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 (isset($_POST['bank_amount']) && input_num('bank_amount')<=0)
173         {
174                 display_error(_("The entered bank amount is zero or negative."));
175                 set_focus('bank_amount');
176                 return false;
177         }
178
179
180         if (!is_date($_POST['DatePaid']))
181         {
182                 display_error(_("The entered date is invalid."));
183                 set_focus('DatePaid');
184                 return false;
185         } 
186         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
187         {
188                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
189                 set_focus('DatePaid');
190                 return false;
191         }
192
193         $limit = get_bank_account_limit($_POST['bank_account'], $_POST['DatePaid']);
194
195         if (($limit !== null) && (floatcmp($limit, input_num('amount')) < 0))
196         {
197                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
198                 set_focus('amount');
199                 return false;
200         }
201
202     if (!$Refs->is_valid($_POST['ref'])) 
203     {
204                 display_error(_("You must enter a reference."));
205                 set_focus('ref');
206                 return false;
207         }
208
209         if (!is_new_reference($_POST['ref'], ST_SUPPAYMENT)) 
210         {
211                 display_error(_("The entered reference is already in use."));
212                 set_focus('ref');
213                 return false;
214         }
215
216         if (!db_has_currency_rates(get_supplier_currency($_POST['supplier_id']), $_POST['DatePaid'], 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 function handle_add_payment()
230 {
231         $payment_id = write_supp_payment(0, $_POST['supplier_id'], $_POST['bank_account'],
232                 $_POST['DatePaid'], $_POST['ref'], input_num('amount'), input_num('discount'), $_POST['memo_'], 
233                 input_num('charge'), input_num('bank_amount', input_num('amount')));
234         new_doc_date($_POST['DatePaid']);
235
236         $_SESSION['alloc']->trans_no = $payment_id;
237         $_SESSION['alloc']->write();
238
239         unset($_POST['bank_account']);
240         unset($_POST['DatePaid']);
241         unset($_POST['currency']);
242         unset($_POST['memo_']);
243         unset($_POST['amount']);
244         unset($_POST['discount']);
245         unset($_POST['ProcessSuppPayment']);
246
247         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
248 }
249
250 //----------------------------------------------------------------------------------------
251
252 if (isset($_POST['ProcessSuppPayment']))
253 {
254          /*First off  check for valid inputs */
255     if (check_inputs() == true) 
256     {
257         handle_add_payment();
258         end_page();
259         exit;
260     }
261 }
262
263 //----------------------------------------------------------------------------------------
264
265 start_form();
266
267         start_outer_table(TABLESTYLE2, "width=60%", 5);
268
269         table_section(1);
270
271     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
272
273         if (list_updated('supplier_id') || list_updated('bank_account')) {
274           $_SESSION['alloc']->read();
275           $_POST['memo_'] = $_POST['amount'] = '';
276           $Ajax->activate('alloc_tbl');
277         }
278
279         set_global_supplier($_POST['supplier_id']);
280
281         if (!list_updated('bank_account') && !get_post('__ex_rate_changed'))
282                 $_POST['bank_account'] = get_default_supplier_bank_account($_POST['supplier_id']);
283         else
284                 $_POST['amount'] = price_format(0);
285
286     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
287
288         bank_balance_row($_POST['bank_account']);
289
290         table_section(2);
291
292     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
293
294     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));
295
296         table_section(3);
297
298         $comp_currency = get_company_currency();
299         $supplier_currency = $_SESSION['alloc']->set_person($_POST['supplier_id'], PT_SUPPLIER);
300         if (!$supplier_currency)
301                         $supplier_currency = $comp_currency;
302         $_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);
303
304         if ($bank_currency != $supplier_currency) 
305         {
306                 amount_row(_("Bank Amount:"), 'bank_amount', null, '', $bank_currency, 2);
307         }
308
309         amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);
310
311
312         end_outer_table(1);
313
314         div_start('alloc_tbl');
315         show_allocatable(false);
316         div_end();
317
318         start_table(TABLESTYLE, "width=60%");
319         amount_row(_("Amount of Discount:"), 'discount', null, '', $supplier_currency);
320         amount_row(_("Amount of Payment:"), 'amount', null, '', $supplier_currency);
321         textarea_row(_("Memo:"), 'memo_', null, 22, 4);
322         end_table(1);
323
324         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
325
326 end_form();
327
328 end_page();
329 ?>