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