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");
12 include_once($path_to_root . "/includes/data_checks.inc");
14 if (isset($_GET['New']) || !isset($_POST['customer_id']) || $_POST['customer_id'] == "")
19 if (isset($_POST['SelectCustomer']))
24 //--------------------------------------------------------------------------------------------
26 function can_process()
28 if (strlen($_POST['CustName']) == 0)
30 display_error(_("The customer name cannot be empty."));
34 if (!check_num('credit_limit', 0))
36 display_error(_("The credit limit must be numeric and not less than zero."));
40 if (!check_num('pymt_discount', 0, 100))
42 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
46 if (!check_num('discount', 0, 100))
48 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
55 //--------------------------------------------------------------------------------------------
57 function handle_submit()
63 if (!isset($_POST['New']))
66 $sql = "UPDATE ".TB_PREF."debtors_master SET name='" . $_POST['CustName'] . "',
67 address='" . $_POST['address'] . "',
68 tax_id='" . $_POST['tax_id'] . "',
69 curr_code='" . $_POST['curr_code'] . "',
70 email='" . $_POST['email'] . "',
71 dimension_id=" . $_POST['dimension_id'] . ",
72 dimension2_id=" . $_POST['dimension2_id'] . ",
73 credit_status='" . $_POST['credit_status'] . "',
74 payment_terms='" . $_POST['payment_terms'] . "',
75 discount=" . input_num('discount') / 100 . ",
76 pymt_discount=" . input_num('pymt_discount') / 100 . ",
77 credit_limit=" . input_num('credit_limit') . ",
78 sales_type = '" . $_POST['sales_type'] . "'
79 WHERE debtor_no = '" . $_POST['customer_id'] . "'";
81 db_query($sql,"The customer could not be updated");
82 display_notification(_("Customer has been updated."));
87 { //it is a new customer
91 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
92 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
93 sales_type) VALUES ('" . $_POST['CustName'] ."', '" . $_POST['address'] . "', '" . $_POST['tax_id'] . "',
94 '" . $_POST['email'] . "', " . $_POST['dimension_id'] . ", " . $_POST['dimension2_id'] . ", '" . $_POST['curr_code'] . "',
95 " . $_POST['credit_status'] . ", '" . $_POST['payment_terms'] . "', " . input_num('discount')/100 . ",
96 " . input_num('pymt_discount')/100 . ", " . $_POST['credit_limit'] . ", '" . $_POST['sales_type'] . "')";
98 db_query($sql,"The customer could not be added");
100 $new_customer_id = db_insert_id();
102 commit_transaction();
104 display_notification(_("A new customer has been added."));
106 hyperlink_params($path_to_root . "/sales/manage/customer_branches.php", _("Add branches for this customer"), "debtor_no=$new_customer_id");
112 //--------------------------------------------------------------------------------------------
114 if (isset($_POST['submit']))
120 //--------------------------------------------------------------------------------------------
122 if (isset($_POST['delete']))
125 //the link to delete a selected record was clicked instead of the submit button
129 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
131 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
132 $result = db_query($sql,"check failed");
133 $myrow = db_fetch_row($result);
137 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
141 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
142 $result = db_query($sql,"check failed");
143 $myrow = db_fetch_row($result);
147 display_error(_("Cannot delete the customer record because orders have been created against it."));
151 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
152 $result = db_query($sql,"check failed");
153 $myrow = db_fetch_row($result);
157 display_error(_("Cannot delete this customer because there are branch records set up against it."));
158 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
163 if ($cancel_delete == 0)
164 { //ie not cancelled the delete as a result of above tests
165 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
166 db_query($sql,"cannot delete customer");
168 meta_forward($_SERVER['PHP_SELF']);
169 } //end if Delete Customer
172 function clear_fields()
174 unset($_POST['CustName']);
175 unset($_POST['address']);
176 unset($_POST['tax_id']);
177 unset($_POST['email']);
178 unset($_POST['dimension_id']);
179 unset($_POST['dimension2_id']);
180 unset($_POST['credit_status']);
181 unset($_POST['payment_terms']);
182 unset($_POST['discount']);
183 unset($_POST['pymt_discount']);
184 unset($_POST['credit_limit']);
185 unset($_POST['sales_type']);
186 unset($_POST['customer_id']);
190 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
194 if (db_has_customers())
196 start_table("class = 'tablestyle_noborder'");
198 customer_list_cells(_("Select a customer: "), 'customer_id', null);
199 submit_cells('SelectCustomer', _("Edit Customer"));
205 hidden('customer_id', $_POST['customer_id']);
208 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new customer"), "New=1");
211 start_table($table_style2, 7, 6);
212 echo "<tr valign=top><td>"; // outer table
214 start_table("class='tablestyle_noborder'");
216 if (isset($_POST['New']))
219 hidden('New', 'Yes');
221 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
222 $_POST['dimension_id'] = 0;
223 $_POST['dimension2_id'] = 0;
224 $_POST['sales_type'] = -1;
225 $_POST['curr_code'] = get_company_currency();
226 $_POST['credit_status'] = -1;
227 $_POST['payment_terms'] = '';
228 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
229 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
234 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
235 $result = db_query($sql,"check failed");
237 $myrow = db_fetch($result);
239 $_POST['CustName'] = $myrow["name"];
240 $_POST['address'] = $myrow["address"];
241 $_POST['tax_id'] = $myrow["tax_id"];
242 $_POST['email'] = $myrow["email"];
243 $_POST['dimension_id'] = $myrow["dimension_id"];
244 $_POST['dimension2_id'] = $myrow["dimension2_id"];
245 $_POST['sales_type'] = $myrow["sales_type"];
246 $_POST['curr_code'] = $myrow["curr_code"];
247 $_POST['credit_status'] = $myrow["credit_status"];
248 $_POST['payment_terms'] = $myrow["payment_terms"];
249 $_POST['discount'] = percent_format($myrow["discount"] * 100);
250 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
251 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
254 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
255 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
257 text_row(_("Email:"), 'email', null, 40, 40);
258 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
261 // Sherifoz 23.09.03 currency can't be changed if editing
262 if (isset($_POST['New']))
264 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
268 label_row(_("Customer's Currency:"), $_POST['curr_code']);
269 hidden('curr_code', $_POST['curr_code']);
273 echo "</td><td class='tableseparator'>"; // outer table
275 start_table("class='tablestyle_noborder'");
277 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
278 $dim = get_company_pref('use_dimension');
280 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
282 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
284 hidden('dimension_id', 0);
286 hidden('dimension2_id', 0);
288 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
289 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
290 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
292 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
293 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
297 end_table(1); // outer table
299 if (isset($_POST['New']))
301 submit_center('submit', _("Add New Customer"));
305 submit_center_first('submit', _("Update Customer"));
306 submit_center_last('delete', _("Delete Customer"));