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 = "../..";
15 include_once($path_to_root . "/includes/session.inc");
17 if ($use_popup_windows)
18 $js .= get_js_open_window(900, 500);
19 page(_($help_context = "Customers"), @$_REQUEST['popup'], false, "", $js);
21 include_once($path_to_root . "/includes/date_functions.inc");
22 include_once($path_to_root . "/includes/banking.inc");
23 include_once($path_to_root . "/includes/ui.inc");
24 include_once($path_to_root . "/includes/ui/contacts_view.inc");
26 if (isset($_GET['debtor_no']))
28 $_POST['customer_id'] = $_GET['debtor_no'];
31 $selected_id = get_post('customer_id','');
32 //--------------------------------------------------------------------------------------------
34 function can_process()
36 if (strlen($_POST['CustName']) == 0)
38 display_error(_("The customer name cannot be empty."));
39 set_focus('CustName');
43 if (strlen($_POST['cust_ref']) == 0)
45 display_error(_("The customer short name cannot be empty."));
46 set_focus('cust_ref');
50 if (!check_num('credit_limit', 0))
52 display_error(_("The credit limit must be numeric and not less than zero."));
53 set_focus('credit_limit');
57 if (!check_num('pymt_discount', 0, 100))
59 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
60 set_focus('pymt_discount');
64 if (!check_num('discount', 0, 100))
66 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
67 set_focus('discount');
74 //--------------------------------------------------------------------------------------------
76 function handle_submit(&$selected_id)
78 global $path_to_root, $Ajax, $auto_create_branch;
85 update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
86 $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
87 $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
88 input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
90 update_record_status($_POST['customer_id'], $_POST['inactive'],
91 'debtors_master', 'debtor_no');
93 $Ajax->activate('customer_id'); // in case of status change
94 display_notification(_("Customer has been updated."));
97 { //it is a new customer
100 add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
101 $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
102 $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
103 input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
105 $selected_id = $_POST['customer_id'] = db_insert_id();
107 if (isset($auto_create_branch) && $auto_create_branch == 1)
109 add_branch($selected_id, $_POST['CustName'], $_POST['cust_ref'],
110 $_POST['address'], $_POST['salesman'], $_POST['area'], $_POST['tax_group_id'], '',
111 get_company_pref('default_sales_discount_act'), get_company_pref('debtors_act'), get_company_pref('default_prompt_payment_act'),
112 get_company_pref('default location'), $_POST['address'], 0, 0, get_company_pref('default_ship_via'), $_POST['notes']);
114 $selected_branch = db_insert_id();
116 add_crm_person($_POST['CustName'], $_POST['cust_ref'], '', $_POST['address'],
117 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], '', '');
119 add_crm_contact('cust_branch', 'general', $selected_branch, db_insert_id());
121 commit_transaction();
123 display_notification(_("A new customer has been added."));
125 if (isset($auto_create_branch) && $auto_create_branch == 1)
126 display_notification(_("A default Branch has been automatically created, please check default Branch values by using link below."));
128 $Ajax->activate('_page_body');
131 //--------------------------------------------------------------------------------------------
133 if (isset($_POST['submit']))
135 handle_submit($selected_id);
137 //--------------------------------------------------------------------------------------------
139 if (isset($_POST['delete']))
144 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
146 if (key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no'))
149 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
153 if (key_in_foreign_table($selected_id, 'sales_orders', 'debtor_no'))
156 display_error(_("Cannot delete the customer record because orders have been created against it."));
160 if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
163 display_error(_("Cannot delete this customer because there are branch records set up against it."));
164 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
169 if ($cancel_delete == 0)
170 { //ie not cancelled the delete as a result of above tests
172 delete_customer($selected_id);
174 display_notification(_("Selected customer has been deleted."));
175 unset($_POST['customer_id']);
177 $Ajax->activate('_page_body');
178 } //end if Delete Customer
181 function customer_settings($selected_id)
183 global $SysPrefs, $path_to_root, $auto_create_branch;
187 if (list_updated('customer_id') || !isset($_POST['CustName'])) {
188 $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id'] = '';
189 $_POST['dimension_id'] = 0;
190 $_POST['dimension2_id'] = 0;
191 $_POST['sales_type'] = -1;
192 $_POST['curr_code'] = get_company_currency();
193 $_POST['credit_status'] = -1;
194 $_POST['payment_terms'] = $_POST['notes'] = '';
196 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
197 $_POST['credit_limit'] = price_format($SysPrefs->default_credit_limit());
202 $myrow = get_customer($selected_id);
204 $_POST['CustName'] = $myrow["name"];
205 $_POST['cust_ref'] = $myrow["debtor_ref"];
206 $_POST['address'] = $myrow["address"];
207 $_POST['tax_id'] = $myrow["tax_id"];
208 $_POST['dimension_id'] = $myrow["dimension_id"];
209 $_POST['dimension2_id'] = $myrow["dimension2_id"];
210 $_POST['sales_type'] = $myrow["sales_type"];
211 $_POST['curr_code'] = $myrow["curr_code"];
212 $_POST['credit_status'] = $myrow["credit_status"];
213 $_POST['payment_terms'] = $myrow["payment_terms"];
214 $_POST['discount'] = percent_format($myrow["discount"] * 100);
215 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
216 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
217 $_POST['notes'] = $myrow["notes"];
218 $_POST['inactive'] = $myrow["inactive"];
221 start_outer_table(TABLESTYLE2);
223 table_section_title(_("Name and Address"));
225 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
226 text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
227 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
229 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
232 if (!$selected_id || is_new_customer($selected_id))
234 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
238 label_row(_("Customer's Currency:"), $_POST['curr_code']);
239 hidden('curr_code', $_POST['curr_code']);
241 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
245 record_status_list_row(_("Customer status:"), 'inactive');
246 if (!@$_REQUEST['popup'])
249 echo '<td class="label"> </td><td>';
250 echo viewer_link('<b>'. _('Customer Transactions').'</b>', "sales/inquiry/customer_inquiry.php?customer_id=$selected_id&popup=1");
255 elseif (isset($auto_create_branch) && $auto_create_branch == 1)
257 table_section_title(_("Branch"));
258 text_row(_("Phone:"), 'phone', null, 32, 30);
259 text_row(_("Secondary Phone Number:"), 'phone2', null, 32, 30);
260 text_row(_("Fax Number:"), 'fax', null, 32, 30);
261 email_row(_("E-mail:"), 'email', null, 35, 55);
265 table_section_title(_("Sales"));
267 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
268 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
269 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
271 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
272 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
273 $dim = get_company_pref('use_dimension');
275 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
277 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
279 hidden('dimension_id', 0);
281 hidden('dimension2_id', 0);
285 echo '<td class="label">'._('Customer branches').':</td>';
286 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
287 '<b>'. (@$_REQUEST['popup'] ? _("Select or &Add") : _("&Add or Edit ")).'</b>',
288 "debtor_no=".$selected_id.(@$_REQUEST['popup'] ? '&popup=1':''));
292 textarea_row(_("General Notes:"), 'notes', null, 35, 5);
293 if (!$selected_id && isset($auto_create_branch) && $auto_create_branch == 1)
295 table_section_title(_("Branch"));
296 sales_persons_list_row( _("Sales Person:"), 'salesman', null);
297 sales_areas_list_row( _("Sales Area:"), 'area', null);
298 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
302 div_start('controls');
305 submit_center('submit', _("Add New Customer"), true, '', 'default');
309 submit_center_first('submit', _("Update Customer"),
310 _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
311 submit_return('select', $selected_id, _("Select this customer and return to document entry."));
312 submit_center_last('delete', _("Delete Customer"),
313 _('Delete customer data if have been never used'), true);
318 //--------------------------------------------------------------------------------------------
320 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
324 if (db_has_customers())
326 start_table(TABLESTYLE_NOBORDER);
328 customer_list_cells(_("Select a customer: "), 'customer_id', null,
329 _('New customer'), true, check_value('show_inactive'));
330 check_cells(_("Show inactive:"), 'show_inactive', null, true);
334 if (get_post('_show_inactive_update')) {
335 $Ajax->activate('customer_id');
336 set_focus('customer_id');
341 hidden('customer_id');
345 unset($_POST['_tabs_sel']); // force settings tab for new customer
347 tabbed_content_start('tabs', array(
348 'settings' => array(_('&General settings'), $selected_id),
349 'contacts' => array(_('&Contacts'), $selected_id),
352 switch (get_post('_tabs_sel')) {
355 customer_settings($selected_id);
358 $contacts = new contacts('contacts', $selected_id, 'customer');
364 tabbed_content_end();
366 hidden('popup', @$_REQUEST['popup']);