viernes, 23 de marzo de 2018
Don't let select a paid invoice on another payment #1
-- SET search_path TO adempiere;
-- SELECT * FROM fnt_validar_factura_varios_pagos(1000000);
-- SELECT * FROM fnt_validar_factura_varios_pagos(1109368);
-- SELECT * FROM fnt_validar_factura_varios_pagos(1008186);
-- SELECT * FROM fnt_validar_factura_varios_pagos(1074626);
CREATE OR REPLACE FUNCTION fnt_validar_factura_varios_pagos(p_invoice_id NUMERIC(10,0))
RETURNS BOOLEAN
AS $$
DECLARE
v_resultado INT = 0;
BEGIN
v_resultado =
(
WITH cte_factura_pagos AS
(
SELECT
p.c_payment_id AS id_pago,
pa.c_paymentallocate_id AS id_abono,
pa.c_invoice_id AS id_factura,
pa.overunderamt AS monto_subpago
FROM c_payment p
JOIN c_paymentallocate pa
ON p.c_payment_id = pa.c_payment_id
WHERE p.ad_client_id = 1000000
AND p.docstatus IN ('CO', 'CL')
AND pa.c_invoice_id = p_invoice_id
)
SELECT COALESCE(
(
SELECT DISTINCT 1 FROM cte_factura_pagos fp
WHERE
((
SELECT
COUNT(cp.id_factura) + 1
FROM cte_factura_pagos AS cp
) > 1
AND (
SELECT
fr.monto_subpago
FROM cte_factura_pagos fr
WHERE fr.id_abono =
(
SELECT
MAX(r.id_abono)
FROM cte_factura_pagos AS r
)
) = 0)
), 0)
);
IF(v_resultado = 1)THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;
END;
$$ LANGUAGE plpgsql;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario