2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 $path_to_root="../..";
15 include_once($path_to_root . "/includes/session.inc");
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/banking.inc");
20 include_once($path_to_root . "/includes/ui.inc");
22 if (isset($_GET['debtor_no']))
24 $_POST['customer_id'] = $_GET['debtor_no'];
26 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == "");
27 //--------------------------------------------------------------------------------------------
29 function can_process()
31 if (strlen($_POST['CustName']) == 0)
33 display_error(_("The customer name cannot be empty."));
34 set_focus('CustName');
38 if (!check_num('credit_limit', 0))
40 display_error(_("The credit limit must be numeric and not less than zero."));
41 set_focus('credit_limit');
45 if (!check_num('pymt_discount', 0, 100))
47 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
48 set_focus('pymt_discount');
52 if (!check_num('discount', 0, 100))
54 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
55 set_focus('discount');
62 //--------------------------------------------------------------------------------------------
64 function handle_submit()
66 global $path_to_root, $new_customer, $Ajax;
71 if ($new_customer == false)
74 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($_POST['CustName']) . ",
75 address=".db_escape($_POST['address']) . ",
76 tax_id=".db_escape($_POST['tax_id']) . ",
77 curr_code=".db_escape($_POST['curr_code']) . ",
78 email=".db_escape($_POST['email']) . ",
79 dimension_id=".db_escape($_POST['dimension_id']) . ",
80 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
81 credit_status=".db_escape($_POST['credit_status']) . ",
82 payment_terms=".db_escape($_POST['payment_terms']) . ",
83 discount=" . input_num('discount') / 100 . ",
84 pymt_discount=" . input_num('pymt_discount') / 100 . ",
85 credit_limit=" . input_num('credit_limit') . ",
86 sales_type = ".db_escape($_POST['sales_type']) . "
87 WHERE debtor_no = '". $_POST['customer_id'] . "'";
89 db_query($sql,"The customer could not be updated");
90 display_notification(_("Customer has been updated."));
93 { //it is a new customer
97 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,
98 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,
99 sales_type) VALUES (".db_escape($_POST['CustName']) .", "
100 .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
101 .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", "
102 .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ",
103 " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ",
104 " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
106 db_query($sql,"The customer could not be added");
108 $_POST['customer_id'] = db_insert_id();
109 $new_customer = false;
110 commit_transaction();
112 display_notification(_("A new customer has been added."));
114 $Ajax->activate('_page_body');
117 //--------------------------------------------------------------------------------------------
119 if (isset($_POST['submit']))
123 //--------------------------------------------------------------------------------------------
125 if (isset($_POST['select']))
127 context_return(array('customer_id' => $_POST['customer_id'],
128 'branch_id' => '')); // this fires customer history checks
130 //--------------------------------------------------------------------------------------------
132 if (isset($_POST['delete']))
135 //the link to delete a selected record was clicked instead of the submit button
139 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
141 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
142 $result = db_query($sql,"check failed");
143 $myrow = db_fetch_row($result);
147 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
151 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
152 $result = db_query($sql,"check failed");
153 $myrow = db_fetch_row($result);
157 display_error(_("Cannot delete the customer record because orders have been created against it."));
161 $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
162 $result = db_query($sql,"check failed");
163 $myrow = db_fetch_row($result);
167 display_error(_("Cannot delete this customer because there are branch records set up against it."));
168 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
173 if ($cancel_delete == 0)
174 { //ie not cancelled the delete as a result of above tests
175 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
176 db_query($sql,"cannot delete customer");
178 display_notification(_("Selected customer has been deleted."));
179 unset($_POST['customer_id']);
180 $new_customer = true;
181 $Ajax->activate('_page_body');
182 } //end if Delete Customer
185 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
189 if (db_has_customers())
191 start_table("class = 'tablestyle_noborder'");
192 customer_list_row(_("Select a customer: "), 'customer_id', null,
193 _('New customer'), true);
198 hidden('customer_id');
201 start_outer_table($table_style2, 5);
207 $_POST['CustName'] = $_POST['address'] = $_POST['tax_id'] = '';
208 $_POST['dimension_id'] = 0;
209 $_POST['dimension2_id'] = 0;
210 $_POST['sales_type'] = -1;
211 $_POST['email'] = '';
212 $_POST['curr_code'] = get_company_currency();
213 $_POST['credit_status'] = -1;
214 $_POST['payment_terms'] = '';
215 $_POST['discount'] = $_POST['pymt_discount'] = percent_format(0);
216 $_POST['credit_limit'] = price_format(sys_prefs::default_credit_limit());
221 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
222 $result = db_query($sql,"check failed");
224 $myrow = db_fetch($result);
226 $_POST['CustName'] = $myrow["name"];
227 $_POST['address'] = $myrow["address"];
228 $_POST['tax_id'] = $myrow["tax_id"];
229 $_POST['email'] = $myrow["email"];
230 $_POST['dimension_id'] = $myrow["dimension_id"];
231 $_POST['dimension2_id'] = $myrow["dimension2_id"];
232 $_POST['sales_type'] = $myrow["sales_type"];
233 $_POST['curr_code'] = $myrow["curr_code"];
234 $_POST['credit_status'] = $myrow["credit_status"];
235 $_POST['payment_terms'] = $myrow["payment_terms"];
236 $_POST['discount'] = percent_format($myrow["discount"] * 100);
237 $_POST['pymt_discount'] = percent_format($myrow["pymt_discount"] * 100);
238 $_POST['credit_limit'] = price_format($myrow["credit_limit"]);
241 table_section_title(_("Name and Address"));
243 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
244 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
246 email_row(_("E-mail:"), 'email', null, 40, 40);
247 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
252 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
256 label_row(_("Customer's Currency:"), $_POST['curr_code']);
257 hidden('curr_code', $_POST['curr_code']);
262 table_section_title(_("Sales"));
264 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
265 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
266 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
267 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
269 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
270 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);
271 $dim = get_company_pref('use_dimension');
273 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
275 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
277 hidden('dimension_id', 0);
279 hidden('dimension2_id', 0);
281 if (!$new_customer) {
283 echo '<td>'._('Customer branches').':</td>';
284 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
285 '<b>'. (count($_SESSION['Context']) ? _("Select or &Add") : _("&Add or Edit ")).'</b>',
286 "debtor_no=".$_POST['customer_id']);
292 div_start('controls');
295 submit_center('submit', _("Add New Customer"), true, '', true);
299 submit_center_first('submit', _("Update Customer"),
300 _('Update customer data'), true);
301 submit_return('select', _("Return"), _("Select this customer and return to document entry."), true);
302 submit_center_last('delete', _("Delete Customer"),
303 _('Delete customer data if have been never used'), true);