Sunteți pe pagina 1din 71

IF object_id('GST_MD_ExportType','u') is not null

drop table GST_MD_ExportType


go
create table GST_MD_ExportType(
DutyStatus nvarchar(1),
Name nvarchar(10))
insert into GST_MD_ExportType values('Y', 'WPAY')
insert into GST_MD_ExportType values('N', 'WOPAY')

IF object_id('GST_MD_NoteType','u') is not null


drop table GST_MD_NoteType
go
create table GST_MD_NoteType(
ObjType integer,
GstTransType nvarchar(2),
NoteType nvarchar(10))
insert into GST_MD_NoteType values(13, 'GD', 'D')
insert into GST_MD_NoteType values(14, 'GA', 'C')
insert into GST_MD_NoteType values(18, 'GD', 'D')
insert into GST_MD_NoteType values(19, 'GA', 'C')
insert into GST_MD_NoteType values(19, '--', 'C')

IF object_id('GST_MD_PoS','u') is not null


drop table GST_MD_PoS
go
create table GST_MD_PoS(
StateGSTN nvarchar(2),
PoS nvarchar(40))
insert into GST_MD_PoS values('01', '01-Jammu & Kashmir')
insert into GST_MD_PoS values('02', '02-Himachal Pradesh')
insert into GST_MD_PoS values('03', '03-Punjab')
insert into GST_MD_PoS values('04', '04-Chandigarh')
insert into GST_MD_PoS values('05', '05-Uttarakhand')
insert into GST_MD_PoS values('06', '06-Haryana')
insert into GST_MD_PoS values('07', '07-Delhi')
insert into GST_MD_PoS values('08', '08-Rajasthan')
insert into GST_MD_PoS values('09', '09-Uttar Pradesh')
insert into GST_MD_PoS values('10', '10-Bihar')
insert into GST_MD_PoS values('11', '11-Sikkim')
insert into GST_MD_PoS values('12', '12-Arunachal Pradesh')
insert into GST_MD_PoS values('13', '13-Nagaland')
insert into GST_MD_PoS values('14', '14-Manipur')
insert into GST_MD_PoS values('15', '15-Mizoram')
insert into GST_MD_PoS values('16', '16-Tripura')
insert into GST_MD_PoS values('17', '17-Meghalaya')
insert into GST_MD_PoS values('18', '18-Assam')
insert into GST_MD_PoS values('19', '19-West Bengal')
insert into GST_MD_PoS values('20', '20-Jharkhand')
insert into GST_MD_PoS values('21', '21-Odisha')
insert into GST_MD_PoS values('22', '22-Chhattisgarh')
insert into GST_MD_PoS values('23', '23-Madhya Pradesh')
insert into GST_MD_PoS values('24', '24-Gujarat')
insert into GST_MD_PoS values('25', '25-Daman & Diu')
insert into GST_MD_PoS values('26', '26-Dadra & Nagar Haveli')
insert into GST_MD_PoS values('27', '27-Maharashtra')
insert into GST_MD_PoS values('29', '29-Karnataka')
insert into GST_MD_PoS values('30', '30-Goa')
insert into GST_MD_PoS values('31', '31-Lakshdweep')
insert into GST_MD_PoS values('32', '32-Kerala')
insert into GST_MD_PoS values('33', '33-Tamil Nadu')
insert into GST_MD_PoS values('34', '34-Pondicherry')
insert into GST_MD_PoS values('35', '35-Andaman & Nicobar Islands')
insert into GST_MD_PoS values('36', '36-Telengana')
insert into GST_MD_PoS values('37', '37-Andhra Pradesh')
insert into GST_MD_PoS values('97', '97-Other Territory')

IF object_id('GST_MD_InvoiceType','u') is not null


