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