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