cc5390795c3f56b36b3d4f0ead0162bd496a8010
[fa-stable.git] / fixed_assets / includes / depreciation.inc
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
13 function next_depreciation_date($depreciation_date) {
14
15   $start = strtotime($depreciation_date);
16
17   $y = date('Y', $start);
18   $m = date('n', $start) + 1;
19
20   if ($m > 12) {
21     $m = 1;
22     $y++;
23   }
24
25   return strtotime("$y-$m-1");
26 }
27
28 function depreciation_months($depreciation_start) {
29
30   // assume that depreciation start is the same fiscal year
31   //$start = strtotime($depreciation_start);
32   //$end = strtotime($year['end']);
33
34   $start = next_depreciation_date($depreciation_start);
35
36   return 12 - date('n', $start) + 1;
37
38   //$d1 = date('j', $start); // day of the month
39   //$d2 = date('t', $start); // number of days in month
40
41   //if ($d2 > $d1)
42     //$months++;
43 }
44
45 function months_between_dates($start, $end) {
46   $start = strtotime($start);
47   $end = strtotime($end);
48
49   $y1 = date('Y', $start);
50   $m1 = date('n', $start);
51
52   $y2 = date('Y', $end);
53   $m2 = date('n', $end);
54
55   return 12 * ($y2 - $y1) + $m2 - $m1;
56 }
57
58 function compute_gl_rows_for_depreciation($item, $no_months, $period) {
59   $rows = array();
60
61   $year = get_current_fiscalyear();
62   $y = date('Y', strtotime($year['end']));
63
64   switch ($item['depreciation_method']) {
65         case 'D':
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)
69                         $value = $line_value;
70                 break;
71
72     case 'S':   // actual_cost stores start cost of item
73                 $value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
74                 break;
75
76         case 'N':
77                 $N = $item['depreciation_rate'];
78                 $done_years = months_between_dates($item['depreciation_start'], $item['depreciation_date'])/12;
79                 $value = $item['purchase_cost']* ($N-$done_years)/($N*($N+1)/2)/12;
80                 break;
81
82     case 'O':
83                 $value = $item['material_cost'];
84                 break;
85   }
86
87   $next = next_depreciation_date($item['depreciation_date']);
88   $m = date('n', $next);
89
90   $total = 0;
91   $cnt = 0;
92   for ($i=$m; $i < $m + $no_months; $i++) {
93     $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));
94
95     $total += $value;
96
97     if (FA_YEARLY == $period) {
98       // yearly
99       if ($i == $m + $no_months - 1)
100         $rows[] = array('date' => $date, 'value' => $total);
101       /*else
102         $rows[] = array('date' => $date, 'value' => 0);
103         */
104     }
105     else {
106       // monthly
107       $rows[] = array('date' => $date, 'value' => $value);
108     }
109
110     $cnt++;
111
112     if ($item['depreciation_method'] == 'S') {
113       if ($cnt >= $remaining_months)
114         $value = 0;
115     }
116     elseif ($item['depreciation_method'] == 'O') {
117       // depreciate only in the first month
118       $value = 0;
119     }
120   }
121
122   return $rows;
123 }