/*
select * from GK_DEPOORDER where trunc(DD) = :D$DD and CDDEPOOP_ID = 1
select * from GK_DEPOORDER where trunc(DD) = :D$DD and CDDEPOOP_ID = 2

select * from GK_REESTR where CDKDSH_ID = 9943881706 and AMSHALL > 0 order by ID
select * from COM_AC_ACF where DOC_ID = 10150218870
select * from GK_REESTR_COMMS where VNESHID in (select ID from GK_REESTR where CDKDSH_ID = 9943881706 and AMSHALL > 0) order by ID
select SHPRICE from GK_DEPODEAL where ID = 9941623319
*/
declare
  nID number(12);
  nID2 number(12);
  nID3 number(12);
  v_OrderSellID number(12);
  v_OrderBuyID number(12);
  v_TopParentID number(12);

  nSum number(22,2);
begin
  begin
    select ID into v_OrderSellID from GK_DEPOORDER where trunc(DD) = :D$DD and CDDEPOOP_ID = 1 and S_S = 1000035 and CDKDSH = 9943881706;
  exception
    when NO_DATA_FOUND then
      raise_application_error(-20202,'#I Не найдено инвернтарное поручение по списанию ц/б из старой цели учёта');
  end;

  begin
    select ID into v_OrderBuyID from GK_DEPOORDER where trunc(DD) = :D$DD and CDDEPOOP_ID = 1 and S_S = 1000035 and CDKDSH = 9943881706;
  exception
    when NO_DATA_FOUND then
      raise_application_error(-20202,'#I Не найдено инвернтарное поручение по зачислению ц/б в новую цель учёта');
  end;
  v_TopParentID := 9945066765;

  nID := MainID.NextVal;
  nID2 := MainID.NextVal;
  insert into GK_REESTR (ID, PARENT_ID, PORTFPARENT_ID, TOPPARENT_ID, CDKDSH_ID, TIMEDEAL, DEALTYPE, AMSHALL, ORDER_ID, PURPOSE, PRICE, SUM_BS, PKDSH, SUM_PKDUPL, ORDER_STR, DOCOID, DOCID, TOPDOCOID, TOPDOCID, S_CR, S_CDT, IS_VALUEBLE, BUH_OFORML_ID, PKDUPL_NALOG, TELO_NALOG, BASE_AMSHALL)
  select nID ID, ID as PARENT_ID, null PORTFPARENT_ID, ID as TOPPARENT_ID, CDKDSH_ID, :D$DD TIMEDEAL, 2 DEALTYPE, AMSHALL
  , v_OrderSellID ORDER_ID
  , 0 PURPOSE, PRICE, SUM_BS, PKDSH, SUM_PKDUPL, null ORDER_STR, 1003114 DOCOID
  , v_OrderSellID DOCID
  , null TOPDOCOID, null TOPDOCID, dbo.S_CONTEXT(1) S_CR, sysdate S_CDT, 0 IS_VALUEBLE, null BUH_OFORML_ID, PKDUPL_NALOG, TELO_NALOG, AMSHALL BASE_AMSHALL
    from GK_REESTR
   where ID = v_TopParentID;

  insert into GK_REESTR (ID, PARENT_ID, PORTFPARENT_ID, TOPPARENT_ID, CDKDSH_ID, TIMEDEAL, DEALTYPE, AMSHALL, ORDER_ID, PURPOSE, PRICE, SUM_BS, PKDSH, SUM_PKDUPL, ORDER_STR, DOCOID, DOCID, TOPDOCOID, TOPDOCID, S_CR, S_CDT, IS_VALUEBLE, BUH_OFORML_ID, PKDUPL_NALOG, TELO_NALOG, BASE_AMSHALL)
  select nID2 ID, null as PARENT_ID, null PORTFPARENT_ID, ID as TOPPARENT_ID, CDKDSH_ID, :D$DD TIMEDEAL, 2 DEALTYPE, AMSHALL
  ,v_OrderBuyID ORDER_ID
  , 2 PURPOSE, PRICE, SUM_BS, PKDSH, SUM_PKDUPL, null ORDER_STR, 1003114 DOCOID
  ,v_OrderBuyID DOCID
  , null TOPDOCOID, null TOPDOCID, dbo.S_CONTEXT(1) S_CR, sysdate S_CDT, 0 IS_VALUEBLE, null BUH_OFORML_ID, PKDUPL_NALOG, TELO_NALOG, AMSHALL BASE_AMSHALL
    from GK_REESTR
   where ID = v_TopParentID;
   
   insert into GK_REESTR_COMMS (ID, VNESHID, COMM_TYPE, COMM_SUM, COMM_SUM_NO_NDS, COMM_VALUT, COMM_SUM_RUB, COMM_DATE)
   select MainID.NextVal ID, nID VNESHID, COMM_TYPE+20, COMM_SUM, COMM_SUM_NO_NDS, COMM_VALUT, COMM_SUM_RUB, COMM_DATE
     from GK_REESTR_COMMS
    where VNESHID = v_TopParentID;

   insert into GK_REESTR_COMMS (ID, VNESHID, COMM_TYPE, COMM_SUM, COMM_SUM_NO_NDS, COMM_VALUT, COMM_SUM_RUB, COMM_DATE)
   select MainID.NextVal ID, nID2 VNESHID, COMM_TYPE, COMM_SUM, COMM_SUM_NO_NDS, COMM_VALUT, COMM_SUM_RUB, COMM_DATE
     from GK_REESTR_COMMS
    where VNESHID = v_TopParentID;

  begin
    select ID
      into nID3
      from COM_AC_ACF_DATE
     where VNESHID = 9945066768
       and DD = :D$DD;
  exception
    when NO_DATA_FOUND then
      nID3 := null;
  end;
  if nID3 is null then
    insert into COM_AC_ACF_DATE(ID,VNESHID,DD)
    values(MainID.NextVal,9945066768,:D$DD);
  end if;

  begin
    select AMORT_COST_SUM
      into nSum
      from COM_AC_ACF_DATE
     where VNESHID = 9945066768
       and DD = :D$DD;
  exception
    when NO_DATA_FOUND then
      nSum := null;
  end;
  if nSum is null then
    dbo.S_COM_AC_ACF_CALC(9945066768);
    begin
      select AMORT_COST_SUM
        into nSum
        from COM_AC_ACF_DATE
       where VNESHID = 9945066768
         and DD = :D$DD;
    exception
      when NO_DATA_FOUND then
        nSum := null;
    end;
  end if;

  nID3 := MainID.NextVal;
  insert into COM_AC_ACF(TOPDOC_OID, TOPDOC_ID, ID, S_CR, S_CDT, S_S, TYPE, DD, EFFECT_STAVKA, INIT_SUMMA, DOC_OID, DOC_ID, INIT_ADJ_SUM)
  select 1005427 TOPDOC_OID, nID2 TOPDOC_ID, nID3, dbo.S_CONTEXT(1) S_CR, sysdate S_CDT, 1001749 S_S, TYPE, :D$DD DD, null EFFECT_STAVKA, nSum INIT_SUMMA, 1005427 DOC_OID, nID2 DOC_ID, 0 INIT_ADJ_SUM
    from COM_AC_ACF
   where ID = 9945066768;

  insert into COM_AC_CASH_FLOW(ID, VNESHID, DD, TYPE, SUMMA)
  select MainID.NextVal ID, nID3 VNESHID, case when SUMMA < 0 then :D$DD else DD end DD, TYPE, case when SUMMA < 0 then -nSum else SUMMA end SUMMA
    from COM_AC_CASH_FLOW
   where VNESHID = 9945066768;

  update COM_AC_ACF set EFFECT_STAVKA = dbo.S_COM_AC_ACF_GET_EFFECT_STAVKA(nID3) where ID = nID3;
  
  dbo.S_COM_AC_ACF_CALC(nID3); 
end;
