https://aporaclepayables.blogspot.com/2018/11/capturing-withholding-tax-deduction.html
select
INVOICE_ID ,
null INVOICE_DISTRIBUTION_ID ,
null TAX_NAME,
sum(AMT) AMT,
null GL_COSTCNTR,
null GL_LOC,
null GL_LOB,
null GL_PROD,
sum(round(PROJ_TAX)) PROJ_TAX ,
sum(round(PROJ_TAX)) TAX_SUM ,
sum(CNT) EXCNT ,
RATE_PROJ TAX_RATE,
SECTION
FROM (
------------------------------------------------------------------
select
INVOICE_ID ,
null INVOICE_DISTRIBUTION_ID ,
TAX_NAME,
RATE_PROJ,
sum(AMT) AMT,
null GL_COSTCNTR,
null GL_LOC,
null GL_LOB,
null GL_PROD,
sum(PROJ_TAX) PROJ_TAX,
sum(CNT) CNT,
null TAX_RATE,
SECTION
from
(
----------------------------------------------------------------------------
SELECT
g.GROUP_ID
, AIDA.INVOICE_ID
, null Invoice_Distribution_Id
, g.NAME TAX_NAME
, ( AIDA.AMOUNT) AMT
/* ,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016491 --DOM COST
CENTRE VS'1014611'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT3
) GL_COSTCNTR
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016490 --DOM LOCATION
VS'1014608'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT2
) GL_LOC
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016487 --DOM COMPANY
VALUE SET '1014609'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT1
) GL_LOB
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016494 --DOM PRODUCT
VS'1014610'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT6
) GL_PROD*/
, CASE WHEN ATC.AWT_PERIOD_TYPE='AWT - Property' THEN
((least(AIDA.AMOUNT,nVL(ATR.END_AMOUNT,AIDA.AMOUNT))-ATR.START_AMOUNT)*ATR.TAX_RATE/100)
ELSE
case when nvl(EXC.CNT,0) >= 1 then 0
when nvl(EXC.CNT,0) >= 0 then
((AIDA.AMOUNT*ATR.TAX_RATE/100) )
else 0
end
END PROJ_TAX
, nvl(EXC.CNT,0) CNT
,ATC.ATTRIBUTE4 TAX_RATE
,ATC.ATTRIBUTE1||'-'||ATC.ATTRIBUTE2||'-'||ATC.ATTRIBUTE3
SECTION
,ATR.TAX_RATE RATE_PROJ
FROM
AP_AWT_GROUPS g
,AP_INVOICE_DISTRIBUTIONS_ALL AIDA
,AP_AWT_GROUP_TAXES_ALL ATG
,AP_TAX_CODES_ALL ATC
,AP_AWT_TAX_RATES_ALL ATR
,GL_CODE_COMBINATIONS GCC
,(
select 1 CNT, x.tax_name
from AP_AWT_TAX_RATES_ALL x
where x.vendor_id= ( select aia.vendor_id from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
and /*'12-JUN-2016'*/ ( select trunc(aia.gl_date) from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*'15-DEC-2017'*/ BETWEEN trunc(x.start_date) and trunc( x.end_date)
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
and x.org_id = /*865 */ ( select aia.org_id from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
) exc
WHERE
g.GROUP_ID = atg.GROUP_ID
and AIDA.PAY_AWT_GROUP_ID = ATG.GROUP_ID
AND AIDA.ORG_ID = ATG.ORG_ID
AND ATG.TAX_NAME = ATC.NAME
AND AIDA.ORG_ID = ATC.ORG_ID
AND ATC.NAME = ATR.TAX_NAME
AND AIDA.ORG_ID = ATR.ORG_ID
AND ATR.RATE_TYPE = 'STANDARD'
AND AIDA.INVOICE_ID = &P_INVOICE_ID --3450660 --
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
AND NVL(AIDA.REVERSAL_FLAG,'N') <> 'Y'
AND ATR.END_DATE IS NULL
AND AIDA.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
AND ATG.TAX_NAME = EXC.TAX_NAME(+)
-----------------------------------------------------------
)
GROUP BY INVOICE_ID ,TAX_NAME , RATE_PROJ,SECTION
-------------------------------------------------------------------
) Q1
GROUP BY
--INVOICE_ID,Invoice_Distribution_Id,TAX_NAME,AMT,GL_COSTCNTR,GL_LOC,GL_LOB,GL_PROD,PROJ_TAX,TAX_RATE,SECTION,cnt
INVOICE_ID ,
--TAX_NAME,
RATE_PROJ ,
SECTION
ORDER BY 1,6
select
INVOICE_ID ,
null INVOICE_DISTRIBUTION_ID ,
null TAX_NAME,
sum(AMT) AMT,
null GL_COSTCNTR,
null GL_LOC,
null GL_LOB,
null GL_PROD,
sum(round(PROJ_TAX)) PROJ_TAX ,
sum(round(PROJ_TAX)) TAX_SUM ,
sum(CNT) EXCNT ,
RATE_PROJ TAX_RATE,
SECTION
FROM (
------------------------------------------------------------------
select
INVOICE_ID ,
null INVOICE_DISTRIBUTION_ID ,
TAX_NAME,
RATE_PROJ,
sum(AMT) AMT,
null GL_COSTCNTR,
null GL_LOC,
null GL_LOB,
null GL_PROD,
sum(PROJ_TAX) PROJ_TAX,
sum(CNT) CNT,
null TAX_RATE,
SECTION
from
(
----------------------------------------------------------------------------
SELECT
g.GROUP_ID
, AIDA.INVOICE_ID
, null Invoice_Distribution_Id
, g.NAME TAX_NAME
, ( AIDA.AMOUNT) AMT
/* ,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016491 --DOM COST
CENTRE VS'1014611'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT3
) GL_COSTCNTR
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016490 --DOM LOCATION
VS'1014608'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT2
) GL_LOC
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016487 --DOM COMPANY
VALUE SET '1014609'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT1
) GL_LOB
,(SELECT FVT.DESCRIPTION
FROM APPLSYS.FND_FLEX_VALUES_TL FVT,
APPLSYS.FND_FLEX_VALUES FFV
WHERE FFV.FLEX_VALUE_SET_ID = 1016494 --DOM PRODUCT
VS'1014610'
AND FFV.FLEX_VALUE_ID = FVT.FLEX_VALUE_ID
AND FVT.FLEX_VALUE_MEANING = gcc.SEGMENT6
) GL_PROD*/
, CASE WHEN ATC.AWT_PERIOD_TYPE='AWT - Property' THEN
((least(AIDA.AMOUNT,nVL(ATR.END_AMOUNT,AIDA.AMOUNT))-ATR.START_AMOUNT)*ATR.TAX_RATE/100)
ELSE
case when nvl(EXC.CNT,0) >= 1 then 0
when nvl(EXC.CNT,0) >= 0 then
((AIDA.AMOUNT*ATR.TAX_RATE/100) )
else 0
end
END PROJ_TAX
, nvl(EXC.CNT,0) CNT
,ATC.ATTRIBUTE4 TAX_RATE
,ATC.ATTRIBUTE1||'-'||ATC.ATTRIBUTE2||'-'||ATC.ATTRIBUTE3
SECTION
,ATR.TAX_RATE RATE_PROJ
FROM
AP_AWT_GROUPS g
,AP_INVOICE_DISTRIBUTIONS_ALL AIDA
,AP_AWT_GROUP_TAXES_ALL ATG
,AP_TAX_CODES_ALL ATC
,AP_AWT_TAX_RATES_ALL ATR
,GL_CODE_COMBINATIONS GCC
,(
select 1 CNT, x.tax_name
from AP_AWT_TAX_RATES_ALL x
where x.vendor_id= ( select aia.vendor_id from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
and /*'12-JUN-2016'*/ ( select trunc(aia.gl_date) from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*'15-DEC-2017'*/ BETWEEN trunc(x.start_date) and trunc( x.end_date)
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
and x.org_id = /*865 */ ( select aia.org_id from
ap_invoices_all aia where aia.invoice_id = &P_INVOICE_ID )
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
) exc
WHERE
g.GROUP_ID = atg.GROUP_ID
and AIDA.PAY_AWT_GROUP_ID = ATG.GROUP_ID
AND AIDA.ORG_ID = ATG.ORG_ID
AND ATG.TAX_NAME = ATC.NAME
AND AIDA.ORG_ID = ATC.ORG_ID
AND ATC.NAME = ATR.TAX_NAME
AND AIDA.ORG_ID = ATR.ORG_ID
AND ATR.RATE_TYPE = 'STANDARD'
AND AIDA.INVOICE_ID = &P_INVOICE_ID --3450660 --
/*
VENDOR_ID 45
INVOICE_DATE 12/15/2016
ORG_ID 82
INVOICE_ID 11463
*/
AND NVL(AIDA.REVERSAL_FLAG,'N') <> 'Y'
AND ATR.END_DATE IS NULL
AND AIDA.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
AND ATG.TAX_NAME = EXC.TAX_NAME(+)
-----------------------------------------------------------
)
GROUP BY INVOICE_ID ,TAX_NAME , RATE_PROJ,SECTION
-------------------------------------------------------------------
) Q1
GROUP BY
--INVOICE_ID,Invoice_Distribution_Id,TAX_NAME,AMT,GL_COSTCNTR,GL_LOC,GL_LOB,GL_PROD,PROJ_TAX,TAX_RATE,SECTION,cnt
INVOICE_ID ,
--TAX_NAME,
RATE_PROJ ,
SECTION
ORDER BY 1,6