Esta consulta toma las facturas de provedores, las notas de credito y los pagos, el problema
es que por cada pago o nota de credito, me agrega una linea y al hacer la suma se del doctotal, el monto se duplica o triplica dependiendo de los pagos o notas de credito que le hicieron a esa factura.
me podrían asesorar de favor
gracias
SELECT DISTINCT T0."DocNum",T0."CardName",T0."DocTotal" as TotalF,(ifnull(T6."DocTotal",0)) as "TotalNC",
T4."DocNum" as "NumPago",cast(T0."DocDate" as date) as "FFactura",
cast(T0."DocDueDate" as date) as "Vencimiento",cast(T4."DocDate" as date) as "FPago",
(T0."DocTotal"-(ifnull(T6."DocTotal",0)))as "Diferencia Nota",T3."SumApplied" AS "Pagado",
T7."AcctName" as "NombreCta",T4."Canceled",T0."CANCELED",T0."DocStatus",T1."SeriesName",T0."PaidToDate"
From OPCH T0
Inner Join NNM1 T1 On T0."Series" = T1."Series"
Inner Join PCH1 T2 On T0."DocEntry" = T2."DocEntry"
Left Join VPM2 T3 On T3."DocEntry" = T0."DocEntry"
Left Join OVPM T4 On T4."DocNum" = T0."ReceiptNum"
Left Join RPC1 T5 On T2."DocEntry" = T5."BaseEntry"
Left Join ORPC T6 On T5."DocEntry" = T6."DocEntry"
left JOIN OACT T7 ON T4."TrsfrAcct" = T7."AcctCode" or
T4."CashAcct" = T7."AcctCode" or T4."CheckAcct" = T7."AcctCode"
WHERE
T0."CANCELED" = 'N'and ((T0."DocTotal"-(ifnull(T6."DocTotal",0)))>0)