[MIG] stock_mts_mto_rule: Migration to 13.0

This commit is contained in:
Deivis Laya
2020-09-19 18:34:23 +00:00
committed by Pierrick Brun
parent 88bafdfd72
commit 58688edb9a
6 changed files with 123 additions and 75 deletions

View File

@@ -47,7 +47,7 @@ You should not select both the mts+mto route and the mto route.
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot :alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/153/12.0 :target: https://runbot.odoo-community.org/runbot/153/13.0
Known issues Known issues
============ ============

View File

@@ -3,7 +3,7 @@
{ {
"name": "Stock MTS+MTO Rule", "name": "Stock MTS+MTO Rule",
"summary": "Add a MTS+MTO route", "summary": "Add a MTS+MTO route",
"version": "12.0.1.0.1", "version": "13.0.1.0.1",
"development_status": "Mature", "development_status": "Mature",
"category": "Warehouse", "category": "Warehouse",
"website": "https://github.com/OCA/stock-logistics-warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse",
@@ -11,6 +11,6 @@
"license": "AGPL-3", "license": "AGPL-3",
"application": False, "application": False,
"installable": True, "installable": True,
"depends": ["stock",], "depends": ["stock"],
"data": ["data/stock_data.xml", "view/pull_rule.xml", "view/warehouse.xml",], "data": ["data/stock_data.xml", "view/pull_rule.xml", "view/warehouse.xml"],
} }

View File

@@ -4,7 +4,7 @@
# #
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Odoo Server 12.0\n" "Project-Id-Version: Odoo Server 13.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n" "Last-Translator: <>\n"
"Language-Team: \n" "Language-Team: \n"

View File

