Activated strict SQL mode, minor SQL injection fix, fixed _vl() debug helper.
[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 = 'SA_INVENTORYLOCATION';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16
17 include_once($path_to_root . "/includes/ui.inc");
18
19 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
20
21 if (isset($_GET['FixedAsset'])) {
22         $help_context = "Fixed Assets Locations";
23         $_POST['fixed_asset'] = 1;
24 } else
25         $help_context = "Inventory Locations";
26
27 page(_($help_context));
28
29 simple_page_mode(true);
30
31 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
32 {
33
34         //initialise no input errors assumed initially before we test
35         $input_error = 0;
36
37         /* actions to take once the user has clicked the submit button
38         ie the page has called itself with some user input */
39
40         //first off validate inputs sensible
41         $_POST['loc_code'] = strtoupper($_POST['loc_code']);
42
43         if ((strlen(db_escape($_POST['loc_code'])) > 7) || empty($_POST['loc_code'])) //check length after conversion
44         {
45                 $input_error = 1;
46                 display_error( _("The location code must be five characters or less long (including converted special chars)."));
47                 set_focus('loc_code');
48         } 
49         elseif (strlen($_POST['location_name']) == 0) 
50         {
51                 $input_error = 1;
52                 display_error( _("The location name must be entered."));                
53                 set_focus('location_name');
54         }
55
56         if ($input_error != 1) 
57         {
58         if ($selected_id != -1) 
59         {
60     
61                 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
62                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], $_POST['contact'], check_value('fixed_asset'));
63                         display_notification(_('Selected location has been updated'));
64         } 
65         else 
66         {
67     
68         /*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 */
69         
70                 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], 
71                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], $_POST['contact'], check_value('fixed_asset'));
72                         display_notification(_('New location has been added'));
73         }
74                 
75                 $Mode = 'RESET';
76         }
77
78
79 function can_delete($selected_id)
80 {
81         if (key_in_foreign_table($selected_id, 'stock_moves', 'loc_code'))
82         {
83                 display_error(_("Cannot delete this location because item movements have been created using this location."));
84                 return false;
85         }
86
87         if (key_in_foreign_table($selected_id, 'workorders', 'loc_code'))
88         {
89                 display_error(_("Cannot delete this location because it is used by some work orders records."));
90                 return false;
91         }
92
93         if (key_in_foreign_table($selected_id, 'cust_branch', 'default_location'))
94         {
95                 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
96                 return false;
97         }
98         
99         if (key_in_foreign_table($selected_id, 'bom', 'loc_code'))
100         {
101                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
102                 return false;
103         }
104         
105         if (key_in_foreign_table($selected_id, 'grn_batch', 'loc_code'))
106         {
107                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
108                 return false;
109         }
110         if (key_in_foreign_table($selected_id, 'purch_orders', 'into_stock_location'))
111         {
112                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
113                 return false;
114         }
115         if (key_in_foreign_table($selected_id, 'sales_orders', 'from_stk_loc'))
116         {
117                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
118                 return false;
119         }
120         if (key_in_foreign_table($selected_id, 'sales_pos', 'pos_location'))
121         {
122                 display_error(_("Cannot delete this location because it is used by some related records in other tables."));
123                 return false;
124         }
125         return true;
126 }
127
128 //----------------------------------------------------------------------------------
129
130 if ($Mode == 'Delete')
131 {
132
133         if (can_delete($selected_id)) 
134         {
135                 delete_item_location($selected_id);
136                 display_notification(_('Selected location has been deleted'));
137         } //end if Delete Location
138         $Mode = 'RESET';
139 }
140
141 if ($Mode == 'RESET')
142 {
143         $selected_id = -1;
144         $sav = get_post('show_inactive');
145         $sav2 = get_post('fixed_asset');
146         unset($_POST);
147         $_POST['show_inactive'] = $sav;
148         $_POST['fixed_asset'] = $sav2;
149 }
150
151 $result = get_item_locations(check_value('show_inactive'), get_post('fixed_asset', 0));
152
153 start_form();
154 start_table(TABLESTYLE);
155 $th = array(_("Location Code"), _("Location Name"), _("Address"), _("Phone"), _("Secondary Phone"), "", "");
156 inactive_control_column($th);
157 table_header($th);
158 $k = 0; //row colour counter
159 while ($myrow = db_fetch($result)) 
160 {
161
162         alt_table_row_color($k);
163         
164         label_cell($myrow["loc_code"]);
165         label_cell($myrow["location_name"]);
166         label_cell($myrow["delivery_address"]);
167         label_cell($myrow["phone"]);
168         label_cell($myrow["phone2"]);
169         inactive_control_cell($myrow["loc_code"], $myrow["inactive"], 'locations', 'loc_code');
170         edit_button_cell("Edit".$myrow["loc_code"], _("Edit"));
171         delete_button_cell("Delete".$myrow["loc_code"], _("Delete"));
172         end_row();
173 }
174         //END WHILE LIST LOOP
175 inactive_control_row($th);
176 end_table();
177
178 echo '<br>';
179
180 start_table(TABLESTYLE2);
181 hidden("fixed_asset");
182
183 $_POST['email'] = "";
184 if ($selected_id != -1) 
185 {
186         //editing an existing Location
187
188         if ($Mode == 'Edit') {
189                 $myrow = get_item_location($selected_id);
190
191                 $_POST['loc_code'] = $myrow["loc_code"];
192                 $_POST['location_name']  = $myrow["location_name"];
193                 $_POST['delivery_address'] = $myrow["delivery_address"];
194                 $_POST['contact'] = $myrow["contact"];
195                 $_POST['phone'] = $myrow["phone"];
196                 $_POST['phone2'] = $myrow["phone2"];
197                 $_POST['fax'] = $myrow["fax"];
198                 $_POST['email'] = $myrow["email"];
199         }
200         hidden("selected_id", $selected_id);
201         hidden("loc_code");
202         label_row(_("Location Code:"), $_POST['loc_code']);
203
204 else 
205 { //end of if $selected_id only do the else when a new record is being entered
206         text_row(_("Location Code:"), 'loc_code', null, 5, 5);
207 }
208
209 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
210 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
211
212 textarea_row(_("Address:"), 'delivery_address', null, 34, 5);   
213
214 text_row_ex(_("Telephone No:"), 'phone', 32, 30);
215 text_row_ex(_("Secondary Phone Number:"), 'phone2', 32, 30);
216 text_row_ex(_("Facsimile No:"), 'fax', 32, 30);
217 email_row_ex(_("E-mail:"), 'email', 50);
218
219 end_table(1);
220 submit_add_or_update_center($selected_id == -1, '', 'both');
221
222 end_form();
223
224 end_page();
225