Switch to new access levels system
[fa-stable.git] / dimensions / dimension_entry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_DIMENSION';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/includes/manufacturing.inc");
18 include_once($path_to_root . "/includes/data_checks.inc");
19
20 include_once($path_to_root . "/dimensions/includes/dimensions_db.inc");
21 include_once($path_to_root . "/dimensions/includes/dimensions_ui.inc");
22
23 $js = "";
24 if ($use_date_picker)
25         $js .= get_js_date_picker();
26 page(_("Dimension Entry"), false, false, "", $js);
27
28 //---------------------------------------------------------------------------------------
29
30 if (isset($_GET['trans_no']))
31 {
32         $selected_id = $_GET['trans_no'];
33
34 elseif(isset($_POST['selected_id']))
35 {
36         $selected_id = $_POST['selected_id'];
37 }
38 else
39         $selected_id = -1;
40 //---------------------------------------------------------------------------------------
41
42 if (isset($_GET['AddedID'])) 
43 {
44         $id = $_GET['AddedID'];
45
46         display_notification_centered(_("The dimension has been entered."));
47
48         safe_exit();
49 }
50
51 //---------------------------------------------------------------------------------------
52
53 if (isset($_GET['UpdatedID'])) 
54 {
55         $id = $_GET['UpdatedID'];
56
57         display_notification_centered(_("The dimension has been updated."));
58         safe_exit();
59 }
60
61 //---------------------------------------------------------------------------------------
62
63 if (isset($_GET['DeletedID'])) 
64 {
65         $id = $_GET['DeletedID'];
66
67         display_notification_centered(_("The dimension has been deleted."));
68         safe_exit();
69 }
70
71 //---------------------------------------------------------------------------------------
72
73 if (isset($_GET['ClosedID'])) 
74 {
75         $id = $_GET['ClosedID'];
76
77         display_notification_centered(_("The dimension has been closed. There can be no more changes to it.") . " #$id");
78         safe_exit();
79 }
80
81 //-------------------------------------------------------------------------------------------------
82
83 function safe_exit()
84 {
85         global $path_to_root;
86
87         hyperlink_no_params("", _("Enter a &new dimension"));
88         echo "<br>";
89         hyperlink_no_params($path_to_root . "/dimensions/inquiry/search_dimensions.php", _("&Select an existing dimension"));
90
91         display_footer_exit();
92 }
93
94 //-------------------------------------------------------------------------------------
95
96 function can_process()
97 {
98         global $selected_id;
99
100         if ($selected_id == -1) 
101         {
102
103         if (!references::is_valid($_POST['ref'])) 
104         {
105                 display_error( _("The dimension reference must be entered."));
106                         set_focus('ref');
107                 return false;
108         }
109
110         if (!is_new_reference($_POST['ref'], systypes::dimension())) 
111         {
112                 display_error(_("The entered reference is already in use."));
113                         set_focus('ref');
114                 return false;
115         }
116         }
117
118         if (strlen($_POST['name']) == 0) 
119         {
120                 display_error( _("The dimension name must be entered."));
121                 set_focus('name');
122                 return false;
123         }
124
125         if (!is_date($_POST['date_']))
126         {
127                 display_error( _("The date entered is in an invalid format."));
128                 set_focus('date_');
129                 return false;
130         }
131
132         if (!is_date($_POST['due_date']))
133         {
134                 display_error( _("The required by date entered is in an invalid format."));
135                 set_focus('due_date');
136                 return false;
137         }
138
139         return true;
140 }
141
142 //-------------------------------------------------------------------------------------
143
144 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
145 {
146
147         if (can_process()) 
148         {
149
150                 if ($selected_id == -1) 
151                 {
152
153                         $id = add_dimension($_POST['ref'], $_POST['name'], $_POST['type_'], $_POST['date_'], $_POST['due_date'], $_POST['memo_']);
154
155                         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
156                 } 
157                 else 
158                 {
159
160                         update_dimension($selected_id, $_POST['name'], $_POST['type_'], $_POST['date_'], $_POST['due_date'], $_POST['memo_']);
161
162                         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
163                 }
164         }
165 }
166
167 //--------------------------------------------------------------------------------------
168
169 if (isset($_POST['delete'])) 
170 {
171
172         $cancel_delete = false;
173
174         // can't delete it there are productions or issues
175         if (dimension_has_payments($selected_id) || dimension_has_deposits($selected_id))
176         {
177                 display_error(_("This dimension cannot be deleted because it has already been processed."));
178                 set_focus('ref');
179                 $cancel_delete = true;
180         }
181
182         if ($cancel_delete == false) 
183         { //ie not cancelled the delete as a result of above tests
184
185                 // delete
186                 delete_dimension($selected_id);
187                 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
188         }
189 }
190
191 //-------------------------------------------------------------------------------------
192
193 if (isset($_POST['close'])) 
194 {
195
196         // update the closed flag
197         close_dimension($selected_id);
198         meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
199 }
200
201 //-------------------------------------------------------------------------------------
202
203 start_form();
204
205 start_table($table_style2);
206
207 if ($selected_id != -1)
208 {
209         $myrow = get_dimension($selected_id);
210
211         if (strlen($myrow[0]) == 0) 
212         {
213                 display_error(_("The dimension sent is not valid."));
214                 display_footer_exit();
215         }
216
217         // if it's a closed dimension can't edit it
218         if ($myrow["closed"] == 1) 
219         {
220                 display_error(_("This dimension is closed and cannot be edited."));
221                 display_footer_exit();
222         }
223
224         $_POST['ref'] = $myrow["reference"];
225         $_POST['closed'] = $myrow["closed"];
226         $_POST['name'] = $myrow["name"];
227         $_POST['type_'] = $myrow["type_"];
228         $_POST['date_'] = sql2date($myrow["date_"]);
229         $_POST['due_date'] = sql2date($myrow["due_date"]);
230         $_POST['memo_'] = get_comments_string(systypes::dimension(), $selected_id);
231
232         hidden('ref', $_POST['ref']);
233
234         label_row(_("Dimension Reference:"), $_POST['ref']);
235
236         hidden('selected_id', $selected_id);
237
238 else 
239 {
240         ref_row(_("Dimension Reference:"), 'ref', '', references::get_next(systypes::dimension()));
241 }
242
243 text_row_ex(_("Name") . ":", 'name', 50, 75);
244
245 $dim = get_company_pref('use_dimension');
246
247 number_list_row(_("Type"), 'type_', null, 1, $dim);
248
249 date_row(_("Start Date") . ":", 'date_');
250
251 date_row(_("Date Required By") . ":", 'due_date', '', null, sys_prefs::default_dimension_required_by());
252
253 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
254
255 end_table(1);
256
257 if ($selected_id != -1) 
258 {
259         echo "<br>";
260         submit_center_first('UPDATE_ITEM', _("Update"), _('Save changes to dimension'), 'default');
261         submit('close', _("Close This Dimension"), true, _('Mark this dimension as closed'), true);
262         submit_center_last('delete', _("Delete This Dimension"), _('Delete unused dimension'), true);
263 }
264 else
265 {
266         submit_center('ADD_ITEM', _("Add"), true, '', 'default');
267 }
268 end_form();
269
270 //--------------------------------------------------------------------------------------------
271
272 end_page();
273
274 ?>