begin
for
select
SUM(A.Incomes),
SUM(A.Expenses),
A.BusOrder_ID,
MAX(A.Nesting),
SUM(A.Incomes)-SUM(A.Expenses),
MAX(Z.Code),
MAX(Z.Parent_ID),
MAX(Z.Name)
from
RepBusOrders1 (:Audited, :DateFrom, :DateTo, :FirmSelID,
:InBusTransactionSelID, :InBusTransactionsWithChilds,
:InDivisionSelID, :InBusOrderSelID, :InBusOrdersWithChilds,
:InBusProjectSelID, :InBusProjectsWithChilds,
:Summarize, :IsRequest, :InDivisionsWithChilds) A
LEFT join BusOrders Z ON A.BusOrder_ID = Z.ID
where
( (:InBusOrderSelID = '') 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 = :InBusOrderSelID)) OR
((:InBusOrdersWithChilds in ('1', 'A')) and (Bx.ID in (select ID from SYS$BusOrders2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :InBusOrderSelID)))))
)
)
)
group by A.BusOrder_ID
into Incomes, Expenses, BusOrder_ID, Nesting, Profit, TempCode, TempParent_ID, TempName
do begin
if (Sorting = 0) then CompletePath = CAST(TempCode as CHAR(20)) || BusOrder_ID;
if (Sorting = 1) then CompletePath = CAST(TempName as CHAR(100)) || BusOrder_ID;
if (Sorting = 2) then CompletePath = ''; /* Tridi se podle vystupniho parametru Profit */
if (Sorting = 3) then CompletePath = CAST(TempCode as CHAR(20));
if (Sorting = 4) then CompletePath = CAST(TempName as CHAR(100));
i = 0;
while (BusOrder_ID is not null and TempParent_ID is not null and i < 100) do begin
select Parent_ID, Code, ID, Name from BusOrders where ID = :TempParent_ID
into TempParent_ID, TempCode, TempID, TempName;
i = i + 1;
if (Sorting = 0) then CompletePath = CAST(TempCode as CHAR(20)) || TempID || CompletePath;
if (Sorting = 1) then CompletePath = CAST(TempName as CHAR(100)) || TempID || CompletePath;
end
Nesting = i;
if ((FromLevel + MaxNesting) >= Nesting and FromLevel <= Nesting) then suspend;
end
end;