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