Skip to content

Commit

Permalink
[MIG] stock_request_analytic: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-tubtim committed Feb 23, 2023
1 parent be42401 commit ba32949
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 59 deletions.
2 changes: 1 addition & 1 deletion stock_request_analytic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
18 changes: 9 additions & 9 deletions stock_request_analytic/models/stock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ 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,
)
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.sudo():
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
"""
Expand Down
2 changes: 1 addition & 1 deletion stock_request_analytic/models/stock_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _get_stock_move_values(
company_id,
values,
):
res = super(ProcurementRule, self)._get_stock_move_values(
res = super()._get_stock_move_values(
product_id,
product_qty,
product_uom,
Expand Down
Binary file modified stock_request_analytic/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 73 additions & 48 deletions stock_request_analytic/tests/test_stock_request_analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,138 @@
from odoo import fields
from odoo.exceptions import UserError
from odoo.tests import Form
from odoo.tests.common import TransactionCase

from odoo.addons.stock_request.tests import test_stock_request

class TestStockRequestAnalytic(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()

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}
# 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"
)
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}
)
self.analytic3 = self.analytic_model.create({"name": "Hamburger"})
self.demand_loc = self.env["stock.location"].create(
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,
},
)
],
}
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,
},
),
],
Expand All @@ -120,26 +145,26 @@ def prepare_order_request_multi_analytic(self, analytic1, analytic2, company):
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)

Expand All @@ -148,24 +173,24 @@ def test_company(self):
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,
Expand Down

0 comments on commit ba32949

Please sign in to comment.