stock_mts_mto_rule: add split option

With this change a new option is available on the stock rule.
It allows to use the MTS rule only if the full quantity is avaiable.
This commit is contained in:
Thierry Ducrest
2024-04-24 08:37:48 +02:00
parent 2a926e38fc
commit 6033e2a8c6
4 changed files with 62 additions and 2 deletions

View File

@@ -15,6 +15,16 @@ class StockRule(models.Model):
)
mts_rule_id = fields.Many2one("stock.rule", string="MTS Rule", check_company=True)
mto_rule_id = fields.Many2one("stock.rule", string="MTO Rule", check_company=True)
mts_quantity_rule = fields.Selection(
string="MTS quantity rule",
selection=[("available", "Available"), ("full", "No Split")],
default="available",
help=(
"With `Available` the MTS rule will be used for any available quantity.\n"
"With`No Split` the MTS rule is only used, if the full requested quantity "
"is available."
),
)
@api.constrains("action", "mts_rule_id", "mto_rule_id")
def _check_mts_mto_rule(self):
@@ -26,6 +36,10 @@ class StockRule(models.Model):
) % (rule.name,)
raise ValidationError(msg)
@api.model
def _get_available_quantity_mts_mto_rule(self, product):
return product.virtual_available
def get_mto_qty_to_order(self, product, product_qty, product_uom, values):
self.ensure_one()
precision = self.env["decimal.precision"].precision_get(
@@ -33,7 +47,7 @@ class StockRule(models.Model):
)
src_location_id = self.mts_rule_id.location_src_id.id
product_location = product.with_context(location=src_location_id)
virtual_available = product_location.virtual_available
virtual_available = self._get_available_quantity_mts_mto_rule(product_location)
qty_available = product.uom_id._compute_quantity(virtual_available, product_uom)
if float_compare(qty_available, 0.0, precision_digits=precision) > 0:
if (
@@ -41,6 +55,8 @@ class StockRule(models.Model):
>= 0
):
return 0.0
elif self.mts_quantity_rule == "full":
return product_qty
else:
return product_qty - qty_available
return product_qty

View File

@@ -2,7 +2,7 @@ This module add a Make To Stock + Make to Order Route.
If you choose the make to stock + make to order rule instead of the make to
order route, the creation of a purchase order will depend on the virtual stock.
There are 3 cases :
There are 3 cases (with the default splitting rule) :
1. The virtual stock of the product is 0
=> It will act exactly like the make to order route.
@@ -25,3 +25,7 @@ A sale Order is made for 3 products A.
2. 1 with a make to order rule and a quantity of 2.
After validation, a purchase order with 2 products will be created.
When using the No Split rule, the case 3 will not happen.
When the virtual stock is less than the ordered quantity, the full quantity
will be proucured through a make to order route (rule 1)

View File

@@ -94,6 +94,36 @@ class TestMtoMtsRoute(TransactionCase):
self.assertEqual(1, len(move_mto))
self.assertEqual("waiting", move_mto.state)
def test_mts_mto_route_no_split(self):
"""Check the no split rule."""
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
mto_mts_route.rule_ids.mts_quantity_rule = "full"
self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(1.0)
self.env["procurement.group"].run(
[
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.env["stock.move"].search(
[
("group_id", "=", self.group.id),
("location_dest_id", "=", self.customer_loc.id),
]
)
self.assertEqual(1, len(moves))
self.assertEqual(2.0, moves[0].product_uom_qty)
self.assertEqual("make_to_order", moves[0].procure_method)
def test_mts_mto_route_mto_only(self):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])]

View File

@@ -16,6 +16,11 @@
groups="stock.group_adv_location"
attrs="{'invisible': [('action', '!=', 'split_procurement')]}"
/>
<field
name="mts_quantity_rule"
groups="stock.group_adv_location"
attrs="{'invisible': [('action', '!=', 'split_procurement')]}"
/>
</field>
</field>
</record>
@@ -35,6 +40,11 @@
groups="stock.group_adv_location"
attrs="{'invisible': [('action', '!=', 'split_procurement')]}"
/>
<field
name="mts_quantity_rule"
groups="stock.group_adv_location"
attrs="{'invisible': [('action', '!=', 'split_procurement')]}"
/>
</field>
</field>
</record>