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