begin
/* Faktury vydane */
for select
'03' as DocumentType,
E.Code as ESLIndicator,
sum(A.LocalTAmountWithoutVAT) as LocalAmount,
sum(A.TAmountWithoutVAT) as Amount,
F.VATIdentNumber as VATIdentNumber,
count(distinct P.ID) as DocumentCount
from IssuedInvoices2 A
inner join IssuedInvoices P on A.Parent_ID=P.ID
inner join Firms F on F.ID = P.Firm_ID
inner join ESLIndicators E on E.ID = A.ESLIndicator_ID
where (A.RowType in (1,2,3,4,5)) and (A.ToESL = 'A') and (P.TradeType = 2)
/* datum plneni */
and (P.VATDate$DATE >= :AVATDateFrom) and (P.VATDate$DATE < :AVATDateTo)
/* stredisko */
and ((:ADivisionsSelID = '')
or ((A.Division_ID in (select Bx.ID from Divisions Bx where Bx.Hidden = 'N'
and (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ADivisionsSelID))
or ((:ADivisionsWithChilds in ('1', 'A'))
and (Bx.ID in (select ID from SYS$Divisions2 where Superior_ID in
(select OBJ_ID from SELDAT where SEL_ID = :ADivisionsSelID))))))))
/* zakazka */
and ((:ABusOrdersSelID = '')
or (A.BusOrder_ID in (select Bx.ID from BusOrders Bx where Bx.Hidden = 'N'
and ((Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusOrdersSelID))
or ((:ABusOrdersWithChilds in ('1', 'A'))
and (Bx.ID in (select ID from SYS$BusOrders2 where Superior_ID in
(select OBJ_ID from SELDAT where SEL_ID = :ABusOrdersSelID))))))))
/* obch. pripad */
and ((:ABusTransactionsSelID = '')
or (A.BusTransaction_ID in (select Bx.ID from BusTransactions Bx where Bx.Hidden = 'N'
and ((Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusTransactionsSelID))
or ((:ABusTransactionsWithChilds in ('1', 'A'))
and (Bx.ID in (select ID from SYS$BusTransactions2 where Superior_ID in
(select OBJ_ID from SELDAT where SEL_ID = :ABusTransactionsSelID))))))))
/* projekt */
and ((:ABusProjectsSelID = '')
or (A.BusProject_ID in (select Bx.ID from BusProjects Bx where Bx.Hidden = 'N'
and ((Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusProjectsSelID))
or ((:ABusProjectsWithChilds in ('1', 'A'))
and (Bx.ID in (select ID from SYS$BusProjects2 where Superior_ID in
(select OBJ_ID from SELDAT where SEL_ID = :ABusProjectsSelID))))))))
/* firma */
and ((:AFirmSelID = '')
or (P.Firm_ID in (select OBJ_ID from SelDat where SEL_ID = :AFirmSelID))
or (P.Firm_ID in (select ID from Firms where Firm_ID in
(select OBJ_ID from SelDat where SEL_ID = :AFirmSelID))))
/* zeme DPH */
and (P.VATCountry_ID = :AVATCountry_ID)
/* vytvoril */
and ((:ACreatedBySelID = '')
or (P.CreatedBy_ID in (select Obj_ID from SelDat where Sel_ID = :ACreatedBySelID)))
/* opravil */
and ((:ACorrectedBySelID = '')
or (P.CorrectedBy_ID in (select Obj_ID from SelDat where Sel_ID = :ACorrectedBySelID)))
/* mena */
and ((:ACurrencySelID = '')
or (P.Currency_ID in (select Obj_ID from SelDat where Sel_ID = :ACurrencySelID)))
/* zeme */
and ((:ACountrySelID = '')
or (P.Country_ID in (select Obj_ID from SelDat where Sel_ID = :ACountrySelID)))
group by E.Code, F.VATIdentNumber
into :DocumentType, :ESLIndicator, :LocalAmount, :Amount, :VATIdentNumber, :DocumentCount
do begin
suspend;
end
end;