viernes, 23 de marzo de 2018
Don't let select a paid invoice on another payment #2
-- SET search_path TO adempiere;
-- SELECT * FROM fnt_obtener_vpagos_factura(1000000);
-- SELECT * FROM fnt_obtener_vpagos_factura(1109368);
-- SELECT * FROM fnt_obtener_vpagos_factura(1008186);
-- SELECT * FROM fnt_obtener_vpagos_factura(1074626);
-- DROP FUNCTION fnt_obtener_vpagos_factura(p_invoice_id NUMERIC(10,0));
CREATE OR REPLACE FUNCTION fnt_obtener_vpagos_factura(p_invoice_id NUMERIC(10,0))
RETURNS VARCHAR
AS $$
BEGIN
RETURN
(
WITH cte_factura_pagos AS
(
SELECT
p.c_payment_id AS id_pago,
p.documentno AS num_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 list_horizontal(fp.num_pago)::VARCHAR 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)
), '')
);
END;
$$ LANGUAGE plpgsql;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario