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