5 include($path_to_root . "/includes/session.inc");
7 page(_("Inventory Locations"));
9 include_once($path_to_root . "/includes/ui.inc");
11 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
13 if (isset($_GET['selected_id']))
15 $selected_id = $_GET['selected_id'];
17 elseif (isset($_POST['selected_id']))
19 $selected_id = $_POST['selected_id'];
22 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
25 //initialise no input errors assumed initially before we test
28 /* actions to take once the user has clicked the submit button
29 ie the page has called itself with some user input */
31 //first off validate inputs sensible
32 $_POST['loc_code'] = strtoupper($_POST['loc_code']);
34 if (strlen($_POST['loc_code']) > 5)
37 display_error( _("The location code must be five characters or less long."));
38 set_focus('loc_code');
40 elseif (strlen($_POST['location_name']) == 0)
43 display_error( _("The location name must be entered."));
44 set_focus('location_name');
47 if ($input_error != 1)
49 if (isset($selected_id))
52 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
53 $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
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 */
60 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'],
61 $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
64 meta_forward($_SERVER['PHP_SELF']);
68 function can_delete($selected_id)
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);
75 display_error(_("Cannot delete this location because item movements have been created using this location."));
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);
84 display_error(_("Cannot delete this location because it is used by some work orders records."));
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);
93 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
100 //----------------------------------------------------------------------------------
102 if (isset($_GET['delete']))
105 if (can_delete($selected_id))
107 delete_item_location($selected_id);
108 meta_forward($_SERVER['PHP_SELF']);
109 } //end if Delete Location
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*/
117 $sql = "SELECT * FROM ".TB_PREF."locations";
118 $result = db_query($sql, "could not query locations");;
120 start_table("$table_style width=30%");
121 $th = array(_("Location Code"), _("Location Name"), "", "");
123 $k = 0; //row colour counter
124 while ($myrow = db_fetch_row($result))
127 alt_table_row_color($k);
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");
135 //END WHILE LIST LOOP
137 //end of ifs and buts!
141 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Location"));
145 start_table($table_style2);
146 if (isset($selected_id))
148 //editing an existing Location
150 $myrow = get_item_location($selected_id);
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"];
160 hidden("selected_id", $selected_id);
161 hidden("loc_code", $_POST['loc_code']);
162 label_row(_("Location Code:"), $_POST['loc_code']);
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);
169 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
170 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
172 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);
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);
179 submit_add_or_update_center(!isset($selected_id));
183 //end if record deleted no point displaying form to add record