32f4020b5f3765f6839343e2d68b3d781a50223b
[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/purchasing_db.inc");
21 include_once($path_to_root . "/reporting/includes/reporting.inc");
22
23 $js = "";
24 if ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 if (user_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 //----------------------------------------------------------------------------------------
61
62 if (!isset($_POST['bank_account'])) { // first page call
63         $_SESSION['alloc'] = new allocation(ST_SUPPAYMENT, 0, get_post('supplier_id'));
64
65         if (isset($_GET['PInvoice'])) {
66                 $supp = isset($_POST['supplier_id']) ? $_POST['supplier_id'] : null;
67                 //  get date and supplier
68                 $inv = get_supp_trans($_GET['PInvoice'], $_GET['trans_type'], $supp);
69                 if ($inv) {
70                         $_SESSION['alloc']->person_id = $_POST['supplier_id'] = $inv['supplier_id'];
71                         $_SESSION['alloc']->read();
72                         $_POST['DatePaid'] = sql2date($inv['tran_date']);
73                         $_POST['memo_'] = $inv['supp_reference'];
74                         foreach($_SESSION['alloc']->allocs as $line => $trans) {
75                                 if ($trans->type == $_GET['trans_type'] && $trans->type_no == $_GET['PInvoice']) {
76                                         $un_allocated = abs($trans->amount) - $trans->amount_allocated;
77                                         $_SESSION['alloc']->amount = $_SESSION['alloc']->allocs[$line]->current_allocated = $un_allocated;
78                                         $_POST['amount'] = $_POST['amount'.$line] = price_format($un_allocated);
79                                         break;
80                                 }
81                         }
82                         unset($inv);
83                 } else
84                         display_error(_("Invalid purchase invoice number."));
85         }
86 }
87 if (isset($_GET['AddedID'])) {
88         $payment_id = $_GET['AddedID'];
89
90         display_notification_centered( _("Payment has been sucessfully entered"));
91
92         submenu_print(_("&Print This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, 'prtopt');
93         submenu_print(_("&Email This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, null, 1);
94
95         submenu_view(_("View this Payment"), ST_SUPPAYMENT, $payment_id);
96     display_note(get_gl_view_str(ST_SUPPAYMENT, $payment_id, _("View the GL &Journal Entries for this Payment")), 0, 1);
97
98         submenu_option(_("Enter another supplier &payment"), "/purchasing/supplier_payment.php?supplier_id=".$_POST['supplier_id']);
99
100         submenu_option(_("Enter &Supplier Invoice"), "/purchasing/supplier_invoice.php?New=1");
101         submenu_option(_("Enter direct &Invoice"), "/purchasing/po_entry_items.php?NewInvoice=Yes");
102
103         submenu_option(_("Enter Other &Payment"), "/gl/gl_bank.php?NewPayment=Yes");
104         submenu_option(_("Enter &Customer Payment"), "/sales/customer_payments.php");
105         submenu_option(_("Enter Other &Deposit"), "/gl/gl_bank.php?NewDeposit=Yes");
106         submenu_option(_("Bank Account &Transfer"), "/gl/bank_transfer.php");
107
108         display_footer_exit();
109 }
110
111 //----------------------------------------------------------------------------------------
112
113 function get_default_supplier_payment_bank_account($supplier_id, $date)
114 {
115         $previous_payment = get_supp_payment_before($supplier_id, date2sql($date));
116         if ($previous_payment)
117         {
118                 return $previous_payment['bank_id'];
119         }
120         return get_default_supplier_bank_account($supplier_id);
121 }
122 //----------------------------------------------------------------------------------------
123
124 function check_inputs()
125 {
126         global $Refs;
127
128         if (!get_post('supplier_id')) 
129         {
130                 display_error(_("There is no supplier selected."));
131                 set_focus('supplier_id');
132                 return false;
133         } 
134         
135         if (@$_POST['amount'] == "") 
136         {
137                 $_POST['amount'] = price_format(0);
138         }
139
140         if (!check_num('amount', 0))
141         {
142                 display_error(_("The entered amount is invalid or less than zero."));
143                 set_focus('amount');
144                 return false;
145         }
146
147         if (isset($_POST['charge']) && !check_num('charge', 0)) {
148                 display_error(_("The entered amount is invalid or less than zero."));
149                 set_focus('charge');
150                 return false;
151         }
152
153         if (isset($_POST['charge']) && input_num('charge') > 0) {
154                 $charge_acct = get_bank_charge_account($_POST['bank_account']);
155                 if (get_gl_account($charge_acct) == false) {
156                         display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
157                         set_focus('charge');
158                         return false;
159                 }       
160         }
161
162         if (@$_POST['discount'] == "") 
163         {
164                 $_POST['discount'] = 0;
165         }
166
167         if (!check_num('discount', 0))
168         {
169                 display_error(_("The entered discount is invalid or less than zero."));
170                 set_focus('amount');
171                 return false;
172         }
173
174         //if (input_num('amount') - input_num('discount') <= 0) 
175         if (input_num('amount') <= 0) 
176         {
177                 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
178                 set_focus('amount');
179                 return false;
180         }
181
182         if (isset($_POST['bank_amount']) && input_num('bank_amount')<=0)
183         {
184                 display_error(_("The entered bank amount is zero or negative."));
185                 set_focus('bank_amount');
186                 return false;
187         }
188
189
190         if (!is_date($_POST['DatePaid']))
191         {
192                 display_error(_("The entered date is invalid."));
193                 set_focus('DatePaid');
194                 return false;
195         } 
196         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
197         {
198                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
199                 set_focus('DatePaid');
200                 return false;
201         }
202
203         $limit = get_bank_account_limit($_POST['bank_account'], $_POST['DatePaid']);
204
205         if (($limit !== null) && (floatcmp($limit, input_num('amount')) < 0))
206         {
207                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
208                 set_focus('amount');
209                 return false;
210         }
211
212         if (!check_reference($_POST['ref'], ST_SUPPAYMENT))
213         {
214                 set_focus('ref');
215                 return false;
216         }
217
218         if (!db_has_currency_rates(get_supplier_currency($_POST['supplier_id']), $_POST['DatePaid'], true))
219                 return false;
220
221         $_SESSION['alloc']->amount = -input_num('amount');
222
223         if (isset($_POST["TotalNumberOfAllocs"]))
224                 return check_allocations();
225         else
226                 return true;
227 }
228
229 //----------------------------------------------------------------------------------------
230
231 function handle_add_payment()
232 {
233         $payment_id = write_supp_payment(0, $_POST['supplier_id'], $_POST['bank_account'],
234                 $_POST['DatePaid'], $_POST['ref'], input_num('amount'), input_num('discount'), $_POST['memo_'], 
235                 input_num('charge'), input_num('bank_amount', input_num('amount')), $_POST['dimension_id'], $_POST['dimension2_id']);
236         new_doc_date($_POST['DatePaid']);
237
238         $_SESSION['alloc']->trans_no = $payment_id;
239         $_SESSION['alloc']->date_ = $_POST['DatePaid'];
240         $_SESSION['alloc']->write();
241
242         unset($_POST['bank_account']);
243         unset($_POST['DatePaid']);
244         unset($_POST['currency']);
245         unset($_POST['memo_']);
246         unset($_POST['amount']);
247         unset($_POST['discount']);
248         unset($_POST['ProcessSuppPayment']);
249
250         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
251 }
252
253 //----------------------------------------------------------------------------------------
254
255 if (isset($_POST['ProcessSuppPayment']))
256 {
257          /*First off  check for valid inputs */
258     if (check_inputs() == true) 
259     {
260         handle_add_payment();
261         end_page();
262         exit;
263     }
264 }
265
266 //----------------------------------------------------------------------------------------
267
268 start_form();
269
270         start_outer_table(TABLESTYLE2, "width='60%'", 5);
271
272         table_section(1);
273
274     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
275
276         if (list_updated('supplier_id')) {
277                 $_POST['amount'] = price_format(0);
278                 $_SESSION['alloc']->person_id = get_post('supplier_id');
279                 $Ajax->activate('amount');
280         } elseif (list_updated('bank_account'))
281                 $Ajax->activate('alloc_tbl');
282
283         if (list_updated('supplier_id') || list_updated('bank_account')) {
284           $_SESSION['alloc']->read();
285           $_POST['memo_'] = $_POST['amount'] = '';
286           $Ajax->activate('alloc_tbl');
287         }
288
289         set_global_supplier($_POST['supplier_id']);
290
291         if (!list_updated('bank_account') && !get_post('__ex_rate_changed'))
292         {
293                 $_POST['bank_account'] = get_default_supplier_payment_bank_account($_POST['supplier_id'], $_POST['DatePaid']);
294         } else
295         {
296                 $_POST['amount'] = price_format(0);
297         }
298
299     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
300
301         bank_balance_row($_POST['bank_account']);
302
303         table_section(2);
304
305     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
306
307     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT, null, 
308         array('supplier'=>get_post('supplier_id'), 'date'=>get_post('DatePaid'))), false, ST_SUPPAYMENT);
309
310
311         table_section(3);
312
313         $comp_currency = get_company_currency();
314         $supplier_currency = $_SESSION['alloc']->set_person($_POST['supplier_id'], PT_SUPPLIER);
315         if (!$supplier_currency)
316                         $supplier_currency = $comp_currency;
317         $_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);
318
319         if ($bank_currency != $supplier_currency) 
320         {
321                 amount_row(_("Bank Amount:"), 'bank_amount', null, '', $bank_currency);
322         }
323
324         amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);
325
326         $row = get_supplier($_POST['supplier_id']);
327         $_POST['dimension_id'] = @$row['dimension_id'];
328         $_POST['dimension2_id'] = @$row['dimension2_id'];
329         $dim = get_company_pref('use_dimension');
330         if ($dim > 0)
331                 dimensions_list_row(_("Dimension").":", 'dimension_id',
332                         null, true, ' ', false, 1, false);
333         else
334                 hidden('dimension_id', 0);
335         if ($dim > 1)
336                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id',
337                         null, true, ' ', false, 2, false);
338         else
339                 hidden('dimension2_id', 0);
340
341         end_outer_table(1);
342
343         div_start('alloc_tbl');
344         show_allocatable(false);
345         div_end();
346
347         start_table(TABLESTYLE, "width='60%'");
348         amount_row(_("Amount of Discount:"), 'discount', null, '', $supplier_currency);
349         amount_row(_("Amount of Payment:"), 'amount', null, '', $supplier_currency);
350         textarea_row(_("Memo:"), 'memo_', null, 22, 4);
351         end_table(1);
352
353         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
354
355 end_form();
356
357 end_page();