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