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
34 changes: 5 additions & 29 deletions stock_ux/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import _, api, fields, models
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare


class StockMove(models.Model):
Expand Down Expand Up @@ -53,35 +52,12 @@ def _compute_origin_description(self):

@api.constrains("quantity")
def _check_quantity(self):
precision = self.env["decimal.precision"].precision_get("Product Unit of Measure")
# Si tenemos este contexto es porque si o si viene de una compra
if "previous_product_qty" in self.env.context:
return super()._check_quantity()
if any(self.filtered(lambda x: x.scrapped)):
return super()._check_quantity()
moves = self.filtered(
lambda x: (
x.picking_id.picking_type_id.block_additional_quantity
and float_compare(x.product_uom_qty, x.quantity, precision_digits=precision) == -1
)
)
if not moves:
return super()._check_quantity()
# Si lo ejecuta el superusuario (scheduler), revertir el cambio y loguear
if self.env.is_superuser():
for move in moves:
# Revertir el cambio de quantity
move.quantity = move.product_uom_qty
move.picking_id.message_post(
body=_(
"Se intentó transferir una cantidad mayor a la demanda inicial en el movimiento %s durante la ejecución automática (scheduler). El sistema ignoró el cambio y mantuvo la cantidad original."
)
% move.display_name
)
"""Basic quantity validation. Main validation is done in picking.button_validate()."""
# Excepciones donde no validamos
if self.env.context.get("previous_product_qty") or self.filtered("scrapped"):
return super()._check_quantity()

# Comportamiento normal: raise si corresponde
raise ValidationError(_("You can not transfer more than the initial demand!"))
return super()._check_quantity()

def action_view_linked_record(self):
"""This function returns an action that display existing sales order
Expand Down
22 changes: 22 additions & 0 deletions stock_ux/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
##############################################################################
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError, UserError
from odoo.tools.float_utils import float_compare


class StockPicking(models.Model):
Expand Down Expand Up @@ -152,3 +153,24 @@ def write(self, vals):
)
)
return super().write(vals)

def button_validate(self):
"""Valida que no se transfiera más de la demanda inicial."""
for picking in self:
if picking.picking_type_id.block_additional_quantity:
precision = self.env["decimal.precision"].precision_get("Product Unit of Measure")
for move in picking.move_ids.filtered(lambda m: m.state not in ("draft", "cancel")):
if float_compare(move.quantity, move.product_uom_qty, precision_digits=precision) == 1:
raise UserError(
_(
"Cannot transfer more than initial demand!\n\n"
"Product: %(product)s\n"
"Initial Demand: %(demand)s\n"
"Attempted Transfer: %(quantity)s\n\n"
"Please update the source document (Purchase/Sales Order) to increase quantities.",
product=move.product_id.display_name,
demand=move.product_uom_qty,
quantity=move.quantity,
)
)
return super().button_validate()