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