Optimizing the F2, F3 and F4 keys in sales/purchase forms.
[fa-stable.git] / sales / manage / customers.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_CUSTOMER';
13 $path_to_root = "../..";
14
15 include_once($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
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 = "Customers"), @$_REQUEST['popup'], false, "", $js); 
24
25 include_once($path_to_root . "/includes/date_functions.inc");
26 include_once($path_to_root . "/includes/banking.inc");
27 include_once($path_to_root . "/includes/ui.inc");
28 include_once($path_to_root . "/includes/ui/contacts_view.inc");
29 include_once($path_to_root . "/includes/ui/attachment.inc");
30
31 if (isset($_GET['debtor_no'])) 
32 {
33         $_POST['customer_id'] = $_GET['debtor_no'];
34 }
35
36 $selected_id = get_post('customer_id','');
37 //--------------------------------------------------------------------------------------------
38
39 function can_process()
40 {
41         if (strlen($_POST['CustName']) == 0) 
42         {
43                 display_error(_("The customer name cannot be empty."));
44                 set_focus('CustName');
45                 return false;
46         } 
47
48         if (strlen($_POST['cust_ref']) == 0) 
49         {
50                 display_error(_("The customer short name cannot be empty."));
51                 set_focus('cust_ref');
52                 return false;
53         } 
54         
55         if (!check_num('credit_limit', 0))
56         {
57                 display_error(_("The credit limit must be numeric and not less than zero."));
58                 set_focus('credit_limit');
59                 return false;           
60         } 
61         
62         if (!check_num('pymt_discount', 0, 100)) 
63         {
64                 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
65                 set_focus('pymt_discount');
66                 return false;           
67         } 
68         
69         if (!check_num('discount', 0, 100)) 
70         {
71                 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
72                 set_focus('discount');
73                 return false;           
74         } 
75
76         return true;
77 }
78
79 //--------------------------------------------------------------------------------------------
80
81 function handle_submit(&$selected_id)
82 {
83         global $path_to_root, $Ajax, $SysPrefs;
84
85         if (!can_process())
86                 return;
87                 
88         if ($selected_id) 
89         {
90                 update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
91                         $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
92                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
93                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
94
95                 update_record_status($_POST['customer_id'], $_POST['inactive'],
96                         'debtors_master', 'debtor_no');
97
98                 $Ajax->activate('customer_id'); // in case of status change
99                 display_notification(_("Customer has been updated."));
100         } 
101         else 
102         {       //it is a new customer
103
104                 begin_transaction();
105                 add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
106                         $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
107                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
108                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
109
110                 $selected_id = $_POST['customer_id'] = db_insert_id();
111          
112                 if (isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
113                 {
114                 add_branch($selected_id, $_POST['CustName'], $_POST['cust_ref'],
115                 $_POST['address'], $_POST['salesman'], $_POST['area'], $_POST['tax_group_id'], '',
116                 get_company_pref('default_sales_discount_act'), get_company_pref('debtors_act'), get_company_pref('default_prompt_payment_act'),
117                 $_POST['location'], $_POST['address'], 0, $_POST['ship_via'], $_POST['notes'], $_POST['bank_account']);
118                 
119                 $selected_branch = db_insert_id();
120         
121                         add_crm_person($_POST['cust_ref'], $_POST['CustName'], '', $_POST['address'], 
122                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], '', '');
123
124                         $pers_id = db_insert_id();
125                         add_crm_contact('cust_branch', 'general', $selected_branch, $pers_id);
126
127                         add_crm_contact('customer', 'general', $selected_id, $pers_id);
128                 }
129                 commit_transaction();
130
131                 display_notification(_("A new customer has been added."));
132
133                 if (isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
134                         display_notification(_("A default Branch has been automatically created, please check default Branch values by using link below."));
135                 
136                 $Ajax->activate('_page_body');
137         }
138 }
139 //--------------------------------------------------------------------------------------------
140
141 if (isset($_POST['submit'])) 
142 {
143         handle_submit($selected_id);
144 }
145 //-------------------------------------------------------------------------------------------- 
146
147 if (isset($_POST['delete'])) 
148 {
149
150         $cancel_delete = 0;
151
152         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
153
154         if (key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no'))
155         {
156                 $cancel_delete = 1;
157                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
158         } 
159         else 
160         {
161                 if (key_in_foreign_table($selected_id, 'sales_orders', 'debtor_no'))
162                 {
163                         $cancel_delete = 1;
164                         display_error(_("Cannot delete the customer record because orders have been created against it."));
165                 } 
166                 else 
167                 {
168                         if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
169                         {
170                                 $cancel_delete = 1;
171                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
172                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
173                         }
174                 }
175         }
176         
177         if ($cancel_delete == 0) 
178         {       //ie not cancelled the delete as a result of above tests
179         
180                 delete_customer($selected_id);
181
182                 display_notification(_("Selected customer has been deleted."));
183                 unset($_POST['customer_id']);
184                 $selected_id = '';
185                 $Ajax->activate('_page_body');
186         } //end if Delete Customer
187 }
188
189 function customer_settings($selected_id) 
190 {
191         global $SysPrefs, $path_to_root, $page_nested;
192         
193         if (!$selected_id) 
194         {
195                 if (list_updated('customer_id') || !isset($_POST['CustName'])) {
196                         $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id']  = '';
197                         $_POST['dimension_id'] = 0;
198                         $_POST['dimension2_id'] = 0;
199                         $_POST['sales_type'] = -1;
200                         $_POST['curr_code']  = get_company_currency();
201                         $_POST['credit_status']  = -1;
202                         $_POST['payment_terms']  = $_POST['notes']  = '';
203
204                         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
205                         $_POST['credit_limit']  = price_format($SysPrefs->default_credit_limit());
206                 }
207         }
208         else 
209         {
210                 $myrow = get_customer($selected_id);
211
212                 $_POST['CustName'] = $myrow["name"];
213                 $_POST['cust_ref'] = $myrow["debtor_ref"];
214                 $_POST['address']  = $myrow["address"];
215                 $_POST['tax_id']  = $myrow["tax_id"];
216                 $_POST['dimension_id']  = $myrow["dimension_id"];
217                 $_POST['dimension2_id']  = $myrow["dimension2_id"];
218                 $_POST['sales_type'] = $myrow["sales_type"];
219                 $_POST['curr_code']  = $myrow["curr_code"];
220                 $_POST['credit_status']  = $myrow["credit_status"];
221                 $_POST['payment_terms']  = $myrow["payment_terms"];
222                 $_POST['discount']  = percent_format($myrow["discount"] * 100);
223                 $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
224                 $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
225                 $_POST['notes']  = $myrow["notes"];
226                 $_POST['inactive'] = $myrow["inactive"];
227         }
228
229         start_outer_table(TABLESTYLE2);
230         table_section(1);
231         table_section_title(_("Name and Address"));
232
233         text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
234         text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
235         textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
236
237         text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
238
239
240         if (!$selected_id || is_new_customer($selected_id) || (!key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no') &&
241                 !key_in_foreign_table($selected_id, 'sales_orders', 'debtor_no'))) 
242         {
243                 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
244         } 
245         else 
246         {
247                 label_row(_("Customer's Currency:"), $_POST['curr_code']);
248                 hidden('curr_code', $_POST['curr_code']);                               
249         }
250         sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
251
252         if($selected_id)
253                 record_status_list_row(_("Customer status:"), 'inactive');
254         elseif (isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
255         {
256                 table_section_title(_("Branch"));
257                 text_row(_("Phone:"), 'phone', null, 32, 30);
258                 text_row(_("Secondary Phone Number:"), 'phone2', null, 32, 30);
259                 text_row(_("Fax Number:"), 'fax', null, 32, 30);
260                 email_row(_("E-mail:"), 'email', null, 35, 55);
261                 text_row(_("Bank Account Number:"), 'bank_account', null, 30, 60);
262                 sales_persons_list_row( _("Sales Person:"), 'salesman', null);
263         }
264         table_section(2);
265
266         table_section_title(_("Sales"));
267
268         percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
269         percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
270         amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
271
272         payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
273         credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
274         $dim = get_company_pref('use_dimension');
275         if ($dim >= 1)
276                 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
277         if ($dim > 1)
278                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
279         if ($dim < 1)
280                 hidden('dimension_id', 0);
281         if ($dim < 2)
282                 hidden('dimension2_id', 0);
283
284         if ($selected_id)  {
285                 start_row();
286                 echo '<td class="label">'._('Customer branches').':</td>';
287                 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
288                         '<b>'. ($page_nested ?  _("Select or &Add") : _("&Add or Edit ")).'</b>', 
289                         "debtor_no=".$selected_id.($page_nested ? '&popup=1':''));
290                 end_row();
291         }
292
293         textarea_row(_("General Notes:"), 'notes', null, 35, 5);
294         if (!$selected_id && isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
295         {
296                 table_section_title(_("Branch"));
297                 locations_list_row(_("Default Inventory Location:"), 'location');
298                 shippers_list_row(_("Default Shipping Company:"), 'ship_via');
299                 sales_areas_list_row( _("Sales Area:"), 'area', null);
300                 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
301         }
302         end_outer_table(1);
303
304         div_start('controls');
305         if (@$_REQUEST['popup']) hidden('popup', 1);
306         if (!$selected_id)
307         {
308                 submit_center('submit', _("Add New Customer"), true, '', false);
309         } 
310         else 
311         {
312                 submit_center_first('submit', _("Update Customer"), 
313                   _('Update customer data'), $page_nested ? true : false);
314                 submit_return('select', $selected_id, _("Select this customer and return to document entry."));
315                 submit_center_last('delete', _("Delete Customer"), 
316                   _('Delete customer data if have been never used'), true);
317         }
318         div_end();
319 }
320
321 //--------------------------------------------------------------------------------------------
322
323 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
324  
325 start_form(true);
326
327 if (db_has_customers()) 
328 {
329         start_table(TABLESTYLE_NOBORDER);
330         start_row();
331         customer_list_cells(_("Select a customer: "), 'customer_id', null,
332                 _('New customer'), true, check_value('show_inactive'));
333         check_cells(_("Show inactive:"), 'show_inactive', null, true);
334         end_row();
335         end_table();
336
337         if (get_post('_show_inactive_update')) {
338                 $Ajax->activate('customer_id');
339                 set_focus('customer_id');
340         }
341
342 else 
343 {
344         hidden('customer_id');
345 }
346
347 //if (!$selected_id || list_updated('customer_id'))
348 if (!$selected_id)
349         unset($_POST['_tabs_sel']); // force settings tab for new customer
350
351 tabbed_content_start('tabs', array(
352                 'settings' => array(_('&General settings'), $selected_id),
353                 'contacts' => array(_('&Contacts'), $selected_id),
354                 'transactions' => array(_('&Transactions'), (user_check_access('SA_SALESTRANSVIEW') ? $selected_id : null)),
355                 'orders' => array(_('Sales &Orders'), (user_check_access('SA_SALESTRANSVIEW') ? $selected_id : null)),
356                 'attachments' => array(_('Attachments'), (user_check_access('SA_ATTACHDOCUMENT') ? $selected_id : null)),
357         ));
358         
359         switch (get_post('_tabs_sel')) {
360                 default:
361                 case 'settings':
362                         customer_settings($selected_id); 
363                         break;
364                 case 'contacts':
365                         $contacts = new contacts('contacts', $selected_id, 'customer');
366                         $contacts->show();
367                         break;
368                 case 'transactions':
369                         $_GET['customer_id'] = $selected_id;
370                         include_once($path_to_root."/sales/inquiry/customer_inquiry.php");
371                         break;
372                 case 'orders':
373                         $_GET['customer_id'] = $selected_id;
374                         include_once($path_to_root."/sales/inquiry/sales_orders_view.php");
375                         break;
376                 case 'attachments':
377                         $_GET['trans_no'] = $selected_id;
378                         $_GET['type_no']= ST_CUSTOMER;
379                         $attachments = new attachments('attachment', $selected_id, 'customers');
380                         $attachments->show();
381         };
382 br();
383 tabbed_content_end();
384
385 end_form();
386 end_page(@$_REQUEST['popup']);
387