drop table GST_MD_InvoiceType
go
create table GST_MD_InvoiceType(
ImpOrExp nvarchar(1),
ExportType nvarchar(1),
DutyStatus nvarchar(1),
Name nvarchar(40))
insert into GST_MD_InvoiceType values('N', 'E', 'Y', 'Regular')
insert into GST_MD_InvoiceType values('N', 'E', 'N', 'Regular')
insert into GST_MD_InvoiceType values('N', 'S', 'Y', 'Regular')
insert into GST_MD_InvoiceType values('N', 'S', 'N', 'Regular')
insert into GST_MD_InvoiceType values('N', 'U', 'Y', 'Regular')
insert into GST_MD_InvoiceType values('N', 'U', 'N', 'Regular')
insert into GST_MD_InvoiceType values('N', 'D', 'Y', 'Regular')
insert into GST_MD_InvoiceType values('N', 'D', 'N', 'Regular')
insert into GST_MD_InvoiceType values('Y', 'E', 'Y', 'Regular')
insert into GST_MD_InvoiceType values('Y', 'E', 'N', 'Regular')
insert into GST_MD_InvoiceType values('Y', 'S', 'Y', 'SEZ supplies with payment')
insert into GST_MD_InvoiceType values('Y', 'U', 'Y', 'SEZ supplies with payment')
insert into GST_MD_InvoiceType values('Y', 'S', 'N', 'SEZ supplies without
payment')
insert into GST_MD_InvoiceType values('Y', 'U', 'N', 'SEZ supplies without
payment')
insert into GST_MD_InvoiceType values('Y', 'D', 'Y', 'Deemed Exp')
insert into GST_MD_InvoiceType values('Y', 'D', 'N', 'Deemed Exp')

IF object_id('GST_MD_ImportType','u') is not null


drop table GST_MD_ImportType
go
create table GST_MD_ImportType(
ImportType nvarchar(1),
Name nvarchar(40))
insert into GST_MD_ImportType values('E', 'Imports')
insert into GST_MD_ImportType values('S', 'Received from SEZ')
insert into GST_MD_ImportType values('U', 'Received from SEZ')
insert into GST_MD_ImportType values('D', 'Imports')

IF object_id('GST_MD_URType','u') is not null


drop table GST_MD_URType
go
create table GST_MD_URType(
GstType integer,
ImpOrExp nvarchar(1),
DutyStatus nvarchar(1),
Name nvarchar(20))

insert into GST_MD_URType values(0, 'N', 'Y', 'B2CL')


insert into GST_MD_URType values(0, 'N', 'N', 'B2CL')
insert into GST_MD_URType values(0, 'Y', 'Y', 'EXPWP')
insert into GST_MD_URType values(0, 'Y', 'N', 'EXPWOP')
IF object_id('GST_MD_MaterialType','u') is not null
drop table GST_MD_MaterialType
go
create table GST_MD_MaterialType(
MaterialType nvarchar(1),
ItemTaxType nvarchar(2),
NonDeductable nvarchar(1),
Name nvarchar(20))

insert into GST_MD_MaterialType values('1', 'GR', 'N', 'Inputs')


insert into GST_MD_MaterialType values('1', 'GN', 'N', 'Inputs')
insert into GST_MD_MaterialType values('1', 'GE', 'N', 'Inputs')
insert into GST_MD_MaterialType values('1', 'NE', 'N', 'Inputs')
insert into GST_MD_MaterialType values('1', 'NN', 'N', 'Inputs')
insert into GST_MD_MaterialType values('3', 'GR', 'N', 'Inputs')
insert into GST_MD_MaterialType values('3', 'GN', 'N', 'Inputs')
insert into GST_MD_MaterialType values('3', 'GE', 'N', 'Inputs')
insert into GST_MD_MaterialType values('3', 'NE', 'N', 'Inputs')
insert into GST_MD_MaterialType values('3', 'NN', 'N', 'Inputs')
insert into GST_MD_MaterialType values('2', 'GR', 'N', 'Capital goods')
insert into GST_MD_MaterialType values('2', 'GN', 'N', 'Capital goods')
insert into GST_MD_MaterialType values('2', 'GE', 'N', 'Capital goods')
insert into GST_MD_MaterialType values('2', 'NE', 'N', 'Capital goods')
insert into GST_MD_MaterialType values('2', 'NN', 'N', 'Capital goods')
insert into GST_MD_MaterialType values('1', 'S', 'N', 'Services')
insert into GST_MD_MaterialType values('2', 'S', 'N', 'Services')
insert into GST_MD_MaterialType values('3', 'S', 'N', 'Services')
insert into GST_MD_MaterialType values('F', 'F', 'N', 'Services')
insert into GST_MD_MaterialType values('X', 'X', 'Y', 'Ineligible')

IF object_id('GST_TAX','v') is not null


drop view GST_TAX
go

create view GST_TAX as


select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
t2.LineNum as LineNum,
case when t2.UomEntry = 0 then -1
else isnull(t2.UomEntry, -1)
end as UomEntry,
t2.unitMsr as UomName,
case when SrcArrType = 12 and t2.NoInvtryMv <> 'Y' and t5.DropShip
<> 'Y' then t2.Quantity
else 0
end as Quantity,
t2.ItemCode,
case when t2.ItmTaxType = 'NE' then 'NN'
else t2.ItmTaxType
end as ItemTaxType,
CONVERT(nvarchar, t6.MatType) as MaterialType,
t2.SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
case when ItmTaxType = 'GN' and
not exists(select 1 from INV1
where DocEntry = t2.DocEntry
and ItmTaxType <>
'GN')
then 'Y'
else 'N'
end as PureNilRateDoc,
-1 as HeaderFreightCode
from INV1 t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry
and t4.SrcArrType in (12, 13) and t4.SrcLineNum =
t2.LineNum
left join OWHS t5 on t2.WhsCode = t5.WhsCode
left join OITM t6 on t2.ItemCode = t6.ItemCode
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
t2.LineNum as LineNum,
case when t2.UomEntry = 0 then -1
else isnull(t2.UomEntry, -1)
end as UomEntry,
t2.unitMsr as UomName,
case when SrcArrType = 12 and t2.NoInvtryMv <> 'Y' and t5.DropShip
<> 'Y' then t2.Quantity
else 0
end as Quantity,
t2.ItemCode,
case when t2.ItmTaxType = 'NE' then 'NN'
else t2.ItmTaxType
end as ItemTaxType,
CONVERT(nvarchar, t6.MatType) as MaterialType,
t2.SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
case when ItmTaxType = 'GN' and
not exists(select 1 from RIN1
where DocEntry = t2.DocEntry
and ItmTaxType <>
'GN')
then 'Y'
else 'N'
end as PureNilRateDoc,
-1 as HeaderFreightCode
from RIN1 t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType in (12, 13)
and t4.SrcLineNum = t2.LineNum
left join OWHS t5 on t2.WhsCode = t5.WhsCode
left join OITM t6 on t2.ItemCode = t6.ItemCode
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
t2.LineNum as LineNum,
case when t2.UomEntry = 0 then -1
else isnull(t2.UomEntry, -1)
end as UomEntry,
t2.unitMsr as UomName,
case when SrcArrType = 12 and t2.NoInvtryMv <> 'Y' and t5.DropShip
<> 'Y' then t2.Quantity
else 0
end as Quantity,
t2.ItemCode,
case when t2.ItmTaxType = 'NE' then 'NN'
else t2.ItmTaxType
end as ItemTaxType,
CONVERT(nvarchar, t6.MatType) as MaterialType,
t2.SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
case when ItmTaxType = 'GN' and
not exists(select 1 from PCH1
where DocEntry = t2.DocEntry
and ItmTaxType <>
'GN')
then 'Y'
else 'N'
end as PureNilRateDoc,
-1 as HeaderFreightCode
from PCH1 t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType in (12, 13)
and t4.SrcLineNum = t2.LineNum
left join OWHS t5 on t2.WhsCode = t5.WhsCode
left join OITM t6 on t2.ItemCode = t6.ItemCode
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
t2.LineNum as LineNum,
case when t2.UomEntry = 0 then -1
else isnull(t2.UomEntry, -1)
end as UomEntry,
t2.unitMsr as UomName,
case when SrcArrType = 12 and t2.NoInvtryMv <> 'Y' and t5.DropShip
<> 'Y' then t2.Quantity
else 0
end as Quantity,
t2.ItemCode,
case when t2.ItmTaxType = 'NE' then 'NN'
else t2.ItmTaxType
end as ItemTaxType,
CONVERT(nvarchar, t6.MatType) as MaterialType,
t2.SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
case when ItmTaxType = 'GN' and
not exists(select 1 from RPC1
where DocEntry = t2.DocEntry
and ItmTaxType <>
'GN')
then 'Y'
else 'N'
end as PureNilRateDoc,
-1 as HeaderFreightCode
from RPC1 t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType in (12, 13)
and t4.SrcLineNum = t2.LineNum
left join OWHS t5 on t2.WhsCode = t5.WhsCode
left join OITM t6 on t2.ItemCode = t6.ItemCode
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
-t4.SrcLineNum as LineNum,
-1 as UomEntry,
'' as UomName,
0 as Quantity,
'Freight' as ItemCode,
'F' as ItemTaxType,
'F' as MaterialType,
0 as SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
'N' as PureNilRateDoc,
t5.ExpnsCode as HeaderFreightCode
from OINV t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType = 14
left join INV3 t5 on t2.DocEntry = t5.DocEntry and t4.SrcLineNum = t5.LineNum
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
-t4.SrcLineNum as LineNum,
-1 as UomEntry,
'' as UomName,
0 as Quantity,
'Freight' as ItemCode,
'F' as ItemTaxType,
'F' as MaterialType,
0 as SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
'N' as PureNilRateDoc,
t5.ExpnsCode as HeaderFreightCode
from ORIN t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType = 14
left join RIN3 t5 on t2.DocEntry = t5.DocEntry and t4.SrcLineNum = t5.LineNum
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
-t4.SrcLineNum as LineNum,
-1 as UomEntry,
'' as UomName,
0 as Quantity,
'Freight' as ItemCode,
'F' as ItemTaxType,
'F' as MaterialType,
0 as SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
'N' as PureNilRateDoc,
t5.ExpnsCode as HeaderFreightCode
from OPCH t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType = 14
left join PCH3 t5 on t2.DocEntry = t5.DocEntry and t4.SrcLineNum = t5.LineNum
union all
select t2.ObjType as ObjType,
t2.DocEntry as DocEntry,
t4.BaseObjTyp as BaseType,
-t4.SrcLineNum as LineNum,
-1 as UomEntry,
'' as UomName,
0 as Quantity,
'Freight' as ItemCode,
'F' as ItemTaxType,
'F' as MaterialType,
0 as SacEntry,
t4.TaxCode,
t4.StaCode,
t4.StaType,
case when t4.StaType = -130 then 0
else t4.VatPercent
end
as VatPercent,
t4.NdPercent,
t4.EqPercent,
t4.RvsChrgPrc,
case when t4.RvsChrgPrc > 0 then 'Y'
else 'N'
end as Reverse_Charge,
case when t4.NdPercent = 100 then 'Y'
else 'N'
end as NonDeductable,
t4.BaseSum,
t4.BaseSumSc,
t4.BaseSumFc,
t4.VatSum,
t4.VatSumSc,
t4.VatSumFc,
t4.DeductSum,
t4.DedctSumSC,
t4.DedctSumFC,
t4.EqSum,
t4.EqSumSC,
t4.EqSumFC,
t4.RvsChrgSum,
t4.RvsChrgSC,
t4.RvsChrgFC,
t4.SrcArrType,
t4.SrcGrpNum as FreightGroup,
case when t4.StaType = -120 then t4.VatSum
else 0
end
as IGST_Sum,
case when t4.StaType = -100 then t4.VatSum
else 0
end
as CGST_Sum,
case when t4.StaType in(-110, -150 )then t4.VatSum
else 0
end
as SGST_Sum,
case when t4.StaType = -130 then t4.VatSum
else 0
end
as Cess_Sum,
case when t4.StaType = -120 then t4.DeductSum
else 0
end
as IGST_Deduct_Sum,
case when t4.StaType = -100 then t4.DeductSum
else 0
end
as CGST_Deduct_Sum,
case when t4.StaType in(-110, -150 )then t4.DeductSum
else 0
end
as SGST_Deduct_Sum,
case when t4.StaType = -130 then t4.DeductSum
else 0
end
as Cess_Deduct_Sum,
case when t4.StaType in (-100, -110, -120, -130, -150) then 'Y'
else 'N'
end as IsGSTTax,
'N' as PureNilRateDoc,
t5.ExpnsCode as HeaderFreightCode
from ORPC t2
inner join OTAX t3 on t3.SrcObjType = t2.ObjType and t2.DocEntry =
t3.SrcObjAbs
inner join TAX1 t4 on t3.AbsEntry = t4.AbsEntry and t4.SrcArrType = 14
left join RPC3 t5 on t2.DocEntry = t5.DocEntry and t4.SrcLineNum = t5.LineNum
go

IF object_id('GST_ODOC_Result','v') is not null


drop view GST_ODOC_Result
go

create view GST_ODOC_Result as


select t0.ObjType,
t0.GstTranTyp as GstTransType,
isnull(t0.CardName, '') as CardName,
t0.DocType,
t0.DocDate,
isnull(t0.DocTotal, 0) - isnull(t0.VatSum, 0) + isnull(t0.DpmAmnt,
0) as TotalBefVat,
isnull(t0.DocTotal, 0) + isnull(t0.DpmAmnt, 0) + isnull(t0.DpmVat,
0) as DocTotal,
t0.DocEntry,
CONVERT(nvarchar, t0.DocNum) as DocNum,
t0.DutyStatus,
t0.CANCELED as Cancelled,
isnull(t0.EComerGSTN, '') as ECommerceGSTN,
isnull(t0.TaxInvNo, '') as TaxInvNo,
isnull(t0.FrmBpDate, '') as TaxInvDate,
isnull(t0.RevRefNo, '') as OrigRefNo,
isnull(t0.RevRefDate, '') as OrigRefDate,
isnull(t0.RevCreRefN, '') as OrigCreditRefNo,
isnull(t0.RevCreRefD, '') as OrigCreditRefDate,
isnull(t0.Comments, '') as Comments,
isnull(t1.BpGSTN, '') as BpGSTN,
isnull(t1.BpGSTType, 0) as BpGSTType,
isnull(t1.BpStateCod, '') as BpStateCode,
isnull(t1.BpStatGSTN, '') as BpStateGSTN,
isnull(t1.LocGSTType, 0) as LocGSTType,
isnull(t1.LocGSTN, '') as LocGSTN,
isnull(t1.LocStatCod, '') as LocStateCode,
isnull(t1.LocStaGSTN, '') as LocStateGSTN,
isnull(t1.ImpOrExp, 'N') as ImpOrExp,
--isnull(t1.ExportType, '') as ExportType,
-- isnull(t1.PortCode, '') as PortCode,
isnull(t1.ImpExpNo, '') as ImpExpNo,
isnull(t1.ImpExpDate, '') as ImpExpDate,
isnull(t1.OrigImpNo, '') as OrigImpExpNo,
isnull(t1.OrigImpDat, '') as OrigImpExpDate,
isnull(t2.GSTCode, '') as PoSGSTN,
t3.LineNum,
isnull(t3.Reverse_Charge, 'N') as Reverse_Charge,
isnull(t3.NonDeductable, 'N') as NonDeductable,
isnull(t3.VatPercentSum, 0) as VatPercentSum,
isnull(t3.BaseSum, 0) as BaseSum,
isnull(t3.VatSum, 0) as VatSum,
isnull(t3.DeductSum, 0) as DeductSum,
isnull(t3.ReverseChargeSum, 0) as ReverseChargeSum,
isnull(t3.IGSTSum, 0) as IGSTSum,
isnull(t3.CGSTSum, 0) as CGSTSum,
isnull(t3.SGSTSum, 0) as SGSTSum,
isnull(t3.CessSum, 0) as CessSum,
isnull(t3.IGSTDeductSum, 0) as IGSTDeductSum,
isnull(t3.CGSTDeductSum, 0) as CGSTDeductSum,
isnull(t3.SGSTDeductSum, 0) as SGSTDeductSum,
isnull(t3.CessDeductSum, 0) as CessDeductSum,
t3.BaseType,
t3.SrcArrType,
t3.FreightGroup,
t3.IsGSTTax,
isnull(t3.ItemTaxType, 'S') as ItemTaxType,
isnull(t3.MaterialType, '1') as MaterialType,
t3.UomEntry,
isnull(t3.UomName, '') as UomName,
t3.Quantity,
t3.ItemCode,
isnull(t3.SacEntry, 0) as SacEntry,
case when (isnull(t1.BpGSTN, '') <> isnull(t1.LocGSTN, '') and
isnull(t1.BpStateCod, '') <> isnull(t1.LocStatCod, '') and isnull(t1.BpStatGSTN,
'') <> isnull(t1.LocStaGSTN, '')) then 'N'
else 'Y'
end as IsIntraState,
case when isnull(t1.BpGSTType, 0) > 0 then 'Y'
else 'N'
end as BpRegistered,
t3.PureNilRateDoc,
t3.HeaderFreightCode
from OINV t0
inner join INV12 t1 on t0.DocEntry = t1.DocEntry
left join OCST t2 on t1.BpStateCod = t2.Code and t2.Country = 'IN'
inner join
(select ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType,
Reverse_Charge, UomEntry, UomName, Quantity, ItemCode,
PureNilRateDoc, SacEntry, HeaderFreightCode,
sum(VatPercent) as VatPercentSum, sum(VatSum) as VatSum,
sum(DeductSum) as DeductSum, sum(RvsChrgSum) as ReverseChargeSum, avg(BaseSum) as
BaseSum,
sum(IGST_sum) as IGSTSum, sum(CGST_Sum) as CGSTSum, sum(SGST_Sum) as
SGSTSum, sum(Cess_Sum) as CessSum,
sum(IGST_Deduct_Sum) as IGSTDeductSum, sum(CGST_Deduct_Sum) as
CGSTDeductSum, sum(SGST_Deduct_Sum) as SGSTDeductSum, sum(Cess_Deduct_Sum) as
CessDeductSum,
MaterialType, NonDeductable from GST_TAX
group by ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType, Reverse_Charge,
UomEntry, UomName, Quantity, ItemCode, PureNilRateDoc, SacEntry,
HeaderFreightCode, MaterialType, NonDeductable) t3 on t0.DocEntry = t3.DocEntry and
t0.ObjType = t3.ObjType
where t0.CANCELED <> 'C' and t0.CANCELED <> 'Y'
union all
select t0.ObjType,
t0.GstTranTyp as GstTransType,
isnull(t0.CardName, '') as CardName,
t0.DocType,
t0.DocDate,
isnull(t0.DocTotal, 0) - isnull(t0.VatSum, 0) + isnull(t0.DpmAmnt,
0) as TotalBefVat,
isnull(t0.DocTotal, 0) + isnull(t0.DpmAmnt, 0) + isnull(t0.DpmVat,
0) as DocTotal,
t0.DocEntry,
CONVERT(nvarchar, t0.DocNum) as DocNum,
t0.DutyStatus,
t0.CANCELED as Cancelled,
isnull(t0.EComerGSTN, '') as ECommerceGSTN,
isnull(t0.TaxInvNo, '') as TaxInvNo,
isnull(t0.FrmBpDate, '') as TaxInvDate,
isnull(t0.RevRefNo, '') as OrigRefNo,
isnull(t0.RevRefDate, '') as OrigRefDate,
isnull(t0.RevCreRefN, '') as OrigCreditRefNo,
isnull(t0.RevCreRefD, '') as OrigCreditRefDate,
isnull(t0.Comments, '') as Comments,
isnull(t1.BpGSTN, '') as BpGSTN,
isnull(t1.BpGSTType, 0) as BpGSTType,
isnull(t1.BpStateCod, '') as BpStateCode,
isnull(t1.BpStatGSTN, '') as BpStateGSTN,
isnull(t1.LocGSTType, 0) as LocGSTType,
isnull(t1.LocGSTN, '') as LocGSTN,
isnull(t1.LocStatCod, '') as LocStateCode,
isnull(t1.LocStaGSTN, '') as LocStateGSTN,
isnull(t1.ImpOrExp, 'N') as ImpOrExp,
-- isnull(t1.ExportType, '') as ExportType,
-- isnull(t1.PortCode, '') as PortCode,
isnull(t1.ImpExpNo, '') as ImpExpNo,
isnull(t1.ImpExpDate, '') as ImpExpDate,
isnull(t1.OrigImpNo, '') as OrigImpExpNo,
isnull(t1.OrigImpDat, '') as OrigImpExpDate,
isnull(t2.GSTCode, '') as PoSGSTN,
t3.LineNum,
isnull(t3.Reverse_Charge, 'N') as Reverse_Charge,
isnull(t3.NonDeductable, 'N') as NonDeductable,
isnull(t3.VatPercentSum, 0) as VatPercentSum,
isnull(t3.BaseSum, 0) as BaseSum,
isnull(t3.VatSum, 0) as VatSum,
isnull(t3.DeductSum, 0) as DeductSum,
isnull(t3.ReverseChargeSum, 0) as ReverseChargeSum,
isnull(t3.IGSTSum, 0) as IGSTSum,
isnull(t3.CGSTSum, 0) as CGSTSum,
isnull(t3.SGSTSum, 0) as SGSTSum,
isnull(t3.CessSum, 0) as CessSum,
isnull(t3.IGSTDeductSum, 0) as IGSTDeductSum,
isnull(t3.CGSTDeductSum, 0) as CGSTDeductSum,
isnull(t3.SGSTDeductSum, 0) as SGSTDeductSum,
isnull(t3.CessDeductSum, 0) as CessDeductSum,
t3.BaseType,
t3.SrcArrType,
t3.FreightGroup,
t3.IsGSTTax,
isnull(t3.ItemTaxType, 'S') as ItemTaxType,
isnull(t3.MaterialType, '1') as MaterialType,
t3.UomEntry,
isnull(t3.UomName, '') as UomName,
t3.Quantity,
t3.ItemCode,
isnull(t3.SacEntry, 0) as SacEntry,
case when (isnull(t1.BpGSTN, '') <> isnull(t1.LocGSTN, '') and
isnull(t1.BpStateCod, '') <> isnull(t1.LocStatCod, '') and isnull(t1.BpStatGSTN,
'') <> isnull(t1.LocStaGSTN, '')) then 'N'
else 'Y'
end as IsIntraState,
case when isnull(t1.BpGSTType, 0) > 0 then 'Y'
else 'N'
end as BpRegistered,
t3.PureNilRateDoc,
t3.HeaderFreightCode
from ORIN t0
inner join RIN12 t1 on t0.DocEntry = t1.DocEntry
left join OCST t2 on t1.BpStateCod = t2.Code and t2.Country = 'IN'
inner join
(select ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType,
Reverse_Charge, UomEntry, UomName, Quantity, ItemCode,
PureNilRateDoc, SacEntry, HeaderFreightCode,
sum(VatPercent) as VatPercentSum, sum(VatSum) as VatSum,
sum(DeductSum) as DeductSum, sum(RvsChrgSum) as ReverseChargeSum, avg(BaseSum) as
BaseSum,
sum(IGST_sum) as IGSTSum, sum(CGST_Sum) as CGSTSum, sum(SGST_Sum) as
SGSTSum, sum(Cess_Sum) as CessSum,
sum(IGST_Deduct_Sum) as IGSTDeductSum, sum(CGST_Deduct_Sum) as
CGSTDeductSum, sum(SGST_Deduct_Sum) as SGSTDeductSum, sum(Cess_Deduct_Sum) as
CessDeductSum,
MaterialType, NonDeductable from GST_TAX
group by ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType, Reverse_Charge,
UomEntry, UomName, Quantity, ItemCode, PureNilRateDoc, SacEntry,
HeaderFreightCode, MaterialType, NonDeductable) t3 on t0.DocEntry = t3.DocEntry and
t0.ObjType = t3.ObjType
where t0.CANCELED <> 'C' and t0.CANCELED <> 'Y'
union all
select t0.ObjType,
t0.GstTranTyp as GstTransType,
isnull(t0.CardName, '') as CardName,
t0.DocType,
t0.DocDate,
isnull(t0.DocTotal, 0) - isnull(t0.VatSum, 0) + isnull(t0.DpmAmnt,
0) as TotalBefVat,
isnull(t0.DocTotal, 0) + isnull(t0.DpmAmnt, 0) + isnull(t0.DpmVat,
0) as DocTotal,
t0.DocEntry,
isnull(CONVERT(nvarchar, t0.DocNum), '') as DocNum,
t0.DutyStatus,
t0.CANCELED as Cancelled,
isnull(t0.EComerGSTN, '') as ECommerceGSTN,
isnull(t0.TaxInvNo, '') as TaxInvNo,
isnull(t0.FrmBpDate, '') as TaxInvDate,
isnull(t0.RevRefNo, '') as OrigRefNo,
isnull(t0.RevRefDate, '') as OrigRefDate,
isnull(t0.RevCreRefN, '') as OrigCreditRefNo,
isnull(t0.RevCreRefD, '') as OrigCreditRefDate,
isnull(t0.Comments, '') as Comments,
isnull(t1.BpGSTN, '') as BpGSTN,
isnull(t1.BpGSTType, 0) as BpGSTType,
isnull(t1.BpStateCod, '') as BpStateCode,
isnull(t1.BpStatGSTN, '') as BpStateGSTN,
isnull(t1.LocGSTType, 0) as LocGSTType,
isnull(t1.LocGSTN, '') as LocGSTN,
isnull(t1.LocStatCod, '') as LocStateCode,
isnull(t1.LocStaGSTN, '') as LocStateGSTN,
isnull(t1.ImpOrExp, 'N') as ImpOrExp,
-- isnull(t1.ExportType, '') as ExportType,
-- isnull(t1.PortCode, '') as PortCode,
isnull(t1.ImpExpNo, '') as ImpExpNo,
isnull(t1.ImpExpDate, '') as ImpExpDate,
isnull(t1.OrigImpNo, '') as OrigImpExpNo,
isnull(t1.OrigImpDat, '') as OrigImpExpDate,
isnull(t2.GSTCode, '') as PoSGSTN,
t3.LineNum,
isnull(t3.Reverse_Charge, 'N') as Reverse_Charge,
isnull(t3.NonDeductable, 'N') as NonDeductable,
isnull(t3.VatPercentSum, 0) as VatPercentSum,
isnull(t3.BaseSum, 0) as BaseSum,
isnull(t3.VatSum, 0) as VatSum,
isnull(t3.DeductSum, 0) as DeductSum,
isnull(t3.ReverseChargeSum, 0) as ReverseChargeSum,
isnull(t3.IGSTSum, 0) as IGSTSum,
isnull(t3.CGSTSum, 0) as CGSTSum,
isnull(t3.SGSTSum, 0) as SGSTSum,
isnull(t3.CessSum, 0) as CessSum,
isnull(t3.IGSTDeductSum, 0) as IGSTDeductSum,
isnull(t3.CGSTDeductSum, 0) as CGSTDeductSum,
isnull(t3.SGSTDeductSum, 0) as SGSTDeductSum,
isnull(t3.CessDeductSum, 0) as CessDeductSum,
t3.BaseType,
t3.SrcArrType,
t3.FreightGroup,
t3.IsGSTTax,
isnull(t3.ItemTaxType, 'S') as ItemTaxType,
isnull(t3.MaterialType, '1') as MaterialType,
t3.UomEntry,
isnull(t3.UomName, '') as UomName,
t3.Quantity,
t3.ItemCode,
isnull(t3.SacEntry, 0) as SacEntry,
case when (isnull(t1.BpGSTN, '') <> isnull(t1.LocGSTN, '') and
isnull(t1.BpStateCod, '') <> isnull(t1.LocStatCod, '') and isnull(t1.BpStatGSTN,
'') <> isnull(t1.LocStaGSTN, '')) then 'N'
else 'Y'
end as IsIntraState,
case when isnull(t1.BpGSTType, 0) > 0 then 'Y'
else 'N'
end as BpRegistered,
t3.PureNilRateDoc,
t3.HeaderFreightCode
from OPCH t0
inner join PCH12 t1 on t0.DocEntry = t1.DocEntry
left join OCST t2 on t1.LocStatCod = t2.Code and t2.Country = 'IN'
inner join
(select ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType,
Reverse_Charge, UomEntry, UomName, Quantity, ItemCode,
PureNilRateDoc, SacEntry, HeaderFreightCode,
sum(VatPercent) as VatPercentSum, sum(VatSum) as VatSum,
sum(DeductSum) as DeductSum, sum(RvsChrgSum) as ReverseChargeSum, avg(BaseSum) as
BaseSum,
sum(IGST_sum) as IGSTSum, sum(CGST_Sum) as CGSTSum, sum(SGST_Sum) as
SGSTSum, sum(Cess_Sum) as CessSum,
sum(IGST_Deduct_Sum) as IGSTDeductSum, sum(CGST_Deduct_Sum) as
CGSTDeductSum, sum(SGST_Deduct_Sum) as SGSTDeductSum, sum(Cess_Deduct_Sum) as
CessDeductSum,
MaterialType, NonDeductable from GST_TAX
group by ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType, Reverse_Charge,
UomEntry, UomName, Quantity, ItemCode, PureNilRateDoc, SacEntry,
HeaderFreightCode, MaterialType, NonDeductable) t3 on t0.DocEntry = t3.DocEntry and
t0.ObjType = t3.ObjType
where t0.CANCELED <> 'C' and t0.CANCELED <> 'Y'
union all
select t0.ObjType,
t0.GstTranTyp as GstTransType,
isnull(t0.CardName, '') as CardName,
t0.DocType,
t0.DocDate,
isnull(t0.DocTotal, 0) - isnull(t0.VatSum, 0) + isnull(t0.DpmAmnt,
0) as TotalBefVat,
isnull(t0.DocTotal, 0) + isnull(t0.DpmAmnt, 0) + isnull(t0.DpmVat,
0) as DocTotal,
t0.DocEntry,
isnull(CONVERT(nvarchar, t0.DocNum), '') as DocNum,
t0.DutyStatus,
t0.CANCELED as Cancelled,
isnull(t0.EComerGSTN, '') as ECommerceGSTN,
isnull(t0.TaxInvNo, '') as TaxInvNo,
isnull(t0.FrmBpDate, '') as TaxInvDate,
isnull(t0.RevRefNo, '') as OrigRefNo,
isnull(t0.RevRefDate, '') as OrigRefDate,
isnull(t0.RevCreRefN, '') as OrigCreditRefNo,
isnull(t0.RevCreRefD, '') as OrigCreditRefDate,
isnull(t0.Comments, '') as Comments,
isnull(t1.BpGSTN, '') as BpGSTN,
isnull(t1.BpGSTType, 0) as BpGSTType,
isnull(t1.BpStateCod, '') as BpStateCode,
isnull(t1.BpStatGSTN, '') as BpStateGSTN,
isnull(t1.LocGSTType, 0) as LocGSTType,
isnull(t1.LocGSTN, '') as LocGSTN,
isnull(t1.LocStatCod, '') as LocStateCode,
isnull(t1.LocStaGSTN, '') as LocStateGSTN,
isnull(t1.ImpOrExp, 'N') as ImpOrExp,
-- isnull(t1.ExportType, '') as ExportType,
-- isnull(t1.PortCode, '') as PortCode,
isnull(t1.ImpExpNo, '') as ImpExpNo,
isnull(t1.ImpExpDate, '') as ImpExpDate,
isnull(t1.OrigImpNo, '') as OrigImpExpNo,
isnull(t1.OrigImpDat, '') as OrigImpExpDate,
isnull(t2.GSTCode, '') as PoSGSTN,
t3.LineNum,
isnull(t3.Reverse_Charge, 'N') as Reverse_Charge,
isnull(t3.NonDeductable, 'N') as NonDeductable,
isnull(t3.VatPercentSum, 0) as VatPercentSum,
isnull(t3.BaseSum, 0) as BaseSum,
isnull(t3.VatSum, 0) as VatSum,
isnull(t3.DeductSum, 0) as DeductSum,
isnull(t3.ReverseChargeSum, 0) as ReverseChargeSum,
isnull(t3.IGSTSum, 0) as IGSTSum,
isnull(t3.CGSTSum, 0) as CGSTSum,
isnull(t3.SGSTSum, 0) as SGSTSum,
isnull(t3.CessSum, 0) as CessSum,
isnull(t3.IGSTDeductSum, 0) as IGSTDeductSum,
isnull(t3.CGSTDeductSum, 0) as CGSTDeductSum,
isnull(t3.SGSTDeductSum, 0) as SGSTDeductSum,
isnull(t3.CessDeductSum, 0) as CessDeductSum,
t3.BaseType,
t3.SrcArrType,
t3.FreightGroup,
t3.IsGSTTax,
isnull(t3.ItemTaxType, 'S') as ItemTaxType,
isnull(t3.MaterialType, '1') as MaterialType,
t3.UomEntry,
isnull(t3.UomName, '') as UomName,
t3.Quantity,
t3.ItemCode,
isnull(t3.SacEntry, 0) as SacEntry,
case when (isnull(t1.BpGSTN, '') <> isnull(t1.LocGSTN, '') and
isnull(t1.BpStateCod, '') <> isnull(t1.LocStatCod, '') and isnull(t1.BpStatGSTN,
'') <> isnull(t1.LocStaGSTN, '')) then 'N'
else 'Y'
end as IsIntraState,
case when isnull(t1.BpGSTType, 0) > 0 then 'Y'
else 'N'
end as BpRegistered,
t3.PureNilRateDoc,
t3.HeaderFreightCode
from ORPC t0
inner join RPC12 t1 on t0.DocEntry = t1.DocEntry
left join OCST t2 on t1.LocStatCod = t2.Code and t2.Country = 'IN'
inner join
(select ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType,
Reverse_Charge, UomEntry, UomName, Quantity, ItemCode,
PureNilRateDoc, SacEntry, HeaderFreightCode,
sum(VatPercent) as VatPercentSum, sum(VatSum) as VatSum,
sum(DeductSum) as DeductSum, sum(RvsChrgSum) as ReverseChargeSum, avg(BaseSum) as
BaseSum,
sum(IGST_sum) as IGSTSum, sum(CGST_Sum) as CGSTSum, sum(SGST_Sum) as
SGSTSum, sum(Cess_Sum) as CessSum,
sum(IGST_Deduct_Sum) as IGSTDeductSum, sum(CGST_Deduct_Sum) as
CGSTDeductSum, sum(SGST_Deduct_Sum) as SGSTDeductSum, sum(Cess_Deduct_Sum) as
CessDeductSum,
MaterialType, NonDeductable from GST_TAX
group by ObjType, BaseType, DocEntry, LineNum, SrcArrType, FreightGroup,
IsGSTTax, ItemTaxType, Reverse_Charge,
UomEntry, UomName, Quantity, ItemCode, PureNilRateDoc, SacEntry,
HeaderFreightCode, MaterialType, NonDeductable) t3 on t0.DocEntry = t3.DocEntry and
t0.ObjType = t3.ObjType
where t0.CANCELED <> 'C' and t0.CANCELED <> 'Y'
go

IF object_id('GST_ODOC_Result_Ignore_RvsCharge','v') is not null


drop view GST_ODOC_Result_Ignore_RvsCharge
go

create view GST_ODOC_Result_Ignore_RvsCharge as


select ObjType,
GstTransType,
CardName,
DocType,
DocDate,
TotalBefVat,
DocTotal,
DocEntry,
DocNum,
DutyStatus,
Cancelled,
ECommerceGSTN,
TaxInvNo,
TaxInvDate,
OrigRefNo,
OrigRefDate,
OrigCreditRefNo,
OrigCreditRefDate,
Comments,
BpGSTN,
BpGSTType,
BpStateCode,
BpStateGSTN,
LocGSTType,
LocGSTN,
LocStateCode,
LocStateGSTN,
ImpOrExp,
-- ExportType,
-- PortCode,
ImpExpNo,
ImpExpDate,
OrigImpExpNo,
OrigImpExpDate,
PoSGSTN,
LineNum,
NonDeductable,
sum(VatPercentSum) as VatPercentSum,
BaseSum,
sum(VatSum) as VatSum,
sum(DeductSum) as DeductSum,
sum(IGSTSum) as IGSTSum,
sum(CGSTSum) as CGSTSum,
sum(SGSTSum) as SGSTSum,
sum(CessSum) as CessSum,
sum(IGSTDeductSum) as IGSTDeductSum,
sum(CGSTDeductSum) as CGSTDeductSum,
sum(SGSTDeductSum) as SGSTDeductSum,
sum(CessDeductSum) as CessDeductSum,
BaseType,
SrcArrType,
FreightGroup,
IsGSTTax,
ItemTaxType,
MaterialType,
UomEntry,
UomName,
Quantity,
ItemCode,
SacEntry,
IsIntraState,
BpRegistered,
PureNilRateDoc,
HeaderFreightCode
from GST_ODOC_Result
group by ObjType,
GstTransType,
CardName,
DocType,
DocDate,
TotalBefVat,
DocTotal,
DocEntry,
DocNum,
DutyStatus,
Cancelled,
ECommerceGSTN,
TaxInvNo,
TaxInvDate,
OrigRefNo,
OrigRefDate,
OrigCreditRefNo,
OrigCreditRefDate,
Comments,
BpGSTN,
BpGSTType,
BpStateCode,
BpStateGSTN,
LocGSTType,
LocGSTN,
LocStateCode,
LocStateGSTN,
ImpOrExp,
-- ExportType,
-- PortCode,
ImpExpNo,
ImpExpDate,
OrigImpExpNo,
OrigImpExpDate,
PoSGSTN,
LineNum,
NonDeductable,
BaseSum,
BaseType,
SrcArrType,
FreightGroup,
IsGSTTax,
ItemTaxType,
MaterialType,
UomEntry,
UomName,
Quantity,
ItemCode,
SacEntry,
IsIntraState,
BpRegistered,
PureNilRateDoc,
HeaderFreightCode
go

IF object_id('GST_ODOC_Result_GST_TAX_ONLY','v') is not null


drop view GST_ODOC_Result_GST_TAX_ONLY
go

create view GST_ODOC_Result_GST_TAX_ONLY as


select * from GST_ODOC_Result_Ignore_RvsCharge
where IsGSTTax = 'Y'

go

IF object_id('GST_TAX_PAYMENT','v') is not null


drop view GST_TAX_PAYMENT
go
create view GST_TAX_PAYMENT as
select t1.DocEntry as PaymentEntry,
t0.InvType as ObjType,
t0.DocEntry,
sum(case when t4.staType = -130 then 0
else t4.VatPercent
end) as VatPercent,
avg(t4.BaseSum) * t0.SumApplied / t2.DocTotal as PaidNet,
sum(case when t4.staType = -130 then t4.VatSum - t4.RvsChrgSum
else 0 end) * t0.SumApplied / t2.DocTotal as PaidCess,
t2.DocTotal,
t0.SumApplied / t2.DocTotal as Ratio,
year(t1.DocDate) as PaymentYear,
month(t1.DocDate) as PaymentMonth,
t2.Posted as IsDpmInv,
t4.SrcLineNum,
'AR' as "Direction"
from RCT2 t0
inner join ORCT t1 on t0.DocNum = t1.DocEntry
inner join ODPI t2 on t0.DocEntry = t2.DocEntry and t2.Posted = 'Y'
inner join OTAX t3 on t3.SrcObjType = 203 and t3.SrcObjAbs = t0.DocEntry
inner join TAX1 t4 on t4.AbsEntry = t3.AbsEntry
where t0.InvType = 203
and t1.Canceled = 'N'
and t4.staType in (-100, -110, -120, -130, -150)
group by t1.DocEntry, t0.InvType, t0.DocEntry, t0.SumApplied, t4.SrcLineNum,
t4.TaxCode, t4.BaseObjTyp, t4.BaseAbs, t4.BaseLinNum, t2.DocTotal, t1.DocDate,
t2.Posted
union all
select t1.DocEntry as PaymentEntry,
t0.InvType as ObjType,
t0.DocEntry,
sum(case when t4.staType = -130 then 0
else t4.VatPercent
end) as VatPercent,
avg(t6.BaseSum) * t0.SumApplied / t2.DocTotal as PaidNet,
sum(case when t4.staType = -130 then t4.VatSum - t4.RvsChrgSum
else 0 end) as PaidCess,
t2.DocTotal,
t0.SumApplied / t2.DocTotal as Ratio,
year(t1.DocDate) as PaymentYear,
month(t1.DocDate) as PaymentMonth,
t2.Posted as IsDpmInv,
t4.SrcLineNum,
'AR' as "Direction"
from RCT2 t0
inner join ORCT t1 on t0.DocNum = t1.DocEntry
inner join ODPI t2 on t0.DocEntry = t2.DocEntry and t2.Posted = 'N'
inner join OTAX t3 on t3.SrcObjType = 24 and t3.SrcObjAbs = t1.DocEntry
inner join TAX1 t4 on t4.AbsEntry = t3.AbsEntry and t4.BaseArrTyp = 12 and
t4.BaseObjTyp = 203 and t4.BaseAbs = t0.DocEntry
inner join OUTX t5 on t5.SrcObjType = 203 and t5.SrcObjAbs = t4.BaseAbs
inner join UTX1 t6 on t5.AbsEntry = t6.AbsEntry and t6.SrcArrType = 12 and
t6.SrcLineNum = t4.BaseLinNum and t4.StaCode = t6.StaCode and t4.StaIndex =
t6.StaIndex
where t0.InvType = 203
and t1.Canceled = 'N'
and t4.staType in (-100, -110, -120, -130, -150)
group by t1.DocEntry, t0.InvType, t0.DocEntry, t0.SumApplied, t4.SrcLineNum,
t4.BaseObjTyp, t4.BaseAbs, t4.BaseLinNum, t4.TaxCode, t2.DocTotal, t1.DocDate,
t2.Posted
union all
select t1.DocEntry as PaymentEntry,
t0.InvType as ObjType,
t0.DocEntry,
sum(case when t4.staType = -130 then 0
else t4.VatPercent
end) as VatPercent,
avg(t4.BaseSum) * t0.SumApplied / t2.DocTotal as PaidNet,
sum(case when t4.staType = -130 then t4.RvsChrgSum
else 0 end) * t0.SumApplied / t2.DocTotal as PaidCess,
t2.DocTotal,
t0.SumApplied / t2.DocTotal as Ratio,
year(t1.DocDate) as PaymentYear,
month(t1.DocDate) as PaymentMonth,
t2.Posted as IsDpmInv,
t4.SrcLineNum,
'AP' as "Direction"
from VPM2 t0
inner join OVPM t1 on t0.DocNum = t1.DocEntry
inner join ODPO t2 on t0.DocEntry = t2.DocEntry and t2.Posted = 'Y'
inner join OTAX t3 on t3.SrcObjType = 204 and t3.SrcObjAbs = t0.DocEntry
inner join TAX1 t4 on t4.AbsEntry = t3.AbsEntry
where t0.InvType = 204
and t1.Canceled = 'N'
and t4.staType in (-100, -110, -120, -130, -150)
and t4.RvsChrgPrc > 0
group by t1.DocEntry, t0.InvType, t0.DocEntry, t0.SumApplied, t4.SrcLineNum,
t4.TaxCode, t4.BaseObjTyp, t4.BaseAbs, t4.BaseLinNum, t2.DocTotal, t1.DocDate,
t2.Posted
union all
select t1.DocEntry as PaymentEntry,
t0.InvType as ObjType,
t0.DocEntry,
sum(case when t4.staType = -130 then 0
else t4.VatPercent
end) as VatPercent,
avg(t6.BaseSum) * t0.SumApplied / t2.DocTotal as PaidNet,
sum(case when t4.staType = -130 then t4.RvsChrgSum
else 0 end) as PaidCess,
t2.DocTotal,
t0.SumApplied / t2.DocTotal as Ratio,
year(t1.DocDate) as PaymentYear,
month(t1.DocDate) as PaymentMonth,
t2.Posted as IsDpmInv,
t4.SrcLineNum,
'AP' as "Direction"
from VPM2 t0
inner join OVPM t1 on t0.DocNum = t1.DocEntry
inner join ODPO t2 on t0.DocEntry = t2.DocEntry and t2.Posted = 'N'
inner join OTAX t3 on t3.SrcObjType = 46 and t3.SrcObjAbs = t1.DocEntry
inner join TAX1 t4 on t4.AbsEntry = t3.AbsEntry and t4.BaseArrTyp = 12 and
t4.BaseObjTyp = 204 and t4.BaseAbs = t0.DocEntry
inner join OUTX t5 on t5.SrcObjType = 204 and t5.SrcObjAbs = t4.BaseAbs
inner join UTX1 t6 on t5.AbsEntry = t6.AbsEntry and t6.SrcArrType = 12 and
t6.SrcLineNum = t4.BaseLinNum and t4.StaCode = t6.StaCode and t4.StaIndex =
t6.StaIndex
where t0.InvType = 204
and t1.Canceled = 'N'
and t4.staType in (-100, -110, -120, -130, -150)
and t4.RvsChrgPrc > 0
group by t1.DocEntry, t0.InvType, t0.DocEntry, t0.SumApplied, t4.SrcLineNum,
t4.BaseObjTyp, t4.BaseAbs, t4.BaseLinNum, t4.TaxCode, t2.DocTotal, t1.DocDate,
t2.Posted

go

IF object_id('GST_TAX_PAYMENT_SUMMARY','v') is not null


drop view GST_TAX_PAYMENT_SUMMARY
go

create view GST_TAX_PAYMENT_SUMMARY as


select DocEntry,
VatPercent,
sum(PaidNet) as PaidNet,
sum(PaidCess) as PaidCess,
PaymentYear,
PaymentMonth,
Direction
from GST_TAX_PAYMENT
group by Direction, DocEntry, PaymentYear, PaymentMonth, VatPercent
go

IF object_id('GST_TAX_DPM','v') is not null


drop view GST_TAX_DPM
go

create view GST_TAX_DPM as


select t0.DocEntry,
year(t0.DocDate) as DocYear,
month(t0.DocDate) as DocMonth,
t3.BaseAbs as LinkedEntry,
sum(case when staType = -130 then 0
else t2.VatPercent
end) as VatPercent,
case when t0.CANCELED = 'C' then -t3.LineTotal
else t3.LineTotal
end as LinkedNet,
-sum(case when staType = -130 and t0.CANCELED <> 'C' then t2.VatSum
- t2.RvsChrgSum
when staType = -130 and t0.CANCELED = 'C' then
t2.RvsChrgSum - t2.VatSum
else 0
end) as LinkedCess,
'AR' as "Direction"
from OINV t0
inner join OTAX t1 on t0.DocEntry = t1.SrcObjAbs and t1.SrcObjType = 13
inner join TAX1 t2 on t1.AbsEntry = t2.AbsEntry and T2.SrcArrType = 22
left join INV11 t3 on t3.DocEntry = t0.DocEntry and t3.LineSeq =
t2.SrcLineNum
where t2.staType in (-100, -110, -120, -130, -150)
group by t0.DocEntry, t0.DocDate, t3.LineTotal, t3.BaseAbs, t2.SrcLineNum,
t0.CANCELED
union all
select t0.DocEntry,
year(t0.DocDate) as DocYear,
month(t0.DocDate) as DocMonth,
t3.BaseAbs as LinkedEntry,
sum(case when staType = -130 then 0
else t2.VatPercent
end) as VatPercent,
case when t0.CANCELED = 'C' then t3.LineTotal
else -t3.LineTotal
end as LinkedNet,
sum(case when staType = -130 and t0.CANCELED <> 'C' then t2.VatSum -
t2.RvsChrgSum
when staType = -130 and t0.CANCELED = 'C' then
t2.RvsChrgSum - t2.VatSum
else 0
end) as LinedCess,
'AR' as "Direction"
from ORIN t0
inner join OTAX t1 on t0.DocEntry = t1.SrcObjAbs and t1.SrcObjType = 14
inner join TAX1 t2 on t1.AbsEntry = t2.AbsEntry and T2.SrcArrType = 22
left join RIN11 t3 on t3.DocEntry = t0.DocEntry and t3.LineSeq =
t2.SrcLineNum
where t2.staType in (-100, -110, -120, -130, -150)
group by t0.DocEntry, t0.DocDate, t3.LineTotal, t3.BaseAbs, t2.SrcLineNum,
t0.CANCELED
union all
select t0.DocEntry,
year(t0.DocDate) as DocYear,
month(t0.DocDate) as DocMonth,
t3.BaseAbs as LinkedEntry,
sum(case when staType = -130 then 0
else t2.VatPercent
end) as VatPercent,
case when t0.CANCELED = 'C' then -t3.LineTotal
else t3.LineTotal
end as LinkedNet,
-sum(case when staType = -130 and t0.CANCELED <> 'C' then
t2.RvsChrgSum
when staType = -130 and t0.CANCELED = 'C' then
-t2.RvsChrgSum
else 0
end) as LinkedCess,
'AP' as "Direction"
from OPCH t0
inner join OTAX t1 on t0.DocEntry = t1.SrcObjAbs and t1.SrcObjType = 18
inner join TAX1 t2 on t1.AbsEntry = t2.AbsEntry and T2.SrcArrType = 22
left join PCH11 t3 on t3.DocEntry = t0.DocEntry and t3.LineSeq =
t2.SrcLineNum
where t2.staType in (-100, -110, -120, -130, -150)
and t2.RvsChrgPrc > 0
group by t0.DocEntry, t0.DocDate, t3.LineTotal, t3.BaseAbs, t2.SrcLineNum,
t0.CANCELED
union all
select t0.DocEntry,
year(t0.DocDate) as DocYear,
month(t0.DocDate) as DocMonth,
t3.BaseAbs as LinkedEntry,
sum(case when staType = -130 then 0
else t2.VatPercent
end) as VatPercent,
case when t0.CANCELED = 'C' then t3.LineTotal
else -t3.LineTotal
end as LinkedNet,
sum(case when staType = -130 and t0.CANCELED <> 'C' then
t2.RvsChrgSum
when staType = -130 and t0.CANCELED = 'C' then
-t2.RvsChrgSum
else 0
end) as LinedCess,
'AP' as "Direction"
from ORPC t0
inner join OTAX t1 on t0.DocEntry = t1.SrcObjAbs and t1.SrcObjType = 19
inner join TAX1 t2 on t1.AbsEntry = t2.AbsEntry and T2.SrcArrType = 22
left join RPC11 t3 on t3.DocEntry = t0.DocEntry and t3.LineSeq =
t2.SrcLineNum
where t2.staType in (-100, -110, -120, -130, -150)
and t2.RvsChrgPrc > 0
group by t0.DocEntry, t0.DocDate, t3.LineTotal, t3.BaseAbs, t2.SrcLineNum,
t0.CANCELED
go

IF object_id('GST_TAX_DPM_SUMMARY','v') is not null


drop view GST_TAX_DPM_SUMMARY
go
create view GST_TAX_DPM_SUMMARY as
select DocYear,
DocMonth,
LinkedEntry,
VatPercent,
sum(LinkedNet) as LinkedNet,
sum(LinkedCess) as LinkedCess,
Direction
from GST_TAX_DPM
group by Direction, LinkedEntry, DocYear, DocMonth, VatPercent
go

IF object_id('GST_Report_B2B','p') is not null


drop procedure GST_Report_B2B
IF object_id('GST_Report_B2B_Tab','u') is not null
drop table GST_Report_B2B_Tab
go

create procedure GST_Report_B2B(


@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfGSTN integer


declare @numOfInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @cessSum numeric(19, 6)

IF object_id('GST_Report_B2B_Tab','u') is not null


drop table GST_Report_B2B_Tab

select * into GST_Report_B2B_Tab from GST_ODOC_Result


where ObjType = 13 and GstTransType = 'GA'
and BpRegistered = 'Y'
and IsGSTTax = 'Y'
and (ImpOrExp <> 'Y' or '' <> 'E')
and (ItemTaxType <> 'GN' or PureNilRateDoc = 'N')
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN

set @numOfGSTN = (select count(distinct t0.BpGSTN)from GST_Report_B2B_Tab t0)

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(t0.DocTotal)
from (select distinct DocEntry, DocNum, DocTotal from GST_Report_B2B_Tab) t0

select @taxiableSum = sum(BaseSum),


@cessSum = sum(CessSum)
from GST_Report_B2B_Tab
if (@dspType = 'B' or @dspType = 'S')
begin
select @numOfGSTN as "No. of Recipients",
@numOfInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Invoice Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess"
end
if(@dspType = 'B' or @dspType = 'D')
begin
select BpGSTN as "GSTIN/UIN of Recipient",
DocNum as "Invoice Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Invoice date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
isnull(t2.PoS, '') as "Place Of Supply",
Reverse_Charge as "Reverse Charge",
isnull(t1.Name, '') as "Invoice Type",
ECommerceGSTN as "E-Commerce GSTIN",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
case when sum(CessSum) = 0 then ''
else convert(nvarchar, convert(numeric(19, 2),
sum(CessSum)))
end as "Cess Amount"
from GST_Report_B2B_Tab t0
left join GST_MD_InvoiceType t1 on t0.ImpOrExp = t1.ImpOrExp --and
--t0.ExportType = t1.ExportType
and t0.DutyStatus = t1.DutyStatus
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
group by BpGSTN, DocEntry, DocNum, DocDate, DocTotal, t2.PoS,
Reverse_Charge, VatPercentSum, ECommerceGSTN, t1.Name
order by BpGSTN, DocNum, DocDate, DocTotal, t2.PoS, Reverse_Charge,
"Invoice Type", ECommerceGSTN, "Rate", "Taxable Value", "Cess Amount"
end
end

go

IF object_id('GST_Report_B2CL','p') is not null


drop procedure GST_Report_B2CL

IF object_id('GST_Report_B2CL_Tab','u') is not null


drop table GST_Report_B2CL_Tab
go
create procedure GST_Report_B2CL(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @minDocTotal numeric(19, 6)

set @minDocTotal = 250000

IF object_id('GST_Report_B2CL_Tab','u') is not null


drop table GST_Report_B2CL_Tab

select * into GST_Report_B2CL_Tab from GST_ODOC_Result_GST_TAX_ONLY


where ObjType = 13 and GstTransType = 'GA'
and BpRegistered = 'N'
and IsIntraState = 'N'
and (ItemTaxType <> 'GN' or PureNilRateDoc = 'N')
and ImpOrExp <> 'Y'
and DocTotal > @minDocTotal
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(t0.DocTotal)
from (select distinct DocEntry, DocNum, DocTotal from GST_Report_B2CL_Tab) t0

select @taxiableSum = sum(BaseSum),


@cessSum = sum(CessSum)
from GST_Report_B2CL_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total Inv
Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select DocNum as "Invoice Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Invoice date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
isnull(t2.PoS, '') as "Place Of Supply",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
case when sum(CessSum) = 0 then ''
else convert(nvarchar, convert(numeric(19, 2),
sum(CessSum)))
end as "Cess Amount",
ECommerceGSTN as "E-Commerce GSTIN"
from GST_Report_B2CL_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
group by DocEntry, DocNum, DocDate, DocTotal, t2.PoS, VatPercentSum,
ECommerceGSTN
order by DocNum, DocDate, DocTotal, t2.PoS, "Rate", "Taxable Value",
"Cess Amount", ECommerceGSTN
end
end

go

IF object_id('GST_Report_B2CS','p') is not null


drop procedure GST_Report_B2CS
IF object_id('GST_Report_B2CS_Tab','u') is not null
drop table GST_Report_B2CS_Tab

go
create procedure GST_Report_B2CS(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @taxiableSum numeric(19, 6)


declare @cessSum numeric(19, 6)
declare @maxDocTotal numeric(19, 6)

set @maxDocTotal = 250000

IF object_id('GST_Report_B2CS_Tab','u') is not null


drop table GST_Report_B2CS_Tab

select case when ECommerceGSTN <> '' then 'E'


else 'OE'
end
as "Type",
PoSGSTN,
VatPercentSum,
case when ObjType = 14 then -BaseSum
else BaseSum
end
as "BaseSum",
case when ObjType = 14 then -CessSum
else CessSum
end
as "CessSum",
ECommerceGSTN
into GST_Report_B2CS_Tab from GST_ODOC_Result_GST_TAX_ONLY
where BpRegistered = 'N'
and(ObjType = 13 and GstTransType = 'GA' --GST Tax Invoice
and (IsIntraState = 'Y' or DocTotal <= @maxDocTotal)
or ((ObjType = 14 and GstTransType = 'GA' or ObjType = 13 and
GstTransType = 'GD') -- Debit Memo/Credit Memo
and BaseType <> 203 and (OrigRefNo = '' or OrigRefDate =
'')))
and (ItemTaxType <> 'GN' or PureNilRateDoc = 'N')
and ImpOrExp <> 'Y'
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(sum(BaseSum), 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(sum(CessSum), 0)) as "Total
Cess"
from GST_Report_B2CS_Tab
end

if(@dspType = 'B' or @dspType = 'D')


begin
select "Type",
isnull(t1.PoS, '') as "Place Of Supply",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
case when sum(CessSum) = 0 then ''
else convert(nvarchar, convert(numeric(19, 2),
sum(CessSum)))
end as "Cess Amount",
ECommerceGSTN as "E-Commerce GSTIN"
from GST_Report_B2CS_Tab t0
left join GST_MD_PoS t1 on t0.PoSGSTN = t1.StateGSTN
group by "Type", t1.PoS,VatPercentSum, ECommerceGSTN
order by "Type", t1.PoS, "Rate", "Taxable Value", "Cess Amount", "E-
Commerce GSTIN"
end
end

go

IF object_id('GST_Report_CDNR','p') is not null


drop procedure GST_Report_CDNR
IF object_id('GST_Report_CDNR_Tab','u') is not null
drop table GST_Report_CDNR_Tab

go
create procedure GST_Report_CDNR(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfGSTN integer


declare @numOfInv integer
declare @numOfOrigInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @cessSum numeric(19, 6)

IF object_id('GST_Report_CDNR_Tab','u') is not null


drop table GST_Report_CDNR_Tab

select t0.*, t1.NoteType into GST_Report_CDNR_Tab from


GST_ODOC_Result_GST_TAX_ONLY t0
left join GST_MD_NoteType t1 on t0.ObjType = t1.ObjType and t0.GstTransType =
t1.GstTransType
where (t0.ObjType = 13 and t0.GstTransType = 'GD'
or t0.ObjType = 14 and t0.GstTransType = 'GA' and BaseType <>
203)
and t0.BpRegistered ='Y'
and (t0.ItemTaxType <> 'GN' or t0.PureNilRateDoc = 'N')
and (t0.ImporExp <> 'Y' or '' <> 'E')
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

set @numOfGSTN = (select count(distinct t0.BpGSTN)from GST_Report_CDNR_Tab


t0)
select @numOfInv = count(t0.DocNum),
@docTotalSum = sum(case when NoteType = 'C' then -DocTotal
else DocTotal
end)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal,
NoteType from GST_Report_CDNR_Tab) t0

set @numOfOrigInv = (select count( distinct(OrigRefNo)) from


GST_Report_CDNR_Tab where OrigRefNo <> '')

select @taxiableSum = sum(case when NoteType = 'C' then -BaseSum


else BaseSum
end),
@cessSum = sum(case when NoteType = 'C' then -CessSum
else CessSum
end)
from GST_Report_CDNR_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfGSTN as "No. of Recipients",
@numOfOrigInv as "No. of Invoices",
@numOfInv as "No. of Notes/Vouchers",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Note/Refund Voucher Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select BpGSTN as "GSTIN/UIN of Recipient",
OrigRefNo as "Invoice/Advance Receipt Number",
case when OrigRefDate = CONVERT(datetime, '') or OrigRefDate =
'' or OrigRefDate is null then ''
else replace(CONVERT(Varchar(20), OrigRefDate, 106), ' ', '-')
end as "Invoice/Advance Receipt date",
DocNum as "Note/Refund Voucher Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Note/Refund Voucher date",
NoteType as "Document Type",
isnull(Comments, '') as "Reason For Issuing document",
isnull(t2.PoS, '') as "Place Of Supply",
convert(numeric(19, 2), DocTotal) as "Note/Refund Voucher
Value",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
case when sum(CessSum) = 0 then ''
else convert(nvarchar, convert(numeric(19, 2),
sum(CessSum)))
end as "Cess Amount",
case when OrigRefDate < '20170701' then 'Y'
else 'N'
end
as "Pre GST"
from GST_Report_CDNR_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
group by BpGSTN, ObjType, GstTransType, DocEntry, DocNum, DocDate,
DocTotal, t2.PoS, VatPercentSum, OrigRefDate, OrigRefNo, Comments, NoteType
order by BpGSTN, OrigRefNo, OrigRefDate, DocNum, DocDate, NoteType,
Comments, t2.PoS, DocTotal, "Rate", "Taxable Value", "Cess Amount", "Pre GST"
end
end

go

IF object_id('GST_Report_CDNUR','p') is not null


drop procedure GST_Report_CDNUR
IF object_id('GST_Report_CDNUR_Tab','u') is not null
drop table GST_Report_CDNUR_Tab
go
create procedure GST_Report_CDNUR(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @numOfOrigInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @cessSum numeric(19, 6)

IF object_id('GST_Report_CDNUR_Tab','u') is not null


drop table GST_Report_CDNUR_Tab
select t0.*, t1.NoteType into GST_Report_CDNUR_Tab from
GST_ODOC_Result_GST_TAX_ONLY t0
left join GST_MD_NoteType t1 on t0.ObjType = t1.ObjType and t0.GstTransType =
t1.GstTransType
where (t0.ObjType = 13 and t0.GstTransType = 'GD'
or t0.ObjType = 14 and t0.GstTransType = 'GA' and BaseType <>
203)
and t0.BpRegistered ='N'
and OrigRefNo <> ''
and OrigRefDate <> ''
and (t0.ItemTaxType <> 'GN' or t0.PureNilRateDoc = 'N')
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(case when NoteType = 'C' then -DocTotal
else DocTotal
end)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal,
NoteType from GST_Report_CDNUR_Tab) t0

set @numOfOrigInv = (select count(distinct(OrigRefNo)) from


GST_Report_CDNUR_Tab where OrigRefNo <> '')

select @taxiableSum = sum(case when NoteType = 'C' then -BaseSum


else BaseSum
end),
@cessSum = sum(case when NoteType = 'C' then -CessSum
else CessSum
end)
from GST_Report_CDNUR_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No. of Notes/Vouchers",
@numOfOrigInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Note Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t1.Name, '') as "UR Type",
DocNum as "Note/Refund Voucher Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Note/Refund Voucher date",
NoteType as "Document Type",
OrigRefNo as "Invoice/Advance Receipt Number",
case when OrigRefDate = CONVERT(datetime, '') or OrigRefDate =
'' or OrigRefDate is null then ''
else replace(CONVERT(Varchar(20), OrigRefDate, 106), ' ', '-')
end as "Invoice/Advance Receipt date",
isnull(Comments, '') as "Reason For Issuing document",
isNull(t2.PoS, '') as "Place Of Supply",
convert(numeric(19, 2), DocTotal) as "Note/Refund Voucher
Value",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
case when sum(CessSum) = 0 then ''
else convert(nvarchar, convert(numeric(19, 2),
sum(CessSum)))
end as "Cess Amount",
case when OrigRefDate < '20170701' then 'Y'
else 'N'
end
as "Pre GST"
from GST_Report_CDNUR_Tab t0
left join GST_MD_URType t1 on t0.BpGstType = t1.GstType and t0.ImpOrExp
= t1.ImpOrExp and t0.DutyStatus = t1.DutyStatus
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
group by ObjType, GstTransType, DocEntry, DocNum, DocDate, DocTotal,
t2.PoS, VatPercentSum, OrigRefDate, OrigRefNo, Comments, NoteType, t1.Name
order by "UR Type", DocNum, DocDate, NoteType, OrigRefNo, OrigRefDate,
Comments, t2.PoS, DocTotal, "Rate", "Taxable Value", "Cess Amount", "Pre GST"
end
end

go

IF object_id('GST_Report_EXP','p') is not null


drop procedure GST_Report_EXP

IF object_id('GST_Report_EXP_Tab','u') is not null


drop table GST_Report_EXP_Tab
go
create procedure GST_Report_EXP(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @numOfBill integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)

IF object_id('GST_Report_EXP_Tab','u') is not null


drop table GST_Report_EXP_Tab

select t0.* into GST_Report_EXP_Tab from GST_ODOC_Result_GST_TAX_ONLY t0


where (t0.ObjType = 13 and t0.GstTransType = 'GA')
and t0.BpRegistered = 'N'
and t0.ImpOrExp = 'Y'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(DocTotal)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal from
GST_Report_EXP_Tab) t0

set @numOfBill = (select count(distinct(ImpExpNo)) from GST_Report_EXP_Tab


where ImpExpNo <> '')

select @taxiableSum = sum(BaseSum)


from GST_Report_EXP_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Invoice Value",
@numOfBill as "No. of Shipping Bill",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t1.Name, '') as "Export Type",
DocNum as "Invoice Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Invoice date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
-- t0.PortCode as "Port Code",
t0.ImpExpNo as "Shipping Bill Number",
case when t0.ImpExpDate = CONVERT(datetime, '') or
t0.ImpExpDate = '' or t0.ImpExpDate is null then ''
else replace(CONVERT(Varchar(20), t0.ImpExpDate, 106), ' ',
'-')
end as "Shipping Bill Date",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value"
from GST_Report_EXP_Tab t0
left join GST_MD_ExportType t1 on t0.DutyStatus = t1.DutyStatus
group by DocEntry, DocNum, DocDate, DocTotal,-- PortCode,
ImpExpNo, ImpExpDate, VatPercentSum, t1.Name
order by t1.Name, DocNum, DocDate, DocTotal, --PortCode,
ImpExpNo, ImpExpDate, "Rate", "Taxable Value"
end
end

go

IF object_id('GST_Report_EXEMP','p') is not null


drop procedure GST_Report_EXEMP
go

IF object_id('GST_Report_EXEMP_Tab','u') is not null


drop table GST_Report_EXEMP_Tab
go
create procedure GST_Report_EXEMP(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalNilSum numeric(19, 6)


declare @totalExemptSum numeric(19, 6)
declare @totalNonGSTSum numeric(19, 6)

IF object_id('GST_Report_EXEMP_Tab','u') is not null


drop table GST_Report_EXEMP_Tab

select sum(BaseSum) as BaseSum,


ItemTaxType,
IsIntraState,
BpRegistered
into GST_Report_EXEMP_Tab
from (select distinct DocEntry, LineNum,
case when ObjType = 14 then -BaseSum
else BaseSum
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge t0
where ObjType in (13, 14)
and ItemTaxType = 'GN'
and PureNilRateDoc = 'Y'
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN
union all
select distinct DocEntry, LineNum,
case when ObjType = 14 then -BaseSum
else BaseSum
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge
where ObjType in (13, 14)
and ItemTaxType = 'GE'
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN
union all
select distinct DocEntry, -1,
case when ObjType = 14 then -DocTotal
else DocTotal
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge
where ObjType in (13, 14)
and ItemTaxType = 'NN'
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN) t0
group by IsIntraState, BpRegistered, ItemTaxType

select @totalNilSum = sum(BaseSum) from GST_Report_EXEMP_Tab where


ItemTaxType = 'GN'
select @totalExemptSum = sum(BaseSum) from GST_Report_EXEMP_Tab where
ItemTaxType = 'GE'
select @totalNonGSTSum = sum(BaseSum) from GST_Report_EXEMP_Tab where
ItemTaxType = 'NN'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(@totalNilSum, 0)) as "Total Nil
Rated Supplies",
convert(numeric(19, 2), isnull(@totalExemptSum, 0)) as "Total
Exempted Supplies",
convert(numeric(19, 2), isnull(@totalNonGSTSum, 0)) as "Total
Non-GST Supplies"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select "Description",
NilRated as "Nil Rated Supplies",
Exempted as "Exempted (other than nil rated/non GST supply )",
NonGST as "Non-GST supplies"
from
(select 'Inter-State supplies to registered persons' as "Description",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)) as "NilRated",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)) as "Exempted",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)) as "NonGST",
1 as TabOrder
from GST_Report_EXEMP_Tab
where IsIntraState = 'N' and BpRegistered = 'Y'
union
select 'Intra-State supplies to registered persons',
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)),
2
from GST_Report_EXEMP_Tab
where IsIntraState = 'Y' and BpRegistered = 'Y'
union
select 'Inter-State supplies to unregistered persons',
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)),
3
from GST_Report_EXEMP_Tab
where IsIntraState = 'N' and BpRegistered = 'N'
union
select 'Intra-State supplies to unregistered persons',
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)),
4
from GST_Report_EXEMP_Tab
where IsIntraState = 'Y' and BpRegistered = 'N') t0
order by TabOrder
end
end

