*** empty log message ***
[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         } 
39         elseif (strlen($_POST['location_name']) == 0) 
40         {
41                 $input_error = 1;
42                 display_error( _("The location name must be entered."));                
43         }
44
45         if ($input_error != 1) 
46         {
47         if (isset($selected_id)) 
48         {
49     
50                 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
51                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);    
52         } 
53         else 
54         {
55     
56         /*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 */
57         
58                 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], 
59                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
60         }
61                 
62                 meta_forward($_SERVER['PHP_SELF']);     
63         }
64
65
66 function can_delete($selected_id)
67 {
68         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE loc_code='$selected_id'";
69         $result = db_query($sql, "could not query stock moves");
70         $myrow = db_fetch_row($result);
71         if ($myrow[0] > 0) 
72         {
73                 display_error(_("Cannot delete this location because item movements have been created using this location."));
74                 return false;
75         }
76
77         $sql= "SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE loc_code='$selected_id'";
78         $result = db_query($sql, "could not query work orders");
79         $myrow = db_fetch_row($result);
80         if ($myrow[0] > 0) 
81         {
82                 display_error(_("Cannot delete this location because it is used by some work orders records."));
83                 return false;
84         }
85
86         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE default_location='$selected_id'";
87         $result = db_query($sql, "could not query customer branches");
88         $myrow = db_fetch_row($result);
89         if ($myrow[0] > 0) 
90         {
91                 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
92                 return false;
93         }
94         
95         return true;
96 }
97
98 //----------------------------------------------------------------------------------
99
100 if (isset($_GET['delete'])) 
101 {
102
103         if (can_delete($selected_id)) 
104         {
105                 delete_item_location($selected_id);
106                 meta_forward($_SERVER['PHP_SELF']);             
107         } //end if Delete Location
108 }
109
110 /* 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
111 then none of the above are true and the list of locations will be displayed with
112 links to delete or edit each. These will call the same page again and allow update/input
113 or deletion of the records*/
114
115 $sql = "SELECT * FROM ".TB_PREF."locations";
116 $result = db_query($sql, "could not query locations");;
117
118 start_table("$table_style width=30%");
119 $th = array(_("Location Code"), _("Location Name"), "", "");
120 table_header($th);
121 $k = 0; //row colour counter
122 while ($myrow = db_fetch_row($result)) 
123 {
124
125         alt_table_row_color($k);
126         
127         label_cell($myrow[0]);
128         label_cell($myrow[1]);
129         edit_link_cell("selected_id=$myrow[0]");
130         delete_link_cell("selected_id=$myrow[0]&delete=1");
131         end_row();
132 }
133         //END WHILE LIST LOOP
134
135 //end of ifs and buts!
136
137 end_table();
138
139 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Location"));
140
141 start_form();
142
143 start_table($table_style2);
144 if (isset($selected_id)) 
145 {
146         //editing an existing Location
147
148         $myrow = get_item_location($selected_id);
149
150         $_POST['loc_code'] = $myrow["loc_code"];
151         $_POST['location_name']  = $myrow["location_name"];
152         $_POST['delivery_address'] = $myrow["delivery_address"];
153         $_POST['contact'] = $myrow["contact"];
154         $_POST['phone'] = $myrow["phone"];
155         $_POST['fax'] = $myrow["fax"];
156         $_POST['email'] = $myrow["email"];
157
158         hidden("selected_id", $selected_id);
159         hidden("loc_code", $_POST['loc_code']);
160         label_row(_("Location Code:"), $_POST['loc_code']);
161
162 else 
163 { //end of if $selected_id only do the else when a new record is being entered
164         text_row(_("Location Code:"), 'loc_code', null, 5, 5);
165 }
166
167 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
168 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
169
170 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);   
171
172 text_row_ex(_("Telephone No:"), 'phone', 30, 30);
173 text_row_ex(_("Facsimile No:"), 'fax', 30, 30);
174 text_row_ex(_("Email:"), 'email', 30, 30);
175
176 end_table(1);
177 submit_add_or_update_center(!isset($selected_id));
178
179 end_form();
180
181 //end if record deleted no point displaying form to add record 
182  
183  end_page();
184
185 ?>