2
0

[IMP] account_asset_management: Add analytic tags and propagate

TT28974
This commit is contained in:
João Marques 2021-04-06 14:24:08 +01:00 committed by Rodrigo
parent 0325891a82
commit 2cbf50e056
8 changed files with 61 additions and 1 deletions

View File

@ -272,6 +272,13 @@ class AccountAsset(models.Model):
readonly=False, readonly=False,
store=True, store=True,
) )
analytic_tag_ids = fields.Many2many(
comodel_name="account.analytic.tag",
string="Analytic tags",
compute="_compute_analytic_tag_ids",
readonly=False,
store=True,
)
@api.model @api.model
def _default_company_id(self): def _default_company_id(self):
@ -374,6 +381,11 @@ class AccountAsset(models.Model):
for asset in self: for asset in self:
asset.account_analytic_id = asset.profile_id.account_analytic_id asset.account_analytic_id = asset.profile_id.account_analytic_id
@api.depends("profile_id")
def _compute_analytic_tag_ids(self):
for asset in self:
asset.analytic_tag_ids = asset.profile_id.analytic_tag_ids
@api.constrains("method", "method_time") @api.constrains("method", "method_time")
def _check_method(self): def _check_method(self):
if self.filtered( if self.filtered(

View File

@ -212,9 +212,11 @@ class AccountAssetLine(models.Model):
return move_data return move_data
def _setup_move_line_data(self, depreciation_date, account, ml_type, move): def _setup_move_line_data(self, depreciation_date, account, ml_type, move):
"""Prepare data to be propagated to account.move.line"""
asset = self.asset_id asset = self.asset_id
amount = self.amount amount = self.amount
analytic_id = False analytic_id = False
analytic_tags = self.env["account.analytic.tag"]
if ml_type == "depreciation": if ml_type == "depreciation":
debit = amount < 0 and -amount or 0.0 debit = amount < 0 and -amount or 0.0
credit = amount > 0 and amount or 0.0 credit = amount > 0 and amount or 0.0
@ -222,6 +224,7 @@ class AccountAssetLine(models.Model):
debit = amount > 0 and amount or 0.0 debit = amount > 0 and amount or 0.0
credit = amount < 0 and -amount or 0.0 credit = amount < 0 and -amount or 0.0
analytic_id = asset.account_analytic_id.id analytic_id = asset.account_analytic_id.id
analytic_tags = asset.analytic_tag_ids
move_line_data = { move_line_data = {
"name": asset.name, "name": asset.name,
"ref": self.name, "ref": self.name,
@ -232,6 +235,7 @@ class AccountAssetLine(models.Model):
"journal_id": asset.profile_id.journal_id.id, "journal_id": asset.profile_id.journal_id.id,
"partner_id": asset.partner_id.id, "partner_id": asset.partner_id.id,
"analytic_account_id": analytic_id, "analytic_account_id": analytic_id,
"analytic_tag_ids": [(4, tag.id) for tag in analytic_tags],
"date": depreciation_date, "date": depreciation_date,
"asset_id": asset.id, "asset_id": asset.id,
} }

View File

@ -15,6 +15,9 @@ class AccountAssetProfile(models.Model):
account_analytic_id = fields.Many2one( account_analytic_id = fields.Many2one(
comodel_name="account.analytic.account", string="Analytic account" comodel_name="account.analytic.account", string="Analytic account"
) )
analytic_tag_ids = fields.Many2many(
comodel_name="account.analytic.tag", string="Analytic tags"
)
account_asset_id = fields.Many2one( account_asset_id = fields.Many2one(
comodel_name="account.account", comodel_name="account.account",
domain=[("deprecated", "=", False)], domain=[("deprecated", "=", False)],

View File

@ -77,6 +77,7 @@ class AccountMove(models.Model):
"partner_id": aml.partner_id.id, "partner_id": aml.partner_id.id,
"date_start": move.date, "date_start": move.date,
"account_analytic_id": aml.analytic_account_id.id, "account_analytic_id": aml.analytic_account_id.id,
"analytic_tag_ids": aml.analytic_tag_ids.id,
} }
if self.env.context.get("company_id"): if self.env.context.get("company_id"):
vals["company_id"] = self.env.context["company_id"] vals["company_id"] = self.env.context["company_id"]

View File

@ -58,6 +58,23 @@ class TestAssetManagement(AccountTestInvoicingCommon):
line_form.price_unit = 20000.00 line_form.price_unit = 20000.00
line_form.quantity = 1 line_form.quantity = 1
cls.invoice_2 = move_form.save() cls.invoice_2 = move_form.save()
# analytic configuration
cls.env.user.write(
{
"groups_id": [
(4, cls.env.ref("analytic.group_analytic_accounting").id),
(4, cls.env.ref("analytic.group_analytic_tags").id),
],
}
)
cls.analytic_account = cls.env["account.analytic.account"].create(
{"name": "test_analytic_account"}
)
cls.analytic_tag = cls.env["account.analytic.tag"].create(
{"name": "test_analytic_tag"}
)
# Asset Profile 1 # Asset Profile 1
cls.ict3Y = cls.asset_profile_model.create( cls.ict3Y = cls.asset_profile_model.create(
{ {
@ -90,6 +107,8 @@ class TestAssetManagement(AccountTestInvoicingCommon):
"method_time": "year", "method_time": "year",
"method_number": 5, "method_number": 5,
"method_period": "year", "method_period": "year",
"account_analytic_id": cls.analytic_account.id,
"analytic_tag_ids": [(4, cls.analytic_tag.id)],
} }
) )
@ -214,7 +233,17 @@ class TestAssetManagement(AccountTestInvoicingCommon):
self.assertEqual(ict0.value_depreciated, 500) self.assertEqual(ict0.value_depreciated, 500)
self.assertEqual(ict0.value_residual, 1000) self.assertEqual(ict0.value_residual, 1000)
vehicle0.validate() vehicle0.validate()
vehicle0.depreciation_line_ids[1].create_move() created_move_ids = vehicle0.depreciation_line_ids[1].create_move()
for move_id in created_move_ids:
move = self.env["account.move"].browse(move_id)
expense_line = move.line_ids.filtered(
lambda line: line.account_id.internal_group == "expense"
)
self.assertEqual(
expense_line.analytic_account_id.id,
self.analytic_account.id,
)
self.assertEqual(expense_line.analytic_tag_ids.id, self.analytic_tag.id)
vehicle0.refresh() vehicle0.refresh()
self.assertEqual(vehicle0.state, "open") self.assertEqual(vehicle0.state, "open")
self.assertEqual(vehicle0.value_depreciated, 2000) self.assertEqual(vehicle0.value_depreciated, 2000)

View File

@ -114,6 +114,11 @@
name="account_analytic_id" name="account_analytic_id"
groups="analytic.group_analytic_accounting" groups="analytic.group_analytic_accounting"
/> />
<field
name="analytic_tag_ids"
groups="analytic.group_analytic_accounting"
widget="many2many_tags"
/>
</group> </group>
<group colspan="4"> <group colspan="4">
<group> <group>

View File

@ -57,6 +57,7 @@
string="Analytic Information" string="Analytic Information"
> >
<field name="account_analytic_id" /> <field name="account_analytic_id" />
<field name="analytic_tag_ids" widget="many2many_tags" />
</group> </group>
</group> </group>
<separator string="Notes" /> <separator string="Notes" />

View File

@ -306,6 +306,7 @@ class AccountAssetRemove(models.TransientModel):
"name": asset.name, "name": asset.name,
"account_id": self.account_residual_value_id.id, "account_id": self.account_residual_value_id.id,
"analytic_account_id": asset.account_analytic_id.id, "analytic_account_id": asset.account_analytic_id.id,
"analytic_tag_ids": [(4, tag.id) for tag in asset.analytic_tag_ids],
"debit": residual_value, "debit": residual_value,
"credit": 0.0, "credit": 0.0,
"partner_id": partner_id, "partner_id": partner_id,
@ -319,6 +320,9 @@ class AccountAssetRemove(models.TransientModel):
"name": asset.name, "name": asset.name,
"account_id": self.account_sale_id.id, "account_id": self.account_sale_id.id,
"analytic_account_id": asset.account_analytic_id.id, "analytic_account_id": asset.account_analytic_id.id,
"analytic_tag_ids": [
(4, tag.id) for tag in asset.analytic_tag_ids
],
"debit": sale_value, "debit": sale_value,
"credit": 0.0, "credit": 0.0,
"partner_id": partner_id, "partner_id": partner_id,
@ -335,6 +339,7 @@ class AccountAssetRemove(models.TransientModel):
"name": asset.name, "name": asset.name,
"account_id": account_id, "account_id": account_id,
"analytic_account_id": asset.account_analytic_id.id, "analytic_account_id": asset.account_analytic_id.id,
"analytic_tag_ids": [(4, tag.id) for tag in asset.analytic_tag_ids],
"debit": balance < 0 and -balance or 0.0, "debit": balance < 0 and -balance or 0.0,
"credit": balance > 0 and balance or 0.0, "credit": balance > 0 and balance or 0.0,
"partner_id": partner_id, "partner_id": partner_id,