go

IF object_id('GST_Report_HSN','p') is not null


drop procedure GST_Report_HSN

IF object_id('GST_Report_HSN_Tab','u') is not null


drop table GST_Report_HSN_Tab
go

create procedure GST_Report_HSN(


@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @hsnCount integer


declare @totalValue numeric(19, 6)
declare @totalTaxable numeric(19, 6)
declare @totalIGST numeric(19, 6)
declare @totalCGST numeric(19, 6)
declare @totalSGST numeric(19, 6)
declare @totalCess numeric(19, 6)

IF object_id('GST_Report_HSN_Tab','u') is not null


drop table GST_Report_HSN_Tab

select sum(Quantity) as Quantity,


round(sum(BaseSum), 2) as BaseSum,
round(sum(IGSTSum), 2) as IGSTSum,
round(sum(CGSTSum), 2) as CGSTSum,
round(sum(SGSTSum), 2) as SGSTSum,
round(sum(CessSum), 2) as CessSum,
UomEntry, SacEntry, HsnCode, HsnName, UQC
into GST_Report_HSN_Tab
from ( select case when DocType = 'S' or isnull(t1.ItemClass, '') = '1'
then 0
when t0.ObjType = 14 then -t0.Quantity
else t0.Quantity
end as Quantity,
case when t0.ObjType = 14 then -t0.BaseSum
else t0.BaseSum
end as BaseSum,
case when t0.ObjType = 14 then -t0.IGSTSum
else t0.IGSTSum
end as IGSTSum,
case when t0.ObjType = 14 then -t0.CGSTSum
else t0.CGSTSum
end as CGSTSum,
case when t0.ObjType = 14 then -t0.SGSTSum
else t0.SGSTSum
end as SGSTSum,
case when t0.ObjType = 14 then -t0.CessSum
else t0.CessSum
end as CessSum,
t0.UomEntry,
t0.SacEntry,
case when DocType = 'S' or isnull(t1.ItemClass, '') =
'1' or ItemTaxType = 'F' then isnull(t5.ServCode, '')
else isnull(t2.ChapterID, '')
end as HsnCode,
case when DocType = 'S' or isnull(t1.ItemClass, '') =
'1' or ItemTaxType = 'F' then isnull(t5.ServName, '')
else isnull(t2.Dscription, '')
end as HsnName,
case when isnull(t0.UomEntry, 0) = 0 or t0.UomEntry =
-1 then t0.UomName
else t3.UomName
end as UQC,
t0.ObjType,
t0.GstTransType,
t0.DocEntry
from GST_ODOC_Result_GST_TAX_ONLY t0
left join OITM t1 on DocType = 'I' and t0.ItemCode = t1.ItemCode
left join OEXD t4 on t0.HeaderFreightCode > 0 and
t0.HeaderFreightCode = t4.ExpnsCode
left join OCHP t2 on isnull(t1.ChapterID, 0) > 0 and t2.AbsEntry
= t1.ChapterID
left join OSAC t5 on ((isnull(t0.SacEntry, 0) <> 0 and
t0.SacEntry <> -1 and t0.SacEntry = t5.AbsEntry)
or (isnull(t4.SacCode, '') <>
'' and t4.SacCode = t5.ServCode))
left join OUOM t3 on isnull(t0.UomEntry, 0) > 0 and t0.UomEntry =
t3.UomEntry
where t0.ObjType in (13, 14)
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN) t0
group by ObjType, GstTransType, DocEntry, UomEntry, SacEntry, HsnCode,
HsnName, UQC

select @hsnCount = count(distinct HsnCode) from GST_Report_HSN_Tab where


isnull(HsnCode, '') <> ''

select @totalValue = sum(BaseSum + IGSTSum + CGSTSum + SGSTSum + CessSum),


@totalTaxable = sum(BaseSum),
@totalIGST = sum(IGSTSum),
@totalCGST = sum(CGSTSum),
@totalSGST = sum(SGSTSum),
@totalCess = sum(CessSum)
from GST_Report_HSN_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @hsnCount as "No. of HSN",
convert(numeric(19, 2), isnull(@totalValue, 0)) as "Total
Value",
convert(numeric(19, 2), isnull(@totalTaxable, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@totalIGST, 0)) as "Total
Integrated Tax",
convert(numeric(19, 2), isnull(@totalCGST, 0)) as "Total
Central Tax",
convert(numeric(19, 2), isnull(@totalSGST, 0)) as "Total
State/UT Tax",
convert(numeric(19, 2), isnull(@totalCess, 0)) as "Total Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select replace(HsnCode, '.', '') as "HSN",
isnull(HsnName, '') as "Description",
isnull(UQC, '') as UQC,
convert(nvarchar, convert(numeric(19, 2), sum(Quantity))) as
"Total Quantity",
convert(numeric(19, 2), sum(BaseSum + IGSTSum + CGSTSum +
SGSTSum + CessSum)) as "Total Value",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(numeric(19, 2), sum(IGSTSum)) as "Integrated Tax
Amount",
convert(numeric(19, 2), sum(CGSTSum)) as "Central Tax Amount",
convert(numeric(19, 2), sum(SGSTSum)) as "State/UT Tax
Amount",
convert(numeric(19, 2), sum(CessSum)) as "Cess Amount"
from GST_Report_HSN_Tab
group by HsnCode, HsnName, UomEntry, UQC
order by HsnCode, HsnName, UQC
end
end

go

IF object_id('GST_Report_AT','p') is not null


drop procedure GST_Report_AT

IF object_id('GST_Report_AT_Tab','u') is not null


drop table GST_Report_AT_Tab
go

create procedure GST_Report_AT(


@year nvarchar(4),
@month nvarchar(2),
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalAdvRec numeric(19, 6)


declare @totalCess numeric(19, 6)

IF object_id('GST_Report_AT_Tab','u') is not null


drop table GST_Report_AT_Tab

select t0.PaidNet - isnull(t1.LinkedNet, 0) as PaidNet,


t0.PaidCess - isnull(t1.LinkedCess, 0) as PaidCess,
t0.DocEntry,
t0.VatPercent,
t2.BPStatGSTN as BpStateGSTN
into GST_Report_AT_Tab
from GST_TAX_PAYMENT_SUMMARY t0
inner join DPI12 t2 on t0.DocEntry = t2.DocEntry
left join GST_TAX_DPM_SUMMARY t1 on t0.DocEntry = t1.LinkedEntry and
t0.VatPercent = t1.VatPercent
and t0.PaymentYear = t1.DocYear and t0.PaymentMonth
= t1.DocMonth
and t0.Direction = t1.Direction
where t0.PaymentYear = @year
and t0.PaymentMonth = @month
and t2.LocGSTN = @locGSTN
and t2.BpCountry = 'IN'
and t0.Direction = 'AR'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(sum(t0.PaidNet), 0)) as "Total
Advance Received",
convert(numeric(19, 2), isnull(sum(t0.PaidCess), 0)) as "Total
Cess"
from GST_Report_AT_Tab t0
where Round(t0.PaidNet, 2) > 0
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t2.PoS, '') as "Place Of Supply",
t0.VatPercent as "Rate",
convert(numeric(19, 2), isnull(sum(t0.PaidNet), 0)) as "Gross
Advance Received",
convert(numeric(19, 2), isnull(sum(t0.PaidCess), 0)) as "Cess
Amount"
from GST_Report_AT_Tab t0
left join GST_MD_PoS t2 on t0.BpStateGSTN = t2.StateGSTN
where Round(PaidNet, 2) > 0
group by t2.PoS, t0.VatPercent
order by PoS, "Rate", "Gross Advance Received", "Cess Amount"
end
end

