Skip to content

Commit

Permalink
[FIX] inspection in mrp
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiocorato committed Nov 21, 2024
1 parent 8665f93 commit 31d4645
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 11 deletions.
1 change: 1 addition & 0 deletions quality_control_stock_oca_validation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"category": "Products",
"depends": [
"purchase_stock",
"quality_control_mrp_oca",
"quality_control_stock_oca",
],
"data": [],
Expand Down
13 changes: 7 additions & 6 deletions quality_control_stock_oca_validation/models/qc_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def _make_inspection(self, object_ref, trigger_line):
picking_id = object_ref
if object_ref._name in ["stock.move", "stock.move.line"]:
picking_id = object_ref.picking_id
inspection_ids = self.sudo().search(
[
("product_id", "=", object_ref.product_id.id),
("picking_id", "=", picking_id.id),
]
)
domain = [
("product_id", "=", object_ref.product_id.id),
("picking_id", "=", picking_id.id),
]
if object_ref.production_id:
domain += [("production_id", "=", object_ref.production_id.id)]
inspection_ids = self.sudo().search(domain)
if inspection_ids:
for inspection in inspection_ids:
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setUp(self):
self.env = self.env(context=dict(self.env.context, tracking_disable=True))
self.user_model = self.env["res.users"].with_context(no_reset_password=True)
self.vendor = self.env.ref("base.res_partner_3")
self.product = self.env.ref("product.product_delivery_01")
self.product1 = self.env.ref("product.product_delivery_01")
self.product2 = self.env.ref("product.product_delivery_02")
self.picking_type_in = self.env.ref("stock.picking_type_in")
self.in_trigger = self.env["qc.trigger"].search(
Expand All @@ -31,18 +31,65 @@ def setUp(self):
with test_line.ql_values.new() as test_question:
test_question.name = "Is Not OK"
self.qc_test = qc_test_form.save()
self.inspection_model = self.env["qc.inspection"]
self.qc_trigger_model = self.env["qc.trigger"]
self.test = self.env.ref("quality_control_oca.qc_test_1")
self.trigger = self.env.ref("quality_control_mrp_oca.qc_trigger_mrp")
# Category
category_form = Form(self.env["product.category"])
category_form.name = "Test category"
self.category = category_form.save()
# Product
product_form = Form(self.env["product.template"])
product_form.name = "Test Product"
product_form.type = "product"
self.product = product_form.save()
# Materials
product_form = Form(self.env["product.product"])
product_form.name = "Part 1 Product"
product_form.type = "product"
self.mat1 = product_form.save()
product_form = Form(self.env["product.product"])
product_form.name = "Part 2 Product"
product_form.type = "product"
self.mat2 = product_form.save()
# Bom
bom_form = Form(self.env["mrp.bom"])
bom_form.product_tmpl_id = self.product
bom_form.product_qty = 1.0
bom_form.type = "normal"
with bom_form.bom_line_ids.new() as material_form:
material_form.product_id = self.mat1
material_form.product_qty = 1
with bom_form.bom_line_ids.new() as material_form:
material_form.product_id = self.mat2
material_form.product_qty = 1
self.bom = bom_form.save()
# Production
production_form = Form(self.env["mrp.production"])
production_form.product_id = self.product.product_variant_id
production_form.bom_id = self.bom
production_form.product_qty = 2.0
self.production1 = production_form.save()
self.production1.action_confirm()
self.production1.action_assign()
# Inspection
inspection_lines = self.inspection_model._prepare_inspection_lines(self.test)
self.inspection1 = self.inspection_model.create(
{"name": "Test Inspection", "inspection_lines": inspection_lines}
)

def _create_purchase_order(self, qty, qty1, ref):
purchase_form = Form(self.env["purchase.order"])
purchase_form.date_order = fields.Date.today()
purchase_form.partner_id = self.vendor
purchase_form.partner_ref = ref
with purchase_form.order_line.new() as purchase_line_form:
purchase_line_form.product_id = self.product
purchase_line_form.product_id = self.product1
purchase_line_form.product_qty = qty
purchase_line_form.product_uom = self.product.uom_po_id
purchase_line_form.price_unit = self.product.list_price
purchase_line_form.name = self.product.name
purchase_line_form.product_uom = self.product1.uom_po_id
purchase_line_form.price_unit = self.product1.list_price
purchase_line_form.name = self.product1.name
purchase_line_form.date_planned = fields.Date.today() + timedelta(days=20)
with purchase_form.order_line.new() as purchase_line_form:
purchase_line_form.product_id = self.product2
Expand Down Expand Up @@ -99,3 +146,96 @@ def test_00_purchase_order(self):
Form(self.env[res["res_model"]].with_context(res["context"])).save().process()
backorder_picking = purchase_order.picking_ids - picking
self.assertTrue(backorder_picking)

@staticmethod
def _auto_fill_consumed_qty(moves):
for move in moves:
move.quantity_done = move.product_uom_qty

def test_inspection_create_for_product(self):
self.product.product_variant_id.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.production1.qty_producing = self.production1.product_qty
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
self.production1.button_mark_done()
self.assertEqual(
self.production1.created_inspections,
1,
"Only one inspection must be created",
)

def test_inspection_create_for_template(self):
self.product.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.production1.qty_producing = self.production1.product_qty
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
self.production1.button_mark_done()
self.assertEqual(
self.production1.created_inspections,
1,
"Only one inspection must be created",
)

def test_inspection_create_for_category(self):
self.product.categ_id.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.production1.qty_producing = self.production1.product_qty
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
self.production1.button_mark_done()
self.assertEqual(
self.production1.created_inspections,
1,
"Only one inspection must be created",
)

def test_inspection_create_only_one(self):
self.product.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.product.categ_id.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.production1.qty_producing = self.production1.product_qty
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
self.production1.button_mark_done()
self.assertEqual(
self.production1.created_inspections,
1,
"Only one inspection must be created",
)

def test_inspection_with_partial_fabrication(self):
self.product.qc_triggers = [
(0, 0, {"trigger": self.trigger.id, "test": self.test.id})
]
self.production1.qty_producing = 1.0
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
action = self.production1.button_mark_done()
backorder_form = Form(
self.env["mrp.production.backorder"].with_context(**action["context"])
)
backorder_form.save().action_backorder()
self.assertEqual(self.production1.state, "progress")
self.assertEqual(
self.production1.created_inspections,
1,
"Only one inspection must be created.",
)
mo_backorder = self.production1.procurement_group_id.mrp_production_ids[-1]
self.assertEqual(mo_backorder.state, "progress")
mo_backorder.qty_producing = self.production1.product_qty
self._auto_fill_consumed_qty(self.production1.move_raw_ids)
mo_backorder.button_mark_done()
self.assertEqual(mo_backorder.state, "done")
self.assertEqual(
mo_backorder.created_inspections, 1, "There must be only 1 inspection."
)

def test_qc_inspection_mo(self):
self.inspection1.write(
{"object_id": "%s,%d" % (self.production1._name, self.production1.id)}
)
self.assertEqual(self.inspection1.production_id, self.production1)

0 comments on commit 31d4645

Please sign in to comment.