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