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