Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.0] Add module account_cutoff_accrual_subscription #118

Merged
merged 3 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions account_cutoff_accrual_base/models/account_cutoff.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2018 Akretion (http://www.akretion.com)
# Copyright 2013-2019 Akretion France (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools import float_is_zero
import odoo.addons.decimal_precision as dp


Expand Down Expand Up @@ -35,6 +37,38 @@ def _get_default_journal(self):
default_journal
return journal

def _prepare_tax_lines(self, tax_compute_all_res, currency):
res = []
ato = self.env['account.tax']
company_currency = self.company_id.currency_id
cur_rprec = company_currency.rounding
for tax_line in tax_compute_all_res['taxes']:
tax = ato.browse(tax_line['id'])
if float_is_zero(tax_line['amount'], precision_rounding=cur_rprec):
continue
if self.type == 'accrued_expense':
tax_accrual_account_id = tax.account_accrued_expense_id.id
tax_account_field_label = _('Accrued Expense Tax Account')
elif self.type == 'accrued_revenue':
tax_accrual_account_id = tax.account_accrued_revenue_id.id
tax_account_field_label = _('Accrued Revenue Tax Account')
if not tax_accrual_account_id:
raise UserError(_(
"Missing '%s' on tax '%s'.") % (
tax_account_field_label, tax.display_name))
tax_amount = currency.round(tax_line['amount'])
tax_accrual_amount = currency.with_context(
date=self.cutoff_date).compute(tax_amount, company_currency)
res.append((0, 0, {
'tax_id': tax_line['id'],
'base': tax_line['base'], # in currency
'amount': tax_amount, # in currency
'sequence': tax_line['sequence'],
'cutoff_account_id': tax_accrual_account_id,
'cutoff_amount': tax_accrual_amount, # in company currency
}))
return res


class AccountCutoffLine(models.Model):
_inherit = 'account.cutoff.line'
Expand Down
53 changes: 8 additions & 45 deletions account_cutoff_accrual_dates/models/account_cutoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,15 @@
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo import models, fields, _
from odoo.exceptions import UserError


class AccountCutoff(models.Model):
_inherit = 'account.cutoff'

@api.model
def _get_default_source_journals(self):
res = super(AccountCutoff, self)._get_default_source_journals()
cutoff_type = self._context.get('default_type')
mapping = {
'accrued_expense': 'purchase',
'accrued_revenue': 'sale',
}
if cutoff_type in mapping:
src_journals = self.env['account.journal'].search(
[('type', '=', mapping[cutoff_type])])
if src_journals:
res = src_journals.ids
return res

def _prepare_accrual_date_lines(self, aml, mapping):
self.ensure_one()
ato = self.env['account.tax']
start_date_dt = fields.Date.from_string(aml.start_date)
end_date_dt = fields.Date.from_string(aml.end_date)
# Here, we compute the amount of the cutoff
Expand All @@ -41,7 +25,7 @@ def _prepare_accrual_date_lines(self, aml, mapping):
prepaid_days = (cutoff_date_dt - start_date_dt).days + 1
assert total_days > 0,\
'Should never happen. Total days should always be > 0'
cutoff_amount = (aml.credit - aml.debit) *\
cutoff_amount = - aml.balance * \
prepaid_days / float(total_days)
cutoff_amount = self.company_currency_id.round(cutoff_amount)
# we use account mapping here
Expand All @@ -62,44 +46,23 @@ def _prepare_accrual_date_lines(self, aml, mapping):
'analytic_account_id': aml.analytic_account_id.id or False,
'total_days': total_days,
'prepaid_days': prepaid_days,
'amount': aml.credit - aml.debit,
'amount': - aml.balance,
'currency_id': self.company_currency_id.id,
'cutoff_amount': cutoff_amount,
'tax_line_ids': [],
}

if aml.tax_ids:
# It won't work with price-included taxes
tax_res = aml.tax_ids.compute_all(
cutoff_amount, product=aml.product_id, partner=aml.partner_id)
for tax_line in tax_res['taxes']:
tax = ato.browse(tax_line['id'])
for tax in aml.tax_ids:
if tax.price_include:
raise UserError(_(
"Price included taxes such as '%s' are not "
"supported by the module account_cutoff_accrual_dates "
"for the moment.") % tax.display_name)
if self.type == 'accrued_expense':
tax_account = tax.account_accrued_expense_id
if not tax_account:
raise UserError(_(
"Missing 'Accrued Expense Tax Account' "
"on tax '%s'") % tax.display_name)
elif self.type == 'accrued_revenue':
tax_account = tax.account_accrued_revenue_id
if not tax_account:
raise UserError(_(
"Missing 'Accrued Revenue Tax Account' "
"on tax '%s'") % tax.display_name)
tamount = self.company_currency_id.round(tax_line['amount'])
res['tax_line_ids'].append((0, 0, {
'tax_id': tax_line['id'],
'base': cutoff_amount,
'amount': tamount,
'sequence': tax_line['sequence'],
'cutoff_account_id': tax_account.id,
'cutoff_amount': tamount,
}))
tax_compute_all_res = aml.tax_ids.compute_all(
cutoff_amount, product=aml.product_id, partner=aml.partner_id)
res['tax_line_ids'] = self._prepare_tax_lines(
tax_compute_all_res, self.company_currency_id)
return res

def get_lines(self):
Expand Down
3 changes: 3 additions & 0 deletions account_cutoff_accrual_subscription/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import models
21 changes: 21 additions & 0 deletions account_cutoff_accrual_subscription/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Account Accrual Subscriptions',
'version': '10.0.1.0.0',
'category': 'Accounting',
'license': 'AGPL-3',
'summary': 'Accrued expenses based on subscriptions',
'author': "Akretion,Odoo Community Association (OCA)",
'website': 'http://www.akretion.com',
'depends': ['account_cutoff_accrual_dates'],
'data': [
'security/ir.model.access.csv',
'security/ir_rule.xml',
'views/account_cutoff_accrual_subscription.xml',
],
'installable': True,
}
4 changes: 4 additions & 0 deletions account_cutoff_accrual_subscription/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-

from . import account_cutoff_accrual_subscription
from . import account_cutoff
Loading