Added Bank Charge field to Customer Payment and Supplier Payment and a new default...
[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/session.inc");
15
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
21 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
22 $js = "";
23 if ($use_popup_windows)
24         $js .= get_js_open_window(900, 500);
25 if ($use_date_picker)
26         $js .= get_js_date_picker();
27 page(_("Supplier Payment Entry"), false, false, "", $js);
28
29
30 if (isset($_GET['supplier_id']))
31 {
32         $_POST['supplier_id'] = $_GET['supplier_id'];
33 }
34
35 //----------------------------------------------------------------------------------------
36
37 check_db_has_suppliers(_("There are no suppliers defined in the system."));
38
39 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
40
41 //----------------------------------------------------------------------------------------
42 if ($ret = context_restore()) {
43         if(isset($ret['supplier_id']))
44                 $_POST['supplier_id'] = $ret['supplier_id'];
45 }
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_') );
50 }
51 if (isset($_POST['_DatePaid_changed'])) {
52   $Ajax->activate('_ex_rate');
53 }
54 //----------------------------------------------------------------------------------------
55
56 if (isset($_GET['AddedID'])) 
57 {
58         $payment_id = $_GET['AddedID'];
59
60         display_notification_centered( _("Payment has been sucessfully entered"));
61
62     display_note(get_gl_view_str(22, $payment_id, _("View the GL &Journal Entries for this Payment")));
63
64     hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
65
66         hyperlink_params($_SERVER['PHP_SELF'], _("Enter another supplier &payment"), "supplier_id=" . $_POST['supplier_id']);
67
68         display_footer_exit();
69 }
70
71 //----------------------------------------------------------------------------------------
72
73 function display_controls()
74 {
75         global $table_style2;
76         start_form();
77
78         if (!isset($_POST['supplier_id']))
79                 $_POST['supplier_id'] = get_global_supplier(false);
80         if (!isset($_POST['DatePaid']))
81         {
82                 $_POST['DatePaid'] = new_doc_date();
83                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
84                         $_POST['DatePaid'] = end_fiscalyear();
85         }               
86         //start_table($table_style2, 5, 7);
87         //echo "<tr><td valign=top>"; // outer table
88         start_outer_table($table_style2, 5);
89
90         //echo "<table>";
91         table_section(1);
92         
93     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
94
95         amount_row(_("Amount of Payment:"), 'amount');
96         amount_row(_("Amount of Discount:"), 'discount');
97         amount_row(_("Bank Charge:"), 'charge');
98
99     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
100
101         table_section(2);
102
103     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
104
105         set_global_supplier($_POST['supplier_id']);
106
107         $supplier_currency = get_supplier_currency($_POST['supplier_id']);
108         $bank_currency = get_bank_account_currency($_POST['bank_account']);
109         if ($bank_currency != $supplier_currency) 
110         {
111                 exchange_rate_display($bank_currency, $supplier_currency, $_POST['DatePaid'], true);
112         }
113
114     ref_row(_("Reference:"), 'ref', '', references::get_next(22));
115
116         textarea_row(_("Memo:"), 'memo_', null, 22, 4);
117
118         end_outer_table(1); // outer table
119
120         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', 'default');
121
122         if ($bank_currency != $supplier_currency) 
123         {
124                 display_note(_("The amount and discount are in the bank account's currency."), 2, 0);
125         }
126
127         end_form();
128 }
129
130 //----------------------------------------------------------------------------------------
131
132 function check_inputs()
133 {
134         if ($_POST['amount'] == "") 
135         {
136                 $_POST['amount'] = price_format(0);
137         }
138
139         if (!check_num('amount', 0))
140         {
141                 display_error(_("The entered amount is invalid or less than zero."));
142                 set_focus('amount');
143                 return false;
144         }
145
146         if (isset($_POST['charge']) && !check_num('charge', 0)) {
147                 display_error(_("The entered amount is invalid or less than zero."));
148                 set_focus('charge');
149                 return false;
150         }
151
152         if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
153         {
154                 display_error(_("The exchange rate must be numeric and greater than zero."));
155                 set_focus('_ex_rate');
156                 return false;
157         }
158
159         if ($_POST['discount'] == "") 
160         {
161                 $_POST['discount'] = 0;
162         }
163
164         if (!check_num('discount', 0))
165         {
166                 display_error(_("The entered discount is invalid or less than zero."));
167                 set_focus('amount');
168                 return false;
169         }
170
171         if (input_num('amount') - input_num('discount') <= 0) 
172         {
173                 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
174                 set_focus('amount');
175                 return false;
176         }
177
178         if (!is_date($_POST['DatePaid']))
179         {
180                 display_error(_("The entered date is invalid."));
181                 set_focus('DatePaid');
182                 return false;
183         } 
184         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
185         {
186                 display_error(_("The entered date is not in fiscal year."));
187                 set_focus('DatePaid');
188                 return false;
189         }
190     if (!references::is_valid($_POST['ref'])) 
191     {
192                 display_error(_("You must enter a reference."));
193                 set_focus('ref');
194                 return false;
195         }
196
197         if (!is_new_reference($_POST['ref'], 22)) 
198         {
199                 display_error(_("The entered reference is already in use."));
200                 set_focus('ref');
201                 return false;
202         }
203
204         return true;
205 }
206
207 //----------------------------------------------------------------------------------------
208
209 function handle_add_payment()
210 {
211         $supp_currency = get_supplier_currency($_POST['supplier_id']);
212         $bank_currency = get_bank_account_currency($_POST['bank_account']);
213         $comp_currency = get_company_currency();
214         if ($comp_currency != $bank_currency && $bank_currency != $supp_currency)
215                 $rate = 0;
216         else
217                 $rate = input_num('_ex_rate');
218
219         $payment_id = add_supp_payment($_POST['supplier_id'], $_POST['DatePaid'],
220                 $_POST['bank_account'], input_num('amount'), input_num('discount'), 
221                 $_POST['ref'], $_POST['memo_'], $rate, input_num('charge'));
222         new_doc_date($_POST['DatePaid']);
223         //unset($_POST['supplier_id']);
224         unset($_POST['bank_account']);
225         unset($_POST['DatePaid']);
226         unset($_POST['currency']);
227         unset($_POST['memo_']);
228         unset($_POST['amount']);
229         unset($_POST['discount']);
230         unset($_POST['ProcessSuppPayment']);
231
232         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
233 }
234
235 //----------------------------------------------------------------------------------------
236
237 if (isset($_POST['ProcessSuppPayment']))
238 {
239          /*First off  check for valid inputs */
240     if (check_inputs() == true) 
241     {
242         handle_add_payment();
243         end_page();
244         exit;
245     }
246 }
247
248 display_controls();
249
250 end_page();
251 ?>