DB error fix on new customer entry
[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 if (isset($_GET['New']) || !isset($_POST['customer_id']) || $_POST['customer_id'] == "") 
14 {
15         $_POST['New'] = "1";
16 }
17
18 if (isset($_POST['SelectCustomer'])) 
19 {
20         unset($_POST['New']);
21 }
22
23 //--------------------------------------------------------------------------------------------
24
25 function can_process()
26 {
27         if (strlen($_POST['CustName']) == 0) 
28         {
29                 display_error(_("The customer name cannot be empty."));
30                 return false;
31         } 
32         
33         if (!check_num('credit_limit', 0))
34         {
35                 display_error(_("The credit limit must be numeric and not less than zero."));
36                 return false;           
37         } 
38         
39         if (!check_num('pymt_discount', 0, 100)) 
40         {
41                 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
42                 return false;           
43         } 
44         
45         if (!check_num('discount', 0, 100)) 
46         {
47                 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
48                 return false;           
49         } 
50
51         return true;
52 }
53
54 //--------------------------------------------------------------------------------------------
55
56 function handle_submit()
57 {
58         global $path_to_root;
59         if (!can_process())
60                 return;
61                 
62         if (!isset($_POST['New'])) 
63         {
64
65                 $sql = "UPDATE ".TB_PREF."debtors_master SET name='" . $_POST['CustName'] . "', 
66                         address='" . $_POST['address'] . "', 
67                         tax_id='" . $_POST['tax_id'] . "', 
68                         curr_code='" . $_POST['curr_code'] . "', 
69                         email='" . $_POST['email'] . "', 
70                         dimension_id=" . $_POST['dimension_id'] . ", 
71                         dimension2_id=" . $_POST['dimension2_id'] . ", 
72             credit_status='" . $_POST['credit_status'] . "', 
73             payment_terms='" . $_POST['payment_terms'] . "', 
74             discount=" . input_num('discount') / 100 . ", 
75             pymt_discount=" . input_num('pymt_discount') / 100 . ", 
76             credit_limit=" . input_num('credit_limit') . ", 
77             sales_type = '" . $_POST['sales_type'] . "' 
78             WHERE debtor_no = '" . $_POST['customer_id'] . "'";
79
80                 db_query($sql,"The customer could not be updated");
81                 display_notification(_("Customer has been updated."));
82                 clear_fields();                 
83
84         } 
85         else 
86         {       //it is a new customer
87
88                 begin_transaction();
89
90                 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, address, tax_id, email, dimension_id, dimension2_id,  
91                         curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit, 
92                         sales_type) VALUES ('" . $_POST['CustName'] ."', '" . $_POST['address'] . "', '" . $_POST['tax_id'] . "',
93                         '" . $_POST['email'] . "', " . $_POST['dimension_id'] . ", " . $_POST['dimension2_id'] . ", '" . $_POST['curr_code'] . "', 
94                         " . $_POST['credit_status'] . ", '" . $_POST['payment_terms'] . "', " . input_num('discount')/100 . ", 
95                         " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", '" . $_POST['sales_type'] . "')";
96
97                 db_query($sql,"The customer could not be added");
98
99                 $new_customer_id = db_insert_id();
100                 
101                 commit_transaction();                   
102
103                 display_notification(_("A new customer has been added."));
104
105                 hyperlink_params($path_to_root . "/sales/manage/customer_branches.php", _("Add branches for this customer"), "debtor_no=$new_customer_id");
106
107                 clear_fields();
108         }
109 }
110
111 //--------------------------------------------------------------------------------------------
112
113 if (isset($_POST['submit'])) 
114 {
115         
116         handle_submit();
117 }
118
119 //-------------------------------------------------------------------------------------------- 
120
121 if (isset($_POST['delete'])) 
122 {
123
124         //the link to delete a selected record was clicked instead of the submit button
125
126         $cancel_delete = 0;
127
128         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
129
130         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans 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(_("This customer cannot be deleted because there are transactions that refer to it."));
137         } 
138         else 
139         {
140                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders 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 the customer record because orders have been created against it."));
147                 } 
148                 else 
149                 {
150                         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no='" . $_POST['customer_id'] . "'";
151                         $result = db_query($sql,"check failed");
152                         $myrow = db_fetch_row($result);
153                         if ($myrow[0] > 0) 
154                         {
155                                 $cancel_delete = 1;
156                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
157                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
158                         }
159                 }
160         }
161         
162         if ($cancel_delete == 0) 
163         {       //ie not cancelled the delete as a result of above tests
164                 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no='" . $_POST['customer_id'] . "'";
165                 db_query($sql,"cannot delete customer");
166                 
167                 meta_forward($_SERVER['PHP_SELF']); 
168         } //end if Delete Customer
169 }
170
171 function clear_fields() 
172 {
173         unset($_POST['CustName']);
174         unset($_POST['address']);
175         unset($_POST['tax_id']);
176         unset($_POST['email']);
177         unset($_POST['dimension_id']);
178         unset($_POST['dimension2_id']);
179         unset($_POST['credit_status']);
180         unset($_POST['payment_terms']);
181         unset($_POST['discount']);
182         unset($_POST['pymt_discount']);
183         unset($_POST['credit_limit']);
184         unset($_POST['sales_type']);
185         unset($_POST['customer_id']);
186         $_POST['New'] = 1;
187 }
188
189 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
190  
191 start_form();
192
193 if (db_has_customers()) 
194 {
195         start_table("class = 'tablestyle_noborder'");
196         start_row();
197         customer_list_cells(_("Select a customer: "), 'customer_id', null);
198         submit_cells('SelectCustomer', _("Edit Customer"));
199         end_row();
200         end_table();
201
202 else 
203 {
204         hidden('customer_id', $_POST['customer_id']);
205 }
206
207 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new customer"), "New=1");
208 echo "<br>";     
209
210 start_table($table_style2, 7, 6);
211 echo "<tr valign=top><td>"; // outer table      
212
213 start_table("class='tablestyle_noborder'");     
214
215 if (isset($_POST['New'])) 
216 {
217
218         hidden('New', 'Yes');
219
220         $_POST['CustName'] = $_POST['address'] = $_POST['tax_id']  = '';
221         $_POST['dimension_id'] = 0;
222         $_POST['dimension2_id'] = 0;
223         $_POST['sales_type'] = -1;
224         $_POST['curr_code']  = get_company_currency();
225         $_POST['credit_status']  = -1;
226         $_POST['payment_terms']  = '';
227         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
228         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
229
230 else 
231 {
232
233         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
234         $result = db_query($sql,"check failed");
235
236         $myrow = db_fetch($result);
237
238         $_POST['CustName'] = $myrow["name"];
239         $_POST['address']  = $myrow["address"];
240         $_POST['tax_id']  = $myrow["tax_id"];
241         $_POST['email']  = $myrow["email"];
242         $_POST['dimension_id']  = $myrow["dimension_id"];
243         $_POST['dimension2_id']  = $myrow["dimension2_id"];
244         $_POST['sales_type'] = $myrow["sales_type"];
245         $_POST['curr_code']  = $myrow["curr_code"];
246         $_POST['credit_status']  = $myrow["credit_status"];
247         $_POST['payment_terms']  = $myrow["payment_terms"];
248         $_POST['discount']  = percent_format($myrow["discount"] * 100);
249         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
250         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
251 }
252
253 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
254 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
255
256 text_row(_("Email:"), 'email', null, 40, 40);
257 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
258
259
260 // Sherifoz 23.09.03 currency can't be changed if editing
261 if (isset($_POST['New'])) 
262 {
263         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
264
265 else 
266 {
267         label_row(_("Customer's Currency:"), $_POST['curr_code']);
268         hidden('curr_code', $_POST['curr_code']);                               
269 }       
270 end_table();
271
272 echo "</td><td class='tableseparator'>"; // outer table
273
274 start_table("class='tablestyle_noborder'");     
275
276 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
277 $dim = get_company_pref('use_dimension');
278 if ($dim >= 1)
279         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
280 if ($dim > 1)
281         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
282 if ($dim < 1)
283         hidden('dimension_id', 0);
284 if ($dim < 2)
285         hidden('dimension2_id', 0);
286
287 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
288 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
289 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
290
291 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
292 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
293
294 end_table();
295
296 end_table(1); // outer table    
297
298 if (isset($_POST['New'])) 
299 {
300         submit_center('submit', _("Add New Customer"));
301
302 else 
303 {
304         submit_center_first('submit', _("Update Customer"));
305         submit_center_last('delete', _("Delete Customer"));
306 }
307
308 end_form();
309 end_page();
310
311 ?>