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