Fixed control buttons reset after record deletion.
[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 simple_page_mode(true);
14
15 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
16 {
17
18         //initialise no input errors assumed initially before we test
19         $input_error = 0;
20
21         /* actions to take once the user has clicked the submit button
22         ie the page has called itself with some user input */
23
24         //first off validate inputs sensible
25         $_POST['loc_code'] = strtoupper($_POST['loc_code']);
26
27         if (strlen($_POST['loc_code']) > 5) 
28         {
29                 $input_error = 1;
30                 display_error( _("The location code must be five characters or less long."));
31                 set_focus('loc_code');
32         } 
33         elseif (strlen($_POST['location_name']) == 0) 
34         {
35                 $input_error = 1;
36                 display_error( _("The location name must be entered."));                
37                 set_focus('location_name');
38         }
39
40         if ($input_error != 1) 
41         {
42         if ($selected_id != -1) 
43         {
44     
45                 update_item_location($selected_id, $_POST['location_name'], $_POST['delivery_address'],
46                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);    
47                         display_notification(_('Selected location has been updated'));
48         } 
49         else 
50         {
51     
52         /*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 */
53         
54                 add_item_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], 
55                         $_POST['phone'], $_POST['fax'], $_POST['email'], $_POST['contact']);
56                         display_notification(_('New location has been added'));
57         }
58                 
59                 $Mode = 'RESET';
60         }
61
62
63 function can_delete($selected_id)
64 {
65         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE loc_code='$selected_id'";
66         $result = db_query($sql, "could not query stock moves");
67         $myrow = db_fetch_row($result);
68         if ($myrow[0] > 0) 
69         {
70                 display_error(_("Cannot delete this location because item movements have been created using this location."));
71                 return false;
72         }
73
74         $sql= "SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE loc_code='$selected_id'";
75         $result = db_query($sql, "could not query work orders");
76         $myrow = db_fetch_row($result);
77         if ($myrow[0] > 0) 
78         {
79                 display_error(_("Cannot delete this location because it is used by some work orders records."));
80                 return false;
81         }
82
83         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE default_location='$selected_id'";
84         $result = db_query($sql, "could not query customer branches");
85         $myrow = db_fetch_row($result);
86         if ($myrow[0] > 0) 
87         {
88                 display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
89                 return false;
90         }
91         
92         return true;
93 }
94
95 //----------------------------------------------------------------------------------
96
97 if ($Mode == 'Delete')
98 {
99
100         if (can_delete($selected_id)) 
101         {
102                 delete_item_location($selected_id);
103                 display_notification(_('Selected location has been deleted'));
104         } //end if Delete Location
105         $Mode = 'RESET';
106 }
107
108 if ($Mode == 'RESET')
109 {
110         $selected_id = -1;
111         unset($_POST);
112 }
113
114 $sql = "SELECT * FROM ".TB_PREF."locations";
115 $result = db_query($sql, "could not query locations");;
116
117 start_form();
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_button_cell("Edit".$myrow[0], _("Edit"));
130         edit_button_cell("Delete".$myrow[0], _("Delete"));
131         end_row();
132 }
133         //END WHILE LIST LOOP
134
135 //end of ifs and buts!
136
137 end_table();
138
139 end_form();
140 echo '<br>';
141
142 start_form();
143
144 start_table($table_style2);
145
146 if ($selected_id != -1) 
147 {
148         //editing an existing Location
149
150         if ($Mode == 'Edit') {
151                 $myrow = get_item_location($selected_id);
152
153                 $_POST['loc_code'] = $myrow["loc_code"];
154                 $_POST['location_name']  = $myrow["location_name"];
155                 $_POST['delivery_address'] = $myrow["delivery_address"];
156                 $_POST['contact'] = $myrow["contact"];
157                 $_POST['phone'] = $myrow["phone"];
158                 $_POST['fax'] = $myrow["fax"];
159                 $_POST['email'] = $myrow["email"];
160         }
161         hidden("selected_id", $selected_id);
162         hidden("loc_code");
163         label_row(_("Location Code:"), $_POST['loc_code']);
164
165 else 
166 { //end of if $selected_id only do the else when a new record is being entered
167         text_row(_("Location Code:"), 'loc_code', null, 5, 5);
168 }
169
170 text_row_ex(_("Location Name:"), 'location_name', 50, 50);
171 text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);
172
173 textarea_row(_("Address:"), 'delivery_address', null, 35, 5);   
174
175 text_row_ex(_("Telephone No:"), 'phone', 30, 30);
176 text_row_ex(_("Facsimile No:"), 'fax', 30, 30);
177 text_row_ex(_("Email:"), 'email', 30, 30);
178
179 end_table(1);
180 submit_add_or_update_center($selected_id == -1, '', true);
181
182 end_form();
183
184 end_page();
185
186 ?>