Security statements update against sql injection attacks.
[fa-stable.git] / inventory / manage / locations.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 11;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Inventory Locations"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19
20 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
21
22 simple_page_mode(true);
23
24 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
25 {
26
27         //initialise no input errors assumed initially before we test
28         $input_error = 0;
29
30         /* actions to take once the user has clicked the submit button
31         ie the page has called itself with some user input */
32
33         //first off validate inputs sensible
34         $_POST['loc_code'] = strtoupper($_POST['loc_code']);
35
36         if (strlen(db_escape($_POST['loc_code'])) > 7) //check length after conversion
37         {
38                 $input_error = 1;
39                 display_error( _("The location code must be five characters or less long (including converted special chars)."));
40                 set_focus('loc_code');
41         } 
42         elseif (strlen($_POST['location_name']) == 0) 
43         {
44                 $input_error = 1;
45                 display_error( _("The location name must be entered."));                
46                 set_focus('location_name');
47         }
48
49         if ($input_error != 1) 
50         {
51         if ($selected_id != -1) 
52         {
53     
54                 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
55                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);    
56                         display_notification(_('Selected location has been updated'));
57         } 
58         else 
59         {
60     
61         /*selected_id is null cos no item selected on first time round so must be adding a      record must be submitting new entries in the new Location form */
62         
63                 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], 
64                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
65                         display_notification(_('New location has been added'));
66         }
67                 
68                 $Mode = 'RESET';
69         }
70
71
72 function can_delete($selected_id)
73 {
74         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE loc_code=".db_escape($selected_id);
75         $result = db_query($sql, "could not query stock moves");
76         $myrow = db_fetch_row($result);
77         if ($myrow[0] > 0) 
78         {
79                 display_error(_("Cannot delete this location because item movements have been created using this location."));
80                 return false;
81         }
82
83         $sql= "SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE loc_code=".db_escape($selected_id);
84         $result = db_query($sql, "could not query work orders");
85         $myrow = db_fetch_row($result);
86         if ($myrow[0] > 0) 
87         {
88                 display_error(_("Cannot delete this location because it is used by some work orders records."));
89                 return false;
90         }
91
92         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE default_location='$selected_id'";
93         $result = db_query($sql, "could not query customer branches");
94         $myrow = db_fetch_row($result);
95         if ($myrow[0] > 0) 
96         {
97                 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
98                 return false;
99         }
100         
101         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE loc_code=".db_escape($selected_id);
102         $result = db_query($sql, "could not query bom");
103         $myrow = db_fetch_row($result);
104         if ($myrow[0] > 0) 
105         {
106                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
107                 return false;
108         }
109         $sql= "SELECT COUNT(*) FROM ".TB_PREF."grn_batch WHERE loc_code=".db_escape($selected_id);
110         $result = db_query($sql, "could not query grn batch");
111         $myrow = db_fetch_row($result);
112         if ($myrow[0] > 0) 
113         {
114                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
115                 return false;
116         }
117         $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE into_stock_location=".db_escape($selected_id);
118         $result = db_query($sql, "could not query purch orders");
119         $myrow = db_fetch_row($result);
120         if ($myrow[0] > 0) 
121         {
122                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
123                 return false;
124         }
125         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE from_stk_loc=".db_escape($selected_id);
126         $result = db_query($sql, "could not query sales orders");
127         $myrow = db_fetch_row($result);
128         if ($myrow[0] > 0) 
129         {
130                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
131                 return false;
132         }
133         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_location=".db_escape($selected_id);
134         $result = db_query($sql, "could not query sales pos");
135         $myrow = db_fetch_row($result);
136         if ($myrow[0] > 0) 
137         {
138                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
139                 return false;
140         }
141         return true;
142 }
143
144 //----------------------------------------------------------------------------------
145
146 if ($Mode == 'Delete')
147 {
148
149         if (can_delete($selected_id)) 
150         {
151                 delete_item_location($selected_id);
152                 display_notification(_('Selected location has been deleted'));
153         } //end if Delete Location
154         $Mode = 'RESET';
155 }
156
157 if ($Mode == 'RESET')
158 {
159         $selected_id = -1;
160         unset($_POST);
161 }
162
163 $sql = "SELECT * FROM ".TB_PREF."locations";
164 $result = db_query($sql, "could not query locations");;
165
166 start_form();
167 start_table("$table_style width=30%");
168 $th = array(_("Location Code"), _("Location Name"), "", "");
169 table_header($th);
170 $k = 0; //row colour counter
171 while ($myrow = db_fetch_row($result)) 
172 {
173
174         alt_table_row_color($k);
175         
176         label_cell($myrow[0]);
177         label_cell($myrow[1]);
178         edit_button_cell("Edit".$myrow[0], _("Edit"));
179         delete_button_cell("Delete".$myrow[0], _("Delete"));
180         end_row();
181 }
182         //END WHILE LIST LOOP
183
184 //end of ifs and buts!
185
186 end_table();
187
188 end_form();
189 echo '<br>';
190
191 start_form();
192
193 start_table($table_style2);
194
195 $_POST['email'] = "";
196 if ($selected_id != -1) 
197 {
198         //editing an existing Location
199
200         if ($Mode == 'Edit') {
201                 $myrow = get_item_location($selected_id);
202
203                 $_POST['loc_code'] = $myrow["loc_code"];
204                 $_POST['location_name']  = $myrow["location_name"];
205                 $_POST['delivery_address'] = $myrow["delivery_address"];
206                 $_POST['contact'] = $myrow["contact"];
207                 $_POST['phone'] = $myrow["phone"];
208                 $_POST['fax'] = $myrow["fax"];
209                 $_POST['email'] = $myrow["email"];
210         }
211         hidden("selected_id", $selected_id);
212         hidden("loc_code");
213         label_row(_("Location Code:"), $_POST['loc_code']);
214
215 else 
216 { //end of if $selected_id only do the else when a new record is being entered
217         text_row(_("Location Code:"), 'loc_code', null, 5, 5);
218 }
219
220 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
221 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
222
223 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);   
224
225 text_row_ex(_("Telephone No:"), 'phone', 30, 30);
226 text_row_ex(_("Facsimile No:"), 'fax', 30, 30);
227 email_row_ex(_("E-mail:"), 'email', 30);
228
229 end_table(1);
230 submit_add_or_update_center($selected_id == -1, '', true);
231
232 end_form();
233
234 end_page();
235
236 ?>