go

IF object_id('GST_Report_AtAdj','p') is not null


drop procedure GST_Report_AtAdj

IF object_id('GST_Report_AtAdj_Tab','u') is not null


drop table GST_Report_AtAdj_Tab
go

create procedure GST_Report_AtAdj(


@year nvarchar(4),
@month nvarchar(2),
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalAdvRec numeric(19, 6)


declare @totalCess numeric(19, 6)

IF object_id('GST_Report_AtAdj_Tab','u') is not null


drop table GST_Report_AtAdj_Tab

select t0.LinkedNet - isnull(t1.PaidNet, 0) as AdjustNet,


t0.LinkedCess - isnull(t1.PaidCess, 0) as AdjustCess,
t0.LinkedEntry,
t0.VatPercent,
t2.BPStatGSTN as BpStateGSTN
into GST_Report_AtAdj_Tab
from GST_TAX_DPM_SUMMARY t0
inner join DPI12 t2 on t0.LinkedEntry = t2.DocEntry
left join GST_TAX_PAYMENT_SUMMARY t1 on t1.DocEntry = t0.LinkedEntry and
t0.VatPercent = t1.VatPercent
and t1.PaymentYear = t0.DocYear and t1.PaymentMonth
= t0.DocMonth
and t0.Direction = t1.Direction
where t0.DocYear = @year
and t0.DocMonth = @month
and t2.LocGSTN = @locGSTN
and t2.BpCountry = 'IN'
and t0.Direction = 'AR'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(sum(t0.AdjustNet), 0)) as "Total
Advance Adjusted",
convert(numeric(19, 2), isnull(sum(t0.AdjustCess), 0)) as
"Total Cess"
from GST_Report_AtAdj_Tab t0
where round(t0.AdjustNet, 2) > 0
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t2.PoS, '') as "Place Of Supply",
t0.VatPercent as "Rate",
convert(numeric(19, 2), isnull(sum(t0.AdjustNet), 0)) as
"Gross Advance Adjusted",
convert(numeric(19, 2), isnull(sum(t0.AdjustCess), 0)) as
"Cess Amount"
from GST_Report_AtAdj_Tab t0
left join GST_MD_PoS t2 on t0.BpStateGSTN = t2.StateGSTN
where round(AdjustNet, 2) > 0
group by t2.PoS, t0.VatPercent
order by PoS, "Rate", "Gross Advance Adjusted", "Cess Amount"
end
end

