diff --git a/stock_request_analytic/models/stock_request.py b/stock_request_analytic/models/stock_request.py index 70ba0cf4d..64b9aa491 100644 --- a/stock_request_analytic/models/stock_request.py +++ b/stock_request_analytic/models/stock_request.py @@ -2,7 +2,7 @@ # Copyright 2021 Tecnativa - João Marques # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo import fields, models +from odoo import api, fields, models class StockRequest(models.Model): @@ -19,3 +19,13 @@ class StockRequest(models.Model): string="Analytic Tags", check_company=True, ) + + @api.onchange("product_id") + def onchange_product_id(self): + """ + Set default analytic account on lines from order if defined. + """ + res = super().onchange_product_id() + if self.order_id and self.order_id.default_analytic_account_id: + self.analytic_account_id = self.order_id.default_analytic_account_id + return res diff --git a/stock_request_analytic/models/stock_request_order.py b/stock_request_analytic/models/stock_request_order.py index eed38a1ae..eadfbda4f 100644 --- a/stock_request_analytic/models/stock_request_order.py +++ b/stock_request_analytic/models/stock_request_order.py @@ -35,6 +35,11 @@ class StockRequestOrder(models.Model): string="Analytic Tags", readonly=True, ) + default_analytic_account_id = fields.Many2one( + comodel_name="account.analytic.account", + string="Default Analytic Account", + help="Set this if you want to define a default analytic account on requests", + ) @api.depends("stock_request_ids") def _compute_analytic_ids(self): diff --git a/stock_request_analytic/readme/CONTRIBUTORS.rst b/stock_request_analytic/readme/CONTRIBUTORS.rst index decc3d3ea..0e00ac65b 100644 --- a/stock_request_analytic/readme/CONTRIBUTORS.rst +++ b/stock_request_analytic/readme/CONTRIBUTORS.rst @@ -1,6 +1,8 @@ * Aaron Henriquez * Lois Rilo * Pimolnat Suntian +* Alan Ramos +* Denis Roussel * `Tecnativa `__: * João Marques diff --git a/stock_request_analytic/tests/test_stock_request_analytic.py b/stock_request_analytic/tests/test_stock_request_analytic.py index c6b343b03..98ed1b7ae 100644 --- a/stock_request_analytic/tests/test_stock_request_analytic.py +++ b/stock_request_analytic/tests/test_stock_request_analytic.py @@ -3,6 +3,7 @@ from odoo import fields from odoo.exceptions import UserError +from odoo.tests import Form from odoo.addons.stock_request.tests import test_stock_request @@ -148,3 +149,25 @@ class TestStockRequestAnalytic(test_stock_request.TestStockRequest): self.analytic2, self.main_company ) self.env["stock.request.order"].create(vals) + + def test_default_analytic(self): + """ + Create request order with a default analytic + """ + vals = self.prepare_order_request_analytic( + self.analytic_model.browse(), self.main_company + ) + vals.update( + { + "default_analytic_account_id": self.analytic.id, + } + ) + order = self.env["stock.request.order"].create(vals) + with Form(order) as order_form: + with order_form.stock_request_ids.new() as line_form: + line_form.product_id = self.pizza + line_form.product_uom_qty = 5.0 + self.assertEqual( + order.default_analytic_account_id, + order.stock_request_ids.mapped("analytic_account_id"), + ) diff --git a/stock_request_analytic/views/stock_request_order_views.xml b/stock_request_analytic/views/stock_request_order_views.xml index 003862010..05af1e48b 100644 --- a/stock_request_analytic/views/stock_request_order_views.xml +++ b/stock_request_analytic/views/stock_request_order_views.xml @@ -41,6 +41,12 @@ /> + + +