Moving 2.0 development version to main trunk.
[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         else 
76         {       //it is a new customer
77
78                 begin_transaction();
79
80                 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,  
81                         curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit, 
82                         sales_type) VALUES (".db_escape($_POST['CustName']) .", " 
83                         .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
84                         .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", " 
85                         .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ", 
86                         " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ", 
87                         " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
88
89                 db_query($sql,"The customer could not be added");
90
91                 $_POST['customer_id'] = db_insert_id();
92                 $new_customer = false;
93                 commit_transaction();                   
94
95                 display_notification(_("A new customer has been added."));
96
97                 $Ajax->activate('_page_body');
98         }
99 }
100
101 //--------------------------------------------------------------------------------------------
102
103 if (isset($_POST['submit'])) 
104 {
105         
106         handle_submit();
107 }
108
109 //-------------------------------------------------------------------------------------------- 
110
111 if (isset($_POST['delete'])) 
112 {
113
114         //the link to delete a selected record was clicked instead of the submit button
115
116         $cancel_delete = 0;
117
118         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
119
120         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no='" . $_POST['customer_id'] . "'";
121         $result = db_query($sql,"check failed");
122         $myrow = db_fetch_row($result);
123         if ($myrow[0] > 0) 
124         {
125                 $cancel_delete = 1;
126                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
127         } 
128         else 
129         {
130                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no='" . $_POST['customer_id'] . "'";
131                 $result = db_query($sql,"check failed");
132                 $myrow = db_fetch_row($result);
133                 if ($myrow[0] > 0) 
134                 {
135                         $cancel_delete = 1;
136                         display_error(_("Cannot delete the customer record because orders have been created against it."));
137                 } 
138                 else 
139                 {
140                         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
141                         $result = db_query($sql,"check failed");
142                         $myrow = db_fetch_row($result);
143                         if ($myrow[0] > 0) 
144                         {
145                                 $cancel_delete = 1;
146                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
147                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
148                         }
149                 }
150         }
151         
152         if ($cancel_delete == 0) 
153         {       //ie not cancelled the delete as a result of above tests
154                 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
155                 db_query($sql,"cannot delete customer");
156
157                 display_notification(_("Selected customer has been deleted."));
158                 unset($_POST['customer_id']);
159                 $new_customer = true;
160                 $Ajax->activate('_page_body');
161         } //end if Delete Customer
162 }
163
164
165 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
166  
167 start_form();
168
169 if (db_has_customers()) 
170 {
171         start_table("class = 'tablestyle_noborder'");
172         customer_list_row(_("Select a customer: "), 'customer_id', null,
173           _('New customer'), true);
174         end_table();
175
176 else 
177 {
178         hidden('customer_id');
179 }
180
181 start_table($table_style2, 7, 6);
182 echo "<tr valign=top><td>"; // outer table      
183
184
185 start_table("class='tablestyle_noborder'");     
186
187 if ($new_customer) 
188 {
189         $_POST['CustName'] = $_POST['address'] = $_POST['tax_id']  = '';
190         $_POST['dimension_id'] = 0;
191         $_POST['dimension2_id'] = 0;
192         $_POST['sales_type'] = -1;
193         $_POST['email'] = '';
194         $_POST['curr_code']  = get_company_currency();
195         $_POST['credit_status']  = -1;
196         $_POST['payment_terms']  = '';
197         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
198         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
199
200 else 
201 {
202
203         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
204         $result = db_query($sql,"check failed");
205
206         $myrow = db_fetch($result);
207
208         $_POST['CustName'] = $myrow["name"];
209         $_POST['address']  = $myrow["address"];
210         $_POST['tax_id']  = $myrow["tax_id"];
211         $_POST['email']  = $myrow["email"];
212         $_POST['dimension_id']  = $myrow["dimension_id"];
213         $_POST['dimension2_id']  = $myrow["dimension2_id"];
214         $_POST['sales_type'] = $myrow["sales_type"];
215         $_POST['curr_code']  = $myrow["curr_code"];
216         $_POST['credit_status']  = $myrow["credit_status"];
217         $_POST['payment_terms']  = $myrow["payment_terms"];
218         $_POST['discount']  = percent_format($myrow["discount"] * 100);
219         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
220         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
221 }
222
223 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
224 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
225
226 text_row(_("Email:"), 'email', null, 40, 40);
227 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
228
229
230 // Sherifoz 23.09.03 currency can't be changed if editing
231 if ($new_customer) 
232 {
233         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
234
235 else 
236 {
237         label_row(_("Customer's Currency:"), $_POST['curr_code']);
238         hidden('curr_code', $_POST['curr_code']);                               
239 }       
240 end_table();
241
242 echo "</td><td class='tableseparator'>"; // outer table
243
244 start_table("class='tablestyle_noborder'");     
245
246 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
247 $dim = get_company_pref('use_dimension');
248 if ($dim >= 1)
249         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
250 if ($dim > 1)
251         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
252 if ($dim < 1)
253         hidden('dimension_id', 0);
254 if ($dim < 2)
255         hidden('dimension2_id', 0);
256
257 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
258 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
259 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
260
261 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
262 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
263 if (!$new_customer)  {
264 start_row();
265  echo '<td>'._('Customer branches').':</td>';
266   hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",'<b>'. _("Add or Edit").'</b>', "debtor_no=".$_POST['customer_id']);
267 end_row();
268 }
269 end_table();
270
271 end_table(1); // outer table    
272 div_start('controls');
273 if ($new_customer)
274 {
275         submit_center('submit', _("Add New Customer"), true, '', true);
276
277 else 
278 {
279         submit_center_first('submit', _("Update Customer"), 
280           _('Update customer data'), true);
281         submit_center_last('delete', _("Delete Customer"), 
282           _('Delete customer data if have been never used'), true);
283 }
284 div_end();
285 end_form();
286 end_page();
287
288 ?>