From 4aefd097edf4b67c52921b329742af9fff84d8ec Mon Sep 17 00:00:00 2001 From: Bhavesh Odedra Date: Wed, 19 Aug 2020 08:55:13 -0700 Subject: [PATCH 1/3] [IMP] reason code location set as a scrap location if set --- scrap_reason_code/models/stock_scrap.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scrap_reason_code/models/stock_scrap.py b/scrap_reason_code/models/stock_scrap.py index afa1dd2c8..9b3f90277 100644 --- a/scrap_reason_code/models/stock_scrap.py +++ b/scrap_reason_code/models/stock_scrap.py @@ -25,21 +25,23 @@ class StockScrap(models.Model): def write(self, vals): if "reason_code_id" in vals: - vals.update( - { - "scrap_location_id": self.env["scrap.reason.code"] - .browse(vals.get("reason_code_id")) - .location_id - } + location_id = ( + self.env["scrap.reason.code"] + .browse(vals.get("reason_code_id")) + .location_id ) + if location_id: + vals.update({"scrap_location_id": location_id}) return super(StockScrap, self).write(vals) @api.model def create(self, vals): if "reason_code_id" in vals: - vals["scrap_location_id"] = ( + location_id = ( self.env["scrap.reason.code"] .browse(vals.get("reason_code_id")) - .location_id.id + .location_id ) + if location_id: + vals["scrap_location_id"] = location_id.id return super(StockScrap, self).create(vals) From e95031209bef39eaa91baeec88f79b795f77b7ac Mon Sep 17 00:00:00 2001 From: Patrick Wilson <36892066+patrickrwilson@users.noreply.github.com> Date: Thu, 12 May 2022 14:53:11 -0400 Subject: [PATCH 2/3] [IMP] scrap_reason_code: Added Test Coverage --- .../tests/test_scrap_reason_code.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/scrap_reason_code/tests/test_scrap_reason_code.py b/scrap_reason_code/tests/test_scrap_reason_code.py index 80fcfd729..4f9579820 100644 --- a/scrap_reason_code/tests/test_scrap_reason_code.py +++ b/scrap_reason_code/tests/test_scrap_reason_code.py @@ -100,3 +100,67 @@ class StockScrap(TransactionCase): scrapped_move.quantity_done = 8 self.assertEqual(scrap.scrap_qty, 8, "Scrap quantity is not updated.") + + def test_scrap_reason_code_write(self): + """Scrap the product of a picking2. Then modify the + done linked stock move and ensure the scrap quantity is also + updated and verify scrap reason code + """ + self.env["stock.quant"]._update_available_quantity( + self.scrap_product, self.stock_location, 10 + ) + partner2 = self.env["res.partner"].create({"name": "BOdedra 2"}) + picking2 = self.env["stock.picking"].create( + { + "name": "A single picking with one move to scrap 2", + "location_id": self.stock_location.id, + "location_dest_id": self.customer_location.id, + "partner_id": partner2.id, + "picking_type_id": self.env.ref("stock.picking_type_out").id, + } + ) + move2 = self.env["stock.move"].create( + { + "name": "A move to confirm and scrap its product", + "location_id": self.stock_location.id, + "location_dest_id": self.customer_location.id, + "product_id": self.scrap_product.id, + "product_uom": self.uom_unit.id, + "product_uom_qty": 1.0, + "picking_id": picking2.id, + } + ) + move2._action_confirm() + + self.assertEqual(move2.state, "confirmed") + scrap2 = self.env["stock.scrap"].create( + { + "product_id": self.scrap_product.id, + "product_uom_id": self.scrap_product.uom_id.id, + "scrap_qty": 5, + "picking_id": picking2.id, + } + ) + scrap2.write( + { + "reason_code_id": self.reason_code.id, + } + ) + scrap2._onchange_reason_code_id() + scrap2.do_scrap() + self.assertEqual(len(picking2.move_lines), 2) + scrapped_move = picking2.move_lines.filtered(lambda m: m.state == "done") + self.assertTrue(scrapped_move, "No scrapped move created.") + self.assertEqual( + scrapped_move.scrap_ids.ids, [scrap2.id], "Wrong scrap linked to the move." + ) + self.assertEqual( + scrap2.scrap_qty, + 5, + "Scrap quantity has been modified and is not " "correct anymore.", + ) + move = scrap2.move_id + self.assertEqual(move.reason_code_id.id, self.reason_code.id) + + scrapped_move.quantity_done = 8 + self.assertEqual(scrap2.scrap_qty, 8, "Scrap quantity is not updated.") From 5628d0356e53783e357d5786597d9931b3a45eed Mon Sep 17 00:00:00 2001 From: Patrick Wilson <36892066+patrickrwilson@users.noreply.github.com> Date: Mon, 16 May 2022 14:22:21 -0400 Subject: [PATCH 3/3] [IMP] scrap_reason_code: Moved common code to own method --- scrap_reason_code/models/stock_scrap.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/scrap_reason_code/models/stock_scrap.py b/scrap_reason_code/models/stock_scrap.py index 9b3f90277..0764ae597 100644 --- a/scrap_reason_code/models/stock_scrap.py +++ b/scrap_reason_code/models/stock_scrap.py @@ -23,25 +23,21 @@ class StockScrap(models.Model): if self.reason_code_id.location_id: self.scrap_location_id = self.reason_code_id.location_id - def write(self, vals): + def _update_scrap_reason_code_location(self, vals): if "reason_code_id" in vals: location_id = ( self.env["scrap.reason.code"] .browse(vals.get("reason_code_id")) - .location_id + .location_id.id ) if location_id: vals.update({"scrap_location_id": location_id}) + + def write(self, vals): + self._update_scrap_reason_code_location(vals) return super(StockScrap, self).write(vals) @api.model def create(self, vals): - if "reason_code_id" in vals: - location_id = ( - self.env["scrap.reason.code"] - .browse(vals.get("reason_code_id")) - .location_id - ) - if location_id: - vals["scrap_location_id"] = location_id.id + self._update_scrap_reason_code_location(vals) return super(StockScrap, self).create(vals)