Snippets
Created by
Alok Parlikar
last modified
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 | """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, only the WDV method of depreciation is supported.
Example: -->
plugin "beancount.plugins.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.
}"
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 amount_mult, amount_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")
print(year_closing_month)
if depr_method not in ['WDV']:
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:
pass
for date, posting, entry in depr_candidates:
narration, rate = posting.meta['depreciation'].split('@')
narration = narration.strip()
rate = Decimal(rate)
current_val = posting.position.get_units()
new_dates = get_closing_dates(date, year_closing_month)
for d in new_dates:
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 = ""
current_depr = amount_mult(current_val, rate_used)
p1 = data.Posting(account=posting.account,
price=None,
meta=None,
flag=None,
position=Position.from_amounts(amount_mult(current_depr, Decimal(-1))))
p2 = data.Posting(account=depr_account,
price=None,
meta=None,
flag=None,
position=Position.from_amounts(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 = amount_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)
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.