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 (!is_numeric( $_POST['credit_limit']))
36 display_error(_("The credit limit must be numeric."));
40 if (!is_numeric( $_POST['pymt_discount']))
42 display_error(_("The payment discount must be numeric."));
46 if (!is_numeric( $_POST['discount']))
48 display_error(_("The discount percentage must be numeric."));
52 if ($_POST['credit_limit'] < 0)
54 display_error(_("The credit limit must be a positive number."));
58 if (($_POST['pymt_discount'] >= 100) || ($_POST['pymt_discount'] < 0))
60 display_error(_("The payment discount is expected to be less than 100% and greater than or equal to 0."));
64 if (($_POST['discount'] >= 100) || ($_POST['discount'] < 0))
66 display_error(_("The discount percent is expected to be less than 100 and greater than or equal to 0."));
73 //--------------------------------------------------------------------------------------------
75 function handle_submit()
81 if (!isset($_POST['New']))
84 // Sherifoz 22.06.03 convert percent to fraction
85 $sql = "UPDATE ".TB_PREF."debtors_master SET name='" . $_POST['CustName'] . "',
86 address='" . $_POST['address'] . "',
87 tax_id='" . $_POST['tax_id'] . "',
88 curr_code='" . $_POST['curr_code'] . "',
89 email='" . $_POST['email'] . "',
90 dimension_id=" . $_POST['dimension_id'] . ",
91 dimension2_id=" . $_POST['dimension2_id'] . ",
92 credit_status='" . $_POST['credit_status'] . "',
93 payment_terms='" . $_POST['payment_terms'] . "',
94 discount=" . ($_POST['discount']) / 100 . ",
95 pymt_discount=" . ($_POST['pymt_discount']) / 100 . ",
96 credit_limit=" . $_POST['credit_limit'] . ",
97 sales_type = '" . $_POST['sales_type'] . "'
98 WHERE debtor_no = '" . $_POST['customer_id'] . "'";
100 db_query($sql,"The customer could not be updated");
101 display_notification(_("Customer has been updated."));
106 { //it is a new customer
110 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
111 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
112 sales_type) VALUES ('" . $_POST['CustName'] ."', '" . $_POST['address'] . "', '" . $_POST['tax_id'] . "',
113 '" . $_POST['email'] . "', " . $_POST['dimension_id'] . ", " . $_POST['dimension2_id'] . ", '" . $_POST['curr_code'] . "',
114 " . $_POST['credit_status'] . ", '" . $_POST['payment_terms'] . "', " . ($_POST['discount'])/100 . ",
115 " . ($_POST['pymt_discount'])/100 . ", " . $_POST['credit_limit'] . ", '" . $_POST['sales_type'] . "')";
117 db_query($sql,"The customer could not be added");
119 $new_customer_id = db_insert_id();
121 commit_transaction();
123 display_notification(_("A new customer has been added."));
125 hyperlink_params($path_to_root . "/sales/manage/customer_branches.php", _("Add branches for this customer"), "debtor_no=$new_customer_id");
131 //--------------------------------------------------------------------------------------------
133 if (isset($_POST['submit']))
139 //--------------------------------------------------------------------------------------------
141 if (isset($_POST['delete']))
144 //the link to delete a selected record was clicked instead of the submit button
148 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
150 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
151 $result = db_query($sql,"check failed");
152 $myrow = db_fetch_row($result);
156 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
160 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
161 $result = db_query($sql,"check failed");
162 $myrow = db_fetch_row($result);
166 display_error(_("Cannot delete the customer record because orders have been created against it."));
170 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
171 $result = db_query($sql,"check failed");
172 $myrow = db_fetch_row($result);
176 display_error(_("Cannot delete this customer because there are branch records set up against it."));
177 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
182 if ($cancel_delete == 0)
183 { //ie not cancelled the delete as a result of above tests
184 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
185 db_query($sql,"cannot delete customer");
187 meta_forward($_SERVER['PHP_SELF']);
188 } //end if Delete Customer
191 function clear_fields()
193 unset($_POST['CustName']);
194 unset($_POST['address']);
195 unset($_POST['tax_id']);
196 unset($_POST['email']);
197 unset($_POST['dimension_id']);
198 unset($_POST['dimension2_id']);
199 unset($_POST['credit_status']);
200 unset($_POST['payment_terms']);
201 unset($_POST['discount']);
202 unset($_POST['pymt_discount']);
203 unset($_POST['credit_limit']);
204 unset($_POST['sales_type']);
205 unset($_POST['customer_id']);
209 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
213 if (db_has_customers())
215 start_table("class = 'tablestyle_noborder'");
217 customer_list_cells(_("Select a customer: "), 'customer_id', null);
218 submit_cells('SelectCustomer', _("Edit Customer"));
224 hidden('customer_id', $_POST['customer_id']);
227 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new customer"), "New=1");
230 start_table($table_style2, 7, 6);
231 echo "<tr valign=top><td>"; // outer table
233 start_table("class='tablestyle_noborder'");
235 if (isset($_POST['New']))
238 hidden('New', 'Yes');
240 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
241 $_POST['dimension_id'] = 0;
242 $_POST['dimension2_id'] = 0;
243 $_POST['sales_type'] = -1;
244 $_POST['curr_code'] = get_company_currency();
245 $_POST['credit_status'] = -1;
246 $_POST['payment_terms'] = '';
247 $_POST['discount'] = $_POST['pymt_discount'] = 0;
248 $_POST['credit_limit'] = sys_prefs::default_credit_limit();
253 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
254 $result = db_query($sql,"check failed");
256 $myrow = db_fetch($result);
258 $_POST['CustName'] = $myrow["name"];
259 $_POST['address'] = $myrow["address"];
260 $_POST['tax_id'] = $myrow["tax_id"];
261 $_POST['email'] = $myrow["email"];
262 $_POST['dimension_id'] = $myrow["dimension_id"];
263 $_POST['dimension2_id'] = $myrow["dimension2_id"];
264 $_POST['sales_type'] = $myrow["sales_type"];
265 $_POST['curr_code'] = $myrow["curr_code"];
266 $_POST['credit_status'] = $myrow["credit_status"];
267 $_POST['payment_terms'] = $myrow["payment_terms"];
268 $_POST['discount'] = $myrow["discount"] * 100; // Sherifoz 21.6.03 convert to displayable percentage
269 $_POST['pymt_discount'] = $myrow["pymt_discount"] * 100; // Sherifoz 21.6.03 convert to displayable percentage
270 $_POST['credit_limit'] = $myrow["credit_limit"];
273 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
274 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
276 text_row(_("Email:"), 'email', null, 40, 40);
277 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
280 // Sherifoz 23.09.03 currency can't be changed if editing
281 if (isset($_POST['New']))
283 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
287 label_row(_("Customer's Currency:"), $_POST['curr_code']);
288 hidden('curr_code', $_POST['curr_code']);
292 echo "</td><td class='tableseparator'>"; // outer table
294 start_table("class='tablestyle_noborder'");
296 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
297 $dim = get_company_pref('use_dimension');
299 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
301 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
303 hidden('dimension_id', 0);
305 hidden('dimension2_id', 0);
307 text_row(_("Discount Percent:"), 'discount', $_POST['discount'], 5, 4);
308 text_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount'], 5, 4);
309 text_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit'], 16, 14);
311 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
312 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
316 end_table(1); // outer table
318 if (isset($_POST['New']))
320 submit_center('submit', _("Add New Customer"));
324 submit_center_first('submit', _("Update Customer"));
325 submit_center_last('delete', _("Delete Customer"));