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