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(_("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)
81 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($_POST['CustName']) . ",
82 debtor_ref=" . db_escape($_POST['cust_ref']) . ",
83 address=".db_escape($_POST['address']) . ",
84 tax_id=".db_escape($_POST['tax_id']) . ",
85 curr_code=".db_escape($_POST['curr_code']) . ",
86 email=".db_escape($_POST['email']) . ",
87 dimension_id=".db_escape($_POST['dimension_id']) . ",
88 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
89 credit_status=".db_escape($_POST['credit_status']) . ",
90 payment_terms=".db_escape($_POST['payment_terms']) . ",
91 discount=" . input_num('discount') / 100 . ",
92 pymt_discount=" . input_num('pymt_discount') / 100 . ",
93 credit_limit=" . input_num('credit_limit') . ",
94 sales_type = ".db_escape($_POST['sales_type']) . "
95 WHERE debtor_no = '". $_POST['customer_id'] . "'";
97 db_query($sql,"The customer could not be updated");
99 update_record_status($_POST['customer_id'], $_POST['inactive'],
100 'debtors_master', 'debtor_no');
102 $Ajax->activate('customer_id'); // in case of status change
103 display_notification(_("Customer has been updated."));
106 { //it is a new customer
110 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id, email, dimension_id, dimension2_id,
111 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
112 sales_type) VALUES (".db_escape($_POST['CustName']) .", " .db_escape($_POST['cust_ref']) .", "
113 .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
114 .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", "
115 .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ",
116 " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ",
117 " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
119 db_query($sql,"The customer could not be added");
121 $_POST['customer_id'] = db_insert_id();
122 $new_customer = false;
123 commit_transaction();
125 display_notification(_("A new customer has been added."));
127 $Ajax->activate('_page_body');
130 //--------------------------------------------------------------------------------------------
132 if (isset($_POST['submit']))
136 //--------------------------------------------------------------------------------------------
138 if (isset($_POST['delete']))
141 //the link to delete a selected record was clicked instead of the submit button
145 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
146 $sel_id = db_escape($_POST['customer_id']);
147 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no=$sel_id";
148 $result = db_query($sql,"check failed");
149 $myrow = db_fetch_row($result);
153 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
157 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no=$sel_id";
158 $result = db_query($sql,"check failed");
159 $myrow = db_fetch_row($result);
163 display_error(_("Cannot delete the customer record because orders have been created against it."));
167 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no=$sel_id";
168 $result = db_query($sql,"check failed");
169 $myrow = db_fetch_row($result);
173 display_error(_("Cannot delete this customer because there are branch records set up against it."));
174 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
179 if ($cancel_delete == 0)
180 { //ie not cancelled the delete as a result of above tests
181 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=$sel_id";
182 db_query($sql,"cannot delete customer");
184 display_notification(_("Selected customer has been deleted."));
185 unset($_POST['customer_id']);
186 $new_customer = true;
187 $Ajax->activate('_page_body');
188 } //end if Delete Customer
191 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
195 if (db_has_customers())
197 start_table("class = 'tablestyle_noborder'");
199 customer_list_cells(_("Select a customer: "), 'customer_id', null,
200 _('New customer'), true, check_value('show_inactive'));
201 check_cells(_("Show inactive:"), 'show_inactive', null, true);
204 if (get_post('_show_inactive_update')) {
205 $Ajax->activate('customer_id');
206 set_focus('customer_id');
211 hidden('customer_id');
216 $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id'] = '';
217 $_POST['dimension_id'] = 0;
218 $_POST['dimension2_id'] = 0;
219 $_POST['sales_type'] = -1;
220 $_POST['email'] = '';
221 $_POST['curr_code'] = get_company_currency();
222 $_POST['credit_status'] = -1;
223 $_POST['payment_terms'] = '';
224 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
225 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
226 $_POST['inactive'] = 0;
231 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($_POST['customer_id']);
232 $result = db_query($sql,"check failed");
234 $myrow = db_fetch($result);
236 $_POST['CustName'] = $myrow["name"];
237 $_POST['cust_ref'] = $myrow["debtor_ref"];
238 $_POST['address'] = $myrow["address"];
239 $_POST['tax_id'] = $myrow["tax_id"];
240 $_POST['email'] = $myrow["email"];
241 $_POST['dimension_id'] = $myrow["dimension_id"];
242 $_POST['dimension2_id'] = $myrow["dimension2_id"];
243 $_POST['sales_type'] = $myrow["sales_type"];
244 $_POST['curr_code'] = $myrow["curr_code"];
245 $_POST['credit_status'] = $myrow["credit_status"];
246 $_POST['payment_terms'] = $myrow["payment_terms"];
247 $_POST['discount'] = percent_format($myrow["discount"] * 100);
248 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
249 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
250 $_POST['inactive'] = $myrow["inactive"];
253 start_outer_table($table_style2, 5);
255 table_section_title(_("Name and Address"));
257 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
258 text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
259 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
261 email_row(_("E-mail:"), 'email', null, 40, 40);
262 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
267 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
271 label_row(_("Customer's Currency:"), $_POST['curr_code']);
272 hidden('curr_code', $_POST['curr_code']);
274 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
278 table_section_title(_("Sales"));
280 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
281 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
282 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
284 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
285 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
286 $dim = get_company_pref('use_dimension');
288 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
290 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
292 hidden('dimension_id', 0);
294 hidden('dimension2_id', 0);
296 if (!$new_customer) {
298 echo '<td>'._('Customer branches').':</td>';
299 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
300 '<b>'. (@$_REQUEST['popup'] ? _("Select or &Add") : _("&Add or Edit ")).'</b>',
301 "debtor_no=".$_POST['customer_id'].(@$_REQUEST['popup'] ? '&popup=1':''));
306 record_status_list_row(_("Customer status:"), 'inactive');
309 div_start('controls');
312 submit_center('submit', _("Add New Customer"), true, '', 'default');
316 submit_center_first('submit', _("Update Customer"),
317 _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
318 submit_return('select', get_post('customer_id'), _("Select this customer and return to document entry."));
319 submit_center_last('delete', _("Delete Customer"),
320 _('Delete customer data if have been never used'), true);
323 hidden('popup', @$_REQUEST['popup']);