@@ -2,6 +2,7 @@
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.osv import expression
from odoo.tools import float_compare, float_is_zero from odoo.tools import float_compare, float_is_zero
@@ -11,8 +12,8 @@ class StockRule(models.Model):
action = fields.Selection( action = fields.Selection(
selection_add=[("split_procurement", "Choose between MTS and MTO")] selection_add=[("split_procurement", "Choose between MTS and MTO")]
) )
mts_rule_id = fields.Many2one("stock.rule", string="MTS Rule") mts_rule_id = fields.Many2one("stock.rule", string="MTS Rule", check_company=True)
mto_rule_id = fields.Many2one("stock.rule", string="MTO Rule") mto_rule_id = fields.Many2one("stock.rule", string="MTO Rule", check_company=True)
@api.constrains("action", "mts_rule_id", "mto_rule_id") @api.constrains("action", "mts_rule_id", "mto_rule_id")
def _check_mts_mto_rule(self): def _check_mts_mto_rule(self):
@@ -34,7 +35,6 @@ class StockRule(models.Model):
) % (rule.name,) ) % (rule.name,)
raise ValidationError(msg) raise ValidationError(msg)
@api.multi
def get_mto_qty_to_order(self, product, product_qty, product_uom, values): def get_mto_qty_to_order(self, product, product_qty, product_uom, values):
self.ensure_one() self.ensure_one()
precision = self.env["decimal.precision"].precision_get( precision = self.env["decimal.precision"].precision_get(
@@ -54,30 +54,53 @@ class StockRule(models.Model):
return product_qty - qty_available return product_qty - qty_available
return product_qty return product_qty
def _run_split_procurement( def _run_split_procurement(self, procurements):
self, product_id, product_qty, product_uom, location_id, name, origin, values
):
precision = self.env["decimal.precision"].precision_get( precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure" "Product Unit of Measure"
) )
needed_qty = self.get_mto_qty_to_order( for procurement, rule in procurements:
product_id, product_qty, product_uom, values domain = self.env["procurement.group"]._get_moves_to_assign_domain(
procurement.company_id.id
)
needed_qty = rule.get_mto_qty_to_order(
procurement.product_id,
procurement.product_qty,
procurement.product_uom,
procurement.values,
) )
if float_is_zero(needed_qty, precision_digits=precision): if float_is_zero(needed_qty, precision_digits=precision):
getattr(self.mts_rule_id, "_run_%s" % self.mts_rule_id.action)( getattr(self.env["stock.rule"], "_run_%s" % rule.mts_rule_id.action)(
product_id, product_qty, product_uom, location_id, name, origin, values [(procurement, rule.mts_rule_id)]
) )
elif float_compare(needed_qty, product_qty, precision_digits=precision) == 0.0: elif (
getattr(self.mto_rule_id, "_run_%s" % self.mto_rule_id.action)( float_compare(
product_id, product_qty, product_uom, location_id, name, origin, values needed_qty, procurement.product_qty, precision_digits=precision
)
== 0.0
):
getattr(self.env["stock.rule"], "_run_%s" % rule.mto_rule_id.action)(
[(procurement, rule.mto_rule_id)]
) )
else: else:
mts_qty = product_qty - needed_qty mts_qty = procurement.product_qty - needed_qty
getattr(self.mts_rule_id, "_run_%s" % self.mts_rule_id.action)( mts_procurement = procurement._replace(product_qty=mts_qty)
product_id, mts_qty, product_uom, location_id, name, origin, values getattr(self.env["stock.rule"], "_run_%s" % rule.mts_rule_id.action)(
[(mts_procurement, rule.mts_rule_id)]
) )
getattr(self.mto_rule_id, "_run_%s" % self.mto_rule_id.action)(
product_id, needed_qty, product_uom, location_id, name, origin, values # Search all confirmed stock_moves of mts_procuremet and assign them
# to adjust the product's free qty
group_id = mts_procurement.values.get("group_id")
group_domain = expression.AND(
[domain, [("group_id", "=", group_id.id)]]
)
moves_to_assign = self.env["stock.move"].search(
group_domain, order="priority desc, date_expected asc"
)
moves_to_assign._action_assign()
mto_procurement = procurement._replace(product_qty=needed_qty)
getattr(self.env["stock.rule"], "_run_%s" % rule.mto_rule_id.action)(
[(mto_procurement, rule.mto_rule_id)]
) )
return True return True

View File

@@ -12,7 +12,7 @@ class StockWarehouse(models.Model):
"purchase order will be created only if the virtual stock is " "purchase order will be created only if the virtual stock is "
"less than 0 else, the product will be taken from stocks", "less than 0 else, the product will be taken from stocks",
) )
mts_mto_rule_id = fields.Many2one("stock.rule", "MTO+MTS rule") mts_mto_rule_id = fields.Many2one("stock.rule", "MTO+MTS rule", check_company=True)
def _get_all_routes(self): def _get_all_routes(self):
routes = super(StockWarehouse, self)._get_all_routes() routes = super(StockWarehouse, self)._get_all_routes()
@@ -54,7 +54,7 @@ class StockWarehouse(models.Model):
"procure_method": "make_to_order", "procure_method": "make_to_order",
"company_id": self.company_id.id, "company_id": self.company_id.id,
"auto": "manual", "auto": "manual",
"propagate": True, "propagate_cancel": True,
"route_id": self._find_global_route( "route_id": self._find_global_route(
"stock_mts_mto_rule.route_mto_mts", "stock_mts_mto_rule.route_mto_mts",
_("Make To Order + Make To Stock"), _("Make To Order + Make To Stock"),

View File

@@ -18,28 +18,38 @@ class TestMtoMtsRoute(TransactionCase):
def test_standard_mto_route(self): def test_standard_mto_route(self):
mto_route = self.env.ref("stock.route_warehouse0_mto") mto_route = self.env.ref("stock.route_warehouse0_mto")
self.product.route_ids = [(6, 0, [mto_route.id])] self.product.route_ids = [(6, 0, [mto_route.id])]
self.group.run( self.env["procurement.group"].run(
[
self.group.Procurement(
self.product, self.product,
2.0, 2.0,
self.uom, self.uom,
self.customer_loc, self.customer_loc,
self.product.name, self.product.name,
"test", "test",
self.warehouse.company_id,
self.procurement_vals, self.procurement_vals,
) )
]
)
moves = self.move_obj.search([("group_id", "=", self.group.id)]) moves = self.move_obj.search([("group_id", "=", self.group.id)])
self.assertEqual(len(moves), 2) self.assertEqual(len(moves), 2)
def test_standard_mts_route(self): def test_standard_mts_route(self):
self.group.run( self.env["procurement.group"].run(
[
self.group.Procurement(
self.product, self.product,
2.0, 2.0,
self.uom, self.uom,
self.customer_loc, self.customer_loc,
self.product.name, self.product.name,
"test", "test",
self.warehouse.company_id,
self.procurement_vals, self.procurement_vals,
) )
]
)
moves = self.move_obj.search([("group_id", "=", self.group.id)]) moves = self.move_obj.search([("group_id", "=", self.group.id)])
self.assertEqual(len(moves), 1) self.assertEqual(len(moves), 1)
@@ -47,15 +57,20 @@ class TestMtoMtsRoute(TransactionCase):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(1.0) self._create_quant(1.0)
self.group.run( self.env["procurement.group"].run(
[
self.group.Procurement(
self.product, self.product,
2.0, 2.0,
self.uom, self.uom,
self.customer_loc, self.customer_loc,
self.product.name, self.product.name,
"test", "test",
self.warehouse.company_id,
self.procurement_vals, self.procurement_vals,
) )
]
)
moves = self.env["stock.move"].search([("group_id", "=", self.group.id)]) moves = self.env["stock.move"].search([("group_id", "=", self.group.id)])
self.assertEqual(3, len(moves)) self.assertEqual(3, len(moves))
move_mts = self.env["stock.move"].search( move_mts = self.env["stock.move"].search(
@@ -67,7 +82,7 @@ class TestMtoMtsRoute(TransactionCase):
) )
self.assertEqual(1, len(move_mts)) self.assertEqual(1, len(move_mts))
self.assertEqual(1.0, move_mts.product_uom_qty) self.assertEqual(1.0, move_mts.product_uom_qty)
self.assertEqual("confirmed", move_mts.state) self.assertEqual("assigned", move_mts.state)
move_mto = self.env["stock.move"].search( move_mto = self.env["stock.move"].search(
[ [
("group_id", "=", self.group.id), ("group_id", "=", self.group.id),
@@ -81,15 +96,20 @@ class TestMtoMtsRoute(TransactionCase):
def test_mts_mto_route_mto_only(self): def test_mts_mto_route_mto_only(self):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self.group.run( self.env["procurement.group"].run(
[
self.group.Procurement(
self.product, self.product,
2.0, 2.0,
self.uom, self.uom,
self.customer_loc, self.customer_loc,
self.product.name, self.product.name,
"test", "test",
self.warehouse.company_id,
self.procurement_vals, self.procurement_vals,
) )
]
)
moves = self.env["stock.move"].search( moves = self.env["stock.move"].search(
[ [
("group_id", "=", self.group.id), ("group_id", "=", self.group.id),
@@ -104,15 +124,20 @@ class TestMtoMtsRoute(TransactionCase):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(3.0) self._create_quant(3.0)
self.group.run( self.env["procurement.group"].run(
[
self.group.Procurement(
self.product, self.product,
2.0, 2.0,
self.uom, self.uom,
self.customer_loc, self.customer_loc,
self.product.name, self.product.name,
"test", "test",
self.warehouse.company_id,
self.procurement_vals, self.procurement_vals,
) )
]
)
moves = self.env["stock.move"].search([("group_id", "=", self.group.id)]) moves = self.env["stock.move"].search([("group_id", "=", self.group.id)])
self.assertEqual(1, len(moves)) self.assertEqual(1, len(moves))
self.assertEqual(2.0, moves[0].product_uom_qty) self.assertEqual(2.0, moves[0].product_uom_qty)
@@ -195,10 +220,10 @@ class TestMtoMtsRoute(TransactionCase):
self.warehouse.mto_mts_management = True self.warehouse.mto_mts_management = True
self.customer_loc = self.env.ref("stock.stock_location_customers") self.customer_loc = self.env.ref("stock.stock_location_customers")
self.product = self.env["product.product"].create( self.product = self.env["product.product"].create(
{"name": "Test product", "type": "product",} {"name": "Test product", "type": "product"}
) )
self.company_partner = self.env.ref("base.main_partner") self.company_partner = self.env.ref("base.main_partner")
self.group = self.env["procurement.group"].create({"name": "test",}) self.group = self.env["procurement.group"].create({"name": "test"})
self.procurement_vals = {"warehouse_id": self.warehouse, "group_id": self.group} self.procurement_vals = {"warehouse_id": self.warehouse, "group_id": self.group}
# Since mrp and purchase modules may not be installed, we need to # Since mrp and purchase modules may not be installed, we need to
# create a dummy step to show that mts, mto, and mts+mto flows work. # create a dummy step to show that mts, mto, and mts+mto flows work.