Added fixed assets module
[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       $value = $item['material_cost'] * $item['depreciation_rate'] / 100 / 12;
67       break;
68
69     case 'S':
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;
74       break;
75
76     case 'O':
77       $value = $item['material_cost'];
78       break;
79   }
80
81   $next = next_depreciation_date($item['depreciation_date']);
82   $m = date('n', $next);
83
84   $total = 0;
85   $cnt = 0;
86   for ($i=$m; $i < $m + $no_months; $i++) {
87     $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));
88
89     $total += $value;
90
91     if (FA_YEARLY == $period) {
92       // yearly
93       if ($i == $m + $no_months - 1)
94         $rows[] = array('date' => $date, 'value' => $total);
95       /*else
96         $rows[] = array('date' => $date, 'value' => 0);
97         */
98     }
99     else {
100       // monthly
101       $rows[] = array('date' => $date, 'value' => $value);
102     }
103
104     $cnt++;
105
106     if ($item['depreciation_method'] == 'S') {
107       if ($cnt >= $remaining_months)
108         $value = 0;
109     }
110     elseif ($item['depreciation_method'] == 'O') {
111       // depreciate only in the first month
112       $value = 0;
113     }
114   }
115
116   return $rows;
117 }