87c3218d0c09395644b04409fb8fa8e9e575cda8
[fa-stable.git] / fixed_assets / fixed_asset_classes.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_ASSETCLASS';
13 $path_to_root = "..";
14 include($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/ui.inc");
17
18 include_once($path_to_root . "/fixed_assets/includes/fixed_assets_db.inc");
19 include_once($path_to_root . "/fixed_assets/includes/fa_classes_db.inc");
20
21 page(_($help_context = "Fixed asset classes"));
22
23 simple_page_mode(true);
24
25 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
26 {
27
28         //initialise no input errors assumed initially before we test
29         $input_error = 0;
30
31         /* actions to take once the user has clicked the submit button
32         ie the page has called itself with some user input */
33
34         if ($input_error != 1) 
35         {
36         if ($selected_id != -1) 
37         {
38                 update_fixed_asset_class($selected_id, $_POST['parent_id'], $_POST['description'], $_POST['long_description'],
39                         $_POST['depreciation_method'], $_POST['depreciation_rate'], $_POST['depreciation_period']);
40                           display_notification(_('Selected fixed asset class has been updated'));
41         } 
42         else 
43         {
44                 add_fixed_asset_class($_POST['fa_class_id'], $_POST['parent_id'], $_POST['description'], $_POST['long_description'],
45                         $_POST['depreciation_method'], $_POST['depreciation_rate'], $_POST['depreciation_period']);
46                           display_notification(_('New fixed asset class has been added'));
47         }
48                 
49                 $Mode = 'RESET';
50         }
51
52
53 function can_delete($selected_id)
54 {
55         if (key_in_foreign_table($selected_id, 'stock_master', 'fa_class_id'))
56         {
57                 display_error(_("Cannot delete this class because it is used by some fixed asset items."));
58                 return false;
59         }
60         return true;
61 }
62
63 //----------------------------------------------------------------------------------
64
65 if ($Mode == 'Delete')
66 {
67
68         if (can_delete($selected_id)) 
69         {
70                 delete_fixed_asset_class($selected_id);
71                 display_notification(_('Selected fixed asset class has been deleted'));
72         } //end if Delete Location
73         $Mode = 'RESET';
74 }
75
76 if ($Mode == 'RESET')
77 {
78         $selected_id = -1;
79   unset($_POST);
80 }
81
82 $result = get_fixed_asset_classes();
83
84 start_form();
85 start_table(TABLESTYLE);
86 $th = array(_("Fixed asset class"), _("Description"), _("Depreciation method"), _("Depreciation rate"), _("Depreciation period"), "", "");
87 inactive_control_column($th);
88 table_header($th);
89 $k = 0; //row colour counter
90 while ($myrow = db_fetch($result)) 
91 {
92         alt_table_row_color($k);
93         
94         label_cell($myrow["fa_class_id"]);
95         label_cell($myrow["description"]);
96         label_cell($depreciation_methods[$myrow["depreciation_method"]]);
97         label_cell($myrow["depreciation_rate"].'%');
98         label_cell($myrow["depreciation_period"].' years');
99         inactive_control_cell($myrow["fa_class_id"], $myrow["inactive"], 'stock_fa_classes', 'fa_class_id');
100         edit_button_cell("Edit".$myrow["fa_class_id"], _("Edit"));
101         delete_button_cell("Delete".$myrow["fa_class_id"], _("Delete"));
102         end_row();
103 }
104 inactive_control_row($th);
105 end_table(1);
106
107 echo '<br>';
108
109 start_form(true);
110
111 start_table(TABLESTYLE2);
112
113 if ($selected_id != -1) 
114 {
115         if ($Mode == 'Edit') {
116                 $myrow = get_fixed_asset_class($selected_id);
117
118                 $_POST['fa_class_id'] = $myrow["fa_class_id"];
119                 $_POST['parent_id'] = $myrow["parent_id"];
120                 $_POST['description']  = $myrow["description"];
121                 $_POST['long_description']  = $myrow["long_description"];
122                 $_POST['depreciation_method'] = $myrow["depreciation_method"];
123                 $_POST['depreciation_rate'] = $myrow["depreciation_rate"];
124                 $_POST['depreciation_period'] = $myrow["depreciation_period"];
125         }
126         hidden("selected_id", $selected_id);
127         hidden("fa_class_id");
128   hidden('parent_id');
129   label_row(_("Parent class:"), $_POST['parent_id']);
130   label_row(_("Fixed asset class:"), $_POST['fa_class_id']);
131
132 else 
133 {
134   text_row(_("Parent class:"), 'parent_id', null, 3, 3);
135   text_row(_("Fixed asset class:"), 'fa_class_id', null, 3, 3);
136 }
137
138 text_row(_("Description:"), 'description', null, 42, 200);
139 textarea_row(_('Long description:'), 'long_description', null, 42, 3);
140 array_selector_row(_("Depreciation Method").":", "depreciation_method", null, $depreciation_methods, array('select_submit'=> true));
141 small_amount_row(_("Depreciation Rate").':', 'depreciation_rate', null, null, '%', user_percent_dec());
142 text_row_ex(_("Depreciation Period").':', 'depreciation_period', 3, 3, '', null, null, _("years"));
143
144 //text_row(_("Parent id:"), 'parent_id', null, 3, 3);
145
146 end_table(1);
147
148 //if ($selected_id != -1) 
149 submit_add_or_update_center($selected_id == -1, '', 'both');
150
151 end_form();
152
153 end_page();