/
depreciate.py
204 lines (158 loc) · 7.79 KB
/
depreciate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""Beancount Plugin
Automatically add depreciation entries for fixed assets.
# License: The MIT License
# Copyright (c) 2015 Alok Parlikar
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
This plugin looks at postings that have the 'depreciation' metadata, and
generates new entries until the closing of the previous year to depreciate the
value of the account on which the metadata was placed.
Currently, the following methods of depreciation are supported:
WDV: Written Down Value
CRA: Canadian Revenue Agency method (assets purchased in current year are allowed 50% of normal rate)
Example: -->
plugin "beancount_plugins.plugins.flexible_depreciation.depreciate" "{
'method': 'WDV',
'year_closing_month': 12, # Could be 3 for the fiscal year ending Mar 31.
'halfdepr': True, # Assets used less than 180 days will be depreciated at half the allowed rate that year
'account': 'Expenses:Depreciation', # Account to post depreciation entries to.
'2010': 0.5, # Business only open for half year in 2010, adjust depreciation rate down.
'expense_subaccount': True, #If true, will use subaccount for depreciation expense using first word in Narration.
#ie: Expenses:Depreciation:Printer
'asset_subaccount': True, #If true, will use asset subaccount for depreciation expense.
#ie: Assets:Fixed:Comp:Depreciation.
}"
2014-03-02 * "" | "Printer Purchase"
Assets:Cash -100.00 INR
Assets:Fixed:Comp 100.00 INR
depreciation: "Printer Depreciation @0.60"
<--
The "depreciation" metadata has this format:
"NARRATION STRING @RATE"
The narration string here will be used in the newly generated entries.
Rate should be a number, not percentage. Use "0.60" to mean "60%".
"""
__author__ = 'Alok Parlikar <alok@parlikar.com>'
import datetime
from decimal import Decimal
from beancount.core.amount import mul, sub
from beancount.core import data
from beancount.core.position import Position
__plugins__ = ['depreciate']
def depreciate(entries, options_map, config):
"""Add depreciation entries for fixed assets. See module docstring for more
details and example"""
config_obj = eval(config, {}, {})
if not isinstance(config_obj, dict):
raise RuntimeError("Invalid plugin configuration: should be a single dict.")
depr_method = config_obj.pop('method', 'WDV')
year_closing_month = config_obj.pop('year_closing_month', 12)
half_depr = config_obj.pop('half_depr', True)
depr_account = config_obj.pop('account', "Expenses:Depreciation")
expense_subaccount = config_obj.pop('expense_subaccount', False)
asset_subaccount = config_obj.pop('asset_subaccount', False)
if depr_method not in ['WDV','CRA']:
raise RuntimeError("Specified depreciation method in plugin not implemented")
if not 0 < year_closing_month <= 12:
raise RuntimeError("Invalid year-closing-month specified")
errors = []
depr_candidates = []
for entry in entries:
date = entry.date
try:
for p in entry.postings:
if 'depreciation' in p.meta:
depr_candidates.append((date, p, entry))
except (AttributeError, TypeError):
pass
for date, posting, entry in depr_candidates:
narration, rate = posting.meta['depreciation'].split('@')
narration = narration.strip()
rate = Decimal(rate)
rate_used = rate
orig_val = posting.units
current_val = orig_val
new_dates = get_closing_dates(date, year_closing_month)
for d in new_dates:
if depr_method == 'WDV':
if half_depr and d - date < datetime.timedelta(180):
# Asset used for less than 180 days, use half the rate allowed.
rate_used = rate/2
narration_suffix = " - Half Depreciation (<180days)"
else:
rate_used = rate
narration_suffix = ""
elif depr_method == 'CRA':
if half_depr and d < datetime.date(date.year+1, date.month, date.day):
# Asset purchased this year, use half of rate allowed
rate_used = rate/2
narration_suffix = " - Half Depreciation (Same year)"
else:
rate_used = rate
narration_suffix = ""
multiplier = Decimal(config_obj.get(str(d.year),1))
rate_used = rate_used*multiplier
current_depr = mul(current_val, rate_used)
account = posting.account
if asset_subaccount:
account += ":Depreciation"
depr_account_used = depr_account
if expense_subaccount:
depr_account_used = depr_account + ":" + narration.split(" ")[0]
p1 = data.Posting(account=account,
price=None,
cost=None,
meta=None,
flag=None,
units=mul(current_depr, Decimal(-1)))
p2 = data.Posting(account=depr_account_used,
price=None,
cost=None,
meta=None,
flag=None,
units=current_depr)
e = entry._replace(narration=narration + narration_suffix,
date=d,
flag='*',
payee=None,
tags={'AUTO-DEPRECIATION'},
postings=[p1, p2])
entries.append(e)
current_val = sub(current_val, current_depr)
return entries, errors
def get_closing_dates(begin_date, year_closing_month):
"""Given a begin_date, find out all dates until today, where a fiscal year has
ended."""
today = datetime.date.today()
if begin_date.month <= year_closing_month:
first_closing_year = begin_date.year
else:
first_closing_year = begin_date.year+1
# Calculate first closing date as the last date of the closing month that year
first_closing_date = get_last_day_of_month(
datetime.date(first_closing_year,
year_closing_month,
1))
closing_dates = []
d = first_closing_date
while d <= today:
closing_dates.append(d)
d = get_last_day_of_month(datetime.date(d.year+1, year_closing_month, 1))
return closing_dates
def get_last_day_of_month(date):
"""Given a date, find the last date of the month the date belongs to"""
next_month = date.replace(day=28) + datetime.timedelta(days=4)
return next_month - datetime.timedelta(days=next_month.day)