From b53dcc38cfc36fa5e71488ad7b43224905302013 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 16 Dec 2024 12:34:58 +0000 Subject: [PATCH] [IMP] product_expiry_simple: inherit stock.assign.serial wizard Add ability to set expiry date when using the assign serial feature on a stock move --- product_expiry_simple/__init__.py | 1 + product_expiry_simple/__manifest__.py | 2 ++ product_expiry_simple/models/__init__.py | 1 + product_expiry_simple/models/stock_move.py | 28 +++++++++++++++++++ .../models/stock_move_line.py | 17 ++++------- product_expiry_simple/views/stock_move.xml | 24 ++++++++++++++++ product_expiry_simple/wizards/__init__.py | 1 + .../wizards/stock_assign_serial.py | 22 +++++++++++++++ .../wizards/stock_assign_serial_view.xml | 25 +++++++++++++++++ 9 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 product_expiry_simple/models/stock_move.py create mode 100644 product_expiry_simple/views/stock_move.xml create mode 100644 product_expiry_simple/wizards/__init__.py create mode 100644 product_expiry_simple/wizards/stock_assign_serial.py create mode 100644 product_expiry_simple/wizards/stock_assign_serial_view.xml diff --git a/product_expiry_simple/__init__.py b/product_expiry_simple/__init__.py index 0650744f6bc6..aee8895e7a31 100644 --- a/product_expiry_simple/__init__.py +++ b/product_expiry_simple/__init__.py @@ -1 +1,2 @@ from . import models +from . import wizards diff --git a/product_expiry_simple/__manifest__.py b/product_expiry_simple/__manifest__.py index 9ad343a44fff..a594ced7ea13 100644 --- a/product_expiry_simple/__manifest__.py +++ b/product_expiry_simple/__manifest__.py @@ -21,8 +21,10 @@ "views/product_template.xml", "views/stock_lot.xml", "views/stock_quant.xml", + "views/stock_move.xml", "views/stock_move_line.xml", "views/stock_picking.xml", + "wizards/stock_assign_serial_view.xml", ], "installable": True, } diff --git a/product_expiry_simple/models/__init__.py b/product_expiry_simple/models/__init__.py index 44ad76c1e030..19be8c89d66d 100644 --- a/product_expiry_simple/models/__init__.py +++ b/product_expiry_simple/models/__init__.py @@ -2,4 +2,5 @@ from . import stock_lot from . import stock_quant from . import stock_move_line +from . import stock_move from . import stock_picking diff --git a/product_expiry_simple/models/stock_move.py b/product_expiry_simple/models/stock_move.py new file mode 100644 index 000000000000..7ad1b08b1fe3 --- /dev/null +++ b/product_expiry_simple/models/stock_move.py @@ -0,0 +1,28 @@ +# Copyright 2024 Akretion France (https://www.akretion.com/) +# @author: Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + # Add fields for the feature that generate serial numbers from first SN + Number of SN + serial_expiry_date = fields.Date(string="Expiry Date") + product_use_expiry_date = fields.Boolean(related="product_id.use_expiry_date") + + def _generate_serial_move_line_commands(self, lot_names, origin_move_line=None): + move_lines_commands = super()._generate_serial_move_line_commands( + lot_names, origin_move_line=origin_move_line + ) + if self.product_id.use_expiry_date and self.serial_expiry_date: + for move_line_command in move_lines_commands: + move_line_command[2]["expiry_date"] = self.serial_expiry_date + return move_lines_commands + + def action_show_details(self): + action = super().action_show_details() + # empty field 'serial_expiry_date' on each run (like next_serial) + self.serial_expiry_date = False + return action diff --git a/product_expiry_simple/models/stock_move_line.py b/product_expiry_simple/models/stock_move_line.py index f99c9e0d281d..b7f9a76c5f06 100644 --- a/product_expiry_simple/models/stock_move_line.py +++ b/product_expiry_simple/models/stock_move_line.py @@ -11,15 +11,8 @@ class StockMoveLine(models.Model): expiry_date = fields.Date() - # When you read the code of _create_and_assign_production_lot() - # you need the defects of that method: - # 1. it's not possible to inherit the creation of the lot - # 2. it creates one lot per company/product/lot_name - # On that second point, we can consider that, for a particular product, - # lot X will always have the same expiry_date - - def _assign_production_lot(self, lot): - res = super()._assign_production_lot(lot) - if self[0].expiry_date: - self.lot_id.write({"expiry_date": self[0].expiry_date}) - return res + def _get_value_production_lot(self): + vals = super()._get_value_production_lot() + if self.product_id.use_expiry_date: + vals["expiry_date"] = self.expiry_date + return vals diff --git a/product_expiry_simple/views/stock_move.xml b/product_expiry_simple/views/stock_move.xml new file mode 100644 index 000000000000..277ab0d1306c --- /dev/null +++ b/product_expiry_simple/views/stock_move.xml @@ -0,0 +1,24 @@ + + + + + + product_expiry_simple.stock.move.form + stock.move + + + + + + + + + + diff --git a/product_expiry_simple/wizards/__init__.py b/product_expiry_simple/wizards/__init__.py new file mode 100644 index 000000000000..ce837b94bf02 --- /dev/null +++ b/product_expiry_simple/wizards/__init__.py @@ -0,0 +1 @@ +from . import stock_assign_serial diff --git a/product_expiry_simple/wizards/stock_assign_serial.py b/product_expiry_simple/wizards/stock_assign_serial.py new file mode 100644 index 000000000000..ae0e362fbebd --- /dev/null +++ b/product_expiry_simple/wizards/stock_assign_serial.py @@ -0,0 +1,22 @@ +# Copyright 2024 Akretion France (https://www.akretion.com/) +# @author: Alexis de Lattre +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockAssignSerialNumbers(models.TransientModel): + _inherit = "stock.assign.serial" + + serial_expiry_date = fields.Date(string="Expiry Date") + product_use_expiry_date = fields.Boolean( + related="move_id.product_id.use_expiry_date" + ) + + def generate_serial_numbers(self): + self.ensure_one() + if self.product_use_expiry_date and self.serial_expiry_date: + self.move_id.serial_expiry_date = self.serial_expiry_date + else: + self.move_id.serial_expiry_date = False + return super().generate_serial_numbers() diff --git a/product_expiry_simple/wizards/stock_assign_serial_view.xml b/product_expiry_simple/wizards/stock_assign_serial_view.xml new file mode 100644 index 000000000000..0969c8f479ef --- /dev/null +++ b/product_expiry_simple/wizards/stock_assign_serial_view.xml @@ -0,0 +1,25 @@ + + + + + + + stock.assign.serial + + + + + + + + + + +