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