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 ***********************************************************************/
13 function next_depreciation_date($depreciation_date) {
15 $start = strtotime($depreciation_date);
17 $y = date('Y', $start);
18 $m = date('n', $start) + 1;
25 return strtotime("$y-$m-1");
28 function depreciation_months($depreciation_start) {
30 // assume that depreciation start is the same fiscal year
31 //$start = strtotime($depreciation_start);
32 //$end = strtotime($year['end']);
34 $start = next_depreciation_date($depreciation_start);
36 return 12 - date('n', $start) + 1;
38 //$d1 = date('j', $start); // day of the month
39 //$d2 = date('t', $start); // number of days in month
45 function months_between_dates($start, $end) {
46 $start = strtotime($start);
47 $end = strtotime($end);
49 $y1 = date('Y', $start);
50 $m1 = date('n', $start);
52 $y2 = date('Y', $end);
53 $m2 = date('n', $end);
55 return 12 * ($y2 - $y1) + $m2 - $m1;
58 function compute_gl_rows_for_depreciation($item, $no_months, $period) {
61 $year = get_current_fiscalyear();
62 $y = date('Y', strtotime($year['end']));
64 switch ($item['depreciation_method']) {
66 $value = $item['material_cost'] * $item['depreciation_rate'] / 100 / 12;
70 // depreciation_rate is the period here.
71 $done_months = months_between_dates($item['depreciation_start'], $item['depreciation_date']);
72 $remaining_months = $item['depreciation_rate'] * 12 - $done_months;
73 $value = $item['material_cost'] / $remaining_months;
77 $value = $item['material_cost'];
81 $next = next_depreciation_date($item['depreciation_date']);
82 $m = date('n', $next);
86 for ($i=$m; $i < $m + $no_months; $i++) {
87 $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));
91 if (FA_YEARLY == $period) {
93 if ($i == $m + $no_months - 1)
94 $rows[] = array('date' => $date, 'value' => $total);
96 $rows[] = array('date' => $date, 'value' => 0);
101 $rows[] = array('date' => $date, 'value' => $value);
106 if ($item['depreciation_method'] == 'S') {
107 if ($cnt >= $remaining_months)
110 elseif ($item['depreciation_method'] == 'O') {
111 // depreciate only in the first month