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/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
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();
23 page(_($help_context = "Customers"), false, false, "", $js);
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");
30 if (isset($_GET['debtor_no']))
32 $_POST['customer_id'] = $_GET['debtor_no'];
35 $selected_id = get_post('customer_id','');
36 //--------------------------------------------------------------------------------------------
38 function can_process()
40 if (strlen($_POST['CustName']) == 0)
42 display_error(_("The customer name cannot be empty."));
43 set_focus('CustName');
47 if (strlen($_POST['cust_ref']) == 0)
49 display_error(_("The customer short name cannot be empty."));
50 set_focus('cust_ref');
54 if (!check_num('credit_limit', 0))
56 display_error(_("The credit limit must be numeric and not less than zero."));
57 set_focus('credit_limit');
61 if (!check_num('discount', 0, 100))
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');
71 //--------------------------------------------------------------------------------------------
73 function handle_submit(&$selected_id)
75 global $path_to_root, $Ajax, $SysPrefs;
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'));
90 $Ajax->activate('customer_id'); // in case of status change
91 display_notification(_("Customer has been updated."));
94 { //it is a new customer
96 $selected_id = get_post('customer_id');
98 display_notification(_("A new customer has been added."));
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."));
103 $Ajax->activate('_page_body');
106 //--------------------------------------------------------------------------------------------
108 if (isset($_POST['submit']))
110 handle_submit($selected_id);
112 //--------------------------------------------------------------------------------------------
114 if (isset($_POST['delete']))
119 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
121 if (key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no'))
124 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
128 if (key_in_foreign_table($selected_id, 'sales_orders', 'debtor_no'))
131 display_error(_("Cannot delete the customer record because orders have been created against it."));
135 if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
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";
144 if ($cancel_delete == 0)
145 { //ie not cancelled the delete as a result of above tests
147 delete_customer($selected_id);
149 display_notification(_("Selected customer has been deleted."));
150 unset($_POST['customer_id']);
152 $Ajax->activate('_page_body');
153 } //end if Delete Customer
156 function customer_settings($selected_id)
158 global $SysPrefs, $path_to_root, $page_nested;
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'] = '';
171 $_POST['discount'] = percent_format(0);
172 $_POST['credit_limit'] = price_format($SysPrefs->default_credit_limit());
177 $myrow = get_customer($selected_id);
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"];
195 start_outer_table(TABLESTYLE2);
197 table_section_title(_("Name and Address"));
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);
203 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
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')))
209 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
213 label_row(_("Customer's Currency:"), $_POST['curr_code']);
214 hidden('curr_code', $_POST['curr_code']);
216 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
219 record_status_list_row(_("Customer status:"), 'inactive');
220 elseif (isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
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);
232 table_section_title(_("Sales"));
234 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
235 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
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');
241 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
243 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
245 hidden('dimension_id', 0);
247 hidden('dimension2_id', 0);
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':''));
258 textarea_row(_("General Notes:"), 'notes', null, 35, 5);
259 if (!$selected_id && isset($SysPrefs->auto_create_branch) && $SysPrefs->auto_create_branch == 1)
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);
269 div_start('controls');
270 if (@$_REQUEST['popup']) hidden('popup', 1);
273 submit_center('submit', _("Add New Customer"), true, '', 'default');
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);
286 //--------------------------------------------------------------------------------------------
288 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
292 if (db_has_customers())
294 start_table(TABLESTYLE_NOBORDER);
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);
302 if (get_post('_show_inactive_update')) {
303 $Ajax->activate('customer_id');
304 set_focus('customer_id');
309 hidden('customer_id');
312 //if (!$selected_id || list_updated('customer_id'))
314 unset($_POST['_tabs_sel']); // force settings tab for new customer
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)),
323 switch (get_post('_tabs_sel')) {
326 customer_settings($selected_id);
329 $contacts = new contacts('contacts', $selected_id, 'customer');
333 $_GET['customer_id'] = $selected_id;
334 include_once($path_to_root."/sales/inquiry/customer_inquiry.php");
337 $_GET['customer_id'] = $selected_id;
338 include_once($path_to_root."/sales/inquiry/sales_orders_view.php");
342 tabbed_content_end();