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