I'm trying to create a function based on a field that I have in sale.order.line called lot_id, if a product does not have information in the lot_id field, it should send a warning message to the user BEFORE creating the invoice, until now only I carry this:
import logging
from odoo import models, api, _
from odoo.exceptions import UserError
# Definir un logger para el módulo
_logger = logging.getLogger(__name__)
class SaleAdvancePaymentInv(models.TransientModel):
_inherit = 'sale.advance.payment.inv'
def create_invoices(self):
"""
Antes de crear la factura, verifica si hay productos sin pedimento (sin lot_id).
Si existen, muestra una advertencia con el mensaje "Vas a crear la factura sin pedimento. ¿Deseas continuar?".
"""
orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
# Agregar log para verificar los pedidos que estamos procesando
_logger.info("Procesando las órdenes de venta: %s", orders.ids)
for order in orders:
# Filtrar productos sin pedimento (lot_id vacío)
productos_sin_pedimento = order.order_line.filtered(lambda line: not line.lot_id)
# Log para mostrar los productos sin pedimento
_logger.info("Productos sin pedimento en la orden %s: %s", order.name, productos_sin_pedimento.ids)
if productos_sin_pedimento:
# Si hay productos sin pedimento, mostrar un warning
_logger.warning("¡Advertencia! La orden %s tiene productos sin pedimento.", order.name)
# Mostrar un mensaje de advertencia (sin bloquear el proceso)
return {
'warning': {
'title': _("Advertencia"),
'message': _("Vas a crear la factura sin pedimento. ¿Deseas continuar?"),
}
}
# Log para verificar si pasamos a la creación de la factura
_logger.info("Creando la factura para las órdenes: %s", orders.ids)
# Si todos los productos tienen pedimento (lot_id lleno), continuar con la creación de la factura
return super(SaleAdvancePaymentInv, self).create_invoices()
But I have not been able to display the message, I only want the user to be shown the message that has products that contain that empty field, but the user will be able to create the invoice for the order without problems...
Can you help me? I would appreciate it, greetings.