Skip to content

Commit

Permalink
[FEAT] mrp_stock_analytic: add analytic to new raw lines
Browse files Browse the repository at this point in the history
Also add analytic distribution when manually adding raw lines to an MO
with an analytic distribution set.
  • Loading branch information
aisopuro committed Sep 5, 2024
1 parent 6ed3b79 commit 88e2b33
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions mrp_stock_analytic/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import mrp_production
from . import stock_move
24 changes: 24 additions & 0 deletions mrp_stock_analytic/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from odoo import api, models


class StockMove(models.Model):
_inherit = "stock.move"

@api.model_create_multi
def create(self, vals_list):
"""
Extend to copy the analytic distribution of the manufacturing order
if a move is added as a raw material move to it.
"""
for vals in vals_list:
if "analytic_distribution" in vals:
continue
raw_production = (
self.env["mrp.production"]
.browse(vals.get("raw_material_production_id"))
.exists()
)
if not raw_production.analytic_distribution:
continue
vals["analytic_distribution"] = raw_production.analytic_distribution
return super().create(vals_list)
35 changes: 35 additions & 0 deletions mrp_stock_analytic/tests/test_mrp_stock_analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,38 @@ def test_analytic_propagation_backorder(self):
backorder.move_raw_ids.analytic_distribution,
backorder.analytic_distribution,
)

def _check_analytic_when_adding_new_line(self):
self.production.analytic_distribution = self.analytic_distribution
self.product_C = self.env["product.product"].create(
{
"name": "Product C",
"type": "product",
"categ_id": self.product_categ.id,
"standard_price": 50.0,
}
)
self.assertGreater(len(self.production.move_raw_ids), 0)
edit_production = Form(self.production)
with edit_production.move_raw_ids.new() as new_raw:
new_raw.product_id = self.product_C
production = edit_production.save()
for raw_line in production.move_raw_ids:
with self.subTest(
raw_move=raw_line.display_name,
raw_product=raw_line.product_id.display_name,
):
self.assertEqual(
raw_line.analytic_distribution,
self.analytic_distribution,
msg="When a new raw line is added to a draft production, "
"it should get the analytic distribution of the "
"production",
)

def test_analytic_added_to_new_lines_on_draft(self):
self._check_analytic_when_adding_new_line()

def test_analytic_added_to_new_lines_on_confirmed(self):
self.production.action_confirm()
self._check_analytic_when_adding_new_line()

0 comments on commit 88e2b33

Please sign in to comment.