6 include_once($path_to_root . "/includes/session.inc");
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/banking.inc");
11 include_once($path_to_root . "/includes/ui.inc");
13 if (isset($_GET['debtor_no']))
15 $_POST['customer_id'] = $_GET['debtor_no'];
17 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == "");
18 //--------------------------------------------------------------------------------------------
20 function can_process()
22 if (strlen($_POST['CustName']) == 0)
24 display_error(_("The customer name cannot be empty."));
25 set_focus('CustName');
29 if (!check_num('credit_limit', 0))
31 display_error(_("The credit limit must be numeric and not less than zero."));
32 set_focus('credit_limit');
36 if (!check_num('pymt_discount', 0, 100))
38 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
39 set_focus('pymt_discount');
43 if (!check_num('discount', 0, 100))
45 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
46 set_focus('discount');
53 //--------------------------------------------------------------------------------------------
55 function handle_submit()
57 global $path_to_root, $new_customer, $Ajax;
62 if ($new_customer == false)
65 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($_POST['CustName']) . ",
66 address=".db_escape($_POST['address']) . ",
67 tax_id=".db_escape($_POST['tax_id']) . ",
68 curr_code=".db_escape($_POST['curr_code']) . ",
69 email=".db_escape($_POST['email']) . ",
70 dimension_id=".db_escape($_POST['dimension_id']) . ",
71 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
72 credit_status=".db_escape($_POST['credit_status']) . ",
73 payment_terms=".db_escape($_POST['payment_terms']) . ",
74 discount=" . input_num('discount') / 100 . ",
75 pymt_discount=" . input_num('pymt_discount') / 100 . ",
76 credit_limit=" . input_num('credit_limit') . ",
77 sales_type = ".db_escape($_POST['sales_type']) . "
78 WHERE debtor_no = '". $_POST['customer_id'] . "'";
80 db_query($sql,"The customer could not be updated");
81 display_notification(_("Customer has been updated."));
84 { //it is a new customer
88 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
89 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
90 sales_type) VALUES (".db_escape($_POST['CustName']) .", "
91 .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
92 .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", "
93 .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ",
94 " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ",
95 " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
97 db_query($sql,"The customer could not be added");
99 $_POST['customer_id'] = db_insert_id();
100 $new_customer = false;
101 commit_transaction();
103 display_notification(_("A new customer has been added."));
105 $Ajax->activate('_page_body');
108 //--------------------------------------------------------------------------------------------
110 if (isset($_POST['submit']))
114 //--------------------------------------------------------------------------------------------
116 if (isset($_POST['select']))
118 context_return(array('customer_id' => $_POST['customer_id'],
119 'branch_id' => '')); // this fires customer history checks
121 //--------------------------------------------------------------------------------------------
123 if (isset($_POST['delete']))
126 //the link to delete a selected record was clicked instead of the submit button
130 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
132 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
133 $result = db_query($sql,"check failed");
134 $myrow = db_fetch_row($result);
138 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
142 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
143 $result = db_query($sql,"check failed");
144 $myrow = db_fetch_row($result);
148 display_error(_("Cannot delete the customer record because orders have been created against it."));
152 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
153 $result = db_query($sql,"check failed");
154 $myrow = db_fetch_row($result);
158 display_error(_("Cannot delete this customer because there are branch records set up against it."));
159 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
164 if ($cancel_delete == 0)
165 { //ie not cancelled the delete as a result of above tests
166 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
167 db_query($sql,"cannot delete customer");
169 display_notification(_("Selected customer has been deleted."));
170 unset($_POST['customer_id']);
171 $new_customer = true;
172 $Ajax->activate('_page_body');
173 } //end if Delete Customer
176 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
180 if (db_has_customers())
182 start_table("class = 'tablestyle_noborder'");
183 customer_list_row(_("Select a customer: "), 'customer_id', null,
184 _('New customer'), true);
189 hidden('customer_id');
192 start_table($table_style2, 7, 6);
193 echo "<tr valign=top><td>"; // outer table
196 start_table("class='tablestyle_noborder'");
200 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
201 $_POST['dimension_id'] = 0;
202 $_POST['dimension2_id'] = 0;
203 $_POST['sales_type'] = -1;
204 $_POST['email'] = '';
205 $_POST['curr_code'] = get_company_currency();
206 $_POST['credit_status'] = -1;
207 $_POST['payment_terms'] = '';
208 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
209 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
214 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
215 $result = db_query($sql,"check failed");
217 $myrow = db_fetch($result);
219 $_POST['CustName'] = $myrow["name"];
220 $_POST['address'] = $myrow["address"];
221 $_POST['tax_id'] = $myrow["tax_id"];
222 $_POST['email'] = $myrow["email"];
223 $_POST['dimension_id'] = $myrow["dimension_id"];
224 $_POST['dimension2_id'] = $myrow["dimension2_id"];
225 $_POST['sales_type'] = $myrow["sales_type"];
226 $_POST['curr_code'] = $myrow["curr_code"];
227 $_POST['credit_status'] = $myrow["credit_status"];
228 $_POST['payment_terms'] = $myrow["payment_terms"];
229 $_POST['discount'] = percent_format($myrow["discount"] * 100);
230 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
231 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
234 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
235 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
237 text_row(_("Email:"), 'email', null, 40, 40);
238 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
241 // Sherifoz 23.09.03 currency can't be changed if editing
244 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
248 label_row(_("Customer's Currency:"), $_POST['curr_code']);
249 hidden('curr_code', $_POST['curr_code']);
253 echo "</td><td class='tableseparator'>"; // outer table
255 start_table("class='tablestyle_noborder'");
257 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
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 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
269 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
270 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
272 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
273 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
274 if (!$new_customer) {
276 echo '<td>'._('Customer branches').':</td>';
277 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
278 '<b>'. (count($_SESSION['Context']) ? _("Select or &Add") : _("&Add or Edit ")).'</b>',
279 "debtor_no=".$_POST['customer_id']);
284 end_table(1); // outer table
285 div_start('controls');
288 submit_center('submit', _("Add New Customer"), true, '', true);
292 submit_center_first('submit', _("Update Customer"),
293 _('Update customer data'), true);
294 submit_return('select', _("Return"), _("Select this customer and return to document entry."), true);
295 submit_center_last('delete', _("Delete Customer"),
296 _('Delete customer data if have been never used'), true);