BEGIN
if (IDSelID <> '') then begin
for
select D.AccDocQueue_ID, D.Period_ID, D.OrdNumber, max(D.AccDate$DATE)
from SYS$AccRequests A
left join GeneralLedger D ON A.Row_ID = D.ID
where
(D.ID in (select OBJ_ID from SELDAT where SEL_ID=:IDSelID))
group by D.AccDocQueue_ID, D.Period_ID, D.OrdNumber
into AccDocQueue_ID, Period_ID, OrdNumber, AccDate$DATE
do begin
for
select D.ID
from SYS$AccRequests A, GeneralLedger D
where
A.Row_ID = D.ID and
D.AccDocQueue_ID = :AccDocQueue_ID and D.Period_ID = :Period_ID and D.OrdNumber = :OrdNumber and
(D.ID in (select OBJ_ID from SELDAT where SEL_ID=:IDSelID))
into ID
do begin
suspend;
end
end
end else begin
for
select D.AccDocQueue_ID, D.Period_ID, D.OrdNumber, max(D.AccDate$DATE)
from SYS$AccRequests A
left join GeneralLedger D ON A.Row_ID = D.ID
where
(:AccDateUsed <> 'A' or (D.AccDate$DATE >= :AccDateFrom AND D.AccDate$DATE < :AccDateTo)) and
(:TextUsed <> 'A' or D.Text like :Text ESCAPE '~') and
(:AccountCodeUsed <> 'A' or
D.DebitAccount_ID in (select ID from Accounts where Code like :AccountCode ESCAPE '~') or
D.CreditAccount_ID in (select ID from Accounts where Code like :AccountCode ESCAPE '~')) and
((:AccountIsIncomplete = '%') or
(('A' = :AccountIsIncomplete) and
((D.DebitAccount_ID in (select ID from Accounts where IsInComplete = 'A')) or
(D.CreditAccount_ID in (select ID from Accounts where IsInComplete = 'A')) or
((D.DebitAccount_ID is Null) or (D.CreditAccount_ID is null)))) or
(('N' = :AccountIsIncomplete) and
((D.DebitAccount_ID in (select ID from Accounts where IsInComplete = 'N')) and
(D.CreditAccount_ID in (select ID from Accounts where IsInComplete = 'N')) and
((D.DebitAccount_ID is not Null) and (D.CreditAccount_ID is not null))))) and
(:AmountUsed <> 'A' or D.Amount BETWEEN :AmountFrom and :AmountTo) and
(:AmountInCurrencyUsed <> 'A' or D.AmountInCurrency BETWEEN :AmountInCurrencyFrom and :AmountInCurrencyTo) and
(:OrdNumberUsed <> 'A' or D.OrdNumber BETWEEN :OrdNumberFrom and :OrdNumberTo) and
(:AccDocQueueSelID = '' or
D.AccDocQueue_ID in (select OBJ_ID from SELDAT where SEL_ID=:AccDocQueueSelID)) and
(:PeriodSelID = '' or
D.Period_ID in (select OBJ_ID from SELDAT where SEL_ID=:PeriodSelID)) and
(:FirmSelID = '' or
((D.Firm_ID in (select OBJ_ID from SELDAT where SEL_ID = :FirmSelID)) or
(D.Firm_ID in (SELECT ID FROM Firms WHERE Firm_ID in (select OBJ_ID from SELDAT where SEL_ID = :FirmSelID)))))
and
(:CurrencySelID = '' or
D.Currency_ID in (select OBJ_ID from SELDAT where SEL_ID=:CurrencySelID)) and
( (:DivisionSelID = '') OR
((:ADivisionsWithNull in ('1', 'A')) and (D.DebitDivision_ID IS NULL)) OR
(D.DebitDivision_ID IN (SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :DivisionSelID)) 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 = :DivisionSelID)))))) ) OR
((:ADivisionsWithNull in ('1', 'A')) and (D.CreditDivision_ID IS NULL)) OR
(D.CreditDivision_ID IN (SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :DivisionSelID)) 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 = :DivisionSelID)))))) )
) AND
( (:BusOrderSelID = '') OR
(D.DebitBusOrder_ID IN (SELECT Bx.ID FROM BusOrders Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusOrderSelID)) 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 = :BusOrderSelID)))))) ) OR
(D.CreditBusOrder_ID IN (SELECT Bx.ID FROM BusOrders Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusOrderSelID)) 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 = :BusOrderSelID)))))) )
) AND
( (:BusTransactionSelID = '') OR
(D.DebitBusTransaction_ID IN (SELECT Bx.ID FROM BusTransactions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusTransactionSelID)) 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 = :BusTransactionSelID)))))) ) OR
(D.CreditBusTransaction_ID IN (SELECT Bx.ID FROM BusTransactions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusTransactionSelID)) 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 = :BusTransactionSelID)))))) )
) AND
( (:BusProjectSelID = '') OR
(D.DebitBusProject_ID IN (SELECT Bx.ID FROM BusProjects Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusProjectSelID)) 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 = :BusProjectSelID)))))) ) OR
(D.CreditBusProject_ID IN (SELECT Bx.ID FROM BusProjects Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusProjectSelID)) 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 = :BusProjectSelID)))))) )
)
group by D.AccDocQueue_ID, D.Period_ID, D.OrdNumber
into AccDocQueue_ID, Period_ID, OrdNumber, AccDate$DATE
do begin
for
select D.ID
from SYS$AccRequests A, GeneralLedger D
where
A.Row_ID = D.ID and
D.AccDocQueue_ID = :AccDocQueue_ID and D.Period_ID = :Period_ID and D.OrdNumber = :OrdNumber and
((:AccountIsIncomplete = '%') or
(('A' = :AccountIsIncomplete) and
((D.DebitAccount_ID not in (select ID from Accounts where IsInComplete = 'N')) or
(D.CreditAccount_ID not in (select ID from Accounts where IsInComplete = 'N')) or
((D.DebitAccount_ID is Null) or (D.CreditAccount_ID is null)))) or
(('N' = :AccountIsIncomplete) and
((D.DebitAccount_ID in (select ID from Accounts where IsInComplete = 'N')) and
(D.CreditAccount_ID in (select ID from Accounts where IsInComplete = 'N')) and
((D.DebitAccount_ID is not Null) and (D.CreditAccount_ID is not null))))) and
/* celá skupina bez ohledu */
((:WholeGroup = '1') or
/* celá skupina s ohledem pouze na datum účtování a období */
(:WholeGroup = '2' and (:AccDateUsed <> 'A' or (D.AccDate$DATE >= :AccDateFrom AND D.AccDate$DATE < :AccDateTo)) and
(:PeriodSelID = '' or D.Period_ID in (select OBJ_ID from SELDAT where SEL_ID=:PeriodSelID))) or
/* celá skupina s ohledem na všechny podmínky */
(:WholeGroup = '3' and
(
((:AccDateUsed <> 'A' or (AccDate$DATE >= :AccDateFrom AND AccDate$DATE < :AccDateTo)) and
(:TextUsed <> 'A' or D.Text like :Text ESCAPE '~') and
(:AccountCodeUsed <> 'A' or
D.DebitAccount_ID in (select ID from Accounts where Code like :AccountCode ESCAPE '~') or
D.CreditAccount_ID in (select ID from Accounts where Code like :AccountCode ESCAPE '~')) and
(:AmountUsed <> 'A' or D.Amount BETWEEN :AmountFrom and :AmountTo) and
(:AmountInCurrencyUsed <> 'A' or D.AmountInCurrency BETWEEN :AmountInCurrencyFrom and :AmountInCurrencyTo) and
(:OrdNumberUsed <> 'A' or D.OrdNumber BETWEEN :OrdNumberFrom and :OrdNumberTo) and
(:AccDocQueueSelID = '' or
D.AccDocQueue_ID in (select OBJ_ID from SELDAT where SEL_ID=:AccDocQueueSelID)) and
(:PeriodSelID = '' or
D.Period_ID in (select OBJ_ID from SELDAT where SEL_ID=:PeriodSelID)) and
(:FirmSelID = '' or
((D.Firm_ID in (select OBJ_ID from SELDAT where SEL_ID = :FirmSelID)) or
(D.Firm_ID in (SELECT ID FROM Firms WHERE Firm_ID in (select OBJ_ID from SELDAT where SEL_ID = :FirmSelID)))))
and
(:CurrencySelID = '' or
D.Currency_ID in (select OBJ_ID from SELDAT where SEL_ID=:CurrencySelID)) and
( (:DivisionSelID = '') OR
((:ADivisionsWithNull in ('1', 'A')) and (D.DebitDivision_ID IS NULL)) OR
(D.DebitDivision_ID IN (SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :DivisionSelID)) 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 = :DivisionSelID)))))) ) OR
((:ADivisionsWithNull in ('1', 'A')) and (D.CreditDivision_ID IS NULL)) OR
(D.CreditDivision_ID IN (SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :DivisionSelID)) 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 = :DivisionSelID)))))) )
) AND
( (:BusOrderSelID = '') OR
(D.DebitBusOrder_ID IN (SELECT Bx.ID FROM BusOrders Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusOrderSelID)) 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 = :BusOrderSelID)))))) ) OR
(D.CreditBusOrder_ID IN (SELECT Bx.ID FROM BusOrders Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusOrderSelID)) 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 = :BusOrderSelID)))))) )
) AND
( (:BusTransactionSelID = '') OR
(D.DebitBusTransaction_ID IN (SELECT Bx.ID FROM BusTransactions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusTransactionSelID)) 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 = :BusTransactionSelID)))))) ) OR
(D.CreditBusTransaction_ID IN (SELECT Bx.ID FROM BusTransactions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusTransactionSelID)) 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 = :BusTransactionSelID)))))) )
) AND
( (:BusProjectSelID = '') OR
(D.DebitBusProject_ID IN (SELECT Bx.ID FROM BusProjects Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusProjectSelID)) 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 = :BusProjectSelID)))))) ) OR
(D.CreditBusProject_ID IN (SELECT Bx.ID FROM BusProjects Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :BusProjectSelID)) 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 = :BusProjectSelID)))))) )
)))
))
into ID
do begin
suspend;
end
end
end
END;