Switch to new access levels system
[fa-stable.git] / purchasing / manage / suppliers.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_SUPPLIER';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Suppliers"), @$_REQUEST['popup']);
17
18 //include($path_to_root . "/includes/date_functions.inc");
19
20 include($path_to_root . "/includes/ui.inc");
21
22 check_db_has_tax_groups(_("There are no tax groups defined in the system. At least one tax group is required before proceeding."));
23
24 if (isset($_GET['supplier_id'])) 
25 {
26         $_POST['supplier_id'] = $_GET['supplier_id'];
27 }
28 $new_supplier = get_post('supplier_id') == ''; 
29
30 if (isset($_POST['submit'])) 
31 {
32
33         //initialise no input errors assumed initially before we test
34         $input_error = 0;
35
36         /* actions to take once the user has clicked the submit button
37         ie the page has called itself with some user input */
38
39         //first off validate inputs sensible
40
41         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
42         {
43                 $input_error = 1;
44                 display_error(_("The supplier name must be entered."));
45                 set_focus('supp_name');
46         }
47
48         if (strlen($_POST['supp_ref']) == 0 || $_POST['supp_ref'] == "") 
49         {
50                 $input_error = 1;
51                 display_error(_("The supplier short name must be entered."));
52                 set_focus('supp_ref');
53         }
54
55         if ($input_error !=1 )
56         {
57
58                 if (!$new_supplier) 
59                 {
60
61                         $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($_POST['supp_name']) . ",
62                                 supp_ref=".db_escape($_POST['supp_ref']) . ",
63                 address=".db_escape($_POST['address']) . ",
64                 supp_address=".db_escape($_POST['supp_address']) . ",
65                 phone=".db_escape($_POST['phone']) . ",
66                 fax=".db_escape($_POST['fax']) . ",
67                 gst_no=".db_escape($_POST['gst_no']) . ",
68                 email=".db_escape($_POST['email']) . ",
69                 website=".db_escape($_POST['website']) . ",
70                 contact=".db_escape($_POST['contact']) . ",
71                 supp_account_no=".db_escape($_POST['supp_account_no']) . ",
72                 bank_account=".db_escape($_POST['bank_account']) . ",
73                 credit_limit=".input_num('credit_limit', 0) . ",
74                 dimension_id=".db_escape($_POST['dimension_id']) . ",
75                 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
76                 curr_code=".db_escape($_POST['curr_code']).",
77                 payment_terms=".db_escape($_POST['payment_terms']) . ",
78                                 payable_account=".db_escape($_POST['payable_account']) . ",
79                                 purchase_account=".db_escape($_POST['purchase_account']) . ",
80                                 payment_discount_account=".db_escape($_POST['payment_discount_account']) . ",
81                 notes=".db_escape($_POST['notes']) . ",
82                                 tax_group_id=".db_escape($_POST['tax_group_id']) . " WHERE supplier_id = '" . $_POST['supplier_id'] . "'";
83
84                         db_query($sql,"The supplier could not be updated");
85                         update_record_status($_POST['supplier_id'], $_POST['inactive'],
86                                 'suppliers', 'supplier_id');
87
88                         $Ajax->activate('supplier_id'); // in case of status change
89                         display_notification(_("Supplier has been updated."));
90                 } 
91                 else 
92                 {
93
94                         $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, supp_ref, address, supp_address, phone, fax, gst_no, email, website,
95                                 contact, supp_account_no, bank_account, credit_limit, dimension_id, dimension2_id, curr_code,
96                                 payment_terms, payable_account, purchase_account, payment_discount_account, notes, tax_group_id)
97                                 VALUES (".db_escape($_POST['supp_name']). ", "
98                                 .db_escape($_POST['supp_ref']). ", "
99                                 .db_escape($_POST['address']) . ", "
100                                 .db_escape($_POST['supp_address']) . ", "
101                                 .db_escape($_POST['phone']). ", "
102                                 .db_escape($_POST['fax']). ", "
103                                 .db_escape($_POST['gst_no']). ", "
104                                 .db_escape($_POST['email']). ", "
105                                 .db_escape($_POST['website']). ", "
106                                 .db_escape($_POST['contact']). ", "
107                                 .db_escape($_POST['supp_account_no']). ", "
108                                 .db_escape($_POST['bank_account']). ", "
109                                 .input_num('credit_limit',0). ", "
110                                 .db_escape($_POST['dimension_id']). ", "
111                                 .db_escape($_POST['dimension2_id']). ", "
112                                 .db_escape($_POST['curr_code']). ", "
113                                 .db_escape($_POST['payment_terms']). ", "
114                                 .db_escape($_POST['payable_account']). ", "
115                                 .db_escape($_POST['purchase_account']). ", "
116                                 .db_escape($_POST['payment_discount_account']). ", "
117                                 .db_escape($_POST['notes']). ", "
118                                 .db_escape($_POST['tax_group_id']). ")";
119
120                         db_query($sql,"The supplier could not be added");
121                         $_POST['supplier_id'] = db_insert_id();
122                         $new_supplier = false;
123                         display_notification(_("A new supplier has been added."));
124                         $Ajax->activate('_page_body');
125                 }
126         }
127
128
129 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
130 {
131         //the link to delete a selected record was clicked instead of the submit button
132
133         $cancel_delete = 0;
134
135         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
136
137         $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_POST['supplier_id'] . "'";
138         $result = db_query($sql,"check failed");
139         $myrow = db_fetch_row($result);
140         if ($myrow[0] > 0) 
141         {
142                 $cancel_delete = 1;
143                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
144
145         } 
146         else 
147         {
148                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id='" . $_POST['supplier_id'] . "'";
149                 $result = db_query($sql,"check failed");
150                 $myrow = db_fetch_row($result);
151                 if ($myrow[0] > 0) 
152                 {
153                         $cancel_delete = 1;
154                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
155                 }
156
157         }
158         if ($cancel_delete == 0) 
159         {
160                 $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id='" . $_POST['supplier_id']. "'";
161                 db_query($sql,"check failed");
162
163                 unset($_SESSION['supplier_id']);
164                 $new_supplier = true;
165                 $Ajax->activate('_page_body');
166         } //end if Delete supplier
167 }
168
169 start_form();
170
171 if (db_has_suppliers()) 
172 {
173         start_table("", 3);
174 //      start_table("class = 'tablestyle_noborder'");
175         start_row();
176         supplier_list_cells(_("Select a supplier: "), 'supplier_id', null,
177                   _('New supplier'), true, check_value('show_inactive'));
178         check_cells(_("Show inactive:"), 'show_inactive', null, true);
179         end_row();
180         end_table();
181         if (get_post('_show_inactive_update')) {
182                 $Ajax->activate('supplier_id');
183                 set_focus('supplier_id');
184         }
185
186 else 
187 {
188         hidden('supplier_id', get_post('supplier_id'));
189 }
190
191 start_outer_table($table_style2, 5);
192
193 table_section(1);
194
195 if (!$new_supplier) 
196 {
197         //SupplierID exists - either passed when calling the form or from the form itself
198         $myrow = get_supplier($_POST['supplier_id']);
199
200         $_POST['supp_name'] = $myrow["supp_name"];
201         $_POST['supp_ref'] = $myrow["supp_ref"];
202         $_POST['address']  = $myrow["address"];
203         $_POST['supp_address']  = $myrow["supp_address"];
204         $_POST['phone']  = $myrow["phone"];
205         $_POST['fax']  = $myrow["fax"];
206         $_POST['gst_no']  = $myrow["gst_no"];
207         $_POST['email']  = $myrow["email"];
208         $_POST['website']  = $myrow["website"];
209         $_POST['contact']  = $myrow["contact"];
210         $_POST['supp_account_no']  = $myrow["supp_account_no"];
211         $_POST['bank_account']  = $myrow["bank_account"];
212         $_POST['dimension_id']  = $myrow["dimension_id"];
213         $_POST['dimension2_id']  = $myrow["dimension2_id"];
214         $_POST['curr_code']  = $myrow["curr_code"];
215         $_POST['payment_terms']  = $myrow["payment_terms"];
216         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
217         $_POST['tax_group_id'] = $myrow["tax_group_id"];
218         $_POST['payable_account']  = $myrow["payable_account"];
219         $_POST['purchase_account']  = $myrow["purchase_account"];
220         $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
221         $_POST['notes']  = $myrow["notes"];
222         $_POST['inactive'] = $myrow["inactive"];
223
224 else 
225 {
226         $_POST['supp_name'] = $_POST['supp_ref'] = $_POST['address'] = $_POST['supp_address'] = $_POST['tax_group_id']  = 
227                 $_POST['website'] = $_POST['supp_account_no'] = $_POST['notes'] = '';
228         $_POST['dimension_id'] = 0;
229         $_POST['dimension2_id'] = 0;
230         $_POST['sales_type'] = -1;
231         $_POST['email'] = $_POST['phone'] = $_POST['fax'] = $_POST['gst_no'] = $_POST['contact'] = $_POST['bank_account'] = '';
232         $_POST['payment_terms']  = '';
233         $_POST['credit_limit']  = price_format(0);
234
235         $company_record = get_company_prefs();
236         $_POST['curr_code']  = $company_record["curr_default"];
237         $_POST['payable_account'] = $company_record["creditors_act"];
238         $_POST['purchase_account'] = $company_record["default_cogs_act"];
239         $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
240         $_POST['inactive'] = 0;
241 }
242
243 table_section_title(_("Name and Contact"));
244
245 text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
246 text_row(_("Supplier Short Name:"), 'supp_ref', null, 30, 30);
247 text_row(_("Contact Person:"), 'contact', null, 42, 40);
248
249 text_row(_("Phone Number:"), 'phone', null, 42, 40);
250 text_row(_("Fax Number:"), 'fax', null, 42, 40);
251
252 email_row(_("E-mail:"), 'email', null, 35, 55);
253 link_row(_("Website:"), 'website', null, 35, 55);
254 text_row(_("Our Customer No:"), 'supp_account_no', null, 42, 40);
255
256 table_section_title(_("Addresses"));
257 textarea_row(_("Mailing Address:"), 'address', null, 35, 5);
258 textarea_row(_("Physical Address:"), 'supp_address', null, 35, 5);
259
260 table_section(2);
261
262 table_section_title(_("Purchasing"));
263 text_row(_("GSTNo:"), 'gst_no', null, 42, 40);
264 text_row(_("Bank Name/Account:"), 'bank_account', null, 42, 40);
265 amount_row(_("Credit Limit:"), 'credit_limit', null);
266 if (!$new_supplier) 
267 {
268         label_row(_("Supplier's Currency:"), $_POST['curr_code']);
269         hidden('curr_code', $_POST['curr_code']);
270
271 else 
272 {
273         currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
274 }
275
276 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
277
278 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
279
280 table_section_title(_("Accounts"));
281
282 gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
283
284 gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
285
286 gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
287
288 $dim = get_company_pref('use_dimension');
289 if ($dim >= 1)
290 {
291         table_section_title(_("Dimension"));
292
293         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
294         if ($dim > 1)
295                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
296 }
297 if ($dim < 1)
298         hidden('dimension_id', 0);
299 if ($dim < 2)
300         hidden('dimension2_id', 0);
301 table_section_title(_("General"));
302 textarea_row(_("General Notes:"), 'notes', null, 35, 5);
303 record_status_list_row(_("Supplier status:"), 'inactive');
304
305 end_outer_table(1);
306
307 div_start('controls');
308 if (!$new_supplier) 
309 {
310         submit_center_first('submit', _("Update Supplier"), 
311           _('Update supplier data'), @$_REQUEST['popup'] ? true : 'default');
312         submit_return('select', get_post('supplier_id'), _("Select this supplier and return to document entry."));
313         submit_center_last('delete', _("Delete Supplier"), 
314           _('Delete supplier data if have been never used'), true);
315 }
316 else 
317 {
318         submit_center('submit', _("Add New Supplier Details"), true, '', 'default');
319 }
320 div_end();
321 hidden('popup', @$_REQUEST['popup']);
322 end_form();
323
324 end_page();
325
326 ?>