New fields in suppliers, phone, fax, vat no, contact and credit limit
[fa-stable.git] / purchasing / manage / suppliers.php
1 <?php
2
3 $page_security=5;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Suppliers"));
8
9 //include($path_to_root . "/includes/date_functions.inc");
10
11 include($path_to_root . "/includes/ui.inc");
12
13 check_db_has_tax_groups(_("There are no tax groups defined in the system. At least one tax group is required before proceeding."));
14
15 if (isset($_GET['supplier_id'])) 
16 {
17         $_POST['supplier_id'] = $_GET['supplier_id'];
18 }
19 $new_supplier = get_post('supplier_id') == ''; 
20
21 if (isset($_POST['submit'])) 
22 {
23
24         //initialise no input errors assumed initially before we test
25         $input_error = 0;
26
27         /* actions to take once the user has clicked the submit button
28         ie the page has called itself with some user input */
29
30         //first off validate inputs sensible
31
32         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
33         {
34                 $input_error = 1;
35                 display_error(_("The supplier name must be entered."));
36                 set_focus('supp_name');
37         }
38
39         if ($input_error !=1 )
40         {
41
42                 if (!$new_supplier) 
43                 {
44
45                         $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($_POST['supp_name']) . ",
46                 address=".db_escape($_POST['address']) . ",
47                 supp_address=".db_escape($_POST['supp_address']) . ",
48                 phone=".db_escape($_POST['phone']) . ",
49                 fax=".db_escape($_POST['fax']) . ",
50                 gst_no=".db_escape($_POST['gst_no']) . ",
51                 email=".db_escape($_POST['email']) . ",
52                 contact=".db_escape($_POST['contact']) . ",
53                 bank_account=".db_escape($_POST['bank_account']) . ",
54                 credit_limit=".db_escape($_POST['credit_limit']) . ",
55                 dimension_id=".db_escape($_POST['dimension_id']) . ",
56                 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
57                 curr_code=".db_escape($_POST['curr_code']).",
58                 payment_terms=".db_escape($_POST['payment_terms']) . ",
59                                 payable_account=".db_escape($_POST['payable_account']) . ",
60                                 purchase_account=".db_escape($_POST['purchase_account']) . ",
61                                 payment_discount_account=".db_escape($_POST['payment_discount_account']) . ",
62                                 tax_group_id=".db_escape($_POST['tax_group_id']) . " WHERE supplier_id = '" . $_POST['supplier_id'] . "'";
63
64                         db_query($sql,"The supplier could not be updated");
65                         display_notification(_("Supplier has been updated."));
66                 } 
67                 else 
68                 {
69
70                         $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, address, supp_address, phone, fax, gst_no, email, 
71                                 contact, bank_account, credit_limit, dimension_id, dimension2_id, curr_code,
72                                 payment_terms, payable_account, purchase_account, payment_discount_account, tax_group_id)
73                                 VALUES (".db_escape($_POST['supp_name']). ", "
74                                 .db_escape($_POST['address']) . ", "
75                                 .db_escape($_POST['supp_address']) . ", "
76                                 .db_escape($_POST['phone']). ", "
77                                 .db_escape($_POST['fax']). ", "
78                                 .db_escape($_POST['gst_no']). ", "
79                                 .db_escape($_POST['email']). ", "
80                                 .db_escape($_POST['contact']). ", "
81                                 .db_escape($_POST['bank_account']). ", "
82                                 .db_escape($_POST['credit_limit']). ", "
83                                 .db_escape($_POST['dimension_id']). ", "
84                                 .db_escape($_POST['dimension2_id']). ", "
85                                 .db_escape($_POST['curr_code']). ", "
86                                 .db_escape($_POST['payment_terms']). ", "
87                                 .db_escape($_POST['payable_account']). ", "
88                                 .db_escape($_POST['purchase_account']). ", "
89                                 .db_escape($_POST['payment_discount_account']). ", "
90                                 .db_escape($_POST['tax_group_id']). ")";
91
92                         db_query($sql,"The supplier could not be added");
93                         $_POST['supplier_id'] = db_insert_id();
94                         $new_supplier = false;
95                         display_notification(_("A new supplier has been added."));
96                         $Ajax->activate('_page_body');
97                 }
98         }
99
100
101 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
102 {
103         //the link to delete a selected record was clicked instead of the submit button
104
105         $cancel_delete = 0;
106
107         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
108
109         $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_POST['supplier_id'] . "'";
110         $result = db_query($sql,"check failed");
111         $myrow = db_fetch_row($result);
112         if ($myrow[0] > 0) 
113         {
114                 $cancel_delete = 1;
115                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
116
117         } 
118         else 
119         {
120                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id='" . $_POST['supplier_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(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
127                 }
128
129         }
130         if ($cancel_delete == 0) 
131         {
132                 $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id='" . $_POST['supplier_id']. "'";
133                 db_query($sql,"check failed");
134
135                 unset($_SESSION['supplier_id']);
136                 $new_supplier = true;
137                 $Ajax->activate('_page_body');
138         } //end if Delete supplier
139 }
140 elseif (isset($_POST['select']))
141 {
142         context_return(array('supplier_id' => $_POST['supplier_id']));
143 }
144
145 start_form();
146
147 if (db_has_suppliers()) 
148 {
149         start_table("", 3);
150 //      start_table("class = 'tablestyle_noborder'");
151         supplier_list_row(_("Select a supplier: "), 'supplier_id', null,
152                   _('New supplier'), true);
153         end_table();
154
155 else 
156 {
157         hidden('supplier_id', get_post('supplier_id'));
158 }
159
160 //start_table("class='tablestyle2'", 0, 3);
161 start_table("class='tablestyle'", 3);
162
163 table_section_title(_("Supplier"));
164
165 if (!$new_supplier) 
166 {
167         //SupplierID exists - either passed when calling the form or from the form itself
168         $myrow = get_supplier($_POST['supplier_id']);
169
170         $_POST['supp_name'] = $myrow["supp_name"];
171         $_POST['address']  = $myrow["address"];
172         $_POST['supp_address']  = $myrow["supp_address"];
173         $_POST['phone']  = $myrow["phone"];
174         $_POST['fax']  = $myrow["fax"];
175         $_POST['gst_no']  = $myrow["gst_no"];
176         $_POST['email']  = $myrow["email"];
177         $_POST['contact']  = $myrow["contact"];
178         $_POST['bank_account']  = $myrow["bank_account"];
179         $_POST['dimension_id']  = $myrow["dimension_id"];
180         $_POST['dimension2_id']  = $myrow["dimension2_id"];
181         $_POST['curr_code']  = $myrow["curr_code"];
182         $_POST['payment_terms']  = $myrow["payment_terms"];
183         $_POST['credit_limit']  = $myrow["credit_limit"];
184         $_POST['tax_group_id'] = $myrow["tax_group_id"];
185         $_POST['payable_account']  = $myrow["payable_account"];
186         $_POST['purchase_account']  = $myrow["purchase_account"];
187         $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
188
189
190 else 
191 {
192         $_POST['supp_name'] = $_POST['address'] = $_POST['supp_address'] = $_POST['tax_group_id']  = '';
193         $_POST['dimension_id'] = 0;
194         $_POST['dimension2_id'] = 0;
195         $_POST['sales_type'] = -1;
196         $_POST['email'] = $_POST['phone'] = $_POST['fax'] = $_POST['gst_no'] = $_POST['contact'] = $_POST['bank_account'] = '';
197         $_POST['payment_terms']  = '';
198         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
199
200         $company_record = get_company_prefs();
201         $_POST['curr_code']  = $company_record["curr_default"];
202         $_POST['payable_account'] = $company_record["creditors_act"];
203         $_POST['purchase_account'] = $company_record["default_cogs_act"];
204         $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
205 }
206
207 text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
208 textarea_row(_("Postal ddress:"), 'address', null, 35, 5);
209 textarea_row(_("Physical Address:"), 'supp_address', null, 35, 5);
210 text_row(_("Phone:"), 'phone', null, 42, 40);
211 text_row(_("Fax:"), 'fax', null, 42, 40);
212 text_row(_("GSTNo:"), 'gst_no', null, 42, 40);
213 text_row(_("Email:"), 'email', null, 42, 40);
214 text_row(_("Contact:"), 'contact', null, 42, 40);
215 text_row(_("Bank Name/Account:"), 'bank_account', null, 42, 40);
216 amount_row(_("Credit Limit:"), 'credit_limit', null);
217 // Sherifoz 23.09.03 currency can't be changed if editing
218 if (!$new_supplier) 
219 {
220         label_row(_("Supplier's Currency:"), $_POST['curr_code']);
221         hidden('curr_code', $_POST['curr_code']);
222
223 else 
224 {
225         currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
226 }
227
228 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
229
230 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
231
232 table_section_title(_("Accounts"));
233
234 gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
235
236 gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
237
238 gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
239
240 $dim = get_company_pref('use_dimension');
241 if ($dim >= 1)
242 {
243         table_section_title(_("Dimension"));
244
245         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
246         if ($dim > 1)
247                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
248 }
249 if ($dim < 1)
250         hidden('dimension_id', 0);
251 if ($dim < 2)
252         hidden('dimension2_id', 0);
253
254 end_table(1);
255 div_start('controls');
256 if (!$new_supplier) 
257 {
258         submit_center_first('submit', _("Update Supplier"), 
259           _('Update supplier data'), true);
260         submit_return('select', _("Return"), _("Select this supplier and return to document entry."), true);
261         submit_center_last('delete', _("Delete Supplier"), 
262           _('Delete supplier data if have been never used'), true);
263 }
264 else 
265 {
266         submit_center('submit', _("Add New Supplier Details"), true, '', true);
267 }
268 div_end();
269 end_form();
270
271 end_page();
272
273 ?>