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");
16 page(_($help_context = "Customers"), @$_REQUEST['popup']);
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");
22 if (isset($_GET['debtor_no']))
24 $_POST['customer_id'] = $_GET['debtor_no'];
26 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == "");
27 //--------------------------------------------------------------------------------------------
29 function can_process()
31 if (strlen($_POST['CustName']) == 0)
33 display_error(_("The customer name cannot be empty."));
34 set_focus('CustName');
38 if (strlen($_POST['cust_ref']) == 0)
40 display_error(_("The customer short name cannot be empty."));
41 set_focus('cust_ref');
45 if (!check_num('credit_limit', 0))
47 display_error(_("The credit limit must be numeric and not less than zero."));
48 set_focus('credit_limit');
52 if (!check_num('pymt_discount', 0, 100))
54 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
55 set_focus('pymt_discount');
59 if (!check_num('discount', 0, 100))
61 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
62 set_focus('discount');
69 //--------------------------------------------------------------------------------------------
71 function handle_submit()
73 global $path_to_root, $new_customer, $Ajax;
78 if ($new_customer == false)
80 update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
81 $_POST['tax_id'], $_POST['curr_code'], $_POST['email'], $_POST['dimension_id'], $_POST['dimension2_id'],
82 $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
83 input_num('credit_limit'), $_POST['sales_type'], $_POST['notes'], $_POST['rep_lang']);
85 update_record_status($_POST['customer_id'], $_POST['inactive'],
86 'debtors_master', 'debtor_no');
88 $Ajax->activate('customer_id'); // in case of status change
89 display_notification(_("Customer has been updated."));
92 { //it is a new customer
95 add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
96 $_POST['tax_id'], $_POST['curr_code'], $_POST['email'], $_POST['dimension_id'], $_POST['dimension2_id'],
97 $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
98 input_num('credit_limit'), $_POST['sales_type'], $_POST['notes'], $_POST['rep_lang']);
100 $_POST['customer_id'] = db_insert_id();
101 $new_customer = false;
102 commit_transaction();
104 display_notification(_("A new customer has been added."));
106 $Ajax->activate('_page_body');
109 //--------------------------------------------------------------------------------------------
111 if (isset($_POST['submit']))
115 //--------------------------------------------------------------------------------------------
117 if (isset($_POST['delete']))
120 //the link to delete a selected record was clicked instead of the submit button
124 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
125 $sel_id = db_escape($_POST['customer_id']);
127 if (key_in_foreign_table($sel_id, 'debtor_trans', 'debtor_no', true))
130 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
134 if (key_in_foreign_table($sel_id, 'sales_orders', 'debtor_no', true))
137 display_error(_("Cannot delete the customer record because orders have been created against it."));
141 if (key_in_foreign_table($sel_id, 'cust_branch', 'debtor_no', true))
144 display_error(_("Cannot delete this customer because there are branch records set up against it."));
145 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
150 if ($cancel_delete == 0)
151 { //ie not cancelled the delete as a result of above tests
153 delete_customer($sel_id, true);
155 display_notification(_("Selected customer has been deleted."));
156 unset($_POST['customer_id']);
157 $new_customer = true;
158 $Ajax->activate('_page_body');
159 } //end if Delete Customer
162 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
166 if (db_has_customers())
168 start_table("class = 'tablestyle_noborder'");
170 customer_list_cells(_("Select a customer: "), 'customer_id', null,
171 _('New customer'), true, check_value('show_inactive'));
172 check_cells(_("Show inactive:"), 'show_inactive', null, true);
175 if (get_post('_show_inactive_update')) {
176 $Ajax->activate('customer_id');
177 set_focus('customer_id');
182 hidden('customer_id');
187 $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id'] = '';
188 $_POST['dimension_id'] = 0;
189 $_POST['dimension2_id'] = 0;
190 $_POST['sales_type'] = -1;
191 $_POST['rep_lang'] = '';
192 $_POST['email'] = '';
193 $_POST['curr_code'] = get_company_currency();
194 $_POST['credit_status'] = -1;
195 $_POST['payment_terms'] = $_POST['notes'] = '';
197 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
198 $_POST['credit_limit'] = price_format($SysPrefs->default_credit_limit());
199 $_POST['inactive'] = 0;
203 $myrow = get_customer($_POST['customer_id']);
205 $_POST['CustName'] = $myrow["name"];
206 $_POST['cust_ref'] = $myrow["debtor_ref"];
207 $_POST['address'] = $myrow["address"];
208 $_POST['tax_id'] = $myrow["tax_id"];
209 $_POST['email'] = $myrow["email"];
210 $_POST['dimension_id'] = $myrow["dimension_id"];
211 $_POST['dimension2_id'] = $myrow["dimension2_id"];
212 $_POST['sales_type'] = $myrow["sales_type"];
213 $_POST['rep_lang'] = $myrow["rep_lang"];
214 $_POST['curr_code'] = $myrow["curr_code"];
215 $_POST['credit_status'] = $myrow["credit_status"];
216 $_POST['payment_terms'] = $myrow["payment_terms"];
217 $_POST['discount'] = percent_format($myrow["discount"] * 100);
218 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
219 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
220 $_POST['notes'] = $myrow["notes"];
221 $_POST['inactive'] = $myrow["inactive"];
224 start_outer_table($table_style2, 5);
226 table_section_title(_("Name and Address"));
228 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
229 text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
230 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
232 email_row(_("E-mail:"), 'email', null, 40, 40);
233 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
238 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
242 label_row(_("Customer's Currency:"), $_POST['curr_code']);
243 hidden('curr_code', $_POST['curr_code']);
245 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
246 languages_list_row( _("Document Language:"), 'rep_lang', $_POST['rep_lang'], _('System default'));
250 table_section_title(_("Sales"));
252 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
253 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
254 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
256 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
257 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
258 $dim = get_company_pref('use_dimension');
260 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
262 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
264 hidden('dimension_id', 0);
266 hidden('dimension2_id', 0);
268 if (!$new_customer) {
270 echo '<td>'._('Customer branches').':</td>';
271 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
272 '<b>'. (@$_REQUEST['popup'] ? _("Select or &Add") : _("&Add or Edit ")).'</b>',
273 "debtor_no=".$_POST['customer_id'].(@$_REQUEST['popup'] ? '&popup=1':''));
278 textarea_row(_("General Notes:"), 'notes', null, 35, 5);
279 record_status_list_row(_("Customer status:"), 'inactive');
282 div_start('controls');
285 submit_center('submit', _("Add New Customer"), true, '', 'default');
289 submit_center_first('submit', _("Update Customer"),
290 _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
291 submit_return('select', get_post('customer_id'), _("Select this customer and return to document entry."));
292 submit_center_last('delete', _("Delete Customer"),
293 _('Delete customer data if have been never used'), true);
296 hidden('popup', @$_REQUEST['popup']);