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,IsPeriod) 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,nvl((select :"SYS_B_06" from SMDocProps P where P.DocType=H.DocType and P.DocID=H.ID and P.ParamName=:"SYS_B_07" and P.ParamValue=:"SYS_B_08"),:"SYS_B_09") IsPeriod 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_10") 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_11",:"SYS_B_12",:"SYS_B_13",:"SYS_B_14",:"SYS_B_15",:"SYS_B_16",:"SYS_B_17",:"SYS_B_18") and D.CreatedAt<=to_date(:"SYS_B_19",:"SYS_B_20") and D.DocState=:"SYS_B_21" and D.CreatedAt >= to_date(:"SYS_B_22",:"SYS_B_23") and ( D.CreatedAt>to_date(:"SYS_B_24",:"SYS_B_25") or exists (select :"SYS_B_26" from SMDocLog L where L.DocType = D.DocType and L.ID = D.ID and (L.EventTime>to_date(:"SYS_B_27",:"SYS_B_28") and L.EventTime<=to_date(:"SYS_B_29",:"SYS_B_30")))) ) H, SMDocTax T where T.DocType(+) = H.DocType and T.DocID(+) = H.ID and T.TaxId(+) = :"SYS_B_31" 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(+)