Skip to content

Commit

Permalink
[IMP] account_analytic_parent: black, isort
Browse files Browse the repository at this point in the history
  • Loading branch information
pegonzalezspesol authored and cvinh committed Feb 18, 2021
1 parent 404ae73 commit 8351a09
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 146 deletions.
39 changes: 16 additions & 23 deletions account_analytic_parent/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

{
'name': 'Account Analytic Parent',
'summary': """
"name": "Account Analytic Parent",
"summary": """
This module reintroduces the hierarchy to the analytic accounts.""",
'version': '12.0.2.0.0',
'license': 'AGPL-3',
'author': 'Matmoz d.o.o., '
'Luxim d.o.o., '
'Deneroteam, '
'Eficent, '
'Tecnativa, '
'Brainbean Apps, '
'Odoo Community Association (OCA)',
'website': 'https://www.github.com/OCA/account-analytic.git',
'depends': [
'account',
'analytic',
],
'data': [
'views/account_analytic_account_view.xml',
],
'demo': [
'data/analytic_account_demo.xml',
],
'post_init_hook': 'post_init_hook',
"version": "12.0.2.0.0",
"license": "AGPL-3",
"author": "Matmoz d.o.o., "
"Luxim d.o.o., "
"Deneroteam, "
"Eficent, "
"Tecnativa, "
"Brainbean Apps, "
"Odoo Community Association (OCA)",
"website": "https://www.github.com/OCA/account-analytic.git",
"depends": ["account", "analytic"],
"data": ["views/account_analytic_account_view.xml"],
"demo": ["data/analytic_account_demo.xml"],
"post_init_hook": "post_init_hook",
}
4 changes: 2 additions & 2 deletions account_analytic_parent/hooks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2018 Brainbean Apps
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import api, SUPERUSER_ID
from odoo import SUPERUSER_ID, api


def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})

env['account.analytic.account']._parent_store_compute()
env["account.analytic.account"]._parent_store_compute()
141 changes: 68 additions & 73 deletions account_analytic_parent/models/account_analytic_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,127 +11,125 @@


class AccountAnalyticAccount(models.Model):
_inherit = 'account.analytic.account'
_parent_name = 'parent_id'
_inherit = "account.analytic.account"
_parent_name = "parent_id"
_parent_store = True
_order = 'complete_name'
_order = "complete_name"

parent_path = fields.Char(
index=True,
)
parent_path = fields.Char(index=True)
parent_id = fields.Many2one(
string='Parent Analytic Account',
comodel_name='account.analytic.account',
string="Parent Analytic Account",
comodel_name="account.analytic.account",
index=True,
ondelete='cascade',
ondelete="cascade",
)
child_ids = fields.One2many(
string='Child Accounts',
comodel_name='account.analytic.account',
inverse_name='parent_id',
string="Child Accounts",
comodel_name="account.analytic.account",
inverse_name="parent_id",
copy=True,
)
complete_name = fields.Char(
string='Complete Name',
compute='_compute_complete_name',
store=True,
string="Complete Name", compute="_compute_complete_name", store=True
)

@api.multi
@api.depends('child_ids.line_ids.amount')
@api.depends("child_ids.line_ids.amount")
def _compute_debit_credit_balance(self):
"""
Warning, this method overwrites the standard because the hierarchy
of analytic account changes
"""
super()._compute_debit_credit_balance()

ResCurrency = self.env['res.currency']
AccountAnalyticLine = self.env['account.analytic.line']
ResCurrency = self.env["res.currency"]
AccountAnalyticLine = self.env["account.analytic.line"]
user_currency_id = self.env.user.company_id.currency_id

# Re-compute only accounts with children
for account in self.filtered('child_ids'):
domain = [('account_id', 'child_of', account.id)]
for account in self.filtered("child_ids"):
domain = [("account_id", "child_of", account.id)]

credit_groups = AccountAnalyticLine.read_group(
domain=domain + [('amount', '>=', 0.0)],
fields=['currency_id', 'amount'],
groupby=['currency_id'],
domain=domain + [("amount", ">=", 0.0)],
fields=["currency_id", "amount"],
groupby=["currency_id"],
lazy=False,
)
credit = sum(map(
lambda x: ResCurrency.browse(x['currency_id'][0])._convert(
x['amount'],
user_currency_id,
self.env.user.company_id,
fields.Date.today()
),
credit_groups
))
credit = sum(
map(
lambda x: ResCurrency.browse(x["currency_id"][0])._convert(
x["amount"],
user_currency_id,
self.env.user.company_id,
fields.Date.today(),
),
credit_groups,
)
)

debit_groups = AccountAnalyticLine.read_group(
domain=domain + [('amount', '<', 0.0)],
fields=['currency_id', 'amount'],
groupby=['currency_id'],
domain=domain + [("amount", "<", 0.0)],
fields=["currency_id", "amount"],
groupby=["currency_id"],
lazy=False,
)
debit = sum(map(
lambda x: ResCurrency.browse(x['currency_id'][0])._convert(
x['amount'],
user_currency_id,
self.env.user.company_id,
fields.Date.today()
),
debit_groups
))
debit = sum(
map(
lambda x: ResCurrency.browse(x["currency_id"][0])._convert(
x["amount"],
user_currency_id,
self.env.user.company_id,
fields.Date.today(),
),
debit_groups,
)
)

account.debit = abs(debit)
account.credit = credit
account.balance = account.credit - account.debit

@api.multi
@api.constrains('parent_id')
@api.constrains("parent_id")
def check_recursion(self):
for account in self:
if not super(AccountAnalyticAccount, account)._check_recursion():
raise UserError(
_('You can not create recursive analytic accounts.'),
)
raise UserError(_("You can not create recursive analytic accounts."))

@api.multi
@api.onchange('parent_id')
@api.onchange("parent_id")
def _onchange_parent_id(self):
for account in self:
account.partner_id = account.parent_id.partner_id

@api.depends('name', 'parent_id.complete_name')
@api.depends("name", "parent_id.complete_name")
def _compute_complete_name(self):
for account in self:
if account.parent_id:
account.complete_name = _('%(parent)s / %(own)s') % {
'parent': account.parent_id.complete_name,
'own': account.name,
account.complete_name = _("%(parent)s / %(own)s") % {
"parent": account.parent_id.complete_name,
"own": account.name,
}
else:
account.complete_name = account.name

@api.multi
@api.constrains('active')
@api.constrains("active")
def check_parent_active(self):
for account in self:
if (account.active and account.parent_id and
account.parent_id not in self and
not account.parent_id.active):
if (
account.active
and account.parent_id
and account.parent_id not in self
and not account.parent_id.active
):
raise UserError(
_('Please activate first parent account %s')
% account.parent_id.complete_name)
_("Please activate first parent account %s")
% account.parent_id.complete_name
)

@api.depends(
'complete_name',
'code',
'partner_id.commercial_partner_id.name',
)
@api.depends("complete_name", "code", "partner_id.commercial_partner_id.name")
def _compute_display_name(self):
super()._compute_display_name()

Expand All @@ -141,20 +139,17 @@ def name_get(self):
for analytic in self:
name = analytic.complete_name
if analytic.code:
name = _('[%(code)s] %(name)s') % {
'code': analytic.code,
'name': name,
}
name = _("[%(code)s] %(name)s") % {"code": analytic.code, "name": name}
if analytic.partner_id:
name = _('%(name)s - %(partner)s') % {
'name': name,
'partner': analytic.partner_id.commercial_partner_id.name,
name = _("%(name)s - %(partner)s") % {
"name": name,
"partner": analytic.partner_id.commercial_partner_id.name,
}
res.append((analytic.id, name))
return res

@api.multi
def write(self, vals):
if self and 'active' in vals and not vals['active']:
self.mapped('child_ids').write({'active': False})
if self and "active" in vals and not vals["active"]:
self.mapped("child_ids").write({"active": False})
return super().write(vals)
85 changes: 37 additions & 48 deletions account_analytic_parent/tests/test_account_analytic_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class TestAccountAnalyticRecursion(TransactionCase):

def setUp(self):
super(TestAccountAnalyticRecursion, self).setUp()

Expand All @@ -20,66 +19,62 @@ def setUp(self):
self.partner1 = self.env.ref("base.res_partner_1")
self.partner2 = self.env.ref("base.res_partner_2")
self.analytic_parent1 = self.analytic_account_obj.create(
{"name": "parent aa",
"code": "01",
"partner_id": self.partner1.id})
{"name": "parent aa", "code": "01", "partner_id": self.partner1.id}
)
self.analytic_son = self.analytic_account_obj.create(
{"name": "son aa",
"code": "02",
"parent_id": self.analytic_parent1.id})
{"name": "son aa", "code": "02", "parent_id": self.analytic_parent1.id}
)
self.analytic_parent2 = self.analytic_account_obj.create(
{"name": "parent2 aa",
"code": "01",
"partner_id": self.partner2.id})
{"name": "parent2 aa", "code": "01", "partner_id": self.partner2.id}
)
self.analytic_parent3 = self.analytic_account_obj.create(
{"name": "parent3 aa",
"code": "01",
"partner_id": self.partner2.id})
{"name": "parent3 aa", "code": "01", "partner_id": self.partner2.id}
)
self.analytic_son2 = self.analytic_account_obj.create(
{"name": "son aa",
"code": "02",
"parent_id": self.analytic_parent3.id})
self.create_analytic_line(
"Analytic line son", self.analytic_son, 50)
self.create_analytic_line(
"Analytic line parent1", self.analytic_parent1, 100)
self.create_analytic_line(
"Analytic line parent2", self.analytic_parent2, 50)
self.create_analytic_line(
"Analytic line son2", self.analytic_son2, -50)
self.assertEqual(self.analytic_parent1.debit, 0,
"Analytic account in the debit side")
{"name": "son aa", "code": "02", "parent_id": self.analytic_parent3.id}
)
self.create_analytic_line("Analytic line son", self.analytic_son, 50)
self.create_analytic_line("Analytic line parent1", self.analytic_parent1, 100)
self.create_analytic_line("Analytic line parent2", self.analytic_parent2, 50)
self.create_analytic_line("Analytic line son2", self.analytic_son2, -50)
self.assertEqual(
self.analytic_parent1.debit, 0, "Analytic account in the debit side"
)
self.assertEqual(self.analytic_parent3.debit, 50)

def create_analytic_line(self, name, analytic, amount):
return self.analytic_line_obj.create({
"name": name,
"amount": amount,
"account_id": analytic.id})
return self.analytic_line_obj.create(
{"name": name, "amount": amount, "account_id": analytic.id}
)

def test_recursion(self):
with self.assertRaises(ValidationError):
self.analytic_parent1.write(
{"parent_id": self.analytic_son.id})
self.analytic_parent1.write({"parent_id": self.analytic_son.id})

def test_onchange(self):
self.analytic_son._onchange_parent_id()
self.assertEqual(self.analytic_son.partner_id.id, self.partner1.id,
"Partner should not change")
self.assertEqual(
self.analytic_son.partner_id.id,
self.partner1.id,
"Partner should not change",
)
self.analytic_son.write({"parent_id": self.analytic_parent2.id})
self.analytic_son._onchange_parent_id()
self.assertEqual(self.analytic_son.partner_id.id, self.partner2.id,
"Partner should change")
self.assertEqual(
self.analytic_son.partner_id.id, self.partner2.id, "Partner should change"
)

def test_debit_credit_balance(self):
self.assertEqual(self.analytic_parent1.credit, 150, "Wrong amount")
self.assertEqual(self.analytic_parent1.balance, 150, "Wrong amount")
self.assertEqual(self.analytic_son.debit, 0,
"Analytic account in the debit side")
self.assertEqual(
self.analytic_son.debit, 0, "Analytic account in the debit side"
)
self.assertEqual(self.analytic_son.credit, 50, "Wrong amount")
self.assertEqual(self.analytic_son.balance, 50, "Wrong amount")
self.assertEqual(self.analytic_parent2.debit, 0,
"Analytic account in the debit side")
self.assertEqual(
self.analytic_parent2.debit, 0, "Analytic account in the debit side"
)
self.assertEqual(self.analytic_parent2.credit, 50, "Wrong amount")
self.assertEqual(self.analytic_parent2.balance, 50, "Wrong amount")
self.assertEqual(self.analytic_parent3.debit, 50)
Expand All @@ -96,11 +91,5 @@ def test_archive(self):
self.analytic_son.toggle_active()

def test_name(self):
self.assertEqual(
self.analytic_son.complete_name,
'parent aa / son aa'
)
self.assertEqual(
self.analytic_son.display_name,
'[02] parent aa / son aa',
)
self.assertEqual(self.analytic_son.complete_name, "parent aa / son aa")
self.assertEqual(self.analytic_son.display_name, "[02] parent aa / son aa")

0 comments on commit 8351a09

Please sign in to comment.