Dear Experts,
Below the query which i used to get the Opening Stock,Purchase,Issue,Return and closing stock. Here i face some problem that i get negative inventory for some items and the same has been checked in inventory audit report but there it is showing correct value.And also i understand that those items which shows negative inventory in Opening and closing is previously got changed in Item group after some transactions.Can any one help me to sort out this issue ?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Declare @FromDate Datetime
Declare @ToDate Datetime
Declare @ItmsGrpNam varchar(100)
select @FromDate = min(S0.Docdate) from dbo.OINM S0 where S0.Docdate >= '[%0]'
select @ToDate = max(S1.Docdate) from dbo.OINM s1 where S1.Docdate <= '[%1]'
select @ItmsGrpNam = max(s2.ItmsGrpNam) from dbo.OITB S2 Where S2.ItmsGrpNam = '[%2]'
Select distinct a.Itemcode, max(a.Dscription) as ItemName, (Select i.InvntryUom from OITM i where i.ItemCode=a.Itemcode) as UOM,a.InvntAct,
(isnull(( Select distinct sum(isnull(inqty,0)) from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.DocDate<=@FromDate ),0)- isnull(( Select sum(isnull(outqty,0)) from OINM O1
where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct and O1.DocDate<=@FromDate),0)) as [Opening Stock],
(isnull((Select distinct isnull(sum(round(TransValue,0)),0) from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.DocDate<=@FromDate ),0)) as [Opening Stock Value],
isnull((Select ISNULL((sum(isnull(inqty,0))),0) from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.docdate>=@FromDate and O1.docdate<=@ToDate and O1.transtype in (20,18)),0) as [Purchase Quantity],
isnull((Select isnull(sum(round(TransValue,0)),0) from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.docdate>=@FromDate and O1.docdate<=@ToDate and O1.InQty>=0 and O1.JrnlMemo not like '%cancellation%'
and O1.transtype in (20,18)),0 ) as [Purchase Value],
isnull((Select sum(isnull(OutQty,0))from OINM O1 where O1.itemcode=a.ItemCode AND O1.InvntAct = a.InvntAct
and O1.ApplObj = '202' and O1.docdate>=@FromDate and O1.docdate<=@ToDate and O1.OutQty>0 and
O1.transtype = '60'),0) as [Production Issue Quantity],
isnull((Select isnull(sum(round(TransValue,0)),0)from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.ApplObj = '202' and O1.docdate>=@FromDate and O1.docdate<=@ToDate and O1.OutQty>0
and O1.transtype = '60'),0) as [Production Issue Value],
isnull((Select sum(isnull(Inqty,0)) from OINM O1 inner join OIGN G1 on O1.BASE_REF = G1.DocNum inner join IGN1 G2 on G1.DocEntry=G2.DocEntry
where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct and O1.ItemCode = G2.ItemCode
and G2.BaseType = '202' and G2.TranType is null and O1.docdate>=@FromDate
and O1.docdate<=@ToDate and O1.Inqty>0 and O1.transtype = '59'),0) as [Goods Receipt Return Quantity],
isnull((Select sum(isnull(Inqty,0)) from OINM O1
inner join OIGN G1 on O1.BASE_REF = G1.DocNum
inner join IGN1 G2 on G1.DocEntry=G2.DocEntry
--inner join OITM a on a.itemcode= o1.ItemCode
--inner join OACT c on c.AcctCode = o1.InvntAct
where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct and O1.ItemCode = a.ItemCode
and G2.LineNum = O1.DocLineNum and G2.BaseType = '202' and g2.TranType is null
and O1.docdate >= @FromDate and o1.DocDate <= @ToDate
and O1.Inqty>0 and O1.transtype = '59'),0)as [Goods Receipt Return Quantity_1],
isnull((Select sum(round(TransValue,0))from OINM O1 inner join OIGN G1 on O1.BASE_REF = G1.DocNum inner join IGN1 G2 on G1.DocEntry=G2.DocEntry
where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct and O1.ItemCode = G2.ItemCode
and G2.BaseType = '202' and G2.TranType is null and O1.docdate>=@FromDate
and O1.docdate<=@ToDate and O1.Inqty>0 and O1.transtype = '59'),0) as [Goods Receipt Return Value],
(isnull((Select distinct isnull(sum(isnull(inqty,0)),0)from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.DocDate<=@ToDate),0)- isnull((Select isnull(sum(isnull(outqty,0)),0) from OINM O1
where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct and
O1.DocDate<=@ToDate),0)) as [Closing Stock],
(isnull (( Select distinct isnull(sum(round(TransValue,0)),0)from OINM O1 where O1.itemcode=a.itemcode AND O1.InvntAct = a.InvntAct
and O1.DocDate<=@ToDate),0)) as [Closing Stock Value],
(Select distinct i.LastPurDat from OITM i where i.ItemCode=a.Itemcode) as 'Last Purchase Date',
(Select distinct b.ItmsGrpNam from OITB b where b.ItmsGrpCod = I1.ItmsGrpCod) as 'Group Name'
from (Select distinct N1.Itemcode, N1.Dscription, isnull((sum(N1.inqty)),0)-isnull((sum(n1.outqty)),0) as OpeningBalance,
isnull((sum(N1.Transvalue)),0) as OpeningValue,n1.InvntAct,0 as INq,
0 as OUT From dbo.OINM N1 inner join OITM i on i.ItemCode = N1.ItemCode
inner join OITB b on b.ItmsGrpCod = i.ItmsGrpCod
inner join OACT c on c.AcctCode = n1.InvntAct
Where N1.DocDate <=@FromDate and i.ItemCode = n1.ItemCode and b.ItmsGrpNam = @ItmsGrpNam and N1.Transtype <> '67'
Group By N1.ItemCode,N1.Dscription,n1.InvntAct
Union
select distinct N1.Itemcode, N1.Dscription, 0 as OpeningBalance,0 as OpeningValue,
n1.InvntAct,sum(N1.inqty) , 0 as OUT From dbo.OINM N1 inner join OITM i on i.ItemCode = N1.ItemCode
inner join OITB b on b.ItmsGrpCod = i.ItmsGrpCod
inner join OACT c on c.AcctCode = n1.InvntAct
Where N1.DocDate >= @FromDate and N1.DocDate<= @ToDate and N1.Inqty >0 and n1.ItemCode = i.ItemCode
and b.ItmsGrpNam = @ItmsGrpNam and N1.Transtype <> '67'
Group By N1.ItemCode,N1.Dscription,n1.InvntAct
Union
select distinct N1.Itemcode, N1.Dscription, 0 as OpeningBalance,0 as OpeningValue,n1.InvntAct,0,sum(N1.outqty) as OUT
From dbo.OINM N1 inner join OITM i on i.ItemCode = N1.ItemCode
inner join OITB b on b.ItmsGrpCod = i.ItmsGrpCod
Where N1.DocDate >= @FromDate and N1.DocDate <=@ToDate
and N1.OutQty > 0 and b.ItmsGrpNam = @ItmsGrpNam and n1.itemcode = i.itemcode and N1.Transtype <> '67'
Group By N1.ItemCode,N1.Dscription,n1.InvntAct)a,dbo.OITM I1 ,OITB b1
where a.ItemCode=I1.ItemCode and I1.ItmsGrpCod = b1.ItmsGrpCod
Group By a.Itemcode ,I1.ItmsGrpCod , b1.ItmsGrpNam,a.InvntAct
Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0
Order By a.Itemcode
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks & Regads,
Bhoopathi.K