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 $line_value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
67 $value = $item['material_cost'] * $item['depreciation_rate'] * $item['depreciation_factor']/100/12;
68 if ($value < $line_value)
72 case 'S': // purchase_cost stores start cost of item
73 $done_months = months_between_dates($item['depreciation_start'], $item['depreciation_date']);
74 $remaining_months = 12.0 * 100.0/$item['depreciation_rate'] - $done_months;
75 $value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
79 $N = $item['depreciation_rate'];
80 $done_years = months_between_dates($item['depreciation_start'], $item['depreciation_date'])/12;
81 $value = $item['purchase_cost']* ($N-$done_years)/($N*($N+1)/2)/12;
85 $value = $item['material_cost'];
89 $next = next_depreciation_date($item['depreciation_date']);
90 $m = date('n', $next);
94 for ($i=$m; $i < $m + $no_months; $i++) {
96 if ($item['depreciation_method'] == 'S') {
97 if ($cnt >= $remaining_months)
101 $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));
105 if (FA_YEARLY == $period) {
107 if ($i == $m + $no_months - 1)
108 $rows[] = array('date' => $date, 'value' => $total);
110 $rows[] = array('date' => $date, 'value' => 0);
115 $rows[] = array('date' => $date, 'value' => $value);
120 if ($item['depreciation_method'] == 'O') {
121 // depreciate only in the first month