0% found this document useful (0 votes)
3 views33 pages

VW Hanaimpostos

The document contains a SQL statement that alters a view named 'VW_IMPOSTOS_HANA' to include various tax-related fields from invoice and purchase details. It selects data such as document numbers, tax IDs, values, and various tax bases and sums from multiple joined tables. The view is designed to aggregate and present tax information for both sales and purchase invoices.

Uploaded by

douglasmysalles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views33 pages

VW Hanaimpostos

The document contains a SQL statement that alters a view named 'VW_IMPOSTOS_HANA' to include various tax-related fields from invoice and purchase details. It selects data such as document numbers, tax IDs, values, and various tax bases and sums from multiple joined tables. The view is designed to aggregate and present tax information for both sales and purchase invoices.

Uploaded by

douglasmysalles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 33

ALTER VIEW "SBO_UA_BR_HISTORICO".

"VW_IMPOSTOS_HANA" ( "TIPO",
"DocNum",
"CardName",
"State",
"CNPJ",
"IE",
"CPF",
"Serial",
"CFOPCode",
"Usage",
"Mes",
"DocDate",
"Modelo",
"NfeValue",
"TotalExpns",
"Total do Documento Bruto",
"Total do Documento Liquido",
"Total do Documento",
"Total da Linha",
"Base ICMS",
"ICMS",
"Base DIFAL",
"DIFAL",
"VALOR ORIGEM",
"VALOR DESTINO",
"Base IPI",
"IPI",
"Base Pis",
"Pis",
"Base Cofins",
"Cofins",
"Base ICMS ST",
"ICMS ST",
"Base FCP",
"FCP",
"U_ChaveAcesso",
"DflWhs",
"Filial",
"Sequencia",
"POSCashN" ) AS ((((((((SELECT
'NFsaida' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"

ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -
2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
INV."DocNum",
INV."CardName",
INV."POSCashN",
INV."Serial",
INV."DocDate",
INV."TaxDate",
INV."U_ChaveAcesso",
INV."CFOPCode",
INV."Usage",
INV."NfmName",
INV."TaxId0",
INV."TaxId1",
INV."TaxId4",
INV."State",
INV."CANCELED",
INV."DocEntry",
INV."DflWhs",
INV."ObjType",
INV."SeqCode",
INV."NfeValue",
COALESCE( max(INV_T33."LineTotal"),
0 )+ COALESCE( max(INV_T44."LineTotal"),
0 ) TOTALINHA,
max(INV."TotalExpns") AS "TotalExpns",
max(INV."Max1099") AS "Max1099",
max(INV."DocTotal") AS "DocTotal" ,
INV."U_NAME"
FROM
"_SYS_BIC"."public.ua.tax/INVOICE_DETAIL" INV
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/INV_T33"
GROUP BY "CFOPCode",
"DocEntry" ) INV_T33 ON INV."DocEntry" = INV_T33."DocEntry"
AND INV."CFOPCode" =
INV_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/INV_T44"
GROUP BY "CFOPCode",
"DocEntry" ) INV_T44 ON INV."DocEntry" = INV_T44."DocEntry"
AND INV."CFOPCode" =
INV_T44."CFOPCode"
GROUP BY INV."DocNum",
INV."CardName",
INV."POSCashN",
INV."Serial",
INV."DocDate",
INV."TaxDate",
INV."U_ChaveAcesso",
INV."CFOPCode",
INV."Usage",
INV."NfmName",
INV."TaxId0",
INV."TaxId1",
INV."TaxId4",
INV."State",
INV."CANCELED",
INV."DocEntry",
INV."DflWhs",
INV."ObjType",
INV."SeqCode",
INV."NfeValue",
INV."POSCashN",
INV."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs"
= T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid"
= T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode"
= T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode"
-- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode"
-- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode"
-- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode"
--COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode"
-- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode"
-- IPI 16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode"
-- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/INV_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode"
--DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM
"_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN
T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N')
UNION ALL (SELECT
'NFentrada' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"

ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -
2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
PCH."DocNum",
PCH."CardName",
PCH."POSCashN",
PCH."Serial",
PCH."DocDate",
PCH."TaxDate",
PCH."U_ChaveAcesso",
PCH."CFOPCode",
PCH."Usage",
PCH."NfmName",
PCH."TaxId0",
PCH."TaxId1",
PCH."TaxId4",
PCH."State",
PCH."CANCELED",
PCH."DocEntry",
PCH."DflWhs",
PCH."ObjType",
PCH."SeqCode",
PCH."NfeValue",
COALESCE( max(PCH_T33."LineTotal"),
0 )+ COALESCE( max(PCH_T44."LineTotal"),
0 ) TOTALINHA,
max(PCH."TotalExpns") AS "TotalExpns",
max(PCH."Max1099") AS "Max1099",
max(PCH."DocTotal") AS "DocTotal" ,
PCH."U_NAME"
FROM
"_SYS_BIC"."public.ua.tax/PURCHASE_DETAIL" PCH
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/PCH_T33"
GROUP BY "CFOPCode",
"DocEntry" ) PCH_T33 ON PCH."DocEntry" = PCH_T33."DocEntry"
AND PCH."CFOPCode" =
PCH_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/PCH_T44"
GROUP BY "CFOPCode",
"DocEntry" ) PCH_T44 ON PCH."DocEntry" = PCH_T44."DocEntry"
AND PCH."CFOPCode" =
PCH_T44."CFOPCode"
GROUP BY PCH."DocNum",
PCH."CardName",
PCH."POSCashN",
PCH."Serial",
PCH."DocDate",
PCH."TaxDate",
PCH."U_ChaveAcesso",
PCH."CFOPCode",
PCH."Usage",
PCH."NfmName",
PCH."TaxId0",
PCH."TaxId1",
PCH."TaxId4",
PCH."State",
PCH."CANCELED",
PCH."DocEntry",
PCH."DflWhs",
PCH."ObjType",
PCH."SeqCode",
PCH."NfeValue",
PCH."U_NAME") T0
INNER JOIN OWHS T99 ON T0."DflWhs"
= T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid"
= T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode"
= T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode"
-- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode"
-- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode"
-- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode"
--COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode"
-- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode"
-- IPI 16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode"
-- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PCH_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode"
--DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM
"_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN
T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'Entrega' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
DLN."DocNum",
DLN."CardName",
DLN."POSCashN",
DLN."Serial",
DLN."DocDate",
DLN."TaxDate",
DLN."U_ChaveAcesso",
DLN."CFOPCode",
DLN."Usage",
DLN."NfmName",
DLN."TaxId0",
DLN."TaxId1",
DLN."TaxId4",
DLN."State",
DLN."CANCELED",
DLN."DocEntry",
DLN."DflWhs",
DLN."ObjType",
DLN."SeqCode",
DLN."NfeValue",
COALESCE( max(DLN_T33."LineTotal"),
0 )+ COALESCE( max(DLN_T44."LineTotal"),
0 ) TOTALINHA,
max(DLN."TotalExpns") AS "TotalExpns",
max(DLN."Max1099") AS "Max1099",
max(DLN."DocTotal") AS "DocTotal" ,
DLN."U_NAME"
FROM
"_SYS_BIC"."public.ua.tax/DELIVERY_DETAIL" DLN
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/DLN_T33"
GROUP BY "CFOPCode",
"DocEntry" ) DLN_T33 ON DLN."DocEntry" = DLN_T33."DocEntry"
AND DLN."CFOPCode" =
DLN_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/DLN_T44"
GROUP BY "CFOPCode",
"DocEntry" ) DLN_T44 ON DLN."DocEntry" = DLN_T44."DocEntry"
AND DLN."CFOPCode" =
DLN_T44."CFOPCode"
GROUP BY DLN."DocNum",
DLN."CardName",
DLN."POSCashN",
DLN."Serial",
DLN."DocDate",
DLN."TaxDate",
DLN."U_ChaveAcesso",
DLN."CFOPCode",
DLN."Usage",
DLN."NfmName",
DLN."TaxId0",
DLN."TaxId1",
DLN."TaxId4",
DLN."State",
DLN."CANCELED",
DLN."DocEntry",
DLN."DflWhs",
DLN."ObjType",
DLN."SeqCode",
DLN."NfeValue",
DLN."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" =
T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" =
T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" =
T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" --
ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS
19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF
21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS
10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI
16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS
DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/DLN_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --
DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM
"_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN
T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'RecMercadoria' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
PDN."DocNum",
PDN."CardName",
PDN."POSCashN",
PDN."Serial",
PDN."DocDate",
PDN."TaxDate",
PDN."U_ChaveAcesso",
PDN."CFOPCode",
PDN."Usage",
PDN."NfmName",
PDN."TaxId0",
PDN."TaxId1",
PDN."TaxId4",
PDN."State",
PDN."CANCELED",
PDN."DocEntry",
PDN."DflWhs",
PDN."ObjType",
PDN."SeqCode",
PDN."NfeValue",
COALESCE( max(PDN_T33."LineTotal"),
0 )+ COALESCE( max(PDN_T44."LineTotal"),
0 ) TOTALINHA,
max(PDN."TotalExpns") AS "TotalExpns",
max(PDN."Max1099") AS "Max1099",
max(PDN."DocTotal") AS "DocTotal" ,
PDN."U_NAME"
FROM
"_SYS_BIC"."public.ua.tax/PDELIVERY_DETAIL" PDN
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/PDN_T33"
GROUP BY "CFOPCode",
"DocEntry" ) PDN_T33 ON PDN."DocEntry" = PDN_T33."DocEntry"
AND PDN."CFOPCode" = PDN_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM
"_SYS_BIC"."public.ua.tax/PDN_T44"
GROUP BY "CFOPCode",
"DocEntry" ) PDN_T44 ON PDN."DocEntry" = PDN_T44."DocEntry"
AND PDN."CFOPCode" = PDN_T44."CFOPCode"
GROUP BY PDN."DocNum",
PDN."CardName",
PDN."POSCashN",
PDN."Serial",
PDN."DocDate",
PDN."TaxDate",
PDN."U_ChaveAcesso",
PDN."CFOPCode",
PDN."Usage",
PDN."NfmName",
PDN."TaxId0",
PDN."TaxId1",
PDN."TaxId4",
PDN."State",
PDN."CANCELED",
PDN."DocEntry",
PDN."DflWhs",
PDN."ObjType",
PDN."SeqCode",
PDN."NfeValue",
PDN."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" =
T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" =
T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" =
T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" -- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS
10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/PDN_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI
16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PDN_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS DIFAL
26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM
"_SYS_BIC"."public.ua.tax/PDN_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --DESTORIG
27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM
"_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN T0."SeqCode" = 33

THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'DevNFsaida' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
RIN."DocNum",
RIN."CardName",
RIN."POSCashN",
RIN."Serial",
RIN."DocDate",
RIN."TaxDate",
RIN."U_ChaveAcesso",
RIN."CFOPCode",
RIN."Usage",
RIN."NfmName",
RIN."TaxId0",
RIN."TaxId1",
RIN."TaxId4",
RIN."State",
RIN."CANCELED",
RIN."DocEntry",
RIN."DflWhs",
RIN."ObjType",
RIN."SeqCode",
RIN."NfeValue",
COALESCE( max(RIN_T33."LineTotal"),
0 )+ COALESCE( max(RIN_T44."LineTotal"),
0 ) TOTALINHA,
max(RIN."TotalExpns") AS "TotalExpns",
max(RIN."Max1099") AS "Max1099",
max(RIN."DocTotal") AS "DocTotal" ,
RIN."U_NAME"
FROM
"_SYS_BIC"."public.ua.tax/REVINVOICE_DETAIL" RIN
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RIN_T33"
GROUP BY "CFOPCode",
"DocEntry" ) RIN_T33 ON RIN."DocEntry" = RIN_T33."DocEntry"
AND RIN."CFOPCode" = RIN_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RIN_T44"
GROUP BY "CFOPCode",
"DocEntry" ) RIN_T44 ON RIN."DocEntry" = RIN_T44."DocEntry"
AND RIN."CFOPCode" = RIN_T44."CFOPCode"
GROUP BY RIN."DocNum",
RIN."CardName",
RIN."POSCashN",
RIN."Serial",
RIN."DocDate",
RIN."TaxDate",
RIN."U_ChaveAcesso",
RIN."CFOPCode",
RIN."Usage",
RIN."NfmName",
RIN."TaxId0",
RIN."TaxId1",
RIN."TaxId4",
RIN."State",
RIN."CANCELED",
RIN."DocEntry",
RIN."DflWhs",
RIN."ObjType",
RIN."SeqCode",
RIN."NfeValue",
RIN."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" = T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" = T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" = T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" -- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI 16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RIN_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM "_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'DevEntrega' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
RDN."DocNum",
RDN."CardName",
RDN."POSCashN",
RDN."Serial",
RDN."DocDate",
RDN."TaxDate",
RDN."U_ChaveAcesso",
RDN."CFOPCode",
RDN."Usage",
RDN."NfmName",
RDN."TaxId0",
RDN."TaxId1",
RDN."TaxId4",
RDN."State",
RDN."CANCELED",
RDN."DocEntry",
RDN."DflWhs",
RDN."ObjType",
RDN."SeqCode",
RDN."NfeValue",
COALESCE( max(RDN_T33."LineTotal"),
0 )+ COALESCE( max(RDN_T44."LineTotal"),
0 ) TOTALINHA,
max(RDN."TotalExpns") AS "TotalExpns",
max(RDN."Max1099") AS "Max1099",
max(RDN."DocTotal") AS "DocTotal" ,
RDN."U_NAME"
FROM "_SYS_BIC"."public.ua.tax/REVDELIVERY_DETAIL"
RDN
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RDN_T33"
GROUP BY "CFOPCode",
"DocEntry" ) RDN_T33 ON RDN."DocEntry" = RDN_T33."DocEntry"
AND RDN."CFOPCode" = RDN_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RDN_T44"
GROUP BY "CFOPCode",
"DocEntry" ) RDN_T44 ON RDN."DocEntry" = RDN_T44."DocEntry"
AND RDN."CFOPCode" = RDN_T44."CFOPCode"
GROUP BY RDN."DocNum",
RDN."CardName",
RDN."POSCashN",
RDN."Serial",
RDN."DocDate",
RDN."TaxDate",
RDN."U_ChaveAcesso",
RDN."CFOPCode",
RDN."Usage",
RDN."NfmName",
RDN."TaxId0",
RDN."TaxId1",
RDN."TaxId4",
RDN."State",
RDN."CANCELED",
RDN."DocEntry",
RDN."DflWhs",
RDN."ObjType",
RDN."SeqCode",
RDN."NfeValue",
RDN."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" = T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" = T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" = T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" -- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI 16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RDN_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM "_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'DevNFentrada' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
RPC."DocNum",
RPC."CardName",
RPC."POSCashN",
RPC."Serial",
RPC."DocDate",
RPC."TaxDate",
RPC."U_ChaveAcesso",
RPC."CFOPCode",
RPC."Usage",
RPC."NfmName",
RPC."TaxId0",
RPC."TaxId1",
RPC."TaxId4",
RPC."State",
RPC."CANCELED",
RPC."DocEntry",
RPC."DflWhs",
RPC."ObjType",
RPC."SeqCode",
RPC."NfeValue",
COALESCE( max(RPC_T33."LineTotal"),
0 )+ COALESCE( max(RPC_T44."LineTotal"),
0 ) TOTALINHA,
max(RPC."TotalExpns") AS "TotalExpns",
max(RPC."Max1099") AS "Max1099",
max(RPC."DocTotal") AS "DocTotal" ,
RPC."U_NAME"
FROM "_SYS_BIC"."public.ua.tax/REVPURCHASE_DETAIL" RPC
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RPC_T33"
GROUP BY "CFOPCode",
"DocEntry" ) RPC_T33 ON RPC."DocEntry" = RPC_T33."DocEntry"
AND RPC."CFOPCode" = RPC_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RPC_T44"
GROUP BY "CFOPCode",
"DocEntry" ) RPC_T44 ON RPC."DocEntry" = RPC_T44."DocEntry"
AND RPC."CFOPCode" = RPC_T44."CFOPCode"
GROUP BY RPC."DocNum",
RPC."CardName",
RPC."POSCashN",
RPC."Serial",
RPC."DocDate",
RPC."TaxDate",
RPC."U_ChaveAcesso",
RPC."CFOPCode",
RPC."Usage",
RPC."NfmName",
RPC."TaxId0",
RPC."TaxId1",
RPC."TaxId4",
RPC."State",
RPC."CANCELED",
RPC."DocEntry",
RPC."DflWhs",
RPC."ObjType",
RPC."SeqCode",
RPC."NfeValue",
RPC."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" = T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" = T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" = T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" -- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI 16,17,18

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPC_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM "_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N'))
UNION ALL (SELECT
'DevRecMercadoria' AS Tipo,
T0."DocNum",
T0."CardName",
T0."State",
T0."TaxId0" AS "CNPJ",
T0."TaxId1" AS "IE",
T0."TaxId4" AS "CPF",
T0."Serial",
T0."CFOPCode",
T0."Usage",
T0."TaxDate" AS "Mes",
T0."DocDate",
T0."NfmName" AS "Modelo",
T0."NfeValue",
T0."TotalExpns",
T0."Max1099" AS "Total do Documento Bruto",
T0."DocTotal" AS "Total do Documento Liquido",
CASE WHEN IFNULL(T0."DocTotal",
0) = 0
THEN T0."Max1099" + T0."TotalExpns"
ELSE T0."DocTotal"
END AS "Total do Documento",
IFNULL(T0."TOTALINHA",
0) AS "Total da Linha",
T6."BaseSum" AS "Base ICMS",
T6."TaxSum" AS ICMS,
Case when IFNULL(T8."TaxSum",
0)> 0
then T6."BaseSum"
else 0
end as "Base DIFAL",
IFNULL(T8."TaxSum",
0)+ IFNULL(T88."TaxSum",
0) as "DIFAL",
IFNULL(T8."TaxSum",
0) as "VALOR ORIGEM",
IFNULL(T88."TaxSum",
0) AS "VALOR DESTINO",
T7."BaseSum" as "Base IPI",
T7."TaxSum" AS "IPI",
T4."BaseSum" as "Base Pis",
T4."TaxSum" as "Pis",
T5."BaseSum" as "Base Cofins",
T5."TaxSum" as "Cofins",
T11."BaseSum" as "Base ICMS ST",
T11."TaxSum" as "ICMS ST",
T22."BaseSum" as "Base FCP",
T22."TaxSum" as "FCP",
COALESCE( NFE."KeyNfe",
T0."U_ChaveAcesso" ) "U_ChaveAcesso",
T0."DflWhs",
T33."BPLName" "Filial",
CASE WHEN T0."SeqCode" = -1
THEN 'Manual' WHEN T0."SeqCode" = -2
THEN 'Externo'
ELSE T44."SeqName"
END "Sequencia" ,
T0."POSCashN"--,T0."U_NAME"

FROM ( SELECT
RPD."DocNum",
RPD."CardName",
RPD."POSCashN",
RPD."Serial",
RPD."DocDate",
RPD."TaxDate",
RPD."U_ChaveAcesso",
RPD."CFOPCode",
RPD."Usage",
RPD."NfmName",
RPD."TaxId0",
RPD."TaxId1",
RPD."TaxId4",
RPD."State",
RPD."CANCELED",
RPD."DocEntry",
RPD."DflWhs",
RPD."ObjType",
RPD."SeqCode",
RPD."NfeValue",
COALESCE( max(RPD_T33."LineTotal"),
0 )+ COALESCE( max(RPD_T44."LineTotal"),
0 ) TOTALINHA,
max(RPD."TotalExpns") AS "TotalExpns",
max(RPD."Max1099") AS "Max1099",
max(RPD."DocTotal") AS "DocTotal" ,
RPD."U_NAME"
FROM "_SYS_BIC"."public.ua.tax/REVPDELIVERY_DETAIL" RPD
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RPD_T33"
GROUP BY "CFOPCode",
"DocEntry" ) RPD_T33 ON RPD."DocEntry" = RPD_T33."DocEntry"
AND RPD."CFOPCode" = RPD_T33."CFOPCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("LineTotal") AS "LineTotal"
FROM "_SYS_BIC"."public.ua.tax/RPD_T44"
GROUP BY "CFOPCode",
"DocEntry" ) RPD_T44 ON RPD."DocEntry" = RPD_T44."DocEntry"
AND RPD."CFOPCode" = RPD_T44."CFOPCode"
GROUP BY RPD."DocNum",
RPD."CardName",
RPD."POSCashN",
RPD."Serial",
RPD."DocDate",
RPD."TaxDate",
RPD."U_ChaveAcesso",
RPD."CFOPCode",
RPD."Usage",
RPD."NfmName",
RPD."TaxId0",
RPD."TaxId1",
RPD."TaxId4",
RPD."State",
RPD."CANCELED",
RPD."DocEntry",
RPD."DflWhs",
RPD."ObjType",
RPD."SeqCode",
RPD."NfeValue",
RPD."U_NAME" ) T0
INNER JOIN OWHS T99 ON T0."DflWhs" = T99."WhsCode"
INNER JOIN OBPL T33 ON T99."BPLid" = T33."BPLId"
LEFT JOIN NFN1 T44 ON T0."SeqCode" = T44."SeqCode"
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_ST"
GROUP BY "CFOPCode",
"DocEntry" ) T11 ON T11."DocEntry" = T0."DocEntry"
and t11."CFOPCode" = T0."CFOPCode" -- ICMS-ST

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_FCP"
GROUP BY "CFOPCode",
"DocEntry" ) T22 ON T22."DocEntry" = T0."DocEntry"
and t22."CFOPCode" = T0."CFOPCode" -- FCP

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_PIS"
GROUP BY "CFOPCode",
"DocEntry" ) T4 ON T4."DocEntry" = T0."DocEntry"
and t4."CFOPCode" = T0."CFOPCode" -- PIS 19,20

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_COF"
GROUP BY "CFOPCode",
"DocEntry" ) T5 ON T5."DocEntry" = T0."DocEntry"
and t5."CFOPCode" = T0."CFOPCode" --COF 21,22

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_ICMS"
GROUP BY "CFOPCode",
"DocEntry" ) T6 ON T6."DocEntry" = T0."DocEntry"
and t6."CFOPCode" = T0."CFOPCode" -- ICMS 10,11,25

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_IPI"
GROUP BY "CFOPCode",
"DocEntry" ) T7 ON T7."DocEntry" = T0."DocEntry"
and t7."CFOPCode" = T0."CFOPCode" -- IPI 16,17,18
LEFT JOIN ( SELECT
"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_ICMSDIF"
GROUP BY "CFOPCode",
"DocEntry" ) T8 ON T8."DocEntry" = T0."DocEntry"
and t8."CFOPCode" = T0."CFOPCode" -- ICMS DIFAL 26

LEFT JOIN ( SELECT


"CFOPCode",
"DocEntry",
sum("BaseSum") AS "BaseSum",
sum("TaxSum") AS "TaxSum"
FROM "_SYS_BIC"."public.ua.tax/RPD_DESTORIG"
GROUP BY "CFOPCode",
"DocEntry" ) T88 ON T88."DocEntry" = T0."DocEntry"
and t88."CFOPCode" = T0."CFOPCode" --DESTORIG 27

LEFT JOIN ( SELECT


"SeqCode",
"KeyNfe",
max("DocType") AS "DocType",
max("DocEntry") AS "DocEntry"
FROM "_SYS_BIC"."public.ua.tax/INVOICEONE"
GROUP BY "SeqCode",
"KeyNfe" ) NFE ON NFE."DocEntry" = T0."DocEntry"
and NFE."DocType" = T0."ObjType"
and NFE."SeqCode" = CASE WHEN T0."SeqCode" = 33
THEN 35
ELSE T0."SeqCode"
END
WHERE T0."CANCELED" = 'N')) WITH READ ONLY

You might also like