go

IF object_id('GST_Report_DOCS','p') is not null


drop procedure GST_Report_DOCS
IF object_id('GST_Report_DOCS_Tab','u') is not null
drop table GST_Report_DOCS_Tab
go

create procedure GST_Report_DOCS(


@fromDate date,
@toDate date,
@locGSTN nvarchar(15))
as
begin
IF object_id('GST_Report_DOCS_Tab','u') is not null
drop table GST_Report_DOCS_Tab

select * into GST_Report_DOCS_Tab from


(select convert(nvarchar(100), 'Invoice for outward supply') as
"DocName",
min(DocNum) as "FromNum",
max(DocNum) as "ToNum",
count(DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
1 as "TabOrder"
from OINV t0
inner join INV12 t2 on t0.DocEntry = t2.DocEntry
where t0.GstTranTyp = 'GA'
and t0.CANCELED <> 'C'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and isnull(t2.LocGSTN, '') = @LocGSTN
group by t0.Series
union all
select convert(nvarchar(100), 'Debit Note') as "DocName",
min(DocNum) as "FromNum",
max(DocNum) as "ToNum",
count(DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
2 as "TabOrder"
from OINV t0
inner join INV12 t2 on t0.DocEntry = t2.DocEntry
where t0.GstTranTyp = 'GD'
and t0.CANCELED <> 'C'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and isnull(t2.LocGSTN, '') = @LocGSTN
group by t0.Series
union all
select convert(nvarchar(100), 'Credit Note') as "DocName",
min(DocNum) as "FromNum",
max(DocNum) as "ToNum",
count(DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
3 as "TabOrder"
from ORIN t0
inner join RIN12 t2 on t0.DocEntry = t2.DocEntry
where t0.GstTranTyp = 'GA'
and t0.CANCELED <> 'C'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and isnull(t2.LocGSTN, '') = @LocGSTN
group by t0.Series
union all
select convert(nvarchar(100), 'Receipt Voucher') as "DocName",
min(t0.DocNum) as "FromNum",
max(t0.DocNum) as "ToNum",
count(t0.DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
4 as "TabOrder"
from ORCT t0
where exists(select 1 from RCT2 t1
inner join ODPI t2 on t2.DocEntry = t1.DocEntry and
t2.GstTranTyp = 'GA'
inner join DPI12 t3 on t2.DocEntry = t3.DocEntry and
isnull(t3.LocGSTN, '') = @LocGSTN
where t0.DocEntry = t1.DocNum
and t1.InvType = 203)
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
group by t0.Series
union all
select convert(nvarchar(100), 'Payment Voucher') as "DocName",
min(t0.DocNum) as "FromNum",
max(t0.DocNum) as "ToNum",
count(t0.DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
5 as "TabOrder"
from OVPM t0
where (exists(select 1 from VPM2 t1
inner join PCH12 t2 on t1.DocEntry = t2.DocEntry and
isnull(t2.BpGSTType, 0) = 0 and isnull(t2.LocGSTN, '') = @LocGSTN
where t0.DocEntry = t1.DocNum
and t1.InvType = 18)
or exists (select 1 from VPM2 t1
inner join DPO12 t2 on t1.DocEntry =
t2.DocEntry and isnull(t2.BpGSTType, 0) = 0 and isnull(t2.LocGSTN, '') = @LocGSTN
where t0.DocEntry = t1.DocNum
and t1.InvType = 204)
or exists (select 1 from VPM2 t1
inner join RIN12 t2 on t1.DocEntry =
t2.DocEntry and isnull(t2.BpGSTType, 0) = 0 and isnull(t2.LocGSTN, '') = @LocGSTN
where t0.DocEntry = t1.DocNum
and t1.InvType = 14))
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
group by t0.Series
union all
select convert(nvarchar(100), 'Invoice for inward supply from
unregistered person') as "DocName",
min(DocNum) as "FromNum",
max(DocNum) as "ToNum",
count(DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
6 as "TabOrder"
from OPCH t0
inner join PCH12 t1 on t0.DocEntry = t1.DocEntry
where t0.CANCELED <> 'C'
and isnull(t1.BpGSTType, 0) = 0
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and isnull(t1.LocGSTN, '') = @LocGSTN
group by t0.Series
union all
select convert(nvarchar(100), 'Delivery') as "DocName",
min(DocNum) as "FromNum",
max(DocNum) as "ToNum",
count(DocNum) as "DocCount",
sum(case when CANCELED = 'Y' then 1
else 0
end) as "CancelledCount",
t0.Series,
7 as "TabOrder"
from ODLN t0
inner join DLN12 t1 on t0.DocEntry = t1.DocEntry
where t0.CANCELED <> 'C'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and isnull(t1.LocGSTN, '') = @LocGSTN
group by t0.Series) as t0

select isnull(t0.DocName, '') as "Nature of Document",


isnull(t0.FromNum, '') as "Sr. No. From",
isnull(t0.ToNum, '') as "Sr. No. To",
isnull(t0.DocCount, 0) as "Total Number",
isnull(t0.CancelledCount, 0) as "Cancelled",
isnull(t1.SeriesName, '') as "Series Name",
isnull(t1.Remark, '') as "Series Description",
isnull(t1.BeginStr, '') as "Begin String",
isnull(t1.EndStr, '') as "End String",
isnull(t1.Indicator, '') as "Period Indicator"
from GST_Report_DOCS_Tab t0
left join NNM1 t1 on t0.Series = t1.Series
order by TabOrder, t0.Series
end
go

IF object_id('GST_Report_GSTR2_B2B','p') is not null


drop procedure GST_Report_GSTR2_B2B
IF object_id('GST_Report_GSTR2_B2B_Tab','u') is not null
drop table GST_Report_GSTR2_B2B_Tab
go

create procedure GST_Report_GSTR2_B2B(


@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfGSTN integer


declare @numOfInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @CGSTSum numeric(19, 6)
declare @SGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CGSTITCSum numeric(19, 6)
declare @SGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)

IF object_id('GST_Report_GSTR2_B2B_Tab','u') is not null


drop table GST_Report_GSTR2_B2B_Tab

select * into GST_Report_GSTR2_B2B_Tab from GST_ODOC_Result


where ObjType = 18 and GstTransType = 'GA'
and BpRegistered = 'Y'
and IsGSTTax = 'Y'
and (ImpOrExp <> 'Y' or '' <> 'E')
and (ItemTaxType <> 'GN' or PureNilRateDoc = 'N')
and BpGSTN <> LocGSTN
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN

set @numOfGSTN = (select count(distinct t0.BpGSTN)from


GST_Report_GSTR2_B2B_Tab t0)

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(t0.DocTotal)
from (select distinct DocEntry, DocNum, DocTotal from
GST_Report_GSTR2_B2B_Tab) t0

select @taxiableSum = sum(BaseSum),


@IGSTSum = sum(IGSTSum),
@CGSTSum = sum(CGSTSum),
@SGSTSum = sum(SGSTSum),
@cessSum = sum(CessSum),
@IGSTITCSum = sum(IGSTDeductSum),
@CGSTITCSum = sum(CGSTDeductSum),
@SGSTITCSum = sum(SGSTDeductSum),
@CessITCSum = sum(CessDeductSum)
from GST_Report_GSTR2_B2B_Tab

if (@dspType = 'B' or @dspType = 'S')


begin
select @numOfGSTN as "No. of Suppliers",
@numOfInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Invoice Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@CGSTSum, 0)) as "Total Central
Tax Paid",
convert(numeric(19, 2), isnull(@SGSTSum, 0)) as "Total
TState/UT Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total
Availed ITC Integrated Tax ",
convert(numeric(19, 2), isnull(@CGSTITCSum, 0)) as "Total
Availed ITC Central Tax",
convert(numeric(19, 2), isnull(@SGSTITCSum, 0)) as "Total
Availed ITC State/UT Tax ",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total
Availed ITC Cess"
end
if(@dspType = 'B' or @dspType = 'D')
begin
select BpGSTN as "GSTIN of Supplier",
TaxInvNo as "Invoice Number",
case when TaxInvDate = CONVERT(datetime, '') or TaxInvDate =
'' or TaxInvDate is null then ''
else replace(CONVERT(Varchar(20), TaxInvDate, 106), ' ', '-')
end as "Invoice date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
isnull(t2.PoS, '') as "Place Of Supply",
t0.Reverse_Charge as "Reverse Charge",
isnull(t1.Name, '') as "Invoice Type",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTSum))) as
"Central Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTSum))) as
"State/UT Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTDeductSum)))
as "Availed ITC Central Tax",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTDeductSum)))
as "Availed ITC State/UT Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_B2B_Tab t0
left join GST_MD_InvoiceType t1 on t0.ImpOrExp = t1.ImpOrExp --and
t0.ExportType = t1.ExportType
and t0.DutyStatus = t1.DutyStatus
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by BpGSTN, DocEntry, TaxInvNo, TaxInvDate, DocTotal, t2.PoS,
Reverse_Charge, t1.Name, VatPercentSum, t0.NonDeductable, t3.Name
order by BpGSTN, TaxInvNo, TaxInvDate, DocTotal, t2.PoS,
Reverse_Charge, t1.Name, "Rate"
end
end

