mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[MIG] stock_request_analytic: Migration to 15.0
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
{
|
||||
"name": "Stock Request Analytic",
|
||||
"summary": "Internal request for stock",
|
||||
"version": "14.0.1.0.3",
|
||||
"version": "15.0.1.0.0",
|
||||
"license": "AGPL-3",
|
||||
"website": "https://github.com/OCA/stock-logistics-warehouse",
|
||||
"author": "ForgeFlow, Odoo Community Association (OCA)",
|
||||
|
||||
@@ -10,28 +10,30 @@ class StockRequest(models.Model):
|
||||
_check_company_auto = True
|
||||
|
||||
analytic_account_id = fields.Many2one(
|
||||
"account.analytic.account",
|
||||
comodel_name="account.analytic.account",
|
||||
string="Analytic Account",
|
||||
compute="_compute_analytic_id",
|
||||
store=True,
|
||||
readonly=False,
|
||||
check_company=True,
|
||||
compute_sudo=True,
|
||||
)
|
||||
analytic_tag_ids = fields.Many2many(
|
||||
"account.analytic.tag",
|
||||
comodel_name="account.analytic.tag",
|
||||
string="Analytic Tags",
|
||||
check_company=True,
|
||||
)
|
||||
|
||||
@api.onchange("product_id")
|
||||
def onchange_product_id(self):
|
||||
@api.depends("order_id")
|
||||
def _compute_analytic_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
|
||||
for req in self:
|
||||
if req.order_id and req.order_id.default_analytic_account_id:
|
||||
req.analytic_account_id = req.order_id.default_analytic_account_id
|
||||
|
||||
def _prepare_procurement_values(self, group_id=False):
|
||||
|
||||
"""
|
||||
Add analytic account to procurement values
|
||||
"""
|
||||
|
||||
@@ -21,19 +21,29 @@ MAP_VIEWS = {
|
||||
class StockRequestOrder(models.Model):
|
||||
_inherit = "stock.request.order"
|
||||
|
||||
analytic_count = fields.Integer(compute="_compute_analytic_ids", readonly=True)
|
||||
analytic_tag_count = fields.Integer(compute="_compute_analytic_ids", readonly=True)
|
||||
analytic_count = fields.Integer(
|
||||
compute="_compute_analytic_ids",
|
||||
readonly=True,
|
||||
compute_sudo=True,
|
||||
)
|
||||
analytic_tag_count = fields.Integer(
|
||||
compute="_compute_analytic_ids",
|
||||
readonly=True,
|
||||
compute_sudo=True,
|
||||
)
|
||||
analytic_account_ids = fields.One2many(
|
||||
comodel_name="account.analytic.account",
|
||||
compute="_compute_analytic_ids",
|
||||
string="Analytic Accounts",
|
||||
readonly=True,
|
||||
compute_sudo=True,
|
||||
)
|
||||
analytic_tag_ids = fields.One2many(
|
||||
comodel_name="account.analytic.tag",
|
||||
compute="_compute_analytic_ids",
|
||||
string="Analytic Tags",
|
||||
readonly=True,
|
||||
compute_sudo=True,
|
||||
)
|
||||
default_analytic_account_id = fields.Many2one(
|
||||
comodel_name="account.analytic.account",
|
||||
@@ -43,7 +53,7 @@ class StockRequestOrder(models.Model):
|
||||
|
||||
@api.depends("stock_request_ids")
|
||||
def _compute_analytic_ids(self):
|
||||
for req in self.sudo():
|
||||
for req in self:
|
||||
req.analytic_account_ids = req.stock_request_ids.mapped(
|
||||
"analytic_account_id"
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ class ProcurementRule(models.Model):
|
||||
company_id,
|
||||
values,
|
||||
):
|
||||
res = super(ProcurementRule, self)._get_stock_move_values(
|
||||
res = super()._get_stock_move_values(
|
||||
product_id,
|
||||
product_qty,
|
||||
product_uom,
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.3 KiB After Width: | Height: | Size: 15 KiB |
@@ -4,73 +4,99 @@
|
||||
from odoo import fields
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.tests import Form
|
||||
|
||||
from odoo.addons.stock_request.tests import test_stock_request
|
||||
from odoo.tests.common import TransactionCase
|
||||
|
||||
|
||||
class TestStockRequestAnalytic(test_stock_request.TestStockRequest):
|
||||
def setUp(self):
|
||||
super(TestStockRequestAnalytic, self).setUp()
|
||||
self.analytic_model = self.env["account.analytic.account"]
|
||||
self.analytic = self.analytic_model.create({"name": "Pizza"})
|
||||
self.analytic2 = self.analytic_model.create(
|
||||
{"name": "Pizza", "company_id": self.company_2.id}
|
||||
class TestStockRequestAnalytic(TransactionCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
|
||||
# Model
|
||||
cls.AccountAnalyticAccount = cls.env["account.analytic.account"]
|
||||
cls.AccountAnalyticTag = cls.env["account.analytic.tag"]
|
||||
cls.ProductProduct = cls.env["product.product"]
|
||||
cls.ResUsers = cls.env["res.users"]
|
||||
cls.StockRequest = cls.env["stock.request"]
|
||||
cls.StockRequestOrder = cls.env["stock.request.order"]
|
||||
cls.StockLocation = cls.env["stock.location"]
|
||||
cls.StockLocationRoute = cls.env["stock.location.route"]
|
||||
cls.StockRule = cls.env["stock.rule"]
|
||||
|
||||
# Data
|
||||
cls.expected_date = fields.Datetime.now()
|
||||
cls.main_company = cls.env.ref("base.main_company")
|
||||
cls.company_2 = cls.env.ref("stock.res_company_1")
|
||||
cls.warehouse = cls.env.ref("stock.warehouse0")
|
||||
cls.stock_request_user_group = cls.env.ref(
|
||||
"stock_request.group_stock_request_user"
|
||||
)
|
||||
self.analytic3 = self.analytic_model.create({"name": "Hamburger"})
|
||||
self.demand_loc = self.env["stock.location"].create(
|
||||
cls.stock_request_manager_group = cls.env.ref(
|
||||
"stock_request.group_stock_request_manager"
|
||||
)
|
||||
cls.analytic1 = cls.AccountAnalyticAccount.create({"name": "Analytic"})
|
||||
cls.analytic2 = cls.AccountAnalyticAccount.create(
|
||||
{"name": "Analytic", "company_id": cls.company_2.id}
|
||||
)
|
||||
cls.analytic3 = cls.AccountAnalyticAccount.create({"name": "Analytic 3"})
|
||||
cls.demand_loc = cls.StockLocation.create(
|
||||
{
|
||||
"name": "demand_loc",
|
||||
"location_id": self.warehouse.lot_stock_id.id,
|
||||
"location_id": cls.warehouse.lot_stock_id.id,
|
||||
"usage": "internal",
|
||||
}
|
||||
)
|
||||
self.demand_route = self.env["stock.location.route"].create(
|
||||
cls.demand_route = cls.StockLocationRoute.create(
|
||||
{
|
||||
"name": "Transfer",
|
||||
"product_categ_selectable": False,
|
||||
"product_selectable": True,
|
||||
"company_id": self.main_company.id,
|
||||
"company_id": cls.main_company.id,
|
||||
"sequence": 10,
|
||||
}
|
||||
)
|
||||
self.pizza = self._create_product("PZ", "Pizza", False)
|
||||
self.demand_rule = self.env["stock.rule"].create(
|
||||
cls.demand_rule = cls.StockRule.create(
|
||||
{
|
||||
"name": "Transfer",
|
||||
"route_id": self.demand_route.id,
|
||||
"location_src_id": self.warehouse.lot_stock_id.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"route_id": cls.demand_route.id,
|
||||
"location_src_id": cls.warehouse.lot_stock_id.id,
|
||||
"location_id": cls.demand_loc.id,
|
||||
"action": "pull",
|
||||
"picking_type_id": self.warehouse.int_type_id.id,
|
||||
"picking_type_id": cls.warehouse.int_type_id.id,
|
||||
"procure_method": "make_to_stock",
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"company_id": self.main_company.id,
|
||||
"warehouse_id": cls.warehouse.id,
|
||||
"company_id": cls.main_company.id,
|
||||
}
|
||||
)
|
||||
cls.product = cls.ProductProduct.create(
|
||||
{
|
||||
"name": "Test Product",
|
||||
"type": "product",
|
||||
"route_ids": [(6, 0, cls.demand_route.ids)],
|
||||
}
|
||||
)
|
||||
self.pizza.route_ids = [(6, 0, self.demand_route.ids)]
|
||||
|
||||
def prepare_order_request_analytic(self, analytic, company, analytic_tags=None):
|
||||
expected_date = fields.Datetime.now()
|
||||
analytic_tags = analytic_tags or self.env["account.analytic.tag"]
|
||||
analytic_tags = analytic_tags or self.AccountAnalyticTag
|
||||
vals = {
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"expected_date": self.expected_date,
|
||||
"stock_request_ids": [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"product_id": self.pizza.id,
|
||||
"product_uom_id": self.pizza.uom_id.id,
|
||||
"product_id": self.product.id,
|
||||
"product_uom_id": self.product.uom_id.id,
|
||||
"product_uom_qty": 5.0,
|
||||
"analytic_account_id": analytic.id,
|
||||
"analytic_tag_ids": [(4, tag.id) for tag in analytic_tags],
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"expected_date": self.expected_date,
|
||||
},
|
||||
)
|
||||
],
|
||||
@@ -78,39 +104,38 @@ class TestStockRequestAnalytic(test_stock_request.TestStockRequest):
|
||||
return vals
|
||||
|
||||
def prepare_order_request_multi_analytic(self, analytic1, analytic2, company):
|
||||
expected_date = fields.Datetime.now()
|
||||
vals = {
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"expected_date": self.expected_date,
|
||||
"stock_request_ids": [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"product_id": self.pizza.id,
|
||||
"product_uom_id": self.pizza.uom_id.id,
|
||||
"product_id": self.product.id,
|
||||
"product_uom_id": self.product.uom_id.id,
|
||||
"product_uom_qty": 5.0,
|
||||
"analytic_account_id": analytic1.id,
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"expected_date": self.expected_date,
|
||||
},
|
||||
),
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"product_id": self.pizza.id,
|
||||
"product_uom_id": self.pizza.uom_id.id,
|
||||
"product_id": self.product.id,
|
||||
"product_uom_id": self.product.uom_id.id,
|
||||
"product_uom_qty": 5.0,
|
||||
"analytic_account_id": analytic2.id,
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"expected_date": self.expected_date,
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -120,26 +145,26 @@ class TestStockRequestAnalytic(test_stock_request.TestStockRequest):
|
||||
def test_stock_analytic(self):
|
||||
analytic_tag = self.env.ref("analytic.tag_contract")
|
||||
vals = self.prepare_order_request_analytic(
|
||||
self.analytic, self.main_company, analytic_tags=analytic_tag
|
||||
self.analytic1, self.main_company, analytic_tags=analytic_tag
|
||||
)
|
||||
order = self.env["stock.request.order"].create(vals)
|
||||
order = self.StockRequestOrder.create(vals)
|
||||
req = order.stock_request_ids
|
||||
order.action_confirm()
|
||||
self.assertEqual(req.move_ids.mapped("analytic_account_id"), self.analytic)
|
||||
self.assertEqual(req.move_ids.mapped("analytic_account_id"), self.analytic1)
|
||||
self.assertEqual(req.move_ids.mapped("analytic_tag_ids"), analytic_tag)
|
||||
self.assertEqual(order.analytic_count, 1)
|
||||
action = order.with_context(
|
||||
analytic_type="analytic_account"
|
||||
).action_view_analytic()
|
||||
self.assertTrue(action["res_id"], self.analytic.id)
|
||||
action2 = self.analytic.action_view_stock_request()
|
||||
self.assertTrue(action["res_id"], self.analytic1.id)
|
||||
action2 = self.analytic1.action_view_stock_request()
|
||||
self.assertTrue(action2["res_id"], order.id)
|
||||
|
||||
def test_stock_multi_analytic(self):
|
||||
vals = self.prepare_order_request_multi_analytic(
|
||||
self.analytic, self.analytic3, self.main_company
|
||||
self.analytic1, self.analytic3, self.main_company
|
||||
)
|
||||
order = self.env["stock.request.order"].create(vals)
|
||||
order = self.StockRequestOrder.create(vals)
|
||||
order.action_confirm()
|
||||
self.assertEqual(order.analytic_count, 2)
|
||||
|
||||
@@ -148,24 +173,24 @@ class TestStockRequestAnalytic(test_stock_request.TestStockRequest):
|
||||
vals = self.prepare_order_request_analytic(
|
||||
self.analytic2, self.main_company
|
||||
)
|
||||
self.env["stock.request.order"].create(vals)
|
||||
self.StockRequestOrder.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
|
||||
self.AccountAnalyticAccount.browse(), self.main_company
|
||||
)
|
||||
vals.update(
|
||||
{
|
||||
"default_analytic_account_id": self.analytic.id,
|
||||
"default_analytic_account_id": self.analytic1.id,
|
||||
}
|
||||
)
|
||||
order = self.env["stock.request.order"].create(vals)
|
||||
order = self.StockRequestOrder.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_id = self.product
|
||||
line_form.product_uom_qty = 5.0
|
||||
self.assertEqual(
|
||||
order.default_analytic_account_id,
|
||||
|
||||
Reference in New Issue
Block a user