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