Include file conflict fix
[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['New']) || !isset($_POST['supplier_id'])) 
16 {
17         $_POST['New'] = "1";
18 }
19
20 if (isset($_POST['SelectSupplier'])) 
21 {
22         unset($_POST['New']);
23 }
24
25 if (isset($_POST['submit'])) 
26 {
27
28         //initialise no input errors assumed initially before we test
29         $input_error = 0;
30
31         /* actions to take once the user has clicked the submit button
32         ie the page has called itself with some user input */
33
34         //first off validate inputs sensible
35
36         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
37         {
38                 $input_error = 1;
39                 display_error(_("The supplier name must be entered."));
40         }
41
42         if ($input_error !=1 )
43         {
44
45                 if (!isset($_POST['New'])) 
46                 {
47
48                         $sql = "UPDATE ".TB_PREF."suppliers SET supp_name='" . $_POST['supp_name'] . "',
49                 address='" . $_POST['address'] . "',
50                 email='" . $_POST['email'] . "',
51                 bank_account='" . $_POST['bank_account'] . "',
52                 dimension_id=" . $_POST['dimension_id'] . ",
53                 dimension2_id=" . $_POST['dimension2_id'] . ",
54                 curr_code='" . $_POST['curr_code'] . "',
55                 payment_terms='" . $_POST['payment_terms'] . "',
56                                 payable_account='" . $_POST['payable_account'] . "',
57                                 purchase_account='" . $_POST['purchase_account'] . "',
58                                 payment_discount_account='" . $_POST['payment_discount_account'] . "',
59                                 tax_group_id=" . $_POST['tax_group_id'] . " WHERE supplier_id = '" . $_POST['supplier_id'] . "'";
60
61                         db_query($sql,"The supplier could not be updated");
62
63                 } 
64                 else 
65                 { //not a new supplier
66
67                         $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, address, email, bank_account, dimension_id, dimension2_id, curr_code,
68                                 payment_terms, payable_account, purchase_account, payment_discount_account, tax_group_id)
69                                 VALUES ('" . $_POST['supp_name'] . "', '" .
70                                 $_POST['address'] . "', '" .
71                                 $_POST['email'] . "', '" .
72                                 $_POST['bank_account'] . "', " .
73                                 $_POST['dimension_id'] . ", " .
74                                 $_POST['dimension2_id'] . ", '" .
75                                 $_POST['curr_code'] . "', '" .
76                                 $_POST['payment_terms'] . "', '" .
77                                 $_POST['payable_account'] . "', '" .
78                                 $_POST['purchase_account'] . "', '" .
79                                 $_POST['payment_discount_account'] . "', " .
80                                 $_POST['tax_group_id'] . ")";
81
82                         db_query($sql,"The supplier could not be added");
83                 }
84
85                 meta_forward($_SERVER['PHP_SELF']);
86         }
87
88
89 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
90 {
91         //the link to delete a selected record was clicked instead of the submit button
92
93         $cancel_delete = 0;
94
95         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
96
97         $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_POST['supplier_id'] . "'";
98         $result = db_query($sql,"check failed");
99         $myrow = db_fetch_row($result);
100         if ($myrow[0] > 0) 
101         {
102                 $cancel_delete = 1;
103                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
104
105         } 
106         else 
107         {
108                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id='" . $_POST['supplier_id'] . "'";
109                 $result = db_query($sql,"check failed");
110                 $myrow = db_fetch_row($result);
111                 if ($myrow[0] > 0) 
112                 {
113                         $cancel_delete = 1;
114                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
115                 }
116
117         }
118         if ($cancel_delete == 0) 
119         {
120                 $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id='" . $_POST['supplier_id']. "'";
121                 db_query($sql,"check failed");
122
123                 unset($_SESSION['supplier_id']);
124                 meta_forward($_SERVER['PHP_SELF']);
125         } //end if Delete supplier
126 }
127
128 start_form();
129
130 if (db_has_suppliers()) 
131 {
132         start_table("", 3);
133         start_row();
134         supplier_list_cells(_("Select a supplier: "), 'supplier_id', null);
135         submit_cells('SelectSupplier', _("Edit Supplier"));
136         end_row();
137         end_table();
138
139 else 
140 {
141         hidden('supplier_id', $_POST['supplier_id']);
142 }
143
144 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new supplier"), "New=1");
145 echo "<br>";
146
147 //start_table("class='tablestyle2'", 0, 3);
148 start_table("class='tablestyle'", 3);
149
150 table_section_title(_("Supplier"));
151
152 if (isset($_POST['supplier_id']) && !isset($_POST['New'])) 
153 {
154         //SupplierID exists - either passed when calling the form or from the form itself
155         $myrow = get_supplier($_POST['supplier_id']);
156
157         $_POST['supp_name'] = $myrow["supp_name"];
158         $_POST['address']  = $myrow["address"];
159         $_POST['email']  = $myrow["email"];
160         $_POST['bank_account']  = $myrow["bank_account"];
161         $_POST['dimension_id']  = $myrow["dimension_id"];
162         $_POST['dimension2_id']  = $myrow["dimension2_id"];
163         $_POST['curr_code']  = $myrow["curr_code"];
164         $_POST['payment_terms']  = $myrow["payment_terms"];
165         $_POST['tax_group_id'] = $myrow["tax_group_id"];
166         $_POST['payable_account']  = $myrow["payable_account"];
167         $_POST['purchase_account']  = $myrow["purchase_account"];
168         $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
169
170
171 else 
172 {
173         // its a new supplier being added
174         hidden('New', 'Yes');
175
176         $company_record = get_company_prefs();
177
178         $_POST['payable_account'] = $company_record["creditors_act"];
179         $_POST['purchase_account'] = $company_record["default_cogs_act"];
180         $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
181 }
182
183 text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
184 textarea_row(_("Address:"), 'address', null, 35, 5);
185 text_row(_("Email:"), 'email', null, 42, 40);
186 text_row(_("Bank Account:"), 'bank_account', null, 42, 40);
187
188 // Sherifoz 23.09.03 currency can't be changed if editing
189 if (isset($_POST['supplier_id']) && !isset($_POST['New'])) 
190 {
191         label_row(_("Supplier's Currency:"), $_POST['curr_code']);
192         hidden('curr_code', $_POST['curr_code']);
193
194 else 
195 {
196         currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
197 }
198
199 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
200
201 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
202
203 table_section_title(_("Accounts"));
204
205 gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
206
207 gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
208
209 gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
210
211 $dim = get_company_pref('use_dimension');
212 if ($dim >= 1)
213 {
214         table_section_title(_("Dimension"));
215
216         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
217         if ($dim > 1)
218                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
219 }
220 if ($dim < 1)
221         hidden('dimension_id', 0);
222 if ($dim < 2)
223         hidden('dimension2_id', 0);
224
225 end_table(1);
226
227 if (!isset($_POST['New'])) 
228 {
229         submit_center_first('submit', _("Update Supplier"));
230         submit_center_last('delete', _("Delete Supplier"));
231 }
232 else 
233 {
234         submit_center('submit', _("Add New Supplier Details"));
235 }
236
237 end_form();
238
239 end_page();
240
241 ?>