begin
sumPSAmount = 0;
sumPSCreditAmount = 0;
sumPSAmountWithoutVAT = 0;
sumPSCreditAmountWithoutVAT = 0;
/*Souhrnne uctenky pokladniho prodeje */
if (POSsummaredType <> 0) then begin
select sum(PS2.LocalTAmount), sum(PS3.LocalTAmount), sum(PS2.LocalTAmountWithoutVAT), sum(PS3.LocalTAmountWithoutVAT)
from POSsummaredDocuments PS
left join POSsummaredDocuments2 PS2 on PS2.Parent_ID=PS.ID
left join POSsummaredDocuments3 PS3 on PS3.Parent_ID=PS.ID
where PS.Firm_ID = :AFirm_ID and
(:DocDateUsed <> 'A' or (PS.DocDate$Date >= :DocDatefrom and PS.DocDate$Date < :DocDateTo)) and
(:AccDateUsed <> 'A' or (PS.AccDate$Date >= :AccDatefrom and PS.AccDate$Date < :AccDateTo)) and
(:DivisionSelID = '' or
(
(
PS2.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 = :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
(
PS3.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 = :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
(
(
PS2.Parent_ID=PS.ID and PS2.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 = :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
(
PS3.Parent_ID=PS.ID and PS3.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 = :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
(
(
PS2.Parent_ID=PS.ID and PS2.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 = :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
(
PS3.Parent_ID=PS.ID and PS3.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 = :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
(
(
PS2.Parent_ID=PS.ID and PS2.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 = :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
(
PS3.Parent_ID=PS.ID and PS3.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 = :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 :sumPSAmount, :sumPSCreditAmount, :sumPSAmountWithoutVAT, :sumPSCreditAmountWithoutVAT;
if (sumPSAmount is null) then
sumPSAmount = 0;
if (sumPSCreditAmount is null) then
sumPSCreditAmount = 0;
if (sumPSAmountWithoutVAT is null) then
sumPSAmountWithoutVAT = 0;
if (sumPSCreditAmountWithoutVAT is null) then
sumPSCreditAmountWithoutVAT = 0;
end
if (POSsummaredType = 1) then begin
sumPSAmount = sumPSAmount + sumPSCreditAmount;
sumPSAmountWithoutVAT = sumPSAmountWithoutVAT + sumPSCreditAmountWithoutVAT;
end
suspend;
end