6bb732fcb3ae2c10b4b775b46fe8293c8dfd3c37
[fa-stable.git] / inventory / manage / locations.php
1 <?php
2
3 $page_security = 11;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Inventory Locations"));
8
9 include_once($path_to_root . "/includes/ui.inc");
10
11 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
12
13 if (isset($_GET['selected_id']))
14 {
15         $selected_id = $_GET['selected_id'];
16
17 elseif (isset($_POST['selected_id']))
18 {
19         $selected_id = $_POST['selected_id'];
20 }
21
22 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
23 {
24
25         //initialise no input errors assumed initially before we test
26         $input_error = 0;
27
28         /* actions to take once the user has clicked the submit button
29         ie the page has called itself with some user input */
30
31         //first off validate inputs sensible
32         $_POST['loc_code'] = strtoupper($_POST['loc_code']);
33
34         if (strlen($_POST['loc_code']) > 5) 
35         {
36                 $input_error = 1;
37                 display_error( _("The location code must be five characters or less long."));
38                 set_focus('loc_code');
39         } 
40         elseif (strlen($_POST['location_name']) == 0) 
41         {
42                 $input_error = 1;
43                 display_error( _("The location name must be entered."));                
44                 set_focus('location_name');
45         }
46
47         if ($input_error != 1) 
48         {
49         if (isset($selected_id)) 
50         {
51     
52                 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
53                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);    
54         } 
55         else 
56         {
57     
58         /*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 */
59         
60                 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], 
61                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
62         }
63                 
64                 meta_forward($_SERVER['PHP_SELF']);     
65         }
66
67
68 function can_delete($selected_id)
69 {
70         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE loc_code='$selected_id'";
71         $result = db_query($sql, "could not query stock moves");
72         $myrow = db_fetch_row($result);
73         if ($myrow[0] > 0) 
74         {
75                 display_error(_("Cannot delete this location because item movements have been created using this location."));
76                 return false;
77         }
78
79         $sql= "SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE loc_code='$selected_id'";
80         $result = db_query($sql, "could not query work orders");
81         $myrow = db_fetch_row($result);
82         if ($myrow[0] > 0) 
83         {
84                 display_error(_("Cannot delete this location because it is used by some work orders records."));
85                 return false;
86         }
87
88         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE default_location='$selected_id'";
89         $result = db_query($sql, "could not query customer branches");
90         $myrow = db_fetch_row($result);
91         if ($myrow[0] > 0) 
92         {
93                 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
94                 return false;
95         }
96         
97         return true;
98 }
99
100 //----------------------------------------------------------------------------------
101
102 if (isset($_GET['delete'])) 
103 {
104
105         if (can_delete($selected_id)) 
106         {
107                 delete_item_location($selected_id);
108                 meta_forward($_SERVER['PHP_SELF']);             
109         } //end if Delete Location
110 }
111
112 /* It could still be the second time the page has been run and a record has been selected for modification - selected_id will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
113 then none of the above are true and the list of locations will be displayed with
114 links to delete or edit each. These will call the same page again and allow update/input
115 or deletion of the records*/
116
117 $sql = "SELECT * FROM ".TB_PREF."locations";
118 $result = db_query($sql, "could not query locations");;
119
120 start_table("$table_style width=30%");
121 $th = array(_("Location Code"), _("Location Name"), "", "");
122 table_header($th);
123 $k = 0; //row colour counter
124 while ($myrow = db_fetch_row($result)) 
125 {
126
127         alt_table_row_color($k);
128         
129         label_cell($myrow[0]);
130         label_cell($myrow[1]);
131         edit_link_cell("selected_id=$myrow[0]");
132         delete_link_cell("selected_id=$myrow[0]&delete=1");
133         end_row();
134 }
135         //END WHILE LIST LOOP
136
137 //end of ifs and buts!
138
139 end_table();
140
141 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Location"));
142
143 start_form();
144
145 start_table($table_style2);
146 if (isset($selected_id)) 
147 {
148         //editing an existing Location
149
150         $myrow = get_item_location($selected_id);
151
152         $_POST['loc_code'] = $myrow["loc_code"];
153         $_POST['location_name']  = $myrow["location_name"];
154         $_POST['delivery_address'] = $myrow["delivery_address"];
155         $_POST['contact'] = $myrow["contact"];
156         $_POST['phone'] = $myrow["phone"];
157         $_POST['fax'] = $myrow["fax"];
158         $_POST['email'] = $myrow["email"];
159
160         hidden("selected_id", $selected_id);
161         hidden("loc_code", $_POST['loc_code']);
162         label_row(_("Location Code:"), $_POST['loc_code']);
163
164 else 
165 { //end of if $selected_id only do the else when a new record is being entered
166         text_row(_("Location Code:"), 'loc_code', null, 5, 5);
167 }
168
169 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
170 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
171
172 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);   
173
174 text_row_ex(_("Telephone No:"), 'phone', 30, 30);
175 text_row_ex(_("Facsimile No:"), 'fax', 30, 30);
176 text_row_ex(_("Email:"), 'email', 30, 30);
177
178 end_table(1);
179 submit_add_or_update_center(!isset($selected_id));
180
181 end_form();
182
183 //end if record deleted no point displaying form to add record 
184  
185  end_page();
186
187 ?>