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 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == "");
14 //--------------------------------------------------------------------------------------------
16 function can_process()
18 if (strlen($_POST['CustName']) == 0)
20 display_error(_("The customer name cannot be empty."));
24 if (!check_num('credit_limit', 0))
26 display_error(_("The credit limit must be numeric and not less than zero."));
30 if (!check_num('pymt_discount', 0, 100))
32 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
36 if (!check_num('discount', 0, 100))
38 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
45 //--------------------------------------------------------------------------------------------
47 function handle_submit()
49 global $path_to_root, $new_customer, $Ajax;
54 if ($new_customer == false)
57 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($_POST['CustName']) . ",
58 address=".db_escape($_POST['address']) . ",
59 tax_id=".db_escape($_POST['tax_id']) . ",
60 curr_code=".db_escape($_POST['curr_code']) . ",
61 email=".db_escape($_POST['email']) . ",
62 dimension_id=".db_escape($_POST['dimension_id']) . ",
63 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
64 credit_status=".db_escape($_POST['credit_status']) . ",
65 payment_terms=".db_escape($_POST['payment_terms']) . ",
66 discount=" . input_num('discount') / 100 . ",
67 pymt_discount=" . input_num('pymt_discount') / 100 . ",
68 credit_limit=" . input_num('credit_limit') . ",
69 sales_type = ".db_escape($_POST['sales_type']) . "
70 WHERE debtor_no = '". $_POST['customer_id'] . "'";
72 db_query($sql,"The customer could not be updated");
73 display_notification(_("Customer has been updated."));
77 { //it is a new customer
81 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
82 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
83 sales_type) VALUES (".db_escape($_POST['CustName']) .", "
84 .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
85 .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", "
86 .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ",
87 " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ",
88 " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
90 db_query($sql,"The customer could not be added");
92 $_POST['customer_id'] = db_insert_id();
93 $new_customer = false;
96 display_notification(_("A new customer has been added."));
98 $Ajax->activate('_page_body');
102 //--------------------------------------------------------------------------------------------
104 if (isset($_POST['submit']))
110 //--------------------------------------------------------------------------------------------
112 if (isset($_POST['delete']))
115 //the link to delete a selected record was clicked instead of the submit button
119 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
121 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
122 $result = db_query($sql,"check failed");
123 $myrow = db_fetch_row($result);
127 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
131 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
132 $result = db_query($sql,"check failed");
133 $myrow = db_fetch_row($result);
137 display_error(_("Cannot delete the customer record because orders have been created against it."));
141 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
142 $result = db_query($sql,"check failed");
143 $myrow = db_fetch_row($result);
147 display_error(_("Cannot delete this customer because there are branch records set up against it."));
148 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
153 if ($cancel_delete == 0)
154 { //ie not cancelled the delete as a result of above tests
155 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
156 db_query($sql,"cannot delete customer");
158 display_notification(_("Selected customer has been deleted."));
159 unset($_POST['customer_id']);
160 $new_customer = true;
161 $Ajax->activate('_page_body');
162 } //end if Delete Customer
166 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
170 if (db_has_customers())
172 start_table("class = 'tablestyle_noborder'");
173 customer_list_row(_("Select a customer: "), 'customer_id', null,
174 _('New customer'), true);
179 hidden('customer_id', $_POST['customer_id']);
182 start_table($table_style2, 7, 6);
183 echo "<tr valign=top><td>"; // outer table
186 start_table("class='tablestyle_noborder'");
190 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
191 $_POST['dimension_id'] = 0;
192 $_POST['dimension2_id'] = 0;
193 $_POST['sales_type'] = -1;
194 $_POST['email'] = '';
195 $_POST['curr_code'] = get_company_currency();
196 $_POST['credit_status'] = -1;
197 $_POST['payment_terms'] = '';
198 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
199 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
204 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
205 $result = db_query($sql,"check failed");
207 $myrow = db_fetch($result);
209 $_POST['CustName'] = $myrow["name"];
210 $_POST['address'] = $myrow["address"];
211 $_POST['tax_id'] = $myrow["tax_id"];
212 $_POST['email'] = $myrow["email"];
213 $_POST['dimension_id'] = $myrow["dimension_id"];
214 $_POST['dimension2_id'] = $myrow["dimension2_id"];
215 $_POST['sales_type'] = $myrow["sales_type"];
216 $_POST['curr_code'] = $myrow["curr_code"];
217 $_POST['credit_status'] = $myrow["credit_status"];
218 $_POST['payment_terms'] = $myrow["payment_terms"];
219 $_POST['discount'] = percent_format($myrow["discount"] * 100);
220 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
221 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
224 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
225 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
227 text_row(_("Email:"), 'email', null, 40, 40);
228 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
231 // Sherifoz 23.09.03 currency can't be changed if editing
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']);
243 echo "</td><td class='tableseparator'>"; // outer table
245 start_table("class='tablestyle_noborder'");
247 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
248 $dim = get_company_pref('use_dimension');
250 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
252 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
254 hidden('dimension_id', 0);
256 hidden('dimension2_id', 0);
258 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
259 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
260 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
262 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
263 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
264 if (!$new_customer) {
266 echo '<td>'._('Customer branches').':</td>';
267 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",'<b>'. _("Add or Edit").'</b>', "debtor_no=".$_POST['customer_id']);
272 end_table(1); // outer table
276 submit_center('submit', _("Add New Customer"), true, '', true);
280 submit_center_first('submit', _("Update Customer"),
281 _('Update customer data'), true);
282 submit_center_last('delete', _("Delete Customer"),
283 _('Delete user data if have been never used'), true);