go

IF object_id('GST_Report_GSTR2_B2BUR','p') is not null


drop procedure GST_Report_GSTR2_B2BUR

IF object_id('GST_Report_GSTR2_B2BUR_Tab','u') is not null


drop table GST_Report_GSTR2_B2BUR_Tab
go
create procedure GST_Report_GSTR2_B2BUR(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @CGSTSum numeric(19, 6)
declare @SGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CGSTITCSum numeric(19, 6)
declare @SGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)

IF object_id('GST_Report_GSTR2_B2BUR_Tab','u') is not null


drop table GST_Report_GSTR2_B2BUR_Tab

select * into GST_Report_GSTR2_B2BUR_Tab from GST_ODOC_Result


where ObjType = 18 and GstTransType <> 'GD'
and (BpRegistered = 'N' and Reverse_Charge = 'Y'
or BpRegistered = 'Y' and BpGSTN = LocGSTN)
and IsGSTTax = 'Y'
and ImpOrExp <> 'Y'
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(t0.DocTotal)
from (select distinct DocEntry, DocNum, DocTotal from
GST_Report_GSTR2_B2BUR_Tab) t0

select @taxiableSum = sum(BaseSum),


@IGSTSum = sum(IGSTSum),
@CGSTSum = sum(CGSTSum),
@SGSTSum = sum(SGSTSum),
@cessSum = sum(CessSum),
@IGSTITCSum = sum(IGSTDeductSum),
@CGSTITCSum = sum(CGSTDeductSum),
@SGSTITCSum = sum(SGSTDeductSum),
@CessITCSum = sum(CessDeductSum)
from GST_Report_GSTR2_B2BUR_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No. of Invoices (Of Reg Recipient)",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Invoice Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@CGSTSum, 0)) as "Total Central
Tax Paid",
convert(numeric(19, 2), isnull(@SGSTSum, 0)) as "Total
TState/UT Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess
Paid",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total
Availed ITC Integrated Tax ",
convert(numeric(19, 2), isnull(@CGSTITCSum, 0)) as "Total
Availed ITC Central Tax",
convert(numeric(19, 2), isnull(@SGSTITCSum, 0)) as "Total
Availed ITC State/UT Tax ",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total
Availed ITC Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select t0.CardName as "Supplier Name",
DocNum as "Invoice Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Invoice date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
isnull(t2.PoS, '') as "Place Of Supply",
case when IsIntraState = 'Y' then 'Intra State'
else 'Inter State' end as "Supply Type",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTSum))) as
"Central Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTSum))) as
"State/UT Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTDeductSum)))
as "Availed ITC Central Tax",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTDeductSum)))
as "Availed ITC State/UT Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_B2BUR_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by CardName, GstTransType, DocEntry, DocNum, DocDate, DocTotal,
t2.PoS, IsIntraState, Reverse_Charge, VatPercentSum, t0.NonDeductable, t3.Name
order by CardName, DocNum, DocDate, DocTotal, t2.PoS, Reverse_Charge,
"Rate"
end
end

go

IF object_id('GST_Report_GSTR2_CDNR','p') is not null


drop procedure GST_Report_GSTR2_CDNR
IF object_id('GST_Report_GSTR2_CDNR_Tab','u') is not null
drop table GST_Report_GSTR2_CDNR_Tab

