Partial (inventory related) include files cleanup.
[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/data_checks.inc");
18
19 include_once($path_to_root . "/admin/db/tags_db.inc");
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 (user_use_date_picker())
25         $js .= get_js_date_picker();
26 page(_($help_context = "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 if (isset($_GET['ReopenedID'])) 
84 {
85         $id = $_GET['ReopenedID'];
86
87         display_notification_centered(_("The dimension has been re-opened. ") . " #$id");
88         safe_exit();
89 }
90
91 //-------------------------------------------------------------------------------------------------
92
93 function safe_exit()
94 {
95         global $path_to_root;
96
97         hyperlink_no_params("", _("Enter a &new dimension"));
98         echo "<br>";
99         hyperlink_no_params($path_to_root . "/dimensions/inquiry/search_dimensions.php", _("&Select an existing dimension"));
100
101         display_footer_exit();
102 }
103
104 //-------------------------------------------------------------------------------------
105
106 function can_process()
107 {
108         global $selected_id, $Refs;
109
110         if ($selected_id == -1) 
111         {
112
113         if (!$Refs->is_valid($_POST['ref'])) 
114         {
115                 display_error( _("The dimension reference must be entered."));
116                         set_focus('ref');
117                 return false;
118         }
119
120         if (!is_new_reference($_POST['ref'], ST_DIMENSION)) 
121         {
122                 display_error(_("The entered reference is already in use."));
123                         set_focus('ref');
124                 return false;
125         }
126         }
127
128         if (strlen($_POST['name']) == 0) 
129         {
130                 display_error( _("The dimension name must be entered."));
131                 set_focus('name');
132                 return false;
133         }
134
135         if (!is_date($_POST['date_']))
136         {
137                 display_error( _("The date entered is in an invalid format."));
138                 set_focus('date_');
139                 return false;
140         }
141
142         if (!is_date($_POST['due_date']))
143         {
144                 display_error( _("The required by date entered is in an invalid format."));
145                 set_focus('due_date');
146                 return false;
147         }
148
149         return true;
150 }
151
152 //-------------------------------------------------------------------------------------
153
154 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
155 {
156         if (!isset($_POST['dimension_tags']))
157                 $_POST['dimension_tags'] = array();
158                 
159         if (can_process()) 
160         {
161
162                 if ($selected_id == -1) 
163                 {
164                         $id = add_dimension($_POST['ref'], $_POST['name'], $_POST['type_'], $_POST['date_'], $_POST['due_date'], $_POST['memo_']);
165                         add_tag_associations($id, $_POST['dimension_tags']);
166                         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
167                 } 
168                 else 
169                 {
170
171                         update_dimension($selected_id, $_POST['name'], $_POST['type_'], $_POST['date_'], $_POST['due_date'], $_POST['memo_']);
172                         update_tag_associations(TAG_DIMENSION, $selected_id, $_POST['dimension_tags']);
173
174                         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
175                 }
176         }
177 }
178
179 //--------------------------------------------------------------------------------------
180
181 if (isset($_POST['delete'])) 
182 {
183
184         $cancel_delete = false;
185
186         // can't delete it there are productions or issues
187         if (dimension_has_payments($selected_id) || dimension_has_deposits($selected_id))
188         {
189                 display_error(_("This dimension cannot be deleted because it has already been processed."));
190                 set_focus('ref');
191                 $cancel_delete = true;
192         }
193
194         if ($cancel_delete == false) 
195         { //ie not cancelled the delete as a result of above tests
196
197                 // delete
198                 delete_dimension($selected_id);
199                 delete_tag_associations(TAG_DIMENSION,$selected_id, true);
200                 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
201         }
202 }
203
204 //-------------------------------------------------------------------------------------
205
206 if (isset($_POST['close'])) 
207 {
208
209         // update the closed flag
210         close_dimension($selected_id);
211         meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
212 }
213
214 if (isset($_POST['reopen'])) 
215 {
216
217         // update the closed flag
218         reopen_dimension($selected_id);
219         meta_forward($_SERVER['PHP_SELF'], "ReopenedID=$selected_id");
220 }
221 //-------------------------------------------------------------------------------------
222
223 start_form();
224
225 start_table(TABLESTYLE2);
226
227 if ($selected_id != -1)
228 {
229         $myrow = get_dimension($selected_id);
230
231         if (strlen($myrow[0]) == 0) 
232         {
233                 display_error(_("The dimension sent is not valid."));
234                 display_footer_exit();
235         }
236
237         // if it's a closed dimension can't edit it
238         //if ($myrow["closed"] == 1) 
239         //{
240         //      display_error(_("This dimension is closed and cannot be edited."));
241         //      display_footer_exit();
242         //}
243
244         $_POST['ref'] = $myrow["reference"];
245         $_POST['closed'] = $myrow["closed"];
246         $_POST['name'] = $myrow["name"];
247         $_POST['type_'] = $myrow["type_"];
248         $_POST['date_'] = sql2date($myrow["date_"]);
249         $_POST['due_date'] = sql2date($myrow["due_date"]);
250         $_POST['memo_'] = get_comments_string(ST_DIMENSION, $selected_id);
251         
252         $tags_result = get_tags_associated_with_record(TAG_DIMENSION, $selected_id);
253         $tagids = array();
254         while ($tag = db_fetch($tags_result)) 
255                 $tagids[] = $tag['id'];
256         $_POST['dimension_tags'] = $tagids;     
257
258         hidden('ref', $_POST['ref']);
259
260         label_row(_("Dimension Reference:"), $_POST['ref']);
261
262         hidden('selected_id', $selected_id);
263
264 else 
265 {
266         $_POST['dimension_tags'] = array();
267         ref_row(_("Dimension Reference:"), 'ref', '', $Refs->get_next(ST_DIMENSION));
268 }
269
270 text_row_ex(_("Name") . ":", 'name', 50, 75);
271
272 $dim = get_company_pref('use_dimension');
273
274 number_list_row(_("Type"), 'type_', null, 1, $dim);
275
276 date_row(_("Start Date") . ":", 'date_');
277
278 date_row(_("Date Required By") . ":", 'due_date', '', null, $SysPrefs->default_dimension_required_by());
279
280 tag_list_row(_("Tags:"), 'dimension_tags', 5, TAG_DIMENSION, true);
281
282 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
283
284 end_table(1);
285
286 if (isset($_POST['closed']) && $_POST['closed'] == 1)
287         display_note(_("This Dimension is closed."), 0, 0, "class='currentfg'");
288
289 if ($selected_id != -1) 
290 {
291         echo "<br>";
292         submit_center_first('UPDATE_ITEM', _("Update"), _('Save changes to dimension'), 'default');
293         if ($_POST['closed'] == 1)
294                 submit('reopen', _("Re-open This Dimension"), true, _('Mark this dimension as re-opened'), true);
295         else    
296                 submit('close', _("Close This Dimension"), true, _('Mark this dimension as closed'), true);
297         submit_center_last('delete', _("Delete This Dimension"), _('Delete unused dimension'), true);
298 }
299 else
300 {
301         submit_center('ADD_ITEM', _("Add"), true, '', 'default');
302 }
303 end_form();
304
305 //--------------------------------------------------------------------------------------------
306
307 end_page();
308