select
case
when II.ID is not null then II.ID
when CR.ID is not null then CR.ID
when ICN.ID is not null then ICN.ID
when RCR.ID is not null then RCR.ID
else null
end as Document_ID,
case
when II.ID is not null then DQ_II.DocumentType
when CR.ID is not null then DQ_CR.DocumentType
when ICN.ID is not null then DQ_ICN.DocumentType
when RCR.ID is not null then DQ_RCR.DocumentType
else null
end as DocumentType
from
DocRowBatches DRB
join StoreDocuments2 SDocs2 on DRB.Parent_ID = SDocs2.ID
join StoreDocuments SDocs on SDocs2.Parent_ID = SDocs.ID
/*FV*/
left join IssuedInvoices2 II2 on (II2.ProvideRow_ID = SDocs2.ID) /*and (SDocs.DocumentType in ('20', '21'))*/
left join IssuedInvoices II on II2.Parent_ID = II.ID
left join DocQueues DQ_II on II.DocQueue_ID = DQ_II.ID
/*PP*/
left join CashReceived2 CR2 on (CR2.ProvideRow_ID = SDocs2.ID)
left join CashReceived CR on CR2.Parent_ID = CR.ID
left join DocQueues DQ_CR on CR.DocQueue_ID = DQ_CR.ID
/*DV*/
left join IssuedCreditNotes2 ICN2 on (ICN2.ProvideRow_ID = SDocs2.ID)
left join IssuedCreditNotes ICN on ICN2.Parent_ID = ICN.ID
left join DocQueues DQ_ICN on ICN.DocQueue_ID = DQ_ICN.ID
/*VP*/
left join RefundedCashReceived2 RCR2 on (RCR2.ProvideRow_ID = SDocs2.ID)
left join RefundedCashReceived RCR on RCR2.Parent_ID = RCR.ID
left join DocQueues DQ_RCR on RCR.DocQueue_ID = DQ_RCR.ID
where
DRB.StoreBatch_ID = :StoreBatch_ID and
SDocs.DocumentType in ('20', '21', '23') /*příjemky - typ 20 jsou zde kvůli vratným obalům na FV*/
Generated by ABRA Software a.s. 27.10.2021 16:34:11