go
create procedure GST_Report_GSTR2_CDNR(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end
declare @numOfGSTN integer
declare @numOfInv integer
declare @numOfOrigInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @CGSTSum numeric(19, 6)
declare @SGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CGSTITCSum numeric(19, 6)
declare @SGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)

IF object_id('GST_Report_GSTR2_CDNR_Tab','u') is not null


drop table GST_Report_GSTR2_CDNR_Tab

select t0.*, t1.NoteType into GST_Report_GSTR2_CDNR_Tab from


GST_ODOC_Result_GST_TAX_ONLY t0
left join GST_MD_NoteType t1 on t0.ObjType = t1.ObjType and t0.GstTransType =
t1.GstTransType
where (t0.ObjType = 18 and t0.GstTransType = 'GD'
or t0.ObjType = 19 and t0.GstTransType = 'GA' and BaseType <>
204)
and BpRegistered = 'Y'
and (BpGSTN <> LocGSTN)
and (t0.ItemTaxType <> 'GN' or t0.PureNilRateDoc = 'N')
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

set @numOfGSTN = (select count(distinct t0.BpGSTN)from


GST_Report_GSTR2_CDNR_Tab t0)
select @numOfInv = count(t0.DocNum),
@docTotalSum = sum(case when NoteType = 'C' then -DocTotal
else DocTotal
end)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal,
NoteType from GST_Report_GSTR2_CDNR_Tab) t0

set @numOfOrigInv = (select count( distinct(OrigRefNo)) from


GST_Report_GSTR2_CDNR_Tab where OrigRefNo <> '')

select @taxiableSum = sum(case when NoteType = 'C' then -BaseSum


else BaseSum
end),
@IGSTSum = sum(case when NoteType = 'C' then -IGSTSum
else IGSTDeductSum
end),
@CGSTSum = sum(case when NoteType = 'C' then -CGSTSum
else CGSTDeductSum
end),
@SGSTSum = sum(case when NoteType = 'C' then -SGSTSum
else SGSTDeductSum
end),
@cessSum = sum(case when NoteType = 'C' then -CessSum
else CessSum
end),
@IGSTITCSum = sum(case when NoteType = 'C' then -IGSTDeductSum
else IGSTDeductSum
end),
@CGSTITCSum = sum(case when NoteType = 'C' then -CGSTDeductSum
else CGSTDeductSum
end),
@SGSTITCSum = sum(case when NoteType = 'C' then -SGSTDeductSum
else SGSTDeductSum
end),
@CessITCSum = sum(case when NoteType = 'C' then -CessDeductSum
else CessDeductSum
end)
from GST_Report_GSTR2_CDNR_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfGSTN as "No. of Supplier",
@numOfInv as "No. of Notes/Vouchers",
@numOfOrigInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Note/Voucher Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@CGSTSum, 0)) as "Total Central
Tax Paid",
convert(numeric(19, 2), isnull(@SGSTSum, 0)) as "Total
TState/UT Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total
Availed ITC Integrated Tax ",
convert(numeric(19, 2), isnull(@CGSTITCSum, 0)) as "Total
Availed ITC Central Tax",
convert(numeric(19, 2), isnull(@SGSTITCSum, 0)) as "Total
Availed ITC State/UT Tax ",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total
Availed ITC Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select BpGSTN as "GSTIN of Supplier",
TaxInvNo as "Note/Refund Voucher Number",
case when TaxInvDate = CONVERT(datetime, '') or TaxInvDate =
'' or TaxInvDate is null then ''
else replace(CONVERT(Varchar(20), TaxInvDate, 106), ' ', '-')
end as "Note/Refund Voucher date",
OrigRefNo as "Invoice/Advance Payment Voucher Number",
case when OrigRefDate = CONVERT(datetime, '') or OrigRefDate =
'' or OrigRefDate is null then ''
else replace(CONVERT(Varchar(20), OrigRefDate, 106), ' ', '-')
end as "Invoice/Advance Payment Voucher date",
case when OrigRefDate < '20170701' then 'Y'
else 'N'end as "Pre GST",
NoteType as "Document Type",
isnull(Comments, '') as "Reason For Issuing document",
case when IsIntraState = 'Y' then 'Intra State'
else 'Inter State' end as "Supply Type",
convert(numeric(19, 2), DocTotal) as "Note/Refund Voucher
Value",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTSum))) as
"Central Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTSum))) as
"State/UT Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTDeductSum)))
as "Availed ITC Central Tax",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTDeductSum)))
as "Availed ITC State/UT Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_CDNR_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by ObjType, GstTransType, BpGSTN, DocEntry, TaxInvNo, TaxInvDate,
VatPercentSum, OrigRefDate, OrigRefNo, Comments, IsIntraState, DocTotal, NoteType,
t3.Name
order by BpGSTN, TaxInvNo, TaxInvDate, OrigRefNo, OrigRefDate,
NoteType, Comments, IsIntraState, DocTotal, "Rate"
end
end

go

IF object_id('GST_Report_GSTR2_CDNUR','p') is not null


drop procedure GST_Report_GSTR2_CDNUR
IF object_id('GST_Report_GSTR2_CDNUR_Tab','u') is not null
drop table GST_Report_GSTR2_CDNUR_Tab

go
create procedure GST_Report_GSTR2_CDNUR(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfGSTN integer


declare @numOfInv integer
declare @numOfOrigInv integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @CGSTSum numeric(19, 6)
declare @SGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CGSTITCSum numeric(19, 6)
declare @SGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)

IF object_id('GST_Report_GSTR2_CDNUR_Tab','u') is not null


drop table GST_Report_GSTR2_CDNUR_Tab

select t0.*, t1.NoteType into GST_Report_GSTR2_CDNUR_Tab from GST_ODOC_Result


t0
left join GST_MD_NoteType t1 on t0.ObjType = t1.ObjType and t0.GstTransType =
t1.GstTransType
where (t0.ObjType = 18 and t0.GstTransType = 'GD'
or t0.ObjType = 19 and BaseType <> 204)
and (BpRegistered = 'N' and Reverse_Charge = 'Y'
or BpRegistered = 'Y' and BpGSTN = LocGSTN)
and IsGSTTax = 'Y'
and (t0.ItemTaxType <> 'GN' or t0.PureNilRateDoc = 'N')
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

set @numOfGSTN = (select count(distinct t0.BpGSTN)from


GST_Report_GSTR2_CDNUR_Tab t0)
select @numOfInv = count(t0.DocNum),
@docTotalSum = sum(case when NoteType = 'C' then -DocTotal
else DocTotal
end)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal,
NoteType from GST_Report_GSTR2_CDNUR_Tab) t0

set @numOfOrigInv = (select count( distinct(OrigRefNo)) from


GST_Report_GSTR2_CDNUR_Tab where OrigRefNo <> '')

select @taxiableSum = sum(case when NoteType = 'C' then -BaseSum


else BaseSum
end),
@IGSTSum = sum(case when NoteType = 'C' then -IGSTSum
else IGSTDeductSum
end),
@CGSTSum = sum(case when NoteType = 'C' then -CGSTSum
else CGSTDeductSum
end),
@SGSTSum = sum(case when NoteType = 'C' then -SGSTSum
else SGSTDeductSum
end),
@cessSum = sum(case when NoteType = 'C' then -CessSum
else CessSum
end),
@IGSTITCSum = sum(case when NoteType = 'C' then -IGSTDeductSum
else IGSTDeductSum
end),
@CGSTITCSum = sum(case when NoteType = 'C' then -CGSTDeductSum
else CGSTDeductSum
end),
@SGSTITCSum = sum(case when NoteType = 'C' then -SGSTDeductSum
else SGSTDeductSum
end),
@CessITCSum = sum(case when NoteType = 'C' then -CessDeductSum
else CessDeductSum
end)
from GST_Report_GSTR2_CDNUR_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No of Notes/Vouchers",
@numOfOrigInv as "No. of Invoices",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Note/Refund Voucher Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@CGSTSum, 0)) as "Total Central
Tax Paid",
convert(numeric(19, 2), isnull(@SGSTSum, 0)) as "Total
TState/UT Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess
Paid",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total ITC
Integrated Tax Amount ",
convert(numeric(19, 2), isnull(@CGSTITCSum, 0)) as "Total
Central Tax Amount",
convert(numeric(19, 2), isnull(@SGSTITCSum, 0)) as "Total ITC
State/UT Tax Amount",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total ITC
Cess Amount"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select DocNum as "Note/Voucher Number",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Note/Voucher date",
OrigRefNo as "Invoice/Advance Payment Voucher number",
case when OrigRefDate = CONVERT(datetime, '') or OrigRefDate =
'' or OrigRefDate is null then ''
else replace(CONVERT(Varchar(20), OrigRefDate, 106), ' ', '-')
end as "Invoice/Advance Payment Voucher date",
case when OrigRefDate < '20170701' then 'Y'
else 'N'end as "Pre GST",
NoteType as "Document Type",
isnull(Comments, '') as "Reason For Issuing document",
case when IsIntraState = 'Y' then 'Intra State'
else 'Inter State' end as "Supply Type",
convert(numeric(19, 2), DocTotal) as "Note/Voucher Value",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTSum))) as
"Central Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTSum))) as
"State/UT Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CGSTDeductSum)))
as "Availed ITC Central Tax",
convert(nvarchar, convert(numeric(19, 2), sum(SGSTDeductSum)))
as "Availed ITC State/UT Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_CDNUR_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by ObjType, GstTransType, BpGSTN, DocEntry, DocNum, DocDate,
VatPercentSum, OrigRefDate, OrigRefNo, Comments, IsIntraState, DocTotal, NoteType,
t3.Name
order by BpGSTN, DocNum, DocDate, OrigRefNo, OrigRefDate, NoteType,
Comments, IsIntraState, DocTotal, "Rate"
end
end

go

IF object_id('GST_Report_GSTR2_IMP_G','p') is not null


drop procedure GST_Report_GSTR2_IMP_G

IF object_id('GST_Report_GSTR2_IMP_G_Tab','u') is not null


drop table GST_Report_GSTR2_IMP_G_Tab
go
create procedure GST_Report_GSTR2_IMP_G(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @numOfBill integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)
IF object_id('GST_Report_GSTR2_IMP_G_Tab','u') is not null
drop table GST_Report_GSTR2_IMP_G_Tab

select t0.* into GST_Report_GSTR2_IMP_G_Tab from GST_ODOC_Result_GST_TAX_ONLY


t0
where (t0.ObjType = 18 and t0.GstTransType <> 'GD')
and t0.ImpOrExp = 'Y'
and (t0.BpRegistered = 'N' or t0.BpRegistered = 'Y' and '' = 'E')
and DocType = 'I'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(DocTotal)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal from
GST_Report_GSTR2_IMP_G_Tab) t0

set @numOfBill = (select count(distinct(ImpExpNo)) from


GST_Report_GSTR2_IMP_G_Tab where ImpExpNo <> '')

select @taxiableSum = sum(BaseSum),


@IGSTSum = sum(IGSTSum),
@cessSum = sum(CessSum),
@IGSTITCSum = sum(IGSTDeductSum),
@CessITCSum = sum(CessDeductSum)
from GST_Report_GSTR2_IMP_G_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfBill as "No. of Bill of Entry",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Bill of Entry Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess
Paid",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total
Availed ITC Integrated Tax ",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total
Availed ITC Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select --t0.PortCode as "Port Code",
t0.ImpExpNo as "Bill Of Entry Number",
case when t0.ImpExpDate = CONVERT(datetime, '') or
t0.ImpExpDate = '' or t0.ImpExpDate is null then ''
else replace(CONVERT(Varchar(20), t0.ImpExpDate, 106), ' ',
'-')
end as "Bill Of Entry Date",
convert(numeric(19, 2), t2.BillValue) as "Bill Of Entry
Value",
-- isnull(t1.Name, '') as "Document type",
isnull(BpGSTN, '') as "GSTIN Of SEZ Supplier",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_IMP_G_Tab t0
inner join (select sum(BaseSum) as BillValue, ObjType, GstTransType,
DocEntry
from GST_Report_GSTR2_IMP_G_Tab
group by ObjType, GstTransType, DocEntry
) t2 on t0.ObjType = t2.ObjType and t0.GstTransType =
t2.GstTransType and t0.DocEntry = t2.DocEntry
--left join GST_MD_ImportType t1 on t0.ExportType = t1.ImportType
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by t0.GstTransType, t0.DocEntry, ImpExpNo, ImpExpDate,
t2.BillValue, --PortCode, t1.Name,
BpGSTN, VatPercentSum, t3.Name
order by-- PortCode,
ImpExpNo, ImpExpDate, t2.BillValue, "Rate", BpGSTN
end
end

go

IF object_id('GST_Report_GSTR2_IMP_S','p') is not null


drop procedure GST_Report_GSTR2_IMP_S

IF object_id('GST_Report_GSTR2_IMP_S_Tab','u') is not null


drop table GST_Report_GSTR2_IMP_S_Tab
go
create procedure GST_Report_GSTR2_IMP_S(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @numOfInv integer


declare @numOfBill integer
declare @docTotalSum numeric(19, 6)
declare @taxiableSum numeric(19, 6)
declare @IGSTSum numeric(19, 6)
declare @cessSum numeric(19, 6)
declare @IGSTITCSum numeric(19, 6)
declare @CessITCSum numeric(19, 6)

IF object_id('GST_Report_GSTR2_IMP_S_Tab','u') is not null


drop table GST_Report_GSTR2_IMP_S_Tab

select t0.* into GST_Report_GSTR2_IMP_S_Tab from GST_ODOC_Result_GST_TAX_ONLY


t0
where t0.ObjType = 18 and t0.GstTransType <> 'GD'
and t0.ImpOrExp = 'Y'
and (t0.BpRegistered = 'N' or t0.BpRegistered = 'Y' and '' = 'E')
and t0.DocType = 'S'
and t0.DocDate >= @fromDate
and t0.DocDate <= @toDate
and t0.LocGSTN = @locGSTN

select @numOfInv = count(t0.DocNum),


@docTotalSum = sum(DocTotal)
from (select distinct ObjType, GstTransType, DocEntry, DocNum, DocTotal from
GST_Report_GSTR2_IMP_S_Tab) t0

set @numOfBill = (select count(distinct(ImpExpNo)) from


GST_Report_GSTR2_IMP_S_Tab where ImpExpNo <> '')

select @taxiableSum = sum(BaseSum),


@IGSTSum = sum(IGSTSum),
@cessSum = sum(CessSum),
@IGSTITCSum = sum(IGSTDeductSum),
@CessITCSum = sum(CessDeductSum)
from GST_Report_GSTR2_IMP_S_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @numOfInv as "No. of Invoices (Of Reg Recipient)",
convert(numeric(19, 2), isnull(@docTotalSum, 0)) as "Total
Invoice Value",
convert(numeric(19, 2), isnull(@taxiableSum, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@IGSTSum, 0)) as "Total
Integrated Tax Paid",
convert(numeric(19, 2), isnull(@cessSum, 0)) as "Total Cess
Paid",
convert(numeric(19, 2), isnull(@IGSTITCSum, 0)) as "Total
Availed ITC Integrated Tax ",
convert(numeric(19, 2), isnull(@CessITCSum, 0)) as "Total
Availed ITC Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select DocNum as "Invoice Number of Reg Recipient",
case when DocDate = CONVERT(datetime, '') or DocDate = '' or
DocDate is null then ''
else replace(CONVERT(Varchar(20), DocDate, 106), ' ', '-')
end as "Invoice Date",
convert(numeric(19, 2), DocTotal) as "Invoice Value",
isnull(t2.PoS, '') as "Place Of Supply",
convert(numeric(19, 2), VatPercentSum) as "Rate",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTSum))) as
"Integrated Tax Paid",
convert(nvarchar, convert(numeric(19, 2), sum(CessSum))) as
"Cess Paid",
t3.Name as "Eligibility For ITC",
convert(nvarchar, convert(numeric(19, 2), sum(IGSTDeductSum)))
as "Availed ITC Integrated Tax",
convert(nvarchar, convert(numeric(19, 2), sum(CessDeductSum)))
as "Availed ITC Cess"
from GST_Report_GSTR2_IMP_S_Tab t0
left join GST_MD_PoS t2 on t0.PoSGSTN = t2.StateGSTN
left join GST_MD_MaterialType t3 on ((t0.NonDeductable = 'Y' and
t0.NonDeductable = t3.NonDeductable)
or
(t0.NonDeductable = 'N' and t0.NonDeductable = t3.NonDeductable and t0.MaterialType
= t3.MaterialType and t0.ItemTaxType = t3.ItemTaxType))
group by GstTransType, DocEntry, DocNum, DocDate, DocTotal, t2.PoS,
VatPercentSum, t0.NonDeductable, t3.Name
order by DocNum, DocDate, DocTotal, t2.PoS, "Rate"
end
end

