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 ***********************************************************************/
14 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");
21 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
23 if ($use_popup_windows)
24 $js .= get_js_open_window(900, 500);
26 $js .= get_js_date_picker();
27 page(_("Supplier Payment Entry"), false, false, "", $js);
30 if (isset($_GET['supplier_id']))
32 $_POST['supplier_id'] = $_GET['supplier_id'];
35 //----------------------------------------------------------------------------------------
37 check_db_has_suppliers(_("There are no suppliers defined in the system."));
39 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
41 //----------------------------------------------------------------------------------------
42 if ($ret = context_restore()) {
43 if(isset($ret['supplier_id']))
44 $_POST['supplier_id'] = $ret['supplier_id'];
46 if (isset($_POST['_supplier_id_editor'])) {
47 context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'],
48 array( 'supplier_id', 'bank_account', 'DatePaid', 'ref', 'amount',
49 'discount', 'memo_') );
51 if (isset($_POST['_DatePaid_changed'])) {
52 $Ajax->activate('_ex_rate');
54 //----------------------------------------------------------------------------------------
56 if (isset($_GET['AddedID']))
58 $payment_id = $_GET['AddedID'];
60 display_notification_centered( _("Payment has been sucessfully entered"));
62 display_note(get_gl_view_str(22, $payment_id, _("View the GL &Journal Entries for this Payment")));
64 hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
66 hyperlink_params($_SERVER['PHP_SELF'], _("Enter another supplier &payment"), "supplier_id=" . $_POST['supplier_id']);
68 display_footer_exit();
71 //----------------------------------------------------------------------------------------
73 function display_controls()
76 start_form(false, true);
78 if (!isset($_POST['supplier_id']))
79 $_POST['supplier_id'] = get_global_supplier(false);
80 if (!isset($_POST['DatePaid']))
82 $_POST['DatePaid'] = Today();
83 if (!is_date_in_fiscalyear($_POST['DatePaid']))
84 $_POST['DatePaid'] = end_fiscalyear();
86 start_outer_table($table_style2, 5);
90 bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
92 amount_row(_("Amount of Payment:"), 'amount');
93 amount_row(_("Amount of Discount:"), 'discount');
95 date_row(_("Date Paid") . ":", 'DatePaid', '', null, 0, 0, 0, null, true);
99 supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
101 set_global_supplier($_POST['supplier_id']);
103 $comp_currency = get_company_currency();
104 $supplier_currency = get_supplier_currency($_POST['supplier_id']);
105 $bank_currency = get_bank_account_currency($_POST['bank_account']);
106 if ($bank_currency != $supplier_currency)
108 exchange_rate_display($bank_currency, $supplier_currency, $_POST['DatePaid'], ($bank_currency == $comp_currency));
111 ref_row(_("Reference:"), 'ref', '', references::get_next(22));
113 text_row(_("Memo:"), 'memo_', null, 52,50);
115 end_outer_table(1); // outer table
117 submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', true);
119 if ($bank_currency != $supplier_currency)
121 display_note(_("The amount and discount are in the bank account's currency."), 2, 0);
127 //----------------------------------------------------------------------------------------
129 function check_inputs()
131 if ($_POST['amount'] == "")
133 $_POST['amount'] = price_format(0);
136 if (!check_num('amount', 0))
138 display_error(_("The entered amount is invalid or less than zero."));
143 if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
145 display_error(_("The exchange rate must be numeric and greater than zero."));
146 set_focus('_ex_rate');
150 if ($_POST['discount'] == "")
152 $_POST['discount'] = 0;
155 if (!check_num('discount', 0))
157 display_error(_("The entered discount is invalid or less than zero."));
162 if (input_num('amount') - input_num('discount') <= 0)
164 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
169 if (!is_date($_POST['DatePaid']))
171 display_error(_("The entered date is invalid."));
172 set_focus('DatePaid');
175 elseif (!is_date_in_fiscalyear($_POST['DatePaid']))
177 display_error(_("The entered date is not in fiscal year."));
178 set_focus('DatePaid');
181 if (!references::is_valid($_POST['ref']))
183 display_error(_("You must enter a reference."));
188 if (!is_new_reference($_POST['ref'], 22))
190 display_error(_("The entered reference is already in use."));
198 //----------------------------------------------------------------------------------------
200 function handle_add_payment()
202 $supp_currency = get_supplier_currency($_POST['supplier_id']);
203 $bank_currency = get_bank_account_currency($_POST['bank_account']);
204 $comp_currency = get_company_currency();
205 if ($comp_currency != $bank_currency && $bank_currency != $supp_currency)
208 $rate = input_num('_ex_rate');
210 $payment_id = add_supp_payment($_POST['supplier_id'], $_POST['DatePaid'],
211 $_POST['bank_account'], input_num('amount'), input_num('discount'),
212 $_POST['ref'], $_POST['memo_'], $rate);
214 //unset($_POST['supplier_id']);
215 unset($_POST['bank_account']);
216 unset($_POST['DatePaid']);
217 unset($_POST['currency']);
218 unset($_POST['memo_']);
219 unset($_POST['amount']);
220 unset($_POST['discount']);
221 unset($_POST['ProcessSuppPayment']);
223 meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
226 //----------------------------------------------------------------------------------------
228 if (isset($_POST['ProcessSuppPayment']))
230 /*First off check for valid inputs */
231 if (check_inputs() == true)
233 handle_add_payment();