Supplier Payment, Payments, Bank Transfer: fixed account balance limit checks.
[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_no_params($path_to_root . "/purchasing/inquiry/supplier_allocation_inquiry.php?supplier_id=", _("Select Another &Supplier Transaction for Payment"));
99 //    hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
100
101         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Supplier &Payment"), "supplier_id=" . $_POST['supplier_id']);
102
103         display_footer_exit();
104 }
105
106 //----------------------------------------------------------------------------------------
107
108 function check_inputs()
109 {
110         global $Refs;
111
112         if (!get_post('supplier_id')) 
113         {
114                 display_error(_("There is no supplier selected."));
115                 set_focus('supplier_id');
116                 return false;
117         } 
118         
119         if ($_POST['amount'] == "") 
120         {
121                 $_POST['amount'] = price_format(0);
122         }
123
124         if (!check_num('amount', 0))
125         {
126                 display_error(_("The entered amount is invalid or less than zero."));
127                 set_focus('amount');
128                 return false;
129         }
130
131         if (isset($_POST['charge']) && !check_num('charge', 0)) {
132                 display_error(_("The entered amount is invalid or less than zero."));
133                 set_focus('charge');
134                 return false;
135         }
136
137         if (isset($_POST['charge']) && input_num('charge') > 0) {
138                 $charge_acct = get_company_pref('bank_charge_act');
139                 if (get_gl_account($charge_acct) == false) {
140                         display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
141                         set_focus('charge');
142                         return false;
143                 }       
144         }
145
146         if ($_POST['discount'] == "") 
147         {
148                 $_POST['discount'] = 0;
149         }
150
151         if (!check_num('discount', 0))
152         {
153                 display_error(_("The entered discount is invalid or less than zero."));
154                 set_focus('amount');
155                 return false;
156         }
157
158         if (input_num('amount') <= 0) 
159         {
160                 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
161                 set_focus('amount');
162                 return false;
163         }
164
165         if (isset($_POST['bank_amount']) && input_num('bank_amount')<=0)
166         {
167                 display_error(_("The entered bank amount is zero or negative."));
168                 set_focus('bank_amount');
169                 return false;
170         }
171
172
173         if (!is_date($_POST['DatePaid']))
174         {
175                 display_error(_("The entered date is invalid."));
176                 set_focus('DatePaid');
177                 return false;
178         } 
179         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
180         {
181                 display_error(_("The entered date is not in fiscal year."));
182                 set_focus('DatePaid');
183                 return false;
184         }
185
186         $limit = get_bank_account_limit($_POST['bank_account'], $_POST['DatePaid']);
187
188         if ($limit !== null && floatcmp($limit, input_num('amount') < 0))
189         {
190                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
191                 set_focus('amount');
192                 return false;
193         }
194
195     if (!$Refs->is_valid($_POST['ref'])) 
196     {
197                 display_error(_("You must enter a reference."));
198                 set_focus('ref');
199                 return false;
200         }
201
202         if (!is_new_reference($_POST['ref'], ST_SUPPAYMENT)) 
203         {
204                 display_error(_("The entered reference is already in use."));
205                 set_focus('ref');
206                 return false;
207         }
208
209         if (!db_has_currency_rates(get_supplier_currency($_POST['supplier_id']), $_POST['DatePaid'], true))
210                 return false;
211         $_SESSION['alloc']->amount = -input_num('amount');
212
213         if (isset($_POST["TotalNumberOfAllocs"]))
214                 return check_allocations();
215         else
216                 return true;
217 }
218
219 //----------------------------------------------------------------------------------------
220
221 function handle_add_payment()
222 {
223         $payment_id = write_supp_payment(0, $_POST['supplier_id'], $_POST['bank_account'],
224                 $_POST['DatePaid'], $_POST['ref'], input_num('amount'), input_num('discount'), $_POST['memo_'], 
225                 input_num('charge'), input_num('bank_amount', input_num('amount')));
226         new_doc_date($_POST['DatePaid']);
227
228         $_SESSION['alloc']->trans_no = $payment_id;
229         $_SESSION['alloc']->write();
230
231         unset($_POST['bank_account']);
232         unset($_POST['DatePaid']);
233         unset($_POST['currency']);
234         unset($_POST['memo_']);
235         unset($_POST['amount']);
236         unset($_POST['discount']);
237         unset($_POST['ProcessSuppPayment']);
238
239         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
240 }
241
242 //----------------------------------------------------------------------------------------
243
244 if (isset($_POST['ProcessSuppPayment']))
245 {
246          /*First off  check for valid inputs */
247     if (check_inputs() == true) 
248     {
249         handle_add_payment();
250         end_page();
251         exit;
252     }
253 }
254
255 //----------------------------------------------------------------------------------------
256
257 start_form();
258
259         start_outer_table(TABLESTYLE2, "width=60%", 5);
260
261         table_section(1);
262
263     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
264
265         if (list_updated('supplier_id') || list_updated('bank_account')) {
266           $_SESSION['alloc']->read();
267           $_POST['memo_'] = $_POST['amount'] = '';
268           $Ajax->activate('alloc_tbl');
269         }
270
271         set_global_supplier($_POST['supplier_id']);
272
273     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
274
275         bank_balance_row($_POST['bank_account']);
276
277         table_section(2);
278
279     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));
280
281     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
282
283         table_section(3);
284
285         $supplier_currency = get_supplier_currency($_POST['supplier_id']);
286         $bank_currency = get_bank_account_currency($_POST['bank_account']);
287         if ($bank_currency != $supplier_currency) 
288         {
289                 amount_row("Bank Amount:", 'bank_amount', null, '', $bank_currency, 2);
290         }
291
292         amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);
293
294
295         end_outer_table(1);
296
297         div_start('alloc_tbl');
298         display_heading(sprintf(_("Accounts Payable settled in %s:"), $supplier_currency));
299         show_allocatable(false);
300         div_end();
301
302         start_table(TABLESTYLE, "width=60%");
303         amount_row(_("Amount of Discount:"), 'discount', null, '', $supplier_currency);
304         amount_row(_("Amount of Payment:"), 'amount', null, '', $supplier_currency);
305         textarea_row(_("Memo:"), 'memo_', null, 22, 4);
306         end_table(1);
307
308         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
309
310 end_form();
311
312 end_page();
313 ?>