BEGIN
select @Quantity = convert(varchar, cast([Cdad a Facturar] as decimal(18,2))) , @quantiyOpen = convert(varchar, cast([Cdad Pedida] as decimal(18,2)))
, @itemCode = ItemCode from (
select t0.DocNum as [Pedido], T1.Quantity as [Cdad Pedida], t1.ItemCode /*, t3.DocNum as [Factura]*/, sum(T2.Quantity) as [Cdad a Facturar]
from ORDR T0
inner join RDR1 T1 on T0.DocEntry = T1.DocEntry
inner join INV1 T2 on T0.DocEntry = t2.BaseEntry and t1.ItemCode = t2.ItemCode and T1.LineNum = t2.BaseLine and t2.BaseEntry = t1.DocEntry
inner join OINV T3 on T2.DocEntry = t3.DocEntry
and t0.DocEntry = (select top 1 BaseEntry from INV1 where DocEntry = @list_of_cols_val_tab_del)
and t3.CANCELED = 'N'
group by t0.DocNum, t1.ItemCode, t1.LineNum, T1.Quantity) Tabla
where [Cdad Pedida] < [Cdad a Facturar]
;
IF EXISTS
(select Pedido from (
select t0.DocNum as [Pedido], T1.Quantity as [Cdad Pedida] /*, t3.DocNum as [Factura]*/, sum(T2.Quantity) as [Cdad a Facturar]
from ORDR T0
inner join RDR1 T1 on T0.DocEntry = T1.DocEntry
inner join INV1 T2 on T0.DocEntry = t2.BaseEntry and t1.ItemCode = t2.ItemCode and T1.LineNum = t2.BaseLine and t2.BaseEntry = t1.DocEntry
inner join OINV T3 on T2.DocEntry = t3.DocEntry
and t0.DocEntry = (select top 1 BaseEntry from INV1 where DocEntry = @list_of_cols_val_tab_del)
and t3.CANCELED = 'N'
group by t0.DocNum, t1.ItemCode, t1.LineNum, T1.Quantity) Tabla
where [Cdad Pedida] < [Cdad a Facturar]
)
BEGIN
set @error = 1300001
set @error_message = 'No se puede facturar por una cantidad mayor a la del pedido, Cdad Pedida: ' + @quantiyOpen + ' - Cdad a facturar: ' + @Quantity + ' - Artículo: ' + @itemCode + '.'
END
END