BEGIN
FOR
SELECT II2.ID, II2.ProvideRow_ID, II2.Quantity, II2.LocalTAmountWithoutVAT, II2.Parent_ID,
(select DQ.DocumentType from DocQueues DQ where
DQ.ID in (select II.DocQueue_ID from IssuedInvoices II where II2.Parent_ID = II.ID)),
(select DQ.Code from DocQueues DQ where
DQ.ID in (select II.DocQueue_ID from IssuedInvoices II where II2.Parent_ID = II.ID)),
(select II.OrdNumber from IssuedInvoices II where II2.Parent_ID = II.ID),
(select P.Code from Periods P where
P.ID in (select II.Period_ID from IssuedInvoices II where II2.Parent_ID = II.ID)),
(select II.DocDate$DATE from IssuedInvoices II where II2.Parent_ID = II.ID)
FROM IssuedInvoices2 II2 WHERE
II2.Parent_ID = :AIssuedInvoices_ID AND II2.RowType = 3
INTO :II2_ID, :II2_ProvideRow, :II2_Quantity, :GainAmount, :Header_ID, :HeaderDocType,
:DocQueueCode, :OrdNumber, :PeriodCode, :DocDate$DATE
DO BEGIN
CostAmount = 0;
HeadDispName = DocQueueCode || '-' || OrdNumber || '/' || PeriodCode;
SUSPEND;
/* Dodaci listy */
FOR
SELECT (DL.LocalTAmount/DL.Quantity)*:II2_Quantity, DL.Parent_ID,
(select X.DocumentType from StoreDocuments X where X.ID = DL.Parent_ID),
(select DQ.Code from DocQueues DQ where
DQ.ID in (select SD.DocQueue_ID from StoreDocuments SD where DL.Parent_ID = SD.ID)),
(select SD.OrdNumber from StoreDocuments SD where DL.Parent_ID = SD.ID),
(select P.Code from Periods P where
P.ID in (select SD.Period_ID from StoreDocuments SD where DL.Parent_ID = SD.ID)),
(select SD.DocDate$DATE from StoreDocuments SD where DL.Parent_ID = SD.ID)
FROM StoreDocuments2 DL WHERE
DL.ID = :II2_Providerow and DL.RowType = 3
INTO :CostAmount, :Header_ID, :HeaderDocType, :DocQueueCode, :OrdNumber,
:PeriodCode, :DocDate$DATE
DO BEGIN
GainAmount = 0;
HeadDispName = DocQueueCode || '-' || OrdNumber || '/' || PeriodCode;
SUSPEND;
END
FOR
/* Dobropisy vydane */
SELECT ICN.Quantity, ICN.ProvideRow_ID, ICN.LocalTAmountWithoutVAT, ICN.Parent_ID,
(select DQ.DocumentType from DocQueues DQ where
DQ.ID in (select CN.DocQueue_ID from IssuedCreditNotes CN where ICN.Parent_ID = CN.ID)),
(select DQ.Code from DocQueues DQ where
DQ.ID in (select CN.DocQueue_ID from IssuedCreditNotes CN where ICN.Parent_ID = CN.ID)),
(select CN.OrdNumber from IssuedCreditNotes CN where ICN.Parent_ID = CN.ID),
(select P.Code from Periods P where
P.ID in (select CN.Period_ID from IssuedCreditNotes CN where ICN.Parent_ID = CN.ID)),
(select CN.DocDate$DATE from IssuedCreditNotes CN where ICN.Parent_ID = CN.ID)
FROM IssuedCreditNotes2 ICN where
ICN.RSource_ID = :II2_ID and ICN.RowType = 3
INTO :ICN_Quantity, :ICN_ProvideRow, :GainAmount, :Header_ID, :HeaderDocType,
:DocQueueCode, :OrdNumber, :PeriodCode, :DocDate$DATE
DO BEGIN
CostAmount = 0;
GainAmount = (GainAmount * (-1));
HeadDispName = DocQueueCode || '-' || OrdNumber || '/' || PeriodCode;
SUSPEND;
FOR
/* Vratky */
SELECT (VR.LocalTAmount/VR.Quantity)*:ICN_Quantity, VR.Parent_ID,
(select X.DocumentType from StoreDocuments X where X.ID = VR.Parent_ID),
(select DQ.Code from DocQueues DQ where
DQ.ID in (select SD.DocQueue_ID from StoreDocuments SD where VR.Parent_ID = SD.ID)),
(select SD.OrdNumber from StoreDocuments SD where VR.Parent_ID = SD.ID),
(select P.Code from Periods P where
P.ID in (select SD.Period_ID from StoreDocuments SD where VR.Parent_ID = SD.ID)),
(select SD.DocDate$DATE from StoreDocuments SD where VR.Parent_ID = SD.ID)
FROM StoreDocuments2 VR WHERE
VR.ID = :ICN_ProvideRow and VR.RowType = 3
INTO :CostAmount, :Header_ID, :HeaderDocType, :DocQueueCode, :OrdNumber,
:PeriodCode, :DocDate$DATE
DO BEGIN
GainAmount = 0;
CostAmount = (CostAmount * (-1));
HeadDispName = DocQueueCode || '-' || OrdNumber || '/' || PeriodCode;
SUSPEND;
END
END
END
END;