Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rma_lot/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ msgstr "Línea de albarán de devolución"
msgid "Serial Number"
msgstr "Número de serie"

#. module: rma_lot
#. odoo-python
#: code:addons/rma_lot/models/rma.py:0
msgid "The serial number %(serial)s is not available"
msgstr "El número de serie %(serial)s no está disponible"

#. module: rma_lot
#: model:ir.model.fields,field_description:rma_lot.field_rma__product_tracking
#: model:ir.model.fields,field_description:rma_lot.field_rma_delivery_wizard__product_tracking
Expand Down
6 changes: 6 additions & 0 deletions rma_lot/i18n/rma_lot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ msgstr ""
msgid "Serial Number"
msgstr ""

#. module: rma_lot
#. odoo-python
#: code:addons/rma_lot/models/rma.py:0
msgid "The serial number %(serial)s is not available"
msgstr ""

#. module: rma_lot
#: model:ir.model.fields,field_description:rma_lot.field_rma__product_tracking
#: model:ir.model.fields,field_description:rma_lot.field_rma_delivery_wizard__product_tracking
Expand Down
52 changes: 51 additions & 1 deletion rma_lot/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import Command, api, fields, models
from odoo.tools import float_compare
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare, float_is_zero


class Rma(models.Model):
Expand Down Expand Up @@ -97,3 +98,52 @@ def _compute_product_id(self):
@api.depends("product_id")
def _compute_lot_id(self):
self.update({"lot_id": False})

def _get_lot_available_locations(self, lot, action):
"""This method will return the locations where you should search for
the lot stock to determine if there is any free_qty.
It is important to always search in RMA/WH because, even if it is a
replacement (a different product is returned), the product may be
in either RMA/WH or WH/Stock.
"""
self.ensure_one()
locations = self.env["stock.location"]
# It's important to note the warehouse for the lot, because it may not be
# the same as the one listed on the RMA if it has been moved
warehouse = lot.location_id.warehouse_id or self.warehouse_id
locations += warehouse.rma_loc_id
if action == "replace":
locations += warehouse.lot_stock_id
return locations

def _error_message_lot_available(self, action, qty, uom):
for item in self:
lot = self.env.context.get("rma_replace_lot_id") or item.lot_id
if not lot:
continue
product = lot.product_id
if product.tracking != "serial":
continue
free_qty_data = []
for location in item._get_lot_available_locations(lot, action):
product_ctx = product.with_context(location=location.id)
free_qty_item = product_ctx._compute_quantities_dict(
lot_id=lot.id, owner_id=False, package_id=False
)[product.id]["free_qty"]
free_qty_data.append(free_qty_item)
if all(
float_is_zero(f_qty, precision_rounding=uom.rounding)
for f_qty in free_qty_data
):
msg = self.env._("The serial number %(serial)s is not available") % {
"serial": lot.display_name,
}
raise UserError(msg)

def create_return(self, scheduled_date, qty=None, uom=None):
self._error_message_lot_available("return", qty, uom)
return super().create_return(scheduled_date, qty=qty, uom=uom)

def create_replace(self, scheduled_date, warehouse, product, qty, uom):
self._error_message_lot_available("replace", qty, uom)
return super().create_replace(scheduled_date, warehouse, product, qty, uom)
Loading
Loading