e07a93de057b860602a9cb0e5d58dfec713a6dc3
[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':   // purchase_cost stores start cost of item
73                 $done_months = months_between_dates($item['depreciation_start'], $item['depreciation_date']);
74                 $remaining_months = 100.0/$item['depreciation_rate'] - $done_months;
75                 $value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
76                 break;
77
78         case 'N':
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;
82                 break;
83
84     case 'O':
85                 $value = $item['material_cost'];
86                 break;
87   }
88
89   $next = next_depreciation_date($item['depreciation_date']);
90   $m = date('n', $next);
91
92   $total = 0;
93   $cnt = 0;
94   for ($i=$m; $i < $m + $no_months; $i++) {
95
96         if ($item['depreciation_method'] == 'S') {
97                 if ($cnt >= $remaining_months)
98                         $value = 0;
99         }
100
101     $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));
102
103     $total += $value;
104
105     if (FA_YEARLY == $period) {
106       // yearly
107       if ($i == $m + $no_months - 1)
108         $rows[] = array('date' => $date, 'value' => $total);
109       /*else
110         $rows[] = array('date' => $date, 'value' => 0);
111         */
112     }
113     else {
114       // monthly
115       $rows[] = array('date' => $date, 'value' => $value);
116     }
117
118     $cnt++;
119
120         if ($item['depreciation_method'] == 'O') {
121                 // depreciate only in the first month
122                 $value = 0;
123         }
124  }
125
126   return $rows;
127 }