insert /*+ APPEND NOLOGGING */
into TTFIFOChangedDocs
(RowIdx,
DocType,
ID,
CreatedAt,
OpCode,
LocationFrom,
LocationTo,
PayCash,
TotalSum,
ClientIndex,
GoodsOwner,
CurrencyType,
CurrencyRate,
CurrencyMultOrder,
IsRoubles,
TotalSumCur,
UserOp,
SupplierDoc,
SupplierInvoice,
SupplInvoiceCreate,
TotalSumVAT)
select ROWNUM,
H.DocType,
H.ID,
H.CreatedAt,
H.OpCode,
H.LocationFrom,
H.LocationTo,
nvl(nvl(XIn.PayCash, XOut.PayCash),
decode(H.DocType,
:"SYS_B_00",
:"SYS_B_01",
:"SYS_B_02",
:"SYS_B_03",
:"SYS_B_04")) PayCash,
H.TotalSum,
H.ClientIndex,
nvl(XIn.GoodsOwner, :"SYS_B_05"),
H.CurrencyType,
H.CurrencyRate,
H.CurrencyMultOrder,
H.IsRoubles,
H.TotalSumCur,
H.UserOp,
XIn.SupplierDoc,
nvl(XIn.SupplierInvoice, XOut.Invoice),
nvl(XIn.SupplInvoiceCreate, XOut.InvoiceDate),
H.TotalSumVAT
from (select H.DocType,
H.ID,
TRUNC(H.CreatedAt) CreatedAt,
H.OpCode,
H.LocationFrom,
H.LocationTo,
H.TotalSum,
H.ClientIndex,
H.CurrencyType,
H.CurrencyRate,
H.CurrencyMultOrder,
H.IsRoubles,
H.TotalSumCur,
H.UserOp,
nvl(sum(T.TaxSum), :"SYS_B_06") TotalSumVAT
from (select D.ID,
D.DocType,
D.CreatedAt,
D.LocationFrom,
D.LocationTo,
D.OpCode,
D.UserOp,
D.ClientIndex,
D.CurrencyType,
D.CurrencyRate,
D.CurrencyMultOrder,
D.IsRoubles,
D.TotalSum,
D.TotalSumCur
from SMDocuments D
where D.DocType in
(:"SYS_B_07", :"SYS_B_08", :"SYS_B_09", :"SYS_B_10",
:"SYS_B_11", :"SYS_B_12", :"SYS_B_13", :"SYS_B_14")
and D.CreatedAt <= to_date(:"SYS_B_15", :"SYS_B_16")
and D.DocState = :"SYS_B_17"
and D.CreatedAt >= to_date(:"SYS_B_18", :"SYS_B_19")
and (D.CreatedAt > to_date(:"SYS_B_20", :"SYS_B_21") or
exists
(select :"SYS_B_22"
from SMDocLog L
where L.DocType = D.DocType
and L.ID = D.ID
and (L.EventTime >
to_date(:"SYS_B_23", :"SYS_B_24") and
L.EventTime <=
to_date(:"SYS_B_25", :"SYS_B_26"))))) H,
SMDocTax T
where T.DocType(+) = H.DocType
and T.DocID(+) = H.ID
and T.TaxId(+) = :"SYS_B_27"
group by H.DocType,
H.ID,
TRUNC(H.CreatedAt),
H.OpCode,
H.LocationFrom,
H.LocationTo,
H.TotalSum,
H.ClientIndex,
H.CurrencyType,
H.CurrencyRate,
H.CurrencyMultOrder,
H.IsRoubles,
H.TotalSumCur,
H.UserOp) H,
SMWayBillsIn XIn,
SMWayBillsOut XOut
where H.DocType = XIn.DocType(+)
and H.ID = XIn.ID(+)
and H.DocType = XOut.DocType(+)
and H.ID = XOut.ID(+)