go

IF object_id('GST_Report_GSTR2_EXEMP','p') is not null


drop procedure GST_Report_GSTR2_EXEMP
go

IF object_id('GST_Report_GSTR2_EXEMP_Tab','u') is not null


drop table GST_Report_GSTR2_EXEMP_Tab

go
create procedure GST_Report_GSTR2_EXEMP(
@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalNilSum numeric(19, 6)


declare @totalExemptSum numeric(19, 6)
declare @totalNonGSTSum numeric(19, 6)
declare @totalComLevy numeric(19, 6)

IF object_id('GST_Report_GSTR2_EXEMP_Tab','u') is not null


drop table GST_Report_GSTR2_EXEMP_Tab

select sum(BaseSum) as BaseSum,


ItemTaxType,
IsIntraState,
BpRegistered
into GST_Report_GSTR2_EXEMP_Tab
from (select distinct DocEntry, LineNum,
case when ObjType = 19 then -BaseSum
else BaseSum
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge t0
where ObjType in (18, 19)
and ItemTaxType = 'GN'
and PureNilRateDoc = 'Y'
and BpGSTType <> 3
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN
union all
select distinct DocEntry, LineNum,
case when ObjType = 19 then -BaseSum
else BaseSum
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge
where ObjType in (18, 19)
and ItemTaxType = 'GE'
and BpGSTType <> 3
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN
union all
select distinct DocEntry, -1,
case when ObjType = 19 then -DocTotal
else DocTotal
end as BaseSum, ItemTaxType, IsIntraState, BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge
where ObjType in (18, 19)
and ItemTaxType = 'NN'
and BpGSTType <> 3
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN
union all
select distinct DocEntry, LineNum,
case when ObjType = 19 then -BaseSum
else BaseSum
end as BaseSum, 'CL' as ItemTaxType, IsIntraState,
BpRegistered
from GST_ODOC_Result_Ignore_RvsCharge
where ObjType in (18, 19)
and BpGSTType = 3
and ItemTaxType in ('GR', 'GE', 'GN', 'NN')
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN) t0
group by IsIntraState, BpRegistered, ItemTaxType

select @totalNilSum = sum(BaseSum) from GST_Report_GSTR2_EXEMP_Tab where


ItemTaxType = 'GN'
select @totalExemptSum = sum(BaseSum) from GST_Report_GSTR2_EXEMP_Tab where
ItemTaxType = 'GE'
select @totalNonGSTSum = sum(BaseSum) from GST_Report_GSTR2_EXEMP_Tab where
ItemTaxType = 'NN'
select @totalComLevy = sum(BaseSum) from GST_Report_GSTR2_EXEMP_Tab where
ItemTaxType = 'CL'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(@totalComLevy, 0)) as "Total
Composition taxable person",
convert(numeric(19, 2), isnull(@totalNilSum, 0)) as "Total Nil
Rated Supplies",
convert(numeric(19, 2), isnull(@totalExemptSum, 0)) as "Total
Exempted Supplies",
convert(numeric(19, 2), isnull(@totalNonGSTSum, 0)) as "Total
Non-GST Supplies"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select "Description",
CompLevy as "Composition taxable person",
NilRated as "Nil Rated Supplies",
Exempted as "Exempted (other than nil rated/non GST supply )",
NonGST as "Non-GST supplies"
from
(select 'Inter-State supplies' as "Description",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'CL' then BaseSum else 0 end), 0)) as "CompLevy",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)) as "NilRated",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)) as "Exempted",
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)) as "NonGST",
1 as TabOrder
from GST_Report_GSTR2_EXEMP_Tab
where IsIntraState = 'N'
union
select 'Intra-State supplies',
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'CL' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GN' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'GE' then BaseSum else 0 end), 0)),
convert(numeric(19, 2), isnull(sum(case when ItemTaxType =
'NN' then BaseSum else 0 end), 0)),
2
from GST_Report_GSTR2_EXEMP_Tab
where IsIntraState = 'Y') t0
order by TabOrder
end
end

go

IF object_id('GST_Report_GSTR2_HSN','p') is not null


drop procedure GST_Report_GSTR2_HSN

IF object_id('GST_Report_GSTR2_HSN_Tab','u') is not null


drop table GST_Report_GSTR2_HSN_Tab
go

create procedure GST_Report_GSTR2_HSN(


@fromDate date,
@toDate date,
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @hsnCount integer


declare @totalValue numeric(19, 6)
declare @totalTaxable numeric(19, 6)
declare @totalIGST numeric(19, 6)
declare @totalCGST numeric(19, 6)
declare @totalSGST numeric(19, 6)
declare @totalCess numeric(19, 6)

IF object_id('GST_Report_GSTR2_HSN_Tab','u') is not null


drop table GST_Report_GSTR2_HSN_Tab

select sum(Quantity) as Quantity,


round(sum(BaseSum), 2) as BaseSum,
round(sum(IGSTSum), 2) as IGSTSum,
round(sum(CGSTSum), 2) as CGSTSum,
round(sum(SGSTSum), 2) as SGSTSum,
round(sum(CessSum), 2) as CessSum,
UomEntry, SacEntry, HsnCode, HsnName, UQC
into GST_Report_GSTR2_HSN_Tab
from ( select case when DocType = 'S' or isnull(t1.ItemClass, '') = '1'
then 0
when t0.ObjType = 19 then -t0.Quantity
else t0.Quantity
end as Quantity,
case when t0.ObjType = 19 then -t0.BaseSum
else t0.BaseSum
end as BaseSum,
case when t0.ObjType = 19 then -t0.IGSTSum
else t0.IGSTSum
end as IGSTSum,
case when t0.ObjType = 19 then -t0.CGSTSum
else t0.CGSTSum
end as CGSTSum,
case when t0.ObjType = 19 then -t0.SGSTSum
else t0.SGSTSum
end as SGSTSum,
case when t0.ObjType = 19 then -t0.CessSum
else t0.CessSum
end as CessSum,
t0.UomEntry,
t0.SacEntry,
case when DocType = 'S' or isnull(t1.ItemClass, '') =
'1' or ItemTaxType = 'F' then isnull(t5.ServCode, '')
else isnull(t2.ChapterID, '')
end as HsnCode,
case when DocType = 'S' or isnull(t1.ItemClass, '') =
'1' or ItemTaxType = 'F' then isnull(t5.ServName, '')
else isnull(t2.Dscription, '')
end as HsnName,
case when isnull(t0.UomEntry, 0) = 0 or t0.UomEntry = -1
then t0.UomName
else t3.UomName
end as UQC,
t0.ObjType,
t0.GstTransType,
t0.DocEntry
fromGST_ODOC_Result_GST_TAX_ONLY t0
leftjoin OITM t1 on DocType = 'I' and t0.ItemCode = t1.ItemCode
leftjoin OEXD t4 on t0.HeaderFreightCode > 0 and
t0.HeaderFreightCode =t4.ExpnsCode
leftjoin OCHP t2 on isnull(t1.ChapterID, 0) > 0 and t2.AbsEntry
= t1.ChapterID
left join OSAC t5 on ((isnull(t0.SacEntry, 0) <> 0 and
t0.SacEntry <> -1 and t0.SacEntry = t5.AbsEntry)
or (isnull(t4.SacCode, '') <>
'' and t4.SacCode = t5.ServCode))
left join OUOM t3 on isnull(t0.UomEntry, 0) > 0 and t0.UomEntry =
t3.UomEntry
where t0.ObjType in (18, 19)
and DocDate >= @fromDate
and DocDate <= @toDate
and LocGSTN = @locGSTN) t0
group by ObjType, GstTransType, DocEntry, UomEntry, SacEntry, HsnCode,
HsnName, UQC

select @hsnCount = count(distinct HsnCode) from GST_Report_GSTR2_HSN_Tab


where isnull(HsnCode, '') <> ''

select @totalValue = sum(BaseSum + IGSTSum + CGSTSum + SGSTSum + CessSum),


@totalTaxable = sum(BaseSum),
@totalIGST = sum(IGSTSum),
@totalCGST = sum(CGSTSum),
@totalSGST = sum(SGSTSum),
@totalCess = sum(CessSum)
from GST_Report_GSTR2_HSN_Tab

if(@dspType = 'B' or @dspType = 'S')


begin
select @hsnCount as "No. of HSN",
convert(numeric(19, 2), isnull(@totalValue, 0)) as "Total
Value",
convert(numeric(19, 2), isnull(@totalTaxable, 0)) as "Total
Taxable Value",
convert(numeric(19, 2), isnull(@totalIGST, 0)) as "Total
Integrated Tax",
convert(numeric(19, 2), isnull(@totalCGST, 0)) as "Total
Central Tax",
convert(numeric(19, 2), isnull(@totalSGST, 0)) as "Total
State/UT Tax",
convert(numeric(19, 2), isnull(@totalCess, 0)) as "Total Cess"
end

if(@dspType = 'B' or @dspType = 'D')


begin
select replace(HsnCode, '.', '') as "HSN",
isnull(HsnName, '') as "Descripstion",
isnull(UQC, '') as UQC,
convert(nvarchar, convert(numeric(19, 2), sum(Quantity))) as
"Total Quantity",
convert(numeric(19, 2), sum(BaseSum + IGSTSum + CGSTSum +
SGSTSum + CessSum)) as "Total Value",
convert(numeric(19, 2), sum(BaseSum)) as "Taxable Value",
convert(numeric(19, 2), sum(IGSTSum)) as "Integrated Tax
Amount",
convert(numeric(19, 2), sum(CGSTSum)) as "Central Tax Amount",
convert(numeric(19, 2), sum(SGSTSum)) as "State/UT Tax
Amount",
convert(numeric(19, 2), sum(CessSum)) as "Cess Amount"
from GST_Report_GSTR2_HSN_Tab
group by HsnCode, HsnName, UomEntry, UQC
order by HsnCode, HsnName, UQC
end
end

go

IF object_id('GST_Report_GSTR2_AT','p') is not null


drop procedure GST_Report_GSTR2_AT

IF object_id('GST_Report_GSTR2_AT_Tab','u') is not null


drop table GST_Report_GSTR2_AT_Tab
go

create procedure GST_Report_GSTR2_AT(


@year nvarchar(4),
@month nvarchar(2),
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin

if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalAdvRec numeric(19, 6)


declare @totalCess numeric(19, 6)

IF object_id('GST_Report_GSTR2_AT_Tab','u') is not null


drop table GST_Report_GSTR2_AT_Tab

select t0.PaidNet - isnull(t1.LinkedNet, 0) as PaidNet,


t0.PaidCess - isnull(t1.LinkedCess, 0) as PaidCess,
t0.DocEntry,
t0.VatPercent,
t2.BPStatGSTN as BpStateGSTN
into GST_Report_GSTR2_AT_Tab
from GST_TAX_PAYMENT_SUMMARY t0
inner join DPO12 t2 on t0.DocEntry = t2.DocEntry
left join GST_TAX_DPM_SUMMARY t1 on t0.DocEntry = t1.LinkedEntry and
t0.VatPercent = t1.VatPercent
and t0.PaymentYear = t1.DocYear and t0.PaymentMonth
= t1.DocMonth
and t0.Direction = t1.Direction
where t0.PaymentYear = @year
and t0.PaymentMonth = @month
and t2.LocGSTN = @locGSTN
and t2.BpCountry = 'IN'
and t0.Direction = 'AP'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(sum(t0.PaidNet), 0)) as "Total
Advance Paid",
convert(numeric(19, 2), isnull(sum(t0.PaidCess), 0)) as "Total
Cess Amount"
from GST_Report_GSTR2_AT_Tab t0
where Round(t0.PaidNet, 2) > 0
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t2.PoS, '') as "Place Of Supply",
t0.VatPercent as "Rate",
convert(numeric(19, 2), isnull(sum(t0.PaidNet), 0)) as "Gross
Advance Paid",
convert(numeric(19, 2), isnull(sum(t0.PaidCess), 0)) as "Cess
Amount"
from GST_Report_GSTR2_AT_Tab t0
left join GST_MD_PoS t2 on t0.BpStateGSTN = t2.StateGSTN
where Round(PaidNet, 2) > 0
group by t2.PoS, t0.VatPercent
order by PoS, "Rate", "Gross Advance Paid", "Cess Amount"
end
end

go

IF object_id('GST_Report_GSTR2_AtAdj','p') is not null


drop procedure GST_Report_GSTR2_AtAdj

IF object_id('GST_Report_GSTR2_AtAdj_Tab','u') is not null


drop table GST_Report_GSTR2_AtAdj_Tab
go

create procedure GST_Report_GSTR2_AtAdj(


@year nvarchar(4),
@month nvarchar(2),
@locGSTN nvarchar(15),
@dspType nvarchar(1) = 'D')
as
begin
if(@dspType <> 'B' and @dspType <> 'S' and @dspType <> 'D')
begin
set @dspType = 'D'
end

declare @totalAdvRec numeric(19, 6)


declare @totalCess numeric(19, 6)

IF object_id('GST_Report_GSTR2_AtAdj_Tab','u') is not null


drop table GST_Report_GSTR2_AtAdj_Tab

select t0.LinkedNet - isnull(t1.PaidNet, 0) as AdjustNet,


t0.LinkedCess - isnull(t1.PaidCess, 0) as AdjustCess,
t0.LinkedEntry,
t0.VatPercent,
t2.BPStatGSTN as BpStateGSTN
into GST_Report_GSTR2_AtAdj_Tab
from GST_TAX_DPM_SUMMARY t0
inner join DPO12 t2 on t0.LinkedEntry = t2.DocEntry
left join GST_TAX_PAYMENT_SUMMARY t1 on t1.DocEntry = t0.LinkedEntry and
t0.VatPercent = t1.VatPercent
and t1.PaymentYear = t0.DocYear and t1.PaymentMonth
= t0.DocMonth
and t0.Direction = t1.Direction
where t0.DocYear = @year
and t0.DocMonth = @month
and t2.LocGSTN = @locGSTN
and t2.BpCountry = 'IN'
and t0.Direction = 'AP'

if(@dspType = 'B' or @dspType = 'S')


begin
select convert(numeric(19, 2), isnull(sum(t0.AdjustNet), 0)) as "Total
Advance Adjusted",
convert(numeric(19, 2), isnull(sum(t0.AdjustCess), 0)) as
"Total Cess"
from GST_Report_GSTR2_AtAdj_Tab t0
where round(t0.AdjustNet, 2) > 0
end

if(@dspType = 'B' or @dspType = 'D')


begin
select isnull(t2.PoS, '') as "Place Of Supply",
t0.VatPercent as "Rate",
convert(numeric(19, 2), isnull(sum(t0.AdjustNet), 0)) as
"Gross Advance Paid to be Adjusted",
convert(numeric(19, 2), isnull(sum(t0.AdjustCess), 0)) as
"Cess Adjusted"
from GST_Report_GSTR2_AtAdj_Tab t0
left join GST_MD_PoS t2 on t0.BpStateGSTN = t2.StateGSTN
where round(AdjustNet, 2) > 0
group by t2.PoS, t0.VatPercent
order by PoS, "Rate", "Gross Advance Paid to be Adjusted", "Cess
Adjusted"
end
end

go

S-ar putea să vă placă și