Skip to content

Commit

Permalink
Merge d42ba9a into 2f021ed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis de Lattre committed Nov 28, 2019
2 parents 2f021ed + d42ba9a commit 4b272b1
Show file tree
Hide file tree
Showing 18 changed files with 574 additions and 64 deletions.
38 changes: 36 additions & 2 deletions account_cutoff_accrual_base/models/account_cutoff.py
@@ -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
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
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import models
21 changes: 21 additions & 0 deletions account_cutoff_accrual_subscription/__manifest__.py
@@ -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
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-

from . import account_cutoff_accrual_subscription
from . import account_cutoff

0 comments on commit 4b272b1

Please sign in to comment.