[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / fixed_assets / process_depreciation.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_DEPRECIATION';
13 $path_to_root = "..";
14
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/includes/ui/items_cart.inc");
22
23 include_once($path_to_root . "/fixed_assets/includes/depreciation.inc");
24 include_once($path_to_root . "/fixed_assets/includes/fixed_assets_db.inc");
25
26 $js = "";
27 if (user_use_date_picker())
28   $js .= get_js_date_picker();
29
30 page(_($help_context = "Process Depreciation"), false, false, "", $js);
31
32 //---------------------------------------------------------------------------------------------
33 function check_data()
34 {
35   $myrow = get_item($_POST['stock_id']);
36
37   if ($_POST['months'] > depreciation_months($myrow['depreciation_date'])) {
38     display_error(_("The number of months is greater than the timespan between the depreciation start and the end of the fiscal year."));
39     set_focus('months');
40     return false;
41   }
42
43   return true;
44 }
45
46 //---------------------------------------------------------------------------------------------
47
48 function handle_submit()
49 {
50   if (!check_data())
51     return;
52
53   $item = get_item($_POST['stock_id']);
54
55   $period = get_company_pref('depreciation_period'); 
56   $gl_rows = compute_gl_rows_for_depreciation($item, $_POST['months'], $period);
57
58   $trans_no = process_fixed_asset_depreciation($_POST['stock_id'], $gl_rows, $_POST['refline'], $_POST['memo_']);
59
60   meta_forward($_SERVER['PHP_SELF'], "AddedID=".$trans_no);
61 }
62
63 //---------------------------------------------------------------------------------------------
64
65 if (get_post('process'))
66   handle_submit();
67
68 //---------------------------------------------------------------------------------------------
69
70 if (isset($_GET['AddedID'])) 
71 {
72   $trans_no = $_GET['AddedID'];
73   $trans_type = ST_JOURNAL;
74
75   display_notification(_("The fixed asset has been depreciated for this year"));
76
77   display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Depreciation")), 1, 0);
78
79   hyperlink_no_params($_SERVER['PHP_SELF'], _("Depreciate &Another Fixed Asset"));
80
81         display_footer_exit();
82 }
83
84 //--------------------------------------------------------------------------------------
85
86 check_db_has_depreciable_fixed_assets(_("There are no fixed assets that could be depreciated."));
87
88 //---------------------------------------------------------------------------------------------
89
90 function show_gl_rows() {
91
92   $item = get_item($_POST['stock_id']);
93
94   hidden('stock_id');
95   hidden('months');
96   hidden('refline');
97   hidden('memo_');
98
99   start_table(TABLESTYLE, "width=40%");
100   $th = array(_("Item"), _('Date'), _('Account'), _('Debit'), _("Credit"));
101
102   table_header($th);
103   $k = 0; //row colour counter
104
105   $period = get_company_pref('depreciation_period'); 
106   $gl_rows = compute_gl_rows_for_depreciation($item, $_POST['months'], $period);
107
108   foreach($gl_rows as $myrow)
109   {
110     alt_table_row_color($k);
111     label_cell($item['stock_id']);
112     label_cell($myrow["date"]);
113     label_cell($item['cogs_account'].' '.get_gl_account_name($item["cogs_account"]));
114     amount_cell($myrow["value"]);
115     label_cell("");
116     end_row();
117
118     alt_table_row_color($k);
119     label_cell($item['stock_id']);
120     label_cell($myrow["date"]);
121     label_cell($item["adjustment_account"].' '.get_gl_account_name($item["adjustment_account"]));
122     label_cell("");
123     amount_cell($myrow["value"]);
124     end_row();
125   }
126
127   end_table(1);
128
129   submit_center('process', _("Process Depreciation"), true, false);
130 }
131
132 function show_gl_controls() {
133   global $Ajax;
134
135   check_db_has_depreciable_fixed_assets('There are no active fixed asset defined in the system.');
136  
137   start_table(TABLESTYLE_NOBORDER);
138   start_row();
139   stock_depreciable_fa_list_cells(_("Select an item:"), 'stock_id', null,
140       false, true);
141   end_row();
142   end_table();
143
144   $myrow = get_item($_POST['stock_id']);
145
146   if (list_updated('stock_id') || !isset($_POST['months'])) {
147     //$_POST['depreciation_start'] = sql2date($myrow['depreciation_start']);
148     $_POST['months'] = depreciation_months($myrow['depreciation_date']);
149     unset($_POST['memo_']);
150   }
151
152   $Ajax->activate('depreciation_date');
153   $Ajax->activate('months');
154   $Ajax->activate('memo_');
155
156   start_table(TABLESTYLE2);
157
158   if (!isset($_POST['date']))
159     $_POST['date'] = Today();
160
161   $start = next_depreciation_date($myrow['depreciation_date']);
162   $start_text =  _(date('F', $start)).' '.date('Y', $start);
163
164   //date_row(_("Starting from month").":", 'depreciation_start', '', null, 0, 0, 0, null, true);
165   label_row(_("Starting from month").":", $start_text, null, null, 0, 'depreciation_date');
166   $period = get_company_pref('depreciation_period'); 
167   if ($period != FA_YEARLY) {
168     text_row(_("Period").":", 'months', null, 4, 3, null, null, _("months"));
169   }
170   else {
171     label_row(_("Period").":", '1 year');
172     hidden ('months');
173   }
174   refline_list_row(_("Reference line:"), 'refline', ST_JOURNAL, null, false, true);
175   textarea_row(_("Memo:"), 'memo_', null, 40,4);
176
177   end_table(1);
178
179   submit_center_first('show', _("Show GL Rows"), true, false);
180   submit_center_last('process', _("Process Depreciation"), true, false);
181 }
182
183 //---------------------------------------------------------------------------------------------
184
185 start_form();
186
187 if (isset($_POST['show']) && check_data())
188   show_gl_rows();
189 else
190   show_gl_controls();
191
192 end_form();
193
194 end_page();