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['New']) || !isset($_POST['customer_id']) || $_POST['customer_id'] == "")
18 if (isset($_POST['SelectCustomer']))
23 //--------------------------------------------------------------------------------------------
25 function can_process()
27 if (strlen($_POST['CustName']) == 0)
29 display_error(_("The customer name cannot be empty."));
33 if (!check_num('credit_limit', 0))
35 display_error(_("The credit limit must be numeric and not less than zero."));
39 if (!check_num('pymt_discount', 0, 100))
41 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
45 if (!check_num('discount', 0, 100))
47 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
54 //--------------------------------------------------------------------------------------------
56 function handle_submit()
62 if (!isset($_POST['New']))
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."));
86 { //it is a new customer
90 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
91 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
92 sales_type) VALUES (".db_escape($_POST['CustName']) .", "
93 .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
94 .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", "
95 .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ",
96 " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ",
97 " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
99 db_query($sql,"The customer could not be added");
101 $new_customer_id = db_insert_id();
103 commit_transaction();
105 display_notification(_("A new customer has been added."));
107 hyperlink_params($path_to_root . "/sales/manage/customer_branches.php", _("Add branches for this customer"), "debtor_no=$new_customer_id");
113 //--------------------------------------------------------------------------------------------
115 if (isset($_POST['submit']))
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 meta_forward($_SERVER['PHP_SELF']);
170 } //end if Delete Customer
173 function clear_fields()
175 unset($_POST['CustName']);
176 unset($_POST['address']);
177 unset($_POST['tax_id']);
178 unset($_POST['email']);
179 unset($_POST['dimension_id']);
180 unset($_POST['dimension2_id']);
181 unset($_POST['credit_status']);
182 unset($_POST['payment_terms']);
183 unset($_POST['discount']);
184 unset($_POST['pymt_discount']);
185 unset($_POST['credit_limit']);
186 unset($_POST['sales_type']);
187 unset($_POST['customer_id']);
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 submit_cells('SelectCustomer', _("Edit Customer"));
206 hidden('customer_id', $_POST['customer_id']);
209 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new customer"), "New=1");
212 start_table($table_style2, 7, 6);
213 echo "<tr valign=top><td>"; // outer table
215 start_table("class='tablestyle_noborder'");
217 if (isset($_POST['New']))
220 hidden('New', 'Yes');
222 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
223 $_POST['dimension_id'] = 0;
224 $_POST['dimension2_id'] = 0;
225 $_POST['sales_type'] = -1;
226 $_POST['curr_code'] = get_company_currency();
227 $_POST['credit_status'] = -1;
228 $_POST['payment_terms'] = '';
229 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
230 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
235 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
236 $result = db_query($sql,"check failed");
238 $myrow = db_fetch($result);
240 $_POST['CustName'] = $myrow["name"];
241 $_POST['address'] = $myrow["address"];
242 $_POST['tax_id'] = $myrow["tax_id"];
243 $_POST['email'] = $myrow["email"];
244 $_POST['dimension_id'] = $myrow["dimension_id"];
245 $_POST['dimension2_id'] = $myrow["dimension2_id"];
246 $_POST['sales_type'] = $myrow["sales_type"];
247 $_POST['curr_code'] = $myrow["curr_code"];
248 $_POST['credit_status'] = $myrow["credit_status"];
249 $_POST['payment_terms'] = $myrow["payment_terms"];
250 $_POST['discount'] = percent_format($myrow["discount"] * 100);
251 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
252 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
255 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
256 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
258 text_row(_("Email:"), 'email', null, 40, 40);
259 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
262 // Sherifoz 23.09.03 currency can't be changed if editing
263 if (isset($_POST['New']))
265 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
269 label_row(_("Customer's Currency:"), $_POST['curr_code']);
270 hidden('curr_code', $_POST['curr_code']);
274 echo "</td><td class='tableseparator'>"; // outer table
276 start_table("class='tablestyle_noborder'");
278 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
279 $dim = get_company_pref('use_dimension');
281 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
283 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
285 hidden('dimension_id', 0);
287 hidden('dimension2_id', 0);
289 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
290 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
291 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
293 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
294 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
298 end_table(1); // outer table
300 if (isset($_POST['New']))
302 submit_center('submit', _("Add New Customer"));
306 submit_center_first('submit', _("Update Customer"));
307 submit_center_last('delete', _("Delete Customer"));