Ajax improvements
[fa-stable.git] / sales / manage / customers.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="../..";
5
6 include_once($path_to_root . "/includes/session.inc");
7 page(_("Customers")); 
8
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
13 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == ""); 
14 //--------------------------------------------------------------------------------------------
15
16 function can_process()
17 {
18         if (strlen($_POST['CustName']) == 0) 
19         {
20                 display_error(_("The customer name cannot be empty."));
21                 return false;
22         } 
23         
24         if (!check_num('credit_limit', 0))
25         {
26                 display_error(_("The credit limit must be numeric and not less than zero."));
27                 return false;           
28         } 
29         
30         if (!check_num('pymt_discount', 0, 100)) 
31         {
32                 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
33                 return false;           
34         } 
35         
36         if (!check_num('discount', 0, 100)) 
37         {
38                 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
39                 return false;           
40         } 
41
42         return true;
43 }
44
45 //--------------------------------------------------------------------------------------------
46
47 function handle_submit()
48 {
49         global $path_to_root, $new_customer, $Ajax;
50
51         if (!can_process())
52                 return;
53                 
54         if ($new_customer == false) 
55         {
56
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'] . "'";
71
72                 db_query($sql,"The customer could not be updated");
73                 display_notification(_("Customer has been updated."));
74
75         } 
76         else 
77         {       //it is a new customer
78
79                 begin_transaction();
80
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']) . ")";
89
90                 db_query($sql,"The customer could not be added");
91
92                 $_POST['customer_id'] = db_insert_id();
93                 $new_customer = false;
94                 commit_transaction();                   
95
96                 display_notification(_("A new customer has been added."));
97
98                 $Ajax->activate('_page_body');
99         }
100 }
101
102 //--------------------------------------------------------------------------------------------
103
104 if (isset($_POST['submit'])) 
105 {
106         
107         handle_submit();
108 }
109
110 //-------------------------------------------------------------------------------------------- 
111
112 if (isset($_POST['delete'])) 
113 {
114
115         //the link to delete a selected record was clicked instead of the submit button
116
117         $cancel_delete = 0;
118
119         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
120
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);
124         if ($myrow[0] > 0) 
125         {
126                 $cancel_delete = 1;
127                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
128         } 
129         else 
130         {
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);
134                 if ($myrow[0] > 0) 
135                 {
136                         $cancel_delete = 1;
137                         display_error(_("Cannot delete the customer record because orders have been created against it."));
138                 } 
139                 else 
140                 {
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);
144                         if ($myrow[0] > 0) 
145                         {
146                                 $cancel_delete = 1;
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";
149                         }
150                 }
151         }
152         
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");
157
158                 display_notification(_("Selected customer has been deleted."));
159                 unset($_POST['customer_id']);
160                 $new_customer = true;
161                 $Ajax->activate('_page_body');
162 //              meta_forward($_SERVER['PHP_SELF']); 
163         } //end if Delete Customer
164 }
165
166
167 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
168  
169 start_form();
170
171 if (db_has_customers()) 
172 {
173         start_table("class = 'tablestyle_noborder'");
174         customer_list_row(_("Select a customer: "), 'customer_id', null,
175           _('New customer'), true);
176         end_table();
177
178 else 
179 {
180         hidden('customer_id', $_POST['customer_id']);
181 }
182
183 start_table($table_style2, 7, 6);
184 echo "<tr valign=top><td>"; // outer table      
185
186
187 start_table("class='tablestyle_noborder'");     
188
189 if ($new_customer) 
190 {
191         $_POST['CustName'] = $_POST['address'] = $_POST['tax_id']  = '';
192         $_POST['dimension_id'] = 0;
193         $_POST['dimension2_id'] = 0;
194         $_POST['sales_type'] = -1;
195         $_POST['email'] = '';
196         $_POST['curr_code']  = get_company_currency();
197         $_POST['credit_status']  = -1;
198         $_POST['payment_terms']  = '';
199         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
200         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
201
202 else 
203 {
204
205         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
206         $result = db_query($sql,"check failed");
207
208         $myrow = db_fetch($result);
209
210         $_POST['CustName'] = $myrow["name"];
211         $_POST['address']  = $myrow["address"];
212         $_POST['tax_id']  = $myrow["tax_id"];
213         $_POST['email']  = $myrow["email"];
214         $_POST['dimension_id']  = $myrow["dimension_id"];
215         $_POST['dimension2_id']  = $myrow["dimension2_id"];
216         $_POST['sales_type'] = $myrow["sales_type"];
217         $_POST['curr_code']  = $myrow["curr_code"];
218         $_POST['credit_status']  = $myrow["credit_status"];
219         $_POST['payment_terms']  = $myrow["payment_terms"];
220         $_POST['discount']  = percent_format($myrow["discount"] * 100);
221         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
222         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
223 }
224
225 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
226 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
227
228 text_row(_("Email:"), 'email', null, 40, 40);
229 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
230
231
232 // Sherifoz 23.09.03 currency can't be changed if editing
233 if ($new_customer) 
234 {
235         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
236
237 else 
238 {
239         label_row(_("Customer's Currency:"), $_POST['curr_code']);
240         hidden('curr_code', $_POST['curr_code']);                               
241 }       
242 end_table();
243
244 echo "</td><td class='tableseparator'>"; // outer table
245
246 start_table("class='tablestyle_noborder'");     
247
248 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
249 $dim = get_company_pref('use_dimension');
250 if ($dim >= 1)
251         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
252 if ($dim > 1)
253         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
254 if ($dim < 1)
255         hidden('dimension_id', 0);
256 if ($dim < 2)
257         hidden('dimension2_id', 0);
258
259 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
260 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
261 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
262
263 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
264 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
265 if (!$new_customer)  {
266 start_row();
267   hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php", _("Edit customer branches"), "debtor_no=".$_POST['customer_id']);
268 end_row();
269 }
270 end_table();
271
272 end_table(1); // outer table    
273
274 if ($new_customer)
275 {
276         submit_center('submit', _("Add New Customer"), true, '', true);
277
278 else 
279 {
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);
284 }
285
286 end_form();
287 end_page();
288
289 ?>