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