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