Declare @datefrom datetime;
Declare @dateto datetime;
declare @gm int, @gl int;
--Set @datefrom = {?datefrom}
--Set @dateto = {?dateto}
Set @datefrom = '7-1-2011'
Set @dateto = '10-31-2015'
set @gm = 1
set @gl = 16
Select
a.TransType,
b.CompnyName,
b.CompnyAddr,
a.AcctName,
a.DocDate 'Posting Date',
a.Ref1 'Ref No.',
a.NumAtCard 'Invoice No.',
a.Comments 'Detail',
a.Balance 'Balance (LC)',
a.DocRate 'Forex Rate',
Case
When a.DocCur = 'AUD' Then a.[FC Balance]
End 'Balance (AUD)',
Case
When a.DocCur = 'USD' Then a.[FC Balance]
End 'Balance (USD)',
Case
When a.[FC Balance]= 0 then a.Balance
else
a.[FC Balance]*(select top 1 Rate from ORTT where Currency = a.DocCur and RateDate = @dateto order by ratedate asc) End 'Revalued Amount',
Case when a.[FC Balance]=0 then null
else a.Balance - (a.[FC Balance]*(select top 1 Rate from ORTT where Currency = a.DocCur and RateDate = @dateto order by ratedate asc)) End 'Forex Clearing',
a.[FC Balance] 'Outstanding Balance (FC)',
a.Balance 'Outstanding Balance (PHP)',
a.CardName,
Case --Transaction Definition
When a.TransType = 13 Then 'AR Inv'
When a.TransType = 14 Then 'AR CM'
When a.TransType = 18 Then 'AP Inv'
When a.TransType = 19 Then 'AP CM'
When a.TransType = 21 Then 'GR'
When a.TransType = 20 Then 'GRPO'
When a.TransType = 24 Then 'In Pay'
When a.TransType = 30 Then 'JE'
End As 'Trans Def',
a.TransId,
Case When c.Currency = 'AUD' then Rate End 'AUD MER',
Case When c.Currency = 'USD' then Rate End 'USD MER',
a.DocNum
From
(select
a.TransId,
a.DocDate,
a.CardName,
c.AcctName,
a.Comments,
b.BalDueDeb-b.BalDueCred 'Balance',
b.BalFcDeb-b.BalFcCred 'FC Balance',
a.DocCur,
b.TransType,
a.DocRate,
b.Ref1,
a.NumAtCard,
a.docnum
from oinv a
left join jdt1 b on a.TransId = b.TransId
left join oact c on c.AcctCode = b.Account
--left join INV1 d on d.docentry = a.DocEntry
where
--DocCur <> 'PHP'
--and
GroupMask = @gm and GrpLine = @gl and b.BalDueDeb-b.BalDueCred <> 0
and a.docdate between @datefrom and @dateto
union all
select
a.TransId,
a.DocDate,
a.CardName,
c.AcctName,
a.Comments,
b.BalDueDeb-b.BalDueCred 'Balance',
b.BalFcDeb-b.BalFcCred 'FC Balance',
a.DocCur,
b.TransType,
a.DocRate,
b.Ref1,
a.NumAtCard,
a.docnum
from orin a
left join jdt1 b on a.TransId = b.TransId
left join oact c on c.AcctCode = b.Account
--left join INV1 d on d.docentry = a.DocEntry
where
--DocCur <> 'PHP'
--and
GroupMask = @gm and GrpLine = @gl and b.BalDueDeb-b.BalDueCred <> 0
and a.docdate between @datefrom and @dateto
union all
select
a.TransId,
a.DocDate,
a.CardName,
c.AcctName,
a.Comments,
b.BalDueDeb-b.BalDueCred 'Balance',
b.BalFcDeb-b.BalFcCred 'FC Balance',
a.DocCurr,
b.TransType,
a.DocRate,
b.Ref1,
a.Comments,
a.docnum
from orct a
left join jdt1 b on a.TransId = b.TransId
left join oact c on c.AcctCode = b.Account
where
GroupMask = @gm and GrpLine = @gl
and a.DocDate between @datefrom and @dateto
and b.BalDueDeb-b.BalDueCred<>0
union all
select
a.TransId,
a.RefDate,
d.CardName,
b.AcctName,
a.LineMemo,
a.BalDueDeb-a.BalDueCred ' Balance',
a.BalFcDeb-a.BalFcCred 'FC Balance',
e.TransCurr,
a.TransType,
f.Rate,
a.Ref1,
a.Ref2,
a.BaseRef
from jdt1 a
left join oact b on a.Account = b.AcctCode
left join oact c on a.ContraAct = c.AcctCode
left join ocrd d on a.shortname = d.CardCode
left join ojdt e on e.TransId = a.TransId
left join ortt f on f.Currency = e.TransCurr and f.RateDate = e.RefDate
where
a.RefDate between @datefrom and @dateto
and b.GroupMask = @gm and b.GrpLine = @gl
and a.BalDueDeb-a.BalDueCred <> 0
and a.TransType = '30'
and 1 = (case when c.GroupMask=7 and c.GrpLine=4 then 2 else 1 end)
)a
left join ortt c on a.DocCur = c.Currency and c.RateDate = @dateto, OADM b
-- The only problem with this query is that if there is an existing standalon JE for adjustment. If only I can identify those kind of JE then this one could be the answer.