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."));
39 elseif (strlen($_POST['location_name']) == 0)
42 display_error( _("The location name must be entered."));
45 if ($input_error != 1)
47 if (isset($selected_id))
50 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
51 $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
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 */
58 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'],
59 $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
62 meta_forward($_SERVER['PHP_SELF']);
66 function can_delete($selected_id)
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);
73 display_error(_("Cannot delete this location because item movements have been created using this location."));
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);
82 display_error(_("Cannot delete this location because it is used by some work orders records."));
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);
91 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
98 //----------------------------------------------------------------------------------
100 if (isset($_GET['delete']))
103 if (can_delete($selected_id))
105 delete_item_location($selected_id);
106 meta_forward($_SERVER['PHP_SELF']);
107 } //end if Delete Location
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*/
115 $sql = "SELECT * FROM ".TB_PREF."locations";
116 $result = db_query($sql, "could not query locations");;
118 start_table("$table_style width=30%");
119 $th = array(_("Location Code"), _("Location Name"), "", "");
121 $k = 0; //row colour counter
122 while ($myrow = db_fetch_row($result))
125 alt_table_row_color($k);
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");
133 //END WHILE LIST LOOP
135 //end of ifs and buts!
139 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Location"));
143 start_table($table_style2);
144 if (isset($selected_id))
146 //editing an existing Location
148 $myrow = get_item_location($selected_id);
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"];
158 hidden("selected_id", $selected_id);
159 hidden("loc_code", $_POST['loc_code']);
160 label_row(_("Location Code:"), $_POST['loc_code']);
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);
167 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
168 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
170 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);
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);
177 submit_add_or_update_center(!isset($selected_id));
181 //end if record deleted no point displaying form to add record