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