@@ -16,6 +16,22 @@ class StockRule(models.Model):
1616 mts_rule_id = fields .Many2one ("stock.rule" , string = "MTS Rule" , check_company = True )
1717 mto_rule_id = fields .Many2one ("stock.rule" , string = "MTO Rule" , check_company = True )
1818
19+ def _run_subrule_action (self , procurement , subrule ):
20+ """Shortcut to run a subrule for a single procurement."""
21+ self .ensure_one ()
22+ run_subrule = getattr (self .env ["stock.rule" ], f"_run_{ subrule .action } " )
23+ return run_subrule ([(procurement , subrule )])
24+
25+ def _run_mts_action (self , procurement ):
26+ """Shortcut to run the MTS rule linked to this MTS+MTO rule."""
27+ self .mts_rule_id .ensure_one ()
28+ return self ._run_subrule_action (procurement , self .mts_rule_id )
29+
30+ def _run_mto_action (self , procurement ):
31+ """Shortcut to run the MTO rule linked to this MTS+MTO rule."""
32+ self .mto_rule_id .ensure_one ()
33+ return self ._run_subrule_action (procurement , self .mto_rule_id )
34+
1935 @api .constrains ("action" , "mts_rule_id" , "mto_rule_id" )
2036 def _check_mts_mto_rule (self ):
2137 for rule in self :
@@ -63,44 +79,40 @@ def _run_split_procurement(self, procurements):
6379 domain = self .env ["procurement.group" ]._get_moves_to_assign_domain (
6480 procurement .company_id .id
6581 )
82+ # Determine the quantity to order as MTO
6683 needed_qty = rule .get_mto_qty_to_order (
6784 procurement .product_id ,
6885 procurement .product_qty ,
6986 procurement .product_uom ,
7087 procurement .values ,
7188 )
89+ # Enough stock, only MTS
7290 if float_is_zero (needed_qty , precision_digits = precision ):
73- getattr (self .env ["stock.rule" ], f"_run_{ rule .mts_rule_id .action } " )(
74- [(procurement , rule .mts_rule_id )]
75- )
91+ rule ._run_mts_action (procurement )
92+ # No stock, only MTO
7693 elif (
7794 float_compare (
7895 needed_qty , procurement .product_qty , precision_digits = precision
7996 )
8097 == 0.0
8198 ):
82- getattr (self .env ["stock.rule" ], f"_run_{ rule .mto_rule_id .action } " )(
83- [(procurement , rule .mto_rule_id )]
84- )
99+ rule ._run_mto_action (procurement )
100+ # Partial stock, split between MTS and MTO
85101 else :
86102 mts_qty = procurement .product_qty - needed_qty
87103 mts_procurement = procurement ._replace (product_qty = mts_qty )
88- getattr (self .env ["stock.rule" ], f"_run_{ rule .mts_rule_id .action } " )(
89- [(mts_procurement , rule .mts_rule_id )]
90- )
104+ rule ._run_mts_action (mts_procurement )
91105
92106 # Search all confirmed stock_moves of mts_procurement and assign them
93107 # to adjust the product's free qty
94108 group_id = mts_procurement .values .get ("group_id" )
95109 if group_id :
96110 domain = expression .AND ([domain , [("group_id" , "=" , group_id .id )]])
97111 moves_to_assign = self .env ["stock.move" ].search (
98- group_domain , order = "priority desc, date asc"
112+ domain , order = "priority desc, date asc"
99113 )
100114 moves_to_assign ._action_assign ()
101115
102116 mto_procurement = procurement ._replace (product_qty = needed_qty )
103- getattr (self .env ["stock.rule" ], f"_run_{ rule .mto_rule_id .action } " )(
104- [(mto_procurement , rule .mto_rule_id )]
105- )
117+ rule ._run_mto_action (mto_procurement )
106118 return True
0 commit comments