Установка Единого Окна — различия между версиями

Материал из wiki.standart-n.ru
Перейти к: навигация, поиск
(Новая страница: «==Лешины скрипты== <pre> create or alter procedure PR_AUTO_WARESLINK ( DOC_ID DM_ID_NULL, ACTIVE_ID DM_ID_NULL) as declare variable DDA_BARC…»)
 
Строка 38: Строка 38:
 
begin
 
begin
  
  select bcode_izg, ware_id,
+
  select BCODE_IZG,
  (select val_id from PR_GETVAL_ID(dda.sname, 0, dda.part_type)) as name_id,
+
        WARE_ID,
  (select val_id from PR_GETVAL_ID(dda.sizg, 3, dda.part_type)) as izg_id,
+
        (select VAL_ID
  (select val_id from PR_GETVAL_ID(dda.scountry, 2, dda.part_type)) as country_id,
+
        from PR_GETVAL_ID(DDA.SNAME, 0, DDA.PART_TYPE)) as NAME_ID,
  (select val_id from PR_GETVAL_ID(dda.sorig_name, 1, dda.part_type)) as orig_name_id,
+
        (select VAL_ID
  (select val_id from PR_GETVAL_ID(dda.sorig_izg, 6, dda.part_type)) as orgi_izg_id,
+
        from PR_GETVAL_ID(DDA.SIZG, 3, DDA.PART_TYPE)) as IZG_ID,
  (select val_id from PR_GETVAL_ID(dda.sorig_country, 5, dda.part_type)) as orig_country_id,
+
        (select VAL_ID
  Z_ID, SKLAD_ID
+
        from PR_GETVAL_ID(DDA.SCOUNTRY, 2, DDA.PART_TYPE)) as COUNTRY_ID,
  from doc_detail_active dda where doc_id = :doc_id and id = :active_id
+
        (select VAL_ID
  into dda_barcode, dda_ware_id, dda_name_id, dda_izg_id, dda_country_id, dda_orig_nameid, dda_orig_izgid, dda_orig_countryid, dda_z_id, dda_sklad_id;
+
        from PR_GETVAL_ID(DDA.SORIG_NAME, 1, DDA.PART_TYPE)) as ORIG_NAME_ID,
 +
        (select VAL_ID
 +
        from PR_GETVAL_ID(DDA.SORIG_IZG, 6, DDA.PART_TYPE)) as ORGI_IZG_ID,
 +
        (select VAL_ID
 +
        from PR_GETVAL_ID(DDA.SORIG_COUNTRY, 5, DDA.PART_TYPE)) as ORIG_COUNTRY_ID,
 +
        Z_ID,
 +
        SKLAD_ID
 +
  from DOC_DETAIL_ACTIVE DDA
 +
where DOC_ID = :DOC_ID
 +
      and ID = :ACTIVE_ID
 +
  into DDA_BARCODE,
 +
      DDA_WARE_ID,
 +
      DDA_NAME_ID,
 +
      DDA_IZG_ID,
 +
      DDA_COUNTRY_ID,
 +
      DDA_ORIG_NAMEID,
 +
      DDA_ORIG_IZGID,
 +
      DDA_ORIG_COUNTRYID,
 +
      DDA_Z_ID,
 +
      DDA_SKLAD_ID;
  
  select first 1 bcode_izg, ware_id,
+
  select first 1 BCODE_IZG,
  (select val_id from PR_GETVAL_ID(ddal.sname, 0, ddal.part_type)) as name_id,
+
                WARE_ID,
  (select val_id from PR_GETVAL_ID(ddal.sizg, 3, ddal.part_type)) as izg_id,
+
                (select VAL_ID
  (select val_id from PR_GETVAL_ID(ddal.scountry, 2, ddal.part_type)) as country_id,
+
                from PR_GETVAL_ID(DDAL.SNAME, 0, DDAL.PART_TYPE)) as NAME_ID,
  (select val_id from PR_GETVAL_ID(ddal.sorig_name, 1, ddal.part_type)) as orig_name_id,
+
                (select VAL_ID
  (select val_id from PR_GETVAL_ID(ddal.sorig_izg, 6, ddal.part_type)) as orgi_izg_id,
+
                from PR_GETVAL_ID(DDAL.SIZG, 3, DDAL.PART_TYPE)) as IZG_ID,
  (select val_id from PR_GETVAL_ID(ddal.sorig_country, 5, ddal.part_type)) as orig_country_id,
+
                (select VAL_ID
  Z_ID, SKLAD_ID
+
                from PR_GETVAL_ID(DDAL.SCOUNTRY, 2, DDAL.PART_TYPE)) as COUNTRY_ID,
  from doc_detail_active_log ddal where doc_id = :doc_id and id = :active_id order by log_id
+
                (select VAL_ID
  into ddalog_barcode, ddalog_ware_id, ddalog_name_id, ddalog_izg_id, ddalog_country_id, ddalog_orig_nameid, ddalog_orig_izgid, ddalog_orig_countryid, ddalog_z_id, ddalog_sklad_id;
+
                from PR_GETVAL_ID(DDAL.SORIG_NAME, 1, DDAL.PART_TYPE)) as ORIG_NAME_ID,
 +
                (select VAL_ID
 +
                from PR_GETVAL_ID(DDAL.SORIG_IZG, 6, DDAL.PART_TYPE)) as ORGI_IZG_ID,
 +
                (select VAL_ID
 +
                from PR_GETVAL_ID(DDAL.SORIG_COUNTRY, 5, DDAL.PART_TYPE)) as ORIG_COUNTRY_ID,
 +
                Z_ID,
 +
                SKLAD_ID
 +
from DOC_DETAIL_ACTIVE_LOG DDAL
 +
where DOC_ID = :DOC_ID
 +
      and ID = :ACTIVE_ID
 +
order by LOG_ID
 +
into DDALOG_BARCODE,
 +
      DDALOG_WARE_ID,
 +
      DDALOG_NAME_ID,
 +
      DDALOG_IZG_ID,
 +
      DDALOG_COUNTRY_ID,
 +
      DDALOG_ORIG_NAMEID,
 +
      DDALOG_ORIG_IZGID,
 +
      DDALOG_ORIG_COUNTRYID,
 +
      DDALOG_Z_ID,
 +
      DDALOG_SKLAD_ID;
  
  select first 1 skip 1 bcode_izg, ware_id,
+
select first 1 skip 1 BCODE_IZG,
  (select val_id from PR_GETVAL_ID(ddal.sname, 0, ddal.part_type)) as name_id,
+
                      WARE_ID,
  (select val_id from PR_GETVAL_ID(ddal.sizg, 3, ddal.part_type)) as izg_id,
+
                      (select VAL_ID
  (select val_id from PR_GETVAL_ID(ddal.scountry, 2, ddal.part_type)) as country_id,
+
                        from PR_GETVAL_ID(DDAL.SNAME, 0, DDAL.PART_TYPE)) as NAME_ID,
  (select val_id from PR_GETVAL_ID(ddal.sorig_name, 1, ddal.part_type)) as orig_name_id,
+
                      (select VAL_ID
  (select val_id from PR_GETVAL_ID(ddal.sorig_izg, 6, ddal.part_type)) as orgi_izg_id,
+
                        from PR_GETVAL_ID(DDAL.SIZG, 3, DDAL.PART_TYPE)) as IZG_ID,
  (select val_id from PR_GETVAL_ID(ddal.sorig_country, 5, ddal.part_type)) as orig_country_id,
+
                      (select VAL_ID
  Z_ID, SKLAD_ID
+
                        from PR_GETVAL_ID(DDAL.SCOUNTRY, 2, DDAL.PART_TYPE)) as COUNTRY_ID,
  from doc_detail_active_log ddal where doc_id = :doc_id and id = :active_id order by log_id
+
                      (select VAL_ID
  into ddalog2_barcode, ddalog2_ware_id, ddalog2_name_id, ddalog2_izg_id, ddalog2_country_id, ddalog2_orig_nameid, ddalog2_orig_izgid, ddalog2_orig_countryid, ddalog2_z_id, ddalog2_sklad_id;
+
                        from PR_GETVAL_ID(DDAL.SORIG_NAME, 1, DDAL.PART_TYPE)) as ORIG_NAME_ID,
 +
                      (select VAL_ID
 +
                        from PR_GETVAL_ID(DDAL.SORIG_IZG, 6, DDAL.PART_TYPE)) as ORGI_IZG_ID,
 +
                      (select VAL_ID
 +
                        from PR_GETVAL_ID(DDAL.SORIG_COUNTRY, 5, DDAL.PART_TYPE)) as ORIG_COUNTRY_ID,
 +
                      Z_ID,
 +
                      SKLAD_ID
 +
from DOC_DETAIL_ACTIVE_LOG DDAL
 +
where DOC_ID = :DOC_ID
 +
      and ID = :ACTIVE_ID
 +
order by LOG_ID
 +
into DDALOG2_BARCODE,
 +
      DDALOG2_WARE_ID,
 +
      DDALOG2_NAME_ID,
 +
      DDALOG2_IZG_ID,
 +
      DDALOG2_COUNTRY_ID,
 +
      DDALOG2_ORIG_NAMEID,
 +
      DDALOG2_ORIG_IZGID,
 +
      DDALOG2_ORIG_COUNTRYID,
 +
      DDALOG2_Z_ID,
 +
      DDALOG2_SKLAD_ID;
  
  if ( ((dda_ware_id <> '-1') and (dda_ware_id <> '0') and (dda_ware_id is not null)) and
+
  if (((DDA_WARE_ID <> '-1') and
    ((ddalog2_ware_id <> '-1') and (ddalog2_ware_id <> '0') and (ddalog2_ware_id is not null))
+
    (DDA_WARE_ID <> '0') and
    and (dda_barcode = ddalog2_barcode)
+
    (DDA_WARE_ID is not null)) and
    and (dda_ware_id <> ddalog2_ware_id) )
+
    ((DDALOG2_WARE_ID <> '-1') and
then
+
    (DDALOG2_WARE_ID <> '0') and
 +
    (DDALOG2_WARE_ID is not null)) and
 +
    (DDA_BARCODE = DDALOG2_BARCODE) and
 +
    (DDA_WARE_ID <> DDALOG2_WARE_ID)) then
 
  begin
 
  begin
  update wares set name_id = :dda_name_id, izg_id = :dda_izg_id, country_id = :dda_country_id, orig_name_id = :dda_orig_nameid,
+
  update WARES
    orig_izg_id = :dda_orig_izgid, orig_country_id = :dda_orig_countryid, barcode = :dda_barcode where id = :ddalog2_ware_id;
+
  set NAME_ID = :DDA_NAME_ID,
  exit;
+
      IZG_ID = :DDA_IZG_ID,
 +
      COUNTRY_ID = :DDA_COUNTRY_ID,
 +
      ORIG_NAME_ID = :DDA_ORIG_NAMEID,
 +
      ORIG_IZG_ID = :DDA_ORIG_IZGID,
 +
      ORIG_COUNTRY_ID = :DDA_ORIG_COUNTRYID,
 +
      BARCODE = :DDA_BARCODE
 +
  where ID = :DDALOG2_WARE_ID;
 +
  exit;
 
  end
 
  end
  
  if ( (ddalog_ware_id = '0') or (ddalog_ware_id = '-1') or (ddalog_ware_id is null) ) then
+
  if ((DDALOG_WARE_ID = '0') or (DDALOG_WARE_ID = '-1') or (DDALOG_WARE_ID is null)) then
 
  begin
 
  begin
  insert into wares_log (ID,NAME_ID,IZG_ID,COUNTRY_ID,ORIG_NAME_ID,ORIG_IZG_ID,ORIG_COUNTRY_ID,BARCODE, Z_ID, SKLAD_ID)
+
  insert into WARES_LOG (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, BARCODE, Z_ID,
  values (:dda_ware_id, :ddalog_name_id, :ddalog_izg_id, :ddalog_country_id, :ddalog_orig_nameid, :ddalog_orig_izgid,
+
                        SKLAD_ID)
    :ddalog_orig_countryid, :ddalog_barcode, :ddalog_z_id, :ddalog_sklad_id);
+
  values (:DDA_WARE_ID, :DDALOG_NAME_ID, :DDALOG_IZG_ID, :DDALOG_COUNTRY_ID, :DDALOG_ORIG_NAMEID, :DDALOG_ORIG_IZGID,
 +
          :DDALOG_ORIG_COUNTRYID, :DDALOG_BARCODE, :DDALOG_Z_ID, :DDALOG_SKLAD_ID);
  
 
  end
 
  end
  else if ( ((dda_ware_id <> '0') and (dda_ware_id <> '-1') and (dda_ware_id is not null)) and
+
  else
  ((dda_name_id <> ddalog_name_id) or (dda_izg_id <> ddalog_izg_id) or (dda_country_id <> ddalog_country_id) or (dda_orig_nameid <> ddalog_orig_nameid)
+
if (((DDA_WARE_ID <> '0') and
  or (dda_orig_izgid <> ddalog_orig_izgid) or (dda_orig_countryid <> ddalog_orig_countryid))
+
    (DDA_WARE_ID <> '-1') and
  and (dda_barcode = ddalog_barcode)
+
    (DDA_WARE_ID is not null)) and
  ) then
+
    ((DDA_NAME_ID <> DDALOG_NAME_ID) or (DDA_IZG_ID <> DDALOG_IZG_ID) or (DDA_COUNTRY_ID <> DDALOG_COUNTRY_ID) or (DDA_ORIG_NAMEID <> DDALOG_ORIG_NAMEID) or (DDA_ORIG_IZGID <> DDALOG_ORIG_IZGID) or (DDA_ORIG_COUNTRYID <> DDALOG_ORIG_COUNTRYID)) and
 +
    (DDA_BARCODE = DDALOG_BARCODE)) then
 
  begin
 
  begin
  update wares set name_id = :dda_name_id, izg_id = :dda_izg_id, country_id = :dda_country_id, orig_name_id = :dda_orig_nameid,
+
  update WARES
    orig_izg_id = :dda_orig_izgid, orig_country_id = :dda_orig_countryid, barcode = :dda_barcode where id = :ddalog_ware_id;
+
  set NAME_ID = :DDA_NAME_ID,
 +
      IZG_ID = :DDA_IZG_ID,
 +
      COUNTRY_ID = :DDA_COUNTRY_ID,
 +
      ORIG_NAME_ID = :DDA_ORIG_NAMEID,
 +
      ORIG_IZG_ID = :DDA_ORIG_IZGID,
 +
      ORIG_COUNTRY_ID = :DDA_ORIG_COUNTRYID,
 +
      BARCODE = :DDA_BARCODE
 +
  where ID = :DDALOG_WARE_ID;
 
  end
 
  end
  
 
end;
 
end;
  
GRANT EXECUTE ON PROCEDURE PR_GETVAL_ID TO PROCEDURE PR_AUTO_WARESLINK;
+
grant execute
GRANT SELECT ON DOC_DETAIL_ACTIVE TO PROCEDURE PR_AUTO_WARESLINK;
+
    on procedure PR_GETVAL_ID to procedure PR_AUTO_WARESLINK;
GRANT SELECT ON DOC_DETAIL_ACTIVE_LOG TO PROCEDURE PR_AUTO_WARESLINK;
+
grant select on DOC_DETAIL_ACTIVE to procedure PR_AUTO_WARESLINK;
GRANT INSERT ON WARES_LOG TO PROCEDURE PR_AUTO_WARESLINK;
+
grant select on DOC_DETAIL_ACTIVE_LOG to procedure PR_AUTO_WARESLINK;
GRANT SELECT,UPDATE ON WARES TO PROCEDURE PR_AUTO_WARESLINK;
+
grant insert on WARES_LOG to procedure PR_AUTO_WARESLINK;
GRANT EXECUTE ON PROCEDURE PR_AUTO_WARESLINK TO SYSDBA;
+
grant select,
 +
            update on WARES to procedure PR_AUTO_WARESLINK;
 +
grant execute
 +
    on procedure PR_AUTO_WARESLINK to SYSDBA;
  
 
+
create or alter procedure PR_MAKEGOODWAREVALUES(SNAME type of DM_TEXT, SORIG_NAME type of DM_TEXT, SIZG type of DM_TEXT, SORIG_IZG type of DM_TEXT, SCOUNTRY type of DM_TEXT, SORIG_COUNTRY type of DM_TEXT, ORIG_CODE type of DM_TEXT, ACTIVE_ID type of DM_ID, ALTTYPE DM_STATUS, BARCODE DM_TEXT1024 = '') returns(GOODNAME type of DM_TEXT, GOODIZG type of DM_TEXT, GOODCOUNTRY type of DM_TEXT, GOODBARCODE DM_TEXT)
create or alter procedure PR_MAKEGOODWAREVALUES (
+
    SNAME type of DM_TEXT,
+
    SORIG_NAME type of DM_TEXT,
+
    SIZG type of DM_TEXT,
+
    SORIG_IZG type of DM_TEXT,
+
    SCOUNTRY type of DM_TEXT,
+
    SORIG_COUNTRY type of DM_TEXT,
+
    ORIG_CODE type of DM_TEXT,
+
    ACTIVE_ID type of DM_ID,
+
    ALTTYPE DM_STATUS,
+
    BARCODE DM_TEXT1024 = '')
+
returns (
+
    GOODNAME type of DM_TEXT,
+
    GOODIZG type of DM_TEXT,
+
    GOODCOUNTRY type of DM_TEXT,
+
    GOODBARCODE DM_TEXT)
+
 
as
 
as
 
declare variable WARE_ID type of DM_UUID_NULL;
 
declare variable WARE_ID type of DM_UUID_NULL;
Строка 144: Строка 209:
 
  */
 
  */
  
  select id from vals where vtype=0 and alttype=:alttype and svalue=:sname into :name_id;
+
select ID
  select id from vals where vtype=1 and alttype=:alttype and svalue=:sorig_name into :orig_name_id;
+
from VALS
  select id from vals where vtype=3 and alttype=:alttype and svalue=:sizg into :izg_id;
+
where VTYPE = 0
  select id from vals where vtype=6 and alttype=:alttype and svalue=:sorig_izg into :orig_izg_id;
+
      and ALTTYPE = :ALTTYPE
  select id from vals where vtype=2 and alttype=:alttype and svalue=:scountry into :country_id;
+
      and SVALUE = :SNAME
  select id from vals where vtype=5 and alttype=:alttype and svalue=:sorig_country into :orig_country_id;
+
into :NAME_ID;
 +
select ID
 +
from VALS
 +
where VTYPE = 1
 +
      and ALTTYPE = :ALTTYPE
 +
      and SVALUE = :SORIG_NAME
 +
into :ORIG_NAME_ID;
 +
select ID
 +
from VALS
 +
where VTYPE = 3
 +
      and ALTTYPE = :ALTTYPE
 +
      and SVALUE = :SIZG
 +
into :IZG_ID;
 +
select ID
 +
from VALS
 +
where VTYPE = 6
 +
      and ALTTYPE = :ALTTYPE
 +
      and SVALUE = :SORIG_IZG
 +
into :ORIG_IZG_ID;
 +
select ID
 +
from VALS
 +
where VTYPE = 2
 +
      and ALTTYPE = :ALTTYPE
 +
      and SVALUE = :SCOUNTRY
 +
into :COUNTRY_ID;
 +
select ID
 +
from VALS
 +
where VTYPE = 5
 +
      and ALTTYPE = :ALTTYPE
 +
      and SVALUE = :SORIG_COUNTRY
 +
into :ORIG_COUNTRY_ID;
  
 
+
select WARE_ID
  select ware_id from PR_GETWARE_BY_IDS(:name_id,:izg_id,:country_id,:orig_code,:orig_name_id,:orig_izg_id,:orig_country_id,0,1, :barcode) into :ware_id;
+
from PR_GETWARE_BY_IDS(:NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, 1, :BARCODE)
  if (ware_id is not null) then
+
into :WARE_ID;
  begin
+
if (WARE_ID is not null) then
 +
begin
 
   /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
 
   /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
    select sname, sizg, scountry, barcode from VW_WARES where id=:ware_id into :goodname, :goodizg, :goodcountry, :goodbarcode;
+
  select SNAME,
 +
        SIZG,
 +
        SCOUNTRY,
 +
        BARCODE
 +
  from VW_WARES
 +
  where ID = :WARE_ID
 +
  into :GOODNAME,
 +
      :GOODIZG,
 +
      :GOODCOUNTRY,
 +
      :GOODBARCODE;
 
   /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
 
   /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
 
   /* insert into doc_detail_active_log(id,sname,sizg,scountry,part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */
 
   /* insert into doc_detail_active_log(id,sname,sizg,scountry,part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */
 
   /* goodname='2';--sname; */
 
   /* goodname='2';--sname; */
   if (goodname is null)   then goodname = sname;
+
   if (GOODNAME is null) then
   if (goodizg is null) then goodizg = sizg;
+
  GOODNAME = SNAME;
   if (goodcountry is null) then goodcountry = scountry;
+
   if (GOODIZG is null) then
   if (goodname is null)   then goodname = sorig_name;
+
  GOODIZG = SIZG;
   if (goodizg is null) then goodizg = sorig_izg;
+
   if (GOODCOUNTRY is null) then
   if (goodcountry is null) then goodcountry = sorig_country;
+
  GOODCOUNTRY = SCOUNTRY;
   if (goodbarcode is null) then goodbarcode = barcode;
+
   if (GOODNAME is null) then
    suspend;
+
  GOODNAME = SORIG_NAME;
    exit;
+
   if (GOODIZG is null) then
  end  
+
  GOODIZG = SORIG_IZG;
 +
   if (GOODCOUNTRY is null) then
 +
  GOODCOUNTRY = SORIG_COUNTRY;
 +
   if (GOODBARCODE is null) then
 +
  GOODBARCODE = BARCODE;
 +
  suspend;
 +
  exit;
 +
end
  
  select ware_id from PR_GETWARE_BY_IDS(:name_id,:izg_id,:country_id,:orig_code,:orig_name_id,:orig_izg_id,:orig_country_id,0,null, :barcode) into :ware_id;
+
select WARE_ID
  if (ware_id is not null) then
+
from PR_GETWARE_BY_IDS(:NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, null, :BARCODE)
  begin
+
into :WARE_ID;
    /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
+
if (WARE_ID is not null) then
    /* goodname='1';--sname; */
+
begin
    goodname=sname;
+
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
    goodizg=sizg;
+
  /* goodname='1';--sname; */
    goodcountry=scountry;
+
  GOODNAME = SNAME;
    goodbarcode=barcode;
+
  GOODIZG = SIZG;
 +
  GOODCOUNTRY = SCOUNTRY;
 +
  GOODBARCODE = BARCODE;
  
    suspend;
+
  suspend;
    exit;
+
  exit;
  end  
+
end
  
 +
WARE_ID = null;
 +
if (ORIG_NAME_ID is not null) then
 +
begin
 +
  select first 1 ID
 +
  from WARES
 +
  where ORIG_NAME_ID = :ORIG_NAME_ID
 +
  order by ID desc
 +
  into :WARE_ID;
 +
  if (WARE_ID is null) then
 +
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
 +
  from WARES_LOG
 +
  where ORIG_NAME_ID = :ORIG_NAME_ID
 +
  order by LOG_ID desc
 +
  into :WARE_ID;
 +
end
 +
if ((WARE_ID is null) and
 +
    (NAME_ID is not null)) then
 +
begin
 +
  select first 1 ID
 +
  from WARES
 +
  where NAME_ID = :NAME_ID
 +
  into :WARE_ID;
 +
  if (WARE_ID is null) then
 +
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
 +
  from WARES_LOG
 +
  where NAME_ID = :NAME_ID
 +
  order by LOG_ID desc
 +
  into :WARE_ID;
 +
end
 +
if (WARE_ID is null) then
 +
begin
 +
  /* goodname='3';--sname; */
 +
  GOODNAME = SNAME;
 +
  NAME_ID = null;
 +
end
 +
else
 +
begin
 +
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
 +
  select W.NAME_ID,
 +
        V.SVALUE
 +
  from WARES W
 +
  left join VALS V on W.NAME_ID = V.ID
 +
  where W.ID = :WARE_ID
 +
  into :NAME_ID,
 +
      :GOODNAME;
 +
end
  
  ware_id=null;
+
WARE_ID = null;
  if (orig_name_id is not null) then
+
if (ORIG_IZG_ID is not null) then
  begin
+
begin
    select first 1 id from wares where orig_name_id=:orig_name_id order by id desc into :ware_id;
+
  select first 1 ID
    if (ware_id is null) then
+
  from WARES
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where orig_name_id=:orig_name_id order by log_id desc into :ware_id;
+
  where ORIG_IZG_ID = :ORIG_IZG_ID
  end
+
  into :WARE_ID;
  if ((ware_id is null) and (name_id is not null)) then
+
  if (WARE_ID is null) then
  begin
+
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
    select first 1 id from wares where name_id=:name_id into :ware_id;
+
  from WARES_LOG
    if (ware_id is null) then
+
  where ORIG_IZG_ID = :ORIG_IZG_ID
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where name_id=:name_id order by log_id desc into :ware_id;
+
  order by LOG_ID desc
  end
+
  into :WARE_ID;
  if (ware_id is null) then
+
end
   begin
+
if ((WARE_ID is null) and
    /* goodname='3';--sname; */
+
    (IZG_ID is not null)) then
    goodname=sname;
+
begin
    name_id=null;
+
  select first 1 ID
  end
+
  from WARES
  else
+
  where IZG_ID = :IZG_ID
  begin
+
  into :WARE_ID;
    /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
+
  if (WARE_ID is null) then
    select w.name_id, v.svalue from WARES w left join vals v on w.name_id=v.id where w.id=:ware_id into :name_id, :goodname;
+
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
  end
+
  from WARES_LOG
 +
  where IZG_ID = :IZG_ID
 +
  order by LOG_ID desc
 +
  into :WARE_ID;
 +
end
 +
if (WARE_ID is null) then
 +
   GOODIZG = SIZG;
 +
else
 +
begin
 +
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
 +
  select W.IZG_ID,
 +
        V.SVALUE
 +
  from WARES W
 +
  left join VALS V on W.IZG_ID = V.ID
 +
  where W.ID = :WARE_ID
 +
  into :IZG_ID,
 +
      :GOODIZG;
 +
end
  
 
+
WARE_ID = null;
  ware_id=null;
+
if (ORIG_COUNTRY_ID is not null) then
  if (orig_izg_id is not null) then
+
begin
  begin
+
  select first 1 ID
    select first 1 id from wares where orig_izg_id=:orig_izg_id into :ware_id;
+
  from WARES
    if (ware_id is null) then
+
  where ORIG_COUNTRY_ID = :ORIG_COUNTRY_ID
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where orig_izg_id=:orig_izg_id order by log_id desc into :ware_id;
+
  into :WARE_ID;
  end
+
  if (WARE_ID is null) then
  if ((ware_id is null) and (izg_id is not null)) then
+
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
  begin
+
  from WARES_LOG
    select first 1 id from wares where izg_id=:izg_id into :ware_id;
+
  where ORIG_COUNTRY_ID = :ORIG_COUNTRY_ID
    if (ware_id is null) then
+
  order by LOG_ID desc
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where izg_id=:izg_id order by log_id desc into :ware_id;
+
  into :WARE_ID;
   end
+
end
  if (ware_id is null) then
+
if ((WARE_ID is null) and
    goodizg=sizg;
+
    (COUNTRY_ID is not null)) then
  else
+
begin
  begin
+
  select first 1 ID
    /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
+
  from WARES
    select w.izg_id, v.svalue from WARES w left join vals v on w.izg_id=v.id where w.id=:ware_id into :izg_id, :goodizg;
+
   where COUNTRY_ID = :COUNTRY_ID
   end
+
   into :WARE_ID;
 
+
  if (WARE_ID is null) then
 
+
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
  ware_id=null;
+
  from WARES_LOG
  if (orig_country_id is not null) then
+
  where COUNTRY_ID = :COUNTRY_ID
  begin
+
  order by LOG_ID desc
    select first 1 id from wares where orig_country_id=:orig_country_id into :ware_id;
+
  into :WARE_ID;
    if (ware_id is null) then
+
end
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where orig_country_id=:orig_country_id order by log_id desc into :ware_id;
+
if (WARE_ID is null) then
  end
+
  GOODCOUNTRY = SCOUNTRY;
  if ((ware_id is null) and (country_id is not null)) then
+
else
  begin
+
begin
    select first 1 id from wares where country_id=:country_id into :ware_id;
+
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
    if (ware_id is null) then
+
  select W.COUNTRY_ID,
      select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where country_id=:country_id order by log_id desc into :ware_id;
+
        V.SVALUE
  end
+
  from WARES W
  if (ware_id is null) then
+
  left join VALS V on W.COUNTRY_ID = V.ID
    goodcountry=scountry;
+
  where W.ID = :WARE_ID
  else
+
  into :COUNTRY_ID,
  begin
+
      :GOODCOUNTRY;
    /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
+
end
    select w.country_id, v.svalue from WARES w left join vals v on w.country_id=v.id where w.id=:ware_id into :country_id, :goodcountry;
+
  end
+
  
 
  /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
 
  /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
 
  /*  insert into doc_detail_active_log(id,sname,sizg,scountry, part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */
 
  /*  insert into doc_detail_active_log(id,sname,sizg,scountry, part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */
  
  /* goodname='3'; */
+
/* goodname='3'; */
  goodbarcode=barcode;
+
GOODBARCODE = BARCODE;
  
  if (goodname is null)   then goodname = sname;
+
if (GOODNAME is null) then
  if (goodizg is null) then goodizg = sizg;
+
  GOODNAME = SNAME;
  if (goodcountry is null) then goodcountry = scountry;
+
if (GOODIZG is null) then
  if (goodname is null)   then goodname = sorig_name;
+
  GOODIZG = SIZG;
  if (goodizg is null) then goodizg = sorig_izg;
+
if (GOODCOUNTRY is null) then
  if (goodcountry is null) then goodcountry = sorig_country;
+
  GOODCOUNTRY = SCOUNTRY;
 +
if (GOODNAME is null) then
 +
  GOODNAME = SORIG_NAME;
 +
if (GOODIZG is null) then
 +
  GOODIZG = SORIG_IZG;
 +
if (GOODCOUNTRY is null) then
 +
  GOODCOUNTRY = SORIG_COUNTRY;
  
  suspend;
+
suspend;
 
end;
 
end;
 
  
 
create or alter procedure PR_GET_WARE (
 
create or alter procedure PR_GET_WARE (
Строка 296: Строка 476:
 
begin
 
begin
  
  select VAL_ID from PR_GETVAL_ID(:SNAME, 0, :ALTTYPE, :MNN) into :NAME_ID;
+
select VAL_ID
  select VAL_ID from PR_GETVAL_ID(:SIZG, 3, :ALTTYPE) into :IZG_ID;
+
from PR_GETVAL_ID(:SNAME, 0, :ALTTYPE, :MNN)
  select VAL_ID from PR_GETVAL_ID(:SCOUNTRY, 2, :ALTTYPE) into :COUNTRY_ID;
+
into :NAME_ID;
  select val_id from pr_getval_id(:sorig_name,1,:alttype) into :orig_name_id;
+
select VAL_ID
  select val_id from pr_getval_id(:sorig_izg,6,:alttype) into :orig_izg_id;
+
from PR_GETVAL_ID(:SIZG, 3, :ALTTYPE)
  select val_id from pr_getval_id(:sorig_country,5,:alttype) into :orig_country_id;
+
into :IZG_ID;
  /*
+
select VAL_ID
 +
from PR_GETVAL_ID(:SCOUNTRY, 2, :ALTTYPE)
 +
into :COUNTRY_ID;
 +
select VAL_ID
 +
from PR_GETVAL_ID(:SORIG_NAME, 1, :ALTTYPE)
 +
into :ORIG_NAME_ID;
 +
select VAL_ID
 +
from PR_GETVAL_ID(:SORIG_IZG, 6, :ALTTYPE)
 +
into :ORIG_IZG_ID;
 +
select VAL_ID
 +
from PR_GETVAL_ID(:SORIG_COUNTRY, 5, :ALTTYPE)
 +
into :ORIG_COUNTRY_ID;
 +
/*
 
   select first 1 id from wares where NAME_ID=:name_id and IZG_ID=:izg_id and COUNTRY_ID=:country_id and
 
   select first 1 id from wares where NAME_ID=:name_id and IZG_ID=:izg_id and COUNTRY_ID=:country_id and
 
   ORIG_CODE=:orig_code and ORIG_NAME_ID=:orig_name_id and
 
   ORIG_CODE=:orig_code and ORIG_NAME_ID=:orig_name_id and
Строка 308: Строка 500:
 
   and barcode=:barcode into :w_id;
 
   and barcode=:barcode into :w_id;
 
   */
 
   */
  select ID from WARES where NAME_ID = :NAME_ID and IZG_ID = :IZG_ID and COUNTRY_ID = :COUNTRY_ID and BARCODE = :BARCODE into :W_ID;
+
select ID
 +
from WARES
 +
where NAME_ID = :NAME_ID
 +
      and IZG_ID = :IZG_ID
 +
      and COUNTRY_ID = :COUNTRY_ID
 +
      and BARCODE = :BARCODE
 +
into :W_ID;
  
  if (W_ID is null) then
+
if (W_ID is null) then
  begin
+
begin
    /* exception EX_WRONG_OPER; */
+
  /* exception EX_WRONG_OPER; */
    /* w_id=gen_id(gen_wares_id,1); */
+
  /* w_id=gen_id(gen_wares_id,1); */
    W_ID = uuid_to_char(gen_uuid());
+
  W_ID = uuid_to_char(gen_uuid());
    insert into WARES (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, BARCODE, Z_ID, SKLAD_ID)
+
  insert into WARES (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, BARCODE,
    values (:W_ID, :NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, :BARCODE, :Z_ID, :SKLAD_ID);
+
                    Z_ID, SKLAD_ID)
  end
+
  values (:W_ID, :NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, :BARCODE,
  suspend;
+
          :Z_ID, :SKLAD_ID);
 +
end
 +
suspend;
 
end;
 
end;
 
  
 
create or alter procedure PR_GETWARE_BY_IDS (
 
create or alter procedure PR_GETWARE_BY_IDS (
Строка 337: Строка 536:
 
as
 
as
 
begin
 
begin
  if (orig_code is null) then orig_code='';
+
if (ORIG_CODE is null) then
  if (searchinlogs is null) then searchinlogs=0;
+
  ORIG_CODE = '';
  if (searchinlogs=1) then
+
if (SEARCHINLOGS is null) then
  begin  
+
  SEARCHINLOGS = 0;
    select first 1 iif(actual_ware_id is null, id, actual_ware_id) from wares_log where name_id=:name_id and izg_id=:izg_id and country_id=:country_id and barcode=:barcode
+
if (SEARCHINLOGS = 1) then
order by log_insertdt desc into :ware_id;
+
begin
select id from wares where id = :ware_id into :ware_id;
+
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
  end
+
  from WARES_LOG
  else
+
  where NAME_ID = :NAME_ID
  begin
+
        and IZG_ID = :IZG_ID
    select first 1 id from wares where name_id=:name_id and izg_id=:izg_id and country_id=:country_id and barcode=:barcode
+
        and COUNTRY_ID = :COUNTRY_ID
    into :ware_id;
+
        and BARCODE = :BARCODE
  end
+
  order by LOG_INSERTDT desc
  if ((ware_id is null) and (DOINSERT=1) and (searchinlogs<>1)) then
+
  into :WARE_ID;
  begin
+
  select ID
    /* ware_id=gen_id(gen_wares_id,1); */
+
  from WARES
    ware_id=UUID_TO_CHAR(GEN_UUID());
+
  where ID = :WARE_ID
    insert into wares (ID,NAME_ID,IZG_ID,COUNTRY_ID,ORIG_CODE,ORIG_NAME_ID,ORIG_IZG_ID,ORIG_COUNTRY_ID,Z_ID,SKLAD_ID, barcode)
+
  into :WARE_ID;
    values
+
end
      (:ware_id,:NAME_ID,:IZG_ID,:COUNTRY_ID,:ORIG_CODE,:ORIG_NAME_ID,:ORIG_IZG_ID,:ORIG_COUNTRY_ID,0,'',:barcode);
+
else
  end
+
begin
  suspend;
+
  select first 1 ID
 +
  from WARES
 +
  where NAME_ID = :NAME_ID
 +
        and IZG_ID = :IZG_ID
 +
        and COUNTRY_ID = :COUNTRY_ID
 +
        and BARCODE = :BARCODE
 +
  into :WARE_ID;
 +
end
 +
if ((WARE_ID is null) and
 +
    (DOINSERT = 1) and
 +
    (SEARCHINLOGS <> 1)) then
 +
begin
 +
  /* ware_id=gen_id(gen_wares_id,1); */
 +
  WARE_ID = uuid_to_char(gen_uuid());
 +
  insert into WARES (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID,
 +
                    SKLAD_ID, BARCODE)
 +
  values (:WARE_ID, :NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, '',
 +
          :BARCODE);
 +
end
 +
suspend;
 
end;
 
end;
 
  
 
create or alter procedure PR_DOC_COMMIT (
 
create or alter procedure PR_DOC_COMMIT (
Строка 378: Строка 595:
 
declare variable WARES_MODE DM_STATUS;
 
declare variable WARES_MODE DM_STATUS;
 
begin
 
begin
  select DT.BASE_TYPE, D.PRICE_TYPE, D.CONTRACT_ID from DOCS D
+
select DT.BASE_TYPE,
  left join DOC_TYPES DT on D.DOC_TYPE = DT.ID where D.ID = :DOC_ID into :BASE_TYPE, :PRICE_TYPE, :CONTRACT_ID;
+
        D.PRICE_TYPE,
  if (BASE_TYPE in (1, 3, 6, 8)) then
+
        D.CONTRACT_ID
  begin
+
from DOCS D
 +
left join DOC_TYPES DT on D.DOC_TYPE = DT.ID
 +
where D.ID = :DOC_ID
 +
into :BASE_TYPE,
 +
      :PRICE_TYPE,
 +
      :CONTRACT_ID;
 +
if (BASE_TYPE in (1, 3, 6, 8)) then
 +
begin
  
    wrong_wares = 0; wares_mode = 1;
+
  WRONG_WARES = 0;
    select PARAM_VALUE from PR_GETPARAMVALUE('WARES_MODE', 0) into wares_mode;
+
  WARES_MODE = 1;
    if (wares_mode = 0) then
+
  select PARAM_VALUE
    begin
+
  from PR_GETPARAMVALUE('WARES_MODE', 0)
      select count(id) from doc_detail_active where doc_id = :doc_id and ((ware_id = '0') or (ware_id = '-1') or (ware_id = '')) into wrong_wares;
+
  into WARES_MODE;
      if (wrong_wares > 0) then exception EX_WRONG_OPER;
+
   if (WARES_MODE = 0) then
    end
+
 
+
    execute procedure PR_DOC_PRIHOD_COMMIT(:DOC_ID, 0);
+
  end
+
  else
+
   if (BASE_TYPE in (2, 7, 9)) then
+
    execute procedure PR_DOC_RASHOD_COMMIT(:DOC_ID, 0);
+
  else
+
    exception EX_WRONGDOC_BASE_TYPE;
+
  delete from DOC_DETAIL_ACTIVE where DOC_ID = :DOC_ID;
+
  if (BASE_TYPE = 3) then
+
 
   begin
 
   begin
    delete from WAREBASE where PART_ID in (select PART_ID from DOC_DETAIL where DOC_ID = :DOC_ID and QUANT < 0 and PART_TYPE = 1);
+
  select count(ID)
 +
  from DOC_DETAIL_ACTIVE
 +
  where DOC_ID = :DOC_ID
 +
        and ((WARE_ID = '0') or (WARE_ID = '-1') or (WARE_ID = ''))
 +
  into WRONG_WARES;
 +
  if (WRONG_WARES > 0) then
 +
    exception EX_WRONG_OPER;
 
   end
 
   end
   select sum(SUMMA), sum(SUMMA_O), sum(SUM_NDSO), sum(PRICE * QUANT / 10000), sum(SUM_DSC) from DOC_DETAIL where DOC_ID = :DOC_ID into :SUMMA,
+
 
      :SUMMA_O, :SUM_NDSO, :DSCSUMMA, :SUM_DSC;
+
   execute procedure PR_DOC_PRIHOD_COMMIT(:DOC_ID, 0);
  DSCSUMMA = DSCSUMMA * 10000;
+
end
  update DOCS set COMMITSESSION_ID = :SESSION_ID, STATUS = 1, POSTDT = 'now',
+
else
                  CHECKDATA = (select CHECKDATA from PR_DOCCHECKDATA(:DOC_ID)), SUMMA = :SUMMA, SUMMA_O = :SUMMA_O,
+
if (BASE_TYPE in (2, 7, 9)) then
                  SUM_NDSO = :SUM_NDSO, CALCSUMMA = :DSCSUMMA, SUM_DSC = :SUM_DSC, CASHDATA = (iif(:BASE_TYPE = 1, -:SUMMA, null)) where ID = :DOC_ID;
+
  execute procedure PR_DOC_RASHOD_COMMIT(:DOC_ID, 0);
  execute procedure PR_UPDBLOCKINFO_BY_DOC(:DOC_ID, 1);
+
else
  /* if ((base_type = 2) and (contract_id<>0)) then */
+
  exception EX_WRONGDOC_BASE_TYPE;
  if (CONTRACT_ID <> 0) then
+
delete from DOC_DETAIL_ACTIVE
  begin
+
where DOC_ID = :DOC_ID;
    if (BASE_TYPE = 2) then
+
if (BASE_TYPE = 3) then
      execute procedure PR_CREDITCOMMIT(:CONTRACT_ID);
+
begin
    if (BASE_TYPE in (8, 9)) then
+
  delete from WAREBASE
      execute procedure PR_CREDITREINITCOMMIT(:CONTRACT_ID, :SESSION_ID);
+
  where PART_ID in (select PART_ID
    /* execute procedure PR_CREDITCOMMIT(:contract_id); */
+
                    from DOC_DETAIL
  end
+
                    where DOC_ID = :DOC_ID
  execute procedure PR_DOEVENT('EV_DOCCOMMIT');
+
                          and QUANT < 0
 +
                          and PART_TYPE = 1);
 +
end
 +
select sum(SUMMA),
 +
        sum(SUMMA_O),
 +
        sum(SUM_NDSO),
 +
        sum(PRICE * QUANT / 10000),
 +
        sum(SUM_DSC)
 +
from DOC_DETAIL
 +
where DOC_ID = :DOC_ID
 +
into :SUMMA,
 +
      :SUMMA_O,
 +
      :SUM_NDSO,
 +
      :DSCSUMMA,
 +
      :SUM_DSC;
 +
DSCSUMMA = DSCSUMMA * 10000;
 +
update DOCS
 +
set COMMITSESSION_ID = :SESSION_ID,
 +
    STATUS = 1,
 +
    POSTDT = 'now',
 +
    CHECKDATA = (select CHECKDATA
 +
                  from PR_DOCCHECKDATA(:DOC_ID)),
 +
    SUMMA = :SUMMA,
 +
    SUMMA_O = :SUMMA_O,
 +
    SUM_NDSO = :SUM_NDSO,
 +
    CALCSUMMA = :DSCSUMMA,
 +
    SUM_DSC = :SUM_DSC,
 +
    CASHDATA = (iif(:BASE_TYPE = 1, -:SUMMA, null))
 +
where ID = :DOC_ID;
 +
execute procedure PR_UPDBLOCKINFO_BY_DOC(:DOC_ID, 1);
 +
/* if ((base_type = 2) and (contract_id<>0)) then */
 +
if (CONTRACT_ID <> 0) then
 +
begin
 +
  if (BASE_TYPE = 2) then
 +
  execute procedure PR_CREDITCOMMIT(:CONTRACT_ID);
 +
  if (BASE_TYPE in (8, 9)) then
 +
  execute procedure PR_CREDITREINITCOMMIT(:CONTRACT_ID, :SESSION_ID);
 +
  /* execute procedure PR_CREDITCOMMIT(:contract_id); */
 +
end
 +
execute procedure PR_DOEVENT('EV_DOCCOMMIT');
 
end;
 
end;
  
 
+
create or alter trigger DOC_DETAIL_ACTIVE_BI for DOC_DETAIL_ACTIVE
CREATE OR ALTER TRIGGER DOC_DETAIL_ACTIVE_BI FOR DOC_DETAIL_ACTIVE
+
active before insert position 0
ACTIVE BEFORE INSERT POSITION 0
+
 
as
 
as
 
begin
 
begin
  if (new.ID is null) then
+
if (new.ID is null) then
    new.ID = gen_id(GEN_DOC_DETAIL_ACTIVE_ID, 1);
+
  new.ID = gen_id(GEN_DOC_DETAIL_ACTIVE_ID, 1);
  new.INSERTDT = 'now';
+
new.INSERTDT = 'now';
  if (new.BASE_AGENT_ID is null) then
+
if (new.BASE_AGENT_ID is null) then
    new.BASE_AGENT_ID = 0;
+
  new.BASE_AGENT_ID = 0;
  if (new.STATUS is null) then
+
if (new.STATUS is null) then
    new.STATUS = 0;
+
  new.STATUS = 0;
  if (new.PARENT_ID is null) then
+
if (new.PARENT_ID is null) then
    new.PARENT_ID = 0;
+
  new.PARENT_ID = 0;
  if (new.PART_TYPE is null) then
+
if (new.PART_TYPE is null) then
    new.PART_TYPE = 0;
+
  new.PART_TYPE = 0;
  
 +
/* первая строчка =  до привидения, оригианльные значения накладной */
 +
/* select first 1 ID from WARES where NAME_ID = new.NAME_ID and IZG_ID = new.IZG_ID and COUNTRY_ID = new.COUNTRY_ID and BARCODE = new.BCODE_IZG into new.WARE_ID; */
  
  /* первая строчка = до привидения, оригианльные значения накладной */
+
  insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
  /* select first 1 ID from WARES where NAME_ID = new.NAME_ID and IZG_ID = new.IZG_ID and COUNTRY_ID = new.COUNTRY_ID and BARCODE = new.BCODE_IZG into new.WARE_ID; */
+
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
 +
                                    BARCODE1, BCODE_IZG, GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT,
 +
                                    SDSERT, REGN, NGTD, EDIZM, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID,
 +
                                    ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME,
 +
                                    SORIG_IZG, SORIG_COUNTRY, INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC,
 +
                                    HUMAN_QUANT, CUSTOMDRAW, PART_TYPE, BASE_AGENT_ID)
 +
values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
 +
        new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z, new.PRICE_R,
 +
        new.BARCODE, new.BARCODE1, new.BCODE_IZG, new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO, new.SERT,
 +
        new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN, new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID, new.COUNTRY_ID,
 +
        new.ORIG_CODE, new.ORIG_NAME_ID, new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID, new.SNAME,
 +
        new.SIZG, new.SCOUNTRY, new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO, new.KOEF,
 +
        new.MOTHERPART_ID, new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE, new.BASE_AGENT_ID);
 +
/* -- */
  
 +
if ((new.PART_ID is null) or (new.PART_ID = 0)) then
 +
begin
 +
  /* >cf20110923 */
 +
  /* if (new.ware_id<0) then */
 +
  if (new.WARE_ID = '-1') then
 +
  begin
 +
  select GOODNAME,
 +
          GOODIZG,
 +
          GOODCOUNTRY,
 +
          GOODBARCODE
 +
  from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.BCODE_IZG)
 +
  into new.SNAME,
 +
        new.SIZG,
 +
        new.SCOUNTRY,
 +
        new.BCODE_IZG;
 +
  end
 +
  /* <cf20110923 */
 +
  new.PART_ID = 0;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 0
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SNAME
 +
  into new.NAME_ID;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 3
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SIZG
 +
  into new.IZG_ID;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 2
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SCOUNTRY
 +
  into new.COUNTRY_ID;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 1
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SORIG_NAME
 +
  into new.ORIG_NAME_ID;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 6
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SORIG_IZG
 +
  into new.ORIG_IZG_ID;
 +
  select ID
 +
  from VALS
 +
  where VTYPE = 5
 +
        and ALTTYPE = new.PART_TYPE
 +
        and SVALUE = new.SORIG_COUNTRY
 +
  into new.ORIG_COUNTRY_ID;
 +
  new.WARE_ID = 0;
 +
  select first 1 ID
 +
  from WARES
 +
  where NAME_ID = new.NAME_ID
 +
        and IZG_ID = new.IZG_ID
 +
        and COUNTRY_ID = new.COUNTRY_ID
 +
        and BARCODE = new.BCODE_IZG
 +
  into new.WARE_ID;
 +
 +
  /* вторая строчка = после приведения */
 
   insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
 
   insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
 
                                     DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
 
                                     DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
                                     BARCODE1, BCODE_IZG,
+
                                     BARCODE1, BCODE_IZG, GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT,
                                    GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM,
+
                                    SDSERT, REGN, NGTD, EDIZM, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID,
                                    NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID,
+
                                    ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME,
                                    Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME, SORIG_IZG, SORIG_COUNTRY,
+
                                    SORIG_IZG, SORIG_COUNTRY, INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC,
                                    INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC, HUMAN_QUANT, CUSTOMDRAW,
+
                                    HUMAN_QUANT, CUSTOMDRAW, PART_TYPE, BASE_AGENT_ID)
                                    PART_TYPE, BASE_AGENT_ID)
+
 
   values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
 
   values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
 
           new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z,
 
           new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z,
           new.PRICE_R, new.BARCODE, new.BARCODE1, new.BCODE_IZG,
+
           new.PRICE_R, new.BARCODE, new.BARCODE1, new.BCODE_IZG, new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO,
          new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO, new.SERT, new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN,
+
          new.SERT, new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN, new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID,
          new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID, new.COUNTRY_ID, new.ORIG_CODE, new.ORIG_NAME_ID,
+
          new.COUNTRY_ID, new.ORIG_CODE, new.ORIG_NAME_ID, new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID,
          new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID, new.SNAME, new.SIZG, new.SCOUNTRY,
+
          new.SNAME, new.SIZG, new.SCOUNTRY, new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO,
          new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO, new.KOEF, new.MOTHERPART_ID,
+
          new.KOEF, new.MOTHERPART_ID, new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE,
          new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE, new.BASE_AGENT_ID);
+
          new.BASE_AGENT_ID);
 
   /* -- */
 
   /* -- */
  
  if ((new.PART_ID is null) or (new.PART_ID = 0)) then
+
end
  begin
+
if (new.PART_PARENT_ID is null) then
    /* >cf20110923 */
+
  new.PART_PARENT_ID = 0;
    /* if (new.ware_id<0) then */
+
if (new.WARE_ID is null) then
    if (new.WARE_ID = '-1') then
+
  new.WARE_ID = 0;
    begin
+
if (new.NAME_ID is null) then
      select GOODNAME, GOODIZG, GOODCOUNTRY, GOODBARCODE from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.bcode_izg)
+
  new.NAME_ID = 0;
      into new.SNAME, new.SIZG, new.SCOUNTRY, new.BCODE_IZG;
+
if (new.IZG_ID is null) then
    end
+
  new.IZG_ID = 0;
    /* <cf20110923 */
+
if (new.COUNTRY_ID is null) then
    new.PART_ID = 0;
+
  new.COUNTRY_ID = 0;
    select ID from VALS where VTYPE = 0 and ALTTYPE = new.PART_TYPE and SVALUE = new.SNAME into new.NAME_ID;
+
if (new.ORIG_NAME_ID is null) then
    select ID from VALS where VTYPE = 3 and ALTTYPE = new.PART_TYPE and SVALUE = new.SIZG into new.IZG_ID;
+
  new.ORIG_NAME_ID = 0;
    select ID from VALS where VTYPE = 2 and ALTTYPE = new.PART_TYPE and SVALUE = new.SCOUNTRY into new.COUNTRY_ID;
+
if (new.ORIG_IZG_ID is null) then
    select ID from VALS where VTYPE = 1 and ALTTYPE = new.PART_TYPE and SVALUE = new.SORIG_NAME into new.ORIG_NAME_ID;
+
  new.ORIG_IZG_ID = 0;
    select ID from VALS where VTYPE = 6 and ALTTYPE = new.PART_TYPE and SVALUE = new.SORIG_IZG into new.ORIG_IZG_ID;
+
if (new.ORIG_COUNTRY_ID is null) then
    select ID from VALS where VTYPE = 5 and ALTTYPE = new.PART_TYPE and SVALUE = new.SORIG_COUNTRY into new.ORIG_COUNTRY_ID;
+
  new.ORIG_COUNTRY_ID = 0;
    new.WARE_ID = 0;
+
if (new.Z_ID is null) then
    select first 1 ID from WARES where NAME_ID = new.NAME_ID and IZG_ID = new.IZG_ID and COUNTRY_ID = new.COUNTRY_ID and BARCODE = new.BCODE_IZG into new.WARE_ID;
+
  new.Z_ID = 0;
 +
if (new.SKLAD_ID is null) then
 +
  new.SKLAD_ID = '';
 +
if (new.MOTHERPART_ID is null) then
 +
  new.MOTHERPART_ID = 0;
 +
if (new.DOC_DETAIL_ID is null) then
 +
  new.DOC_DETAIL_ID = 0;
  
    /* вторая строчка = после приведения */
+
if (new.PART_ID > 0) then
    insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
+
/* заполняем значения партии */
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
+
begin
                                    BARCODE1, BCODE_IZG,
+
  select WARE_ID,
                                    GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM,
+
        PRICE,
                                    NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID,
+
        PRICE_O,
                                    Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME, SORIG_IZG, SORIG_COUNTRY,
+
        PRICE_Z,
                                    INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC, HUMAN_QUANT, CUSTOMDRAW,
+
        PRICE_R,
                                    PART_TYPE, BASE_AGENT_ID)
+
        /* QUANT, */
    values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
+
        /* BARCODE, */
          new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z,
+
        BARCODE1,
          new.PRICE_R, new.BARCODE, new.BARCODE1, new.BCODE_IZG,
+
        /* KRITK, */
          new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO, new.SERT, new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN,
+
        GODENDO,
          new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID, new.COUNTRY_ID, new.ORIG_CODE, new.ORIG_NAME_ID,
+
        SERIA,
          new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID, new.SNAME, new.SIZG, new.SCOUNTRY,
+
        NDS,
          new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO, new.KOEF, new.MOTHERPART_ID,
+
        /* SUM_NDSO, */
          new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE, new.BASE_AGENT_ID);
+
        SERT,
    /* -- */
+
        DATESERT,
 +
        KEMVSERT,
 +
        SDSERT,
 +
        REGN,
 +
        NGTD,
 +
        EDIZM,
 +
        NAC,
 +
        DEP,
 +
        PART_TYPE,
 +
        BASE_AGENT_ID,
 +
        GROUP_ID
 +
  from PARTS
 +
  where ID = new.PART_ID
 +
  into new.WARE_ID,
 +
      new.PRICE,
 +
      new.PRICE_O,
 +
      new.PRICE_Z,
 +
      new.PRICE_R,
  
  end
+
      /* new.QUANT, */
  if (new.PART_PARENT_ID is null) then
+
      /* new.BARCODE, */
    new.PART_PARENT_ID = 0;
+
      new.BARCODE1,
  if (new.WARE_ID is null) then
+
    new.WARE_ID = 0;
+
  if (new.NAME_ID is null) then
+
    new.NAME_ID = 0;
+
  if (new.IZG_ID is null) then
+
    new.IZG_ID = 0;
+
  if (new.COUNTRY_ID is null) then
+
    new.COUNTRY_ID = 0;
+
  if (new.ORIG_NAME_ID is null) then
+
    new.ORIG_NAME_ID = 0;
+
  if (new.ORIG_IZG_ID is null) then
+
    new.ORIG_IZG_ID = 0;
+
  if (new.ORIG_COUNTRY_ID is null) then
+
    new.ORIG_COUNTRY_ID = 0;
+
  if (new.Z_ID is null) then
+
    new.Z_ID = 0;
+
  if (new.SKLAD_ID is null) then
+
    new.SKLAD_ID = '';
+
  if (new.MOTHERPART_ID is null) then
+
    new.MOTHERPART_ID = 0;
+
  if (new.DOC_DETAIL_ID is null) then
+
    new.DOC_DETAIL_ID = 0;
+
  
   if (new.PART_ID > 0) then /* заполняем значения партии */
+
      /* new.KRITK,*/
 +
      new.GODENDO,
 +
      new.SERIA,
 +
      new.NDS,
 +
 
 +
      /* new.SUM_NDSO, */
 +
      new.SERT,
 +
      new.DATESERT,
 +
      new.KEMVSERT,
 +
      new.SDSERT,
 +
      new.REGN,
 +
      new.NGTD,
 +
      new.EDIZM,
 +
      new.NAC,
 +
      new.DEP,
 +
      new.PART_TYPE,
 +
      new.BASE_AGENT_ID,
 +
      new.GROUP_ID;
 +
end
 +
if (new.GROUP_ID is null) then
 +
   new.GROUP_ID = 0;
 +
/* if (new.ware_id>0) then -- заполняем значения позиции */
 +
if (new.WARE_ID not in ('-1', '0')) then
 +
/* заполняем значения позиции */
 +
begin
 +
  select WW.NAME_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.NAME_ID),
 +
        WW.IZG_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.IZG_ID),
 +
        WW.COUNTRY_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.COUNTRY_ID),
 +
        WW.ORIG_CODE,
 +
        WW.ORIG_NAME_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.ORIG_NAME_ID),
 +
        WW.ORIG_IZG_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.ORIG_IZG_ID),
 +
        WW.ORIG_COUNTRY_ID,
 +
        (select SVALUE
 +
          from VALS
 +
          where ID = WW.ORIG_COUNTRY_ID),
 +
        WW.BARCODE,
 +
        WW.Z_ID,
 +
        WW.SKLAD_ID
 +
  from WARES WW
 +
  where WW.ID = new.WARE_ID
 +
  into new.NAME_ID,
 +
      new.SNAME,
 +
      new.IZG_ID,
 +
      new.SIZG,
 +
      new.COUNTRY_ID,
 +
      new.SCOUNTRY,
 +
      new.ORIG_CODE,
 +
      new.ORIG_NAME_ID,
 +
      new.SORIG_NAME,
 +
      new.ORIG_IZG_ID,
 +
      new.SORIG_IZG,
 +
      new.ORIG_COUNTRY_ID,
 +
      new.SORIG_COUNTRY,
 +
      new.BCODE_IZG,
 +
      new.Z_ID,
 +
      new.SKLAD_ID;
 +
end
 +
else
 +
begin
 +
  /* if (new.ware_id<0) then */
 +
  if (new.WARE_ID = '-1') then
 
   begin
 
   begin
    select WARE_ID, PRICE, PRICE_O, PRICE_Z, PRICE_R,
+
  select GOODNAME,
          /* QUANT, */
+
          GOODIZG,
          /* BARCODE, */
+
          GOODCOUNTRY
          BARCODE1,
+
  from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.BCODE_IZG)
          /* KRITK, */
+
  into new.SNAME,
          GODENDO, SERIA, NDS,
+
        new.SIZG,
          /* SUM_NDSO, */
+
        new.SCOUNTRY;
          SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM, NAC, DEP, PART_TYPE, BASE_AGENT_ID, GROUP_ID from PARTS where ID = new.PART_ID into new.WARE_ID,
+
  new.WARE_ID = 0;
        new.PRICE, new.PRICE_O, new.PRICE_Z, new.PRICE_R,
+
        /* new.QUANT, */
+
        /* new.BARCODE, */
+
        new.BARCODE1,
+
        /* new.KRITK,*/
+
        new.GODENDO, new.SERIA, new.NDS,
+
        /* new.SUM_NDSO, */
+
        new.SERT, new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN, new.NGTD, new.EDIZM, new.NAC, new.DEP,
+
        new.PART_TYPE, new.BASE_AGENT_ID, new.GROUP_ID;
+
  end
+
  if (new.GROUP_ID is null) then
+
    new.GROUP_ID = 0;
+
  /* if (new.ware_id>0) then -- заполняем значения позиции */
+
  if (new.WARE_ID not in ('-1', '0')) then /* заполняем значения позиции */
+
  begin
+
    select WW.NAME_ID,
+
          (select SVALUE from VALS where ID = WW.NAME_ID), WW.IZG_ID,
+
          (select SVALUE from VALS where ID = WW.IZG_ID), WW.COUNTRY_ID,
+
          (select SVALUE from VALS where ID = WW.COUNTRY_ID), WW.ORIG_CODE, WW.ORIG_NAME_ID,
+
          (select SVALUE from VALS where ID = WW.ORIG_NAME_ID), WW.ORIG_IZG_ID,
+
          (select SVALUE from VALS where ID = WW.ORIG_IZG_ID), WW.ORIG_COUNTRY_ID,
+
          (select SVALUE from VALS where ID = WW.ORIG_COUNTRY_ID), WW.BARCODE, WW.Z_ID, WW.SKLAD_ID from WARES WW where WW.ID = new.WARE_ID into new.NAME_ID,
+
        new.SNAME, new.IZG_ID, new.SIZG, new.COUNTRY_ID, new.SCOUNTRY, new.ORIG_CODE, new.ORIG_NAME_ID, new.SORIG_NAME,
+
        new.ORIG_IZG_ID, new.SORIG_IZG, new.ORIG_COUNTRY_ID, new.SORIG_COUNTRY, new.BCODE_IZG, new.Z_ID, new.SKLAD_ID;
+
 
   end
 
   end
 +
end
 +
if (new.QUANT is null) then
 +
  new.QUANT = 0;
 +
if (new.PRICE is null) then
 +
  new.PRICE = 0;
 +
if (new.PRICE_O is null) then
 +
  new.PRICE_O = 0;
 +
if (new.NDS is null) then
 +
  new.NDS = 0;
 +
if (new.SUMMA is null) then
 +
  new.SUMMA = new.QUANT * new.PRICE;
 +
if (new.SUM_DSC is null) then
 +
  new.SUM_DSC = 0;
 +
if (new.SUMMA_O is null) then
 +
begin
 +
  new.SUMMA_O = new.QUANT * new.PRICE_O;
 +
end
 +
else
 +
begin
 +
  if (new.QUANT <> 0) then
 +
  new.PRICE_O = new.SUMMA_O / new.QUANT;
 +
end
 +
if (new.SUM_NDSO is null) then
 +
  new.SUM_NDSO = new.SUMMA_O * new.NDS / 100;
 +
if (((new.NAC is null) or (new.NAC = 0)) and
 +
    new.PRICE_O <> 0) then
 +
  new.NAC = (new.PRICE - new.PRICE_O) * 100 / new.PRICE_O;
 +
if (new.DEP is null) then
 +
begin
 +
  select first 1 ID
 +
  from DEPS
 +
  where NDS = new.NDS
 +
  order by ID
 +
  into new.DEP;
 +
end
 +
if (new.DEP is null) then
 +
  new.DEP = 0;
 +
/* -> 20130301-ann gen_barcode_by_name */
 +
if ((new.BARCODE is null) or (trim(new.BARCODE) = '')) then
 +
begin
 +
  if ((select PARAM_VALUE
 +
      from PR_GETPARAMVALUE('GEN_BARCODE', 0)) = 0) then
 +
  select RES_BCODE
 +
  from UTPR_GETCHECKSUM_EAN13(new.ID)
 +
  into new.BARCODE;
 +
 +
  /* else select res_bcode from utpr_getchecksum_ean13(new.name_id,'88') into new.barcode; */
 
   else
 
   else
  begin
+
  select RES_BCODE
    /* if (new.ware_id<0) then */
+
  from UTPR_GETCHECKSUM_EAN13((select L_ID
    if (new.WARE_ID = '-1') then
+
                                from VALS
    begin
+
                                where D$UUID = new.NAME_ID), '88')
      select GOODNAME, GOODIZG, GOODCOUNTRY from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.bcode_izg) into new.SNAME,
+
  into new.BARCODE;
          new.SIZG, new.SCOUNTRY;
+
end
      new.WARE_ID = 0;
+
/* -> 20130301-ann gen_barcode_by_name */
    end
+
if (new.PART_PARENT_ID > 0) then
  end
+
begin
  if (new.QUANT is null) then
+
  update DOC_DETAIL_ACTIVE
    new.QUANT = 0;
+
  set QUANT = QUANT - new.QUANT,
  if (new.PRICE is null) then
+
      SUMMA = SUMMA - new.SUMMA,
    new.PRICE = 0;
+
      SUMMA_O = SUMMA_O - new.SUMMA_O,
  if (new.PRICE_O is null) then
+
      SUM_NDSO = SUM_NDSO - new.SUM_NDSO
    new.PRICE_O = 0;
+
  where ID = new.PART_PARENT_ID;
  if (new.NDS is null) then
+
  execute procedure PR_COPY_MEMBERSHIP('DOC_DETAIL_ACTIVE', new.PART_PARENT_ID, new.ID);
    new.NDS = 0;
+
end
  if (new.SUMMA is null) then
+
    new.SUMMA = new.QUANT * new.PRICE;
+
  if (new.SUM_DSC is null) then
+
    new.SUM_DSC = 0;
+
  if (new.SUMMA_O is null) then
+
  begin
+
    new.SUMMA_O = new.QUANT * new.PRICE_O;
+
  end
+
  else
+
  begin
+
    if (new.QUANT <> 0) then
+
      new.PRICE_O = new.SUMMA_O / new.QUANT;
+
  end
+
  if (new.SUM_NDSO is null) then
+
    new.SUM_NDSO = new.SUMMA_O * new.NDS / 100;
+
  if (((new.NAC is null) or (new.NAC = 0)) and
+
      new.PRICE_O <> 0) then
+
    new.NAC = (new.PRICE - new.PRICE_O) * 100 / new.PRICE_O;
+
  if (new.DEP is null) then
+
  begin
+
    select first 1 ID from DEPS where NDS = new.NDS order by ID into new.DEP;
+
  end
+
  if (new.DEP is null) then
+
    new.DEP = 0;
+
  /* -> 20130301-ann gen_barcode_by_name */
+
  if ((new.BARCODE is null) or (trim(new.BARCODE) = '')) then
+
  begin
+
    if ((select PARAM_VALUE from PR_GETPARAMVALUE('GEN_BARCODE', 0)) = 0) then
+
      select RES_BCODE from UTPR_GETCHECKSUM_EAN13(new.ID) into new.BARCODE;
+
 
+
    /* else select res_bcode from utpr_getchecksum_ean13(new.name_id,'88') into new.barcode; */
+
    else
+
      select RES_BCODE from UTPR_GETCHECKSUM_EAN13((select L_ID from VALS where D$UUID = new.NAME_ID), '88') into new.BARCODE;
+
  end
+
  /* -> 20130301-ann gen_barcode_by_name */
+
  if (new.PART_PARENT_ID > 0) then
+
  begin
+
    update DOC_DETAIL_ACTIVE set QUANT = QUANT - new.QUANT, SUMMA = SUMMA - new.SUMMA, SUMMA_O = SUMMA_O - new.SUMMA_O, SUM_NDSO = SUM_NDSO - new.SUM_NDSO where ID = new.PART_PARENT_ID;
+
    execute procedure PR_COPY_MEMBERSHIP('DOC_DETAIL_ACTIVE', new.PART_PARENT_ID, new.ID);
+
  end
+
 
end;
 
end;
  
 
+
create or alter trigger DOC_DETAIL_ACTIVE_BU0 for DOC_DETAIL_ACTIVE
CREATE OR ALTER TRIGGER DOC_DETAIL_ACTIVE_BU0 FOR DOC_DETAIL_ACTIVE
+
active before update position 0
ACTIVE BEFORE UPDATE POSITION 0
+
 
as
 
as
 
begin
 
begin
    /*20160513
+
/*20160513
 
     if ((new.SNAME <> old.SNAME) or (new.NAME_ID is null) or (new.NAME_ID = '0')) then
 
     if ((new.SNAME <> old.SNAME) or (new.NAME_ID is null) or (new.NAME_ID = '0')) then
 
       select VAL_ID from PR_GETVAL_ID(new.SNAME, 0, new.PART_TYPE, new.MNN) into new.NAME_ID;
 
       select VAL_ID from PR_GETVAL_ID(new.SNAME, 0, new.PART_TYPE, new.MNN) into new.NAME_ID;
 
     */
 
     */
  
    if (new.NAME_ID is null) then
+
if (new.NAME_ID is null) then
      new.NAME_ID = 0;
+
  new.NAME_ID = 0;
    if ((new.SUMMA is null) and
+
if ((new.SUMMA is null) and
        ((new.QUANT <> 0) and
+
    ((new.QUANT <> 0) and
        (new.PRICE <> 0))) then
+
    (new.PRICE <> 0))) then
    begin
+
begin
      new.SUMMA = new.QUANT * new.PRICE;
+
  new.SUMMA = new.QUANT * new.PRICE;
      if (new.SUM_DSC is not null) then
+
  if (new.SUM_DSC is not null) then
        new.SUMMA = new.SUMMA - new.SUM_DSC;
+
  new.SUMMA = new.SUMMA - new.SUM_DSC;
    end
+
end
    if (((new.SUMMA_O = 0) or (new.SUMMA_O is null)) and
+
if (((new.SUMMA_O = 0) or (new.SUMMA_O is null)) and
        ((new.QUANT <> 0) and
+
    ((new.QUANT <> 0) and
        (new.PRICE_O <> 0))) then
+
    (new.PRICE_O <> 0))) then
      new.SUMMA_O = new.QUANT * new.PRICE_O;
+
  new.SUMMA_O = new.QUANT * new.PRICE_O;
    if (new.NAME_ID <> old.NAME_ID) then
+
if (new.NAME_ID <> old.NAME_ID) then
      execute procedure PR_COPYGROUPS(old.NAME_ID, new.NAME_ID);
+
  execute procedure PR_COPYGROUPS(old.NAME_ID, new.NAME_ID);
    if (new.SUM_DSC is null) then
+
if (new.SUM_DSC is null) then
      if (new.SUMMA <> old.SUMMA) then
+
  if (new.SUMMA <> old.SUMMA) then
      begin
+
  begin
        new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * new.SUMMA / (100 + old.DISCOUNT) - 100 * new.SUMMA / (100 + old.DISCOUNT);
+
  new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * new.SUMMA / (100 + old.DISCOUNT) - 100 * new.SUMMA / (100 + old.DISCOUNT);
        --new.summa=(1+new.discount/100)*100*old.summa/(100+old.discount);
+
  --new.summa=(1+new.discount/100)*100*old.summa/(100+old.discount);
      end
+
  end
    if (new.SUM_DSC is null) then
+
if (new.SUM_DSC is null) then
      if (new.DISCOUNT <> old.DISCOUNT) then
+
  if (new.DISCOUNT <> old.DISCOUNT) then
      begin
+
  begin
        new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT) - 100 * old.SUMMA / (100 + old.DISCOUNT);
+
  new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT) - 100 * old.SUMMA / (100 + old.DISCOUNT);
        new.SUMMA = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT);
+
  new.SUMMA = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT);
      end
+
  end
  
select id from vals where vtype=0 and alttype=new.part_type and svalue=new.sname into new.name_id;
+
select ID
if (new.name_id is null) then new.name_id=0;
+
from VALS
select id from vals where vtype=3 and alttype=new.part_type and svalue=new.sizg into new.izg_id;
+
where VTYPE = 0
if (new.izg_id is null) then new.izg_id=0;
+
      and ALTTYPE = new.PART_TYPE
select id from vals where vtype=2 and alttype=new.part_type and svalue=new.scountry into new.country_id;
+
      and SVALUE = new.SNAME
if (new.country_id is null) then new.country_id=0;
+
into new.NAME_ID;
select id from vals where vtype=1 and alttype=new.part_type and svalue=new.sorig_name into new.orig_name_id;
+
if (new.NAME_ID is null) then
if (new.orig_name_id is null) then new.orig_name_id=0;
+
  new.NAME_ID = 0;
select id from vals where vtype=6 and alttype=new.part_type and svalue=new.sorig_izg into new.orig_izg_id;
+
select ID
if (new.orig_izg_id is null) then new.orig_izg_id=0;
+
from VALS
select id from vals where vtype=5 and alttype=new.part_type and svalue=new.sorig_country into new.orig_country_id;
+
where VTYPE = 3
if (new.orig_country_id is null) then new.orig_country_id=0;
+
      and ALTTYPE = new.PART_TYPE
 +
      and SVALUE = new.SIZG
 +
into new.IZG_ID;
 +
if (new.IZG_ID is null) then
 +
  new.IZG_ID = 0;
 +
select ID
 +
from VALS
 +
where VTYPE = 2
 +
      and ALTTYPE = new.PART_TYPE
 +
      and SVALUE = new.SCOUNTRY
 +
into new.COUNTRY_ID;
 +
if (new.COUNTRY_ID is null) then
 +
  new.COUNTRY_ID = 0;
 +
select ID
 +
from VALS
 +
where VTYPE = 1
 +
      and ALTTYPE = new.PART_TYPE
 +
      and SVALUE = new.SORIG_NAME
 +
into new.ORIG_NAME_ID;
 +
if (new.ORIG_NAME_ID is null) then
 +
  new.ORIG_NAME_ID = 0;
 +
select ID
 +
from VALS
 +
where VTYPE = 6
 +
      and ALTTYPE = new.PART_TYPE
 +
      and SVALUE = new.SORIG_IZG
 +
into new.ORIG_IZG_ID;
 +
if (new.ORIG_IZG_ID is null) then
 +
  new.ORIG_IZG_ID = 0;
 +
select ID
 +
from VALS
 +
where VTYPE = 5
 +
      and ALTTYPE = new.PART_TYPE
 +
      and SVALUE = new.SORIG_COUNTRY
 +
into new.ORIG_COUNTRY_ID;
 +
if (new.ORIG_COUNTRY_ID is null) then
 +
  new.ORIG_COUNTRY_ID = 0;
  
 
+
insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
  insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
+
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
+
                                    BARCODE1, BCODE_IZG,
                                    BARCODE1, BCODE_IZG,
+
                                    --KRITK,
                                    --KRITK,
+
                                    GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM,
                                    GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM,
+
                                    NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID,
                                    NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID,
+
                                    Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME, SORIG_IZG, SORIG_COUNTRY,
                                    Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME, SORIG_IZG, SORIG_COUNTRY,
+
                                    INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC, HUMAN_QUANT, CUSTOMDRAW,
                                    INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC, HUMAN_QUANT, CUSTOMDRAW,
+
                                    PART_TYPE, BASE_AGENT_ID)
                                    PART_TYPE, BASE_AGENT_ID)
+
values (old.ID, old.PARENT_ID, old.DOC_ID, old.PART_ID, old.PART_PARENT_ID, old.DOC_DETAIL_ID, old.PRICE, old.NAC,
  values (old.ID, old.PARENT_ID, old.DOC_ID, old.PART_ID, old.PART_PARENT_ID, old.DOC_DETAIL_ID, old.PRICE, old.NAC,
+
        old.QUANT, old.DISCOUNT, old.SUMMA, old.SUMMA_O, old.DCARD, old.WARE_ID, old.PRICE_O, old.PRICE_Z, old.PRICE_R,
          old.QUANT, old.DISCOUNT, old.SUMMA, old.SUMMA_O, old.DCARD, old.WARE_ID, old.PRICE_O, old.PRICE_Z,
+
        old.BARCODE, old.BARCODE1, old.BCODE_IZG,
          old.PRICE_R, old.BARCODE, old.BARCODE1, old.BCODE_IZG,
+
        --old.KRITK,
          --old.KRITK,
+
        old.GODENDO, old.SERIA, old.NDS, old.SUM_NDSO, old.SERT, old.DATESERT, old.KEMVSERT, old.SDSERT, old.REGN,
          old.GODENDO, old.SERIA, old.NDS, old.SUM_NDSO, old.SERT, old.DATESERT, old.KEMVSERT, old.SDSERT, old.REGN,
+
        old.NGTD, old.EDIZM, old.NAME_ID, old.IZG_ID, old.COUNTRY_ID, old.ORIG_CODE, old.ORIG_NAME_ID, old.ORIG_IZG_ID,
          old.NGTD, old.EDIZM, old.NAME_ID, old.IZG_ID, old.COUNTRY_ID, old.ORIG_CODE, old.ORIG_NAME_ID,
+
        old.ORIG_COUNTRY_ID, old.Z_ID, old.SKLAD_ID, old.SNAME, old.SIZG, old.SCOUNTRY, old.SORIG_NAME, old.SORIG_IZG,
          old.ORIG_IZG_ID, old.ORIG_COUNTRY_ID, old.Z_ID, old.SKLAD_ID, old.SNAME, old.SIZG, old.SCOUNTRY,
+
        old.SORIG_COUNTRY, old.INSERTDT, old.INFO, old.KOEF, old.MOTHERPART_ID, old.DEP, old.SUM_DSC, old.HUMAN_QUANT,
          old.SORIG_NAME, old.SORIG_IZG, old.SORIG_COUNTRY, old.INSERTDT, old.INFO, old.KOEF, old.MOTHERPART_ID,
+
        old.CUSTOMDRAW, old.PART_TYPE, old.BASE_AGENT_ID);
          old.DEP, old.SUM_DSC, old.HUMAN_QUANT, old.CUSTOMDRAW, old.PART_TYPE, old.BASE_AGENT_ID);
+
 
end;
 
end;
  
 
+
create or alter view VW_WARES (
CREATE OR ALTER VIEW VW_WARES(
+
 
     ID,
 
     ID,
 
     NAME_ID,
 
     NAME_ID,
Строка 718: Строка 1145:
 
     MGN_ID,
 
     MGN_ID,
 
     MGN_SOURCE)
 
     MGN_SOURCE)
AS
+
as
select
+
select W.ID,
w.ID,
+
      W.NAME_ID,
w.NAME_ID,
+
      W.SNAME, --(select svalue from vals where id = w.name_id),
w.sname,--(select svalue from vals where id = w.name_id),
+
      W.IZG_ID,
w.IZG_ID,
+
      (select SVALUE
(select svalue from vals where id = w.izg_id),
+
        from VALS
w.COUNTRY_ID,
+
        where ID = W.IZG_ID),
(select svalue from vals where id = w.country_id),
+
      W.COUNTRY_ID,
w.ORIG_CODE,
+
      (select SVALUE
w.ORIG_NAME_ID,
+
        from VALS
(select svalue from vals where id = w.orig_name_id),
+
        where ID = W.COUNTRY_ID),
w.ORIG_IZG_ID,
+
      W.ORIG_CODE,
(select svalue from vals where id = w.orig_izg_id),
+
      W.ORIG_NAME_ID,
w.ORIG_COUNTRY_ID,
+
      (select SVALUE
(select svalue from vals where id = w.orig_country_id),
+
        from VALS
w.BARCODE,
+
        where ID = W.ORIG_NAME_ID),
w.Z_ID,
+
      W.ORIG_IZG_ID,
w.SKLAD_ID,
+
      (select SVALUE
w.INSERTDT,
+
        from VALS
w.PACKET,
+
        where ID = W.ORIG_IZG_ID),
w.mgn_name,
+
      W.ORIG_COUNTRY_ID,
w.mgn_id,
+
      (select SVALUE
w.mgn_source
+
        from VALS
from wares w
+
        where ID = W.ORIG_COUNTRY_ID),
;
+
      W.BARCODE,
 
+
      W.Z_ID,
 
+
      W.SKLAD_ID,
 +
      W.INSERTDT,
 +
      W.PACKET,
 +
      W.MGN_NAME,
 +
      W.MGN_ID,
 +
      W.MGN_SOURCE
 +
from WARES W;
  
 
create or alter procedure PR_DOC_DETAIL_ACTIVE_ONCOMMIT (
 
create or alter procedure PR_DOC_DETAIL_ACTIVE_ONCOMMIT (
Строка 754: Строка 1187:
 
as
 
as
 
begin
 
begin
/*
+
/*
 
   эта процедура выполняется для каждой записи, добавляемой в DOC_DETAIL в процедурах проведения документов
 
   эта процедура выполняется для каждой записи, добавляемой в DOC_DETAIL в процедурах проведения документов
 
*/
 
*/
  /* Для вречей*/
+
/* Для вречей*/
  update doc_detail_doctor set doc_detail_id=:commit_id, doc_id=:doc_id, commitdate=:commitdate where DOC_DETAIL_ACTIVE_ID=:active_id;
+
update DOC_DETAIL_DOCTOR
  /* Старые рецепты*/
+
set DOC_DETAIL_ID = :COMMIT_ID,
  update DOC_DETAIL_MAKE set doc_detail_id=:commit_id, doc_id=:doc_id, commitdate=:commitdate where DOC_DETAIL_ACTIVE_ID=:active_id;
+
    DOC_ID = :DOC_ID,
  /* Новые рецепты*/
+
    COMMITDATE = :COMMITDATE
  update recepts set table_id=:commit_id, table_name='DOC_DETAIL', status=1 where doc_detail_active_id=:active_id; --для рецептов
+
where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;
 +
/* Старые рецепты*/
 +
update DOC_DETAIL_MAKE
 +
set DOC_DETAIL_ID = :COMMIT_ID,
 +
    DOC_ID = :DOC_ID,
 +
    COMMITDATE = :COMMITDATE
 +
where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;
 +
/* Новые рецепты*/
 +
  update RECEPTS
 +
set TABLE_ID = :COMMIT_ID,
 +
    TABLE_NAME = 'DOC_DETAIL',
 +
    STATUS = 1
 +
where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;--для рецептов
  
 
  execute procedure PR_AUTO_WARESLINK(:DOC_ID, :ACTIVE_ID);
 
  execute procedure PR_AUTO_WARESLINK(:DOC_ID, :ACTIVE_ID);
  
end;
+
end;  
 
</pre>
 
</pre>

Версия 15:19, 14 июля 2016

Лешины скрипты

create or alter procedure PR_AUTO_WARESLINK (
    DOC_ID DM_ID_NULL,
    ACTIVE_ID DM_ID_NULL)
as
declare variable DDA_BARCODE DM_TEXT1024;
declare variable DDA_WARE_ID DM_UUID_NULL;
declare variable DDA_ORIG_COUNTRYID DM_UUID_NULL;
declare variable DDA_ORIG_IZGID DM_UUID_NULL;
declare variable DDA_ORIG_NAMEID DM_UUID_NULL;
declare variable DDA_COUNTRY_ID DM_UUID_NULL;
declare variable DDA_IZG_ID DM_UUID_NULL;
declare variable DDA_NAME_ID DM_UUID_NULL;
declare variable DDA_Z_ID DM_UUID_NULL;
declare variable DDA_SKLAD_ID DM_UUID_NULL;
declare variable DDALOG_BARCODE DM_TEXT1024;
declare variable DDALOG_WARE_ID DM_UUID_NULL;
declare variable DDALOG_ORIG_COUNTRYID DM_UUID_NULL;
declare variable DDALOG_ORIG_IZGID DM_UUID_NULL;
declare variable DDALOG_ORIG_NAMEID DM_UUID_NULL;
declare variable DDALOG_COUNTRY_ID DM_UUID_NULL;
declare variable DDALOG_IZG_ID DM_UUID_NULL;
declare variable DDALOG_NAME_ID DM_UUID_NULL;
declare variable DDALOG_Z_ID DM_UUID_NULL;
declare variable DDALOG_SKLAD_ID DM_UUID_NULL;
declare variable DDALOG2_BARCODE DM_TEXT1024;
declare variable DDALOG2_WARE_ID DM_UUID_NULL;
declare variable DDALOG2_ORIG_COUNTRYID DM_UUID_NULL;
declare variable DDALOG2_ORIG_IZGID DM_UUID_NULL;
declare variable DDALOG2_ORIG_NAMEID DM_UUID_NULL;
declare variable DDALOG2_COUNTRY_ID DM_UUID_NULL;
declare variable DDALOG2_IZG_ID DM_UUID_NULL;
declare variable DDALOG2_NAME_ID DM_UUID_NULL;
declare variable DDALOG2_Z_ID DM_UUID_NULL;
declare variable DDALOG2_SKLAD_ID DM_UUID_NULL;
begin

 select BCODE_IZG,
        WARE_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SNAME, 0, DDA.PART_TYPE)) as NAME_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SIZG, 3, DDA.PART_TYPE)) as IZG_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SCOUNTRY, 2, DDA.PART_TYPE)) as COUNTRY_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SORIG_NAME, 1, DDA.PART_TYPE)) as ORIG_NAME_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SORIG_IZG, 6, DDA.PART_TYPE)) as ORGI_IZG_ID,
        (select VAL_ID
         from PR_GETVAL_ID(DDA.SORIG_COUNTRY, 5, DDA.PART_TYPE)) as ORIG_COUNTRY_ID,
        Z_ID,
        SKLAD_ID
 from DOC_DETAIL_ACTIVE DDA
 where DOC_ID = :DOC_ID
       and ID = :ACTIVE_ID
 into DDA_BARCODE,
      DDA_WARE_ID,
      DDA_NAME_ID,
      DDA_IZG_ID,
      DDA_COUNTRY_ID,
      DDA_ORIG_NAMEID,
      DDA_ORIG_IZGID,
      DDA_ORIG_COUNTRYID,
      DDA_Z_ID,
      DDA_SKLAD_ID;

 select first 1 BCODE_IZG,
                WARE_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SNAME, 0, DDAL.PART_TYPE)) as NAME_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SIZG, 3, DDAL.PART_TYPE)) as IZG_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SCOUNTRY, 2, DDAL.PART_TYPE)) as COUNTRY_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SORIG_NAME, 1, DDAL.PART_TYPE)) as ORIG_NAME_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SORIG_IZG, 6, DDAL.PART_TYPE)) as ORGI_IZG_ID,
                (select VAL_ID
                 from PR_GETVAL_ID(DDAL.SORIG_COUNTRY, 5, DDAL.PART_TYPE)) as ORIG_COUNTRY_ID,
                Z_ID,
                SKLAD_ID
 from DOC_DETAIL_ACTIVE_LOG DDAL
 where DOC_ID = :DOC_ID
       and ID = :ACTIVE_ID
 order by LOG_ID
 into DDALOG_BARCODE,
      DDALOG_WARE_ID,
      DDALOG_NAME_ID,
      DDALOG_IZG_ID,
      DDALOG_COUNTRY_ID,
      DDALOG_ORIG_NAMEID,
      DDALOG_ORIG_IZGID,
      DDALOG_ORIG_COUNTRYID,
      DDALOG_Z_ID,
      DDALOG_SKLAD_ID;

 select first 1 skip 1 BCODE_IZG,
                       WARE_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SNAME, 0, DDAL.PART_TYPE)) as NAME_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SIZG, 3, DDAL.PART_TYPE)) as IZG_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SCOUNTRY, 2, DDAL.PART_TYPE)) as COUNTRY_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SORIG_NAME, 1, DDAL.PART_TYPE)) as ORIG_NAME_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SORIG_IZG, 6, DDAL.PART_TYPE)) as ORGI_IZG_ID,
                       (select VAL_ID
                        from PR_GETVAL_ID(DDAL.SORIG_COUNTRY, 5, DDAL.PART_TYPE)) as ORIG_COUNTRY_ID,
                       Z_ID,
                       SKLAD_ID
 from DOC_DETAIL_ACTIVE_LOG DDAL
 where DOC_ID = :DOC_ID
       and ID = :ACTIVE_ID
 order by LOG_ID
 into DDALOG2_BARCODE,
      DDALOG2_WARE_ID,
      DDALOG2_NAME_ID,
      DDALOG2_IZG_ID,
      DDALOG2_COUNTRY_ID,
      DDALOG2_ORIG_NAMEID,
      DDALOG2_ORIG_IZGID,
      DDALOG2_ORIG_COUNTRYID,
      DDALOG2_Z_ID,
      DDALOG2_SKLAD_ID;

 if (((DDA_WARE_ID <> '-1') and
     (DDA_WARE_ID <> '0') and
     (DDA_WARE_ID is not null)) and
     ((DDALOG2_WARE_ID <> '-1') and
     (DDALOG2_WARE_ID <> '0') and
     (DDALOG2_WARE_ID is not null)) and
     (DDA_BARCODE = DDALOG2_BARCODE) and
     (DDA_WARE_ID <> DDALOG2_WARE_ID)) then
 begin
  update WARES
  set NAME_ID = :DDA_NAME_ID,
      IZG_ID = :DDA_IZG_ID,
      COUNTRY_ID = :DDA_COUNTRY_ID,
      ORIG_NAME_ID = :DDA_ORIG_NAMEID,
      ORIG_IZG_ID = :DDA_ORIG_IZGID,
      ORIG_COUNTRY_ID = :DDA_ORIG_COUNTRYID,
      BARCODE = :DDA_BARCODE
  where ID = :DDALOG2_WARE_ID;
  exit;
 end

 if ((DDALOG_WARE_ID = '0') or (DDALOG_WARE_ID = '-1') or (DDALOG_WARE_ID is null)) then
 begin
  insert into WARES_LOG (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, BARCODE, Z_ID,
                         SKLAD_ID)
  values (:DDA_WARE_ID, :DDALOG_NAME_ID, :DDALOG_IZG_ID, :DDALOG_COUNTRY_ID, :DDALOG_ORIG_NAMEID, :DDALOG_ORIG_IZGID,
          :DDALOG_ORIG_COUNTRYID, :DDALOG_BARCODE, :DDALOG_Z_ID, :DDALOG_SKLAD_ID);

 end
 else
 if (((DDA_WARE_ID <> '0') and
     (DDA_WARE_ID <> '-1') and
     (DDA_WARE_ID is not null)) and
     ((DDA_NAME_ID <> DDALOG_NAME_ID) or (DDA_IZG_ID <> DDALOG_IZG_ID) or (DDA_COUNTRY_ID <> DDALOG_COUNTRY_ID) or (DDA_ORIG_NAMEID <> DDALOG_ORIG_NAMEID) or (DDA_ORIG_IZGID <> DDALOG_ORIG_IZGID) or (DDA_ORIG_COUNTRYID <> DDALOG_ORIG_COUNTRYID)) and
     (DDA_BARCODE = DDALOG_BARCODE)) then
 begin
  update WARES
  set NAME_ID = :DDA_NAME_ID,
      IZG_ID = :DDA_IZG_ID,
      COUNTRY_ID = :DDA_COUNTRY_ID,
      ORIG_NAME_ID = :DDA_ORIG_NAMEID,
      ORIG_IZG_ID = :DDA_ORIG_IZGID,
      ORIG_COUNTRY_ID = :DDA_ORIG_COUNTRYID,
      BARCODE = :DDA_BARCODE
  where ID = :DDALOG_WARE_ID;
 end

end;

grant execute
    on procedure PR_GETVAL_ID to procedure PR_AUTO_WARESLINK;
grant select on DOC_DETAIL_ACTIVE to procedure PR_AUTO_WARESLINK;
grant select on DOC_DETAIL_ACTIVE_LOG to procedure PR_AUTO_WARESLINK;
grant insert on WARES_LOG to procedure PR_AUTO_WARESLINK;
grant select,
            update on WARES to procedure PR_AUTO_WARESLINK;
grant execute
    on procedure PR_AUTO_WARESLINK to SYSDBA;

create or alter procedure PR_MAKEGOODWAREVALUES(SNAME type of DM_TEXT, SORIG_NAME type of DM_TEXT, SIZG type of DM_TEXT, SORIG_IZG type of DM_TEXT, SCOUNTRY type of DM_TEXT, SORIG_COUNTRY type of DM_TEXT, ORIG_CODE type of DM_TEXT, ACTIVE_ID type of DM_ID, ALTTYPE DM_STATUS, BARCODE DM_TEXT1024 = '') returns(GOODNAME type of DM_TEXT, GOODIZG type of DM_TEXT, GOODCOUNTRY type of DM_TEXT, GOODBARCODE DM_TEXT)
as
declare variable WARE_ID type of DM_UUID_NULL;
declare variable NAME_ID type of DM_UUID_NULL;
declare variable ORIG_NAME_ID type of DM_UUID_NULL;
declare variable IZG_ID type of DM_UUID_NULL;
declare variable ORIG_IZG_ID type of DM_UUID_NULL;
declare variable COUNTRY_ID type of DM_UUID_NULL;
declare variable ORIG_COUNTRY_ID type of DM_UUID_NULL;
begin

 /*
  select val_id from pr_getval_id(:sname,0,:alttype) into :name_id;
  select val_id from pr_getval_id(:sorig_name,1,:alttype) into :orig_name_id;
  select val_id from pr_getval_id(:sizg,3,:alttype) into :izg_id;
  select val_id from pr_getval_id(:sorig_izg,6,:alttype) into :orig_izg_id;
  select val_id from pr_getval_id(:scountry,2,:alttype) into :country_id;
  select val_id from pr_getval_id(:sorig_country,5,:alttype) into :orig_country_id;
 */

 select ID
 from VALS
 where VTYPE = 0
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SNAME
 into :NAME_ID;
 select ID
 from VALS
 where VTYPE = 1
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SORIG_NAME
 into :ORIG_NAME_ID;
 select ID
 from VALS
 where VTYPE = 3
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SIZG
 into :IZG_ID;
 select ID
 from VALS
 where VTYPE = 6
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SORIG_IZG
 into :ORIG_IZG_ID;
 select ID
 from VALS
 where VTYPE = 2
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SCOUNTRY
 into :COUNTRY_ID;
 select ID
 from VALS
 where VTYPE = 5
       and ALTTYPE = :ALTTYPE
       and SVALUE = :SORIG_COUNTRY
 into :ORIG_COUNTRY_ID;

 select WARE_ID
 from PR_GETWARE_BY_IDS(:NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, 1, :BARCODE)
 into :WARE_ID;
 if (WARE_ID is not null) then
 begin
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
  select SNAME,
         SIZG,
         SCOUNTRY,
         BARCODE
  from VW_WARES
  where ID = :WARE_ID
  into :GOODNAME,
       :GOODIZG,
       :GOODCOUNTRY,
       :GOODBARCODE;
  /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
  /* insert into doc_detail_active_log(id,sname,sizg,scountry,part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */
  /* goodname='2';--sname; */
  if (GOODNAME is null) then
   GOODNAME = SNAME;
  if (GOODIZG is null) then
   GOODIZG = SIZG;
  if (GOODCOUNTRY is null) then
   GOODCOUNTRY = SCOUNTRY;
  if (GOODNAME is null) then
   GOODNAME = SORIG_NAME;
  if (GOODIZG is null) then
   GOODIZG = SORIG_IZG;
  if (GOODCOUNTRY is null) then
   GOODCOUNTRY = SORIG_COUNTRY;
  if (GOODBARCODE is null) then
   GOODBARCODE = BARCODE;
  suspend;
  exit;
 end

 select WARE_ID
 from PR_GETWARE_BY_IDS(:NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, null, :BARCODE)
 into :WARE_ID;
 if (WARE_ID is not null) then
 begin
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
  /* goodname='1';--sname; */
  GOODNAME = SNAME;
  GOODIZG = SIZG;
  GOODCOUNTRY = SCOUNTRY;
  GOODBARCODE = BARCODE;

  suspend;
  exit;
 end

 WARE_ID = null;
 if (ORIG_NAME_ID is not null) then
 begin
  select first 1 ID
  from WARES
  where ORIG_NAME_ID = :ORIG_NAME_ID
  order by ID desc
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where ORIG_NAME_ID = :ORIG_NAME_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if ((WARE_ID is null) and
     (NAME_ID is not null)) then
 begin
  select first 1 ID
  from WARES
  where NAME_ID = :NAME_ID
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where NAME_ID = :NAME_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if (WARE_ID is null) then
 begin
  /* goodname='3';--sname; */
  GOODNAME = SNAME;
  NAME_ID = null;
 end
 else
 begin
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
  select W.NAME_ID,
         V.SVALUE
  from WARES W
  left join VALS V on W.NAME_ID = V.ID
  where W.ID = :WARE_ID
  into :NAME_ID,
       :GOODNAME;
 end

 WARE_ID = null;
 if (ORIG_IZG_ID is not null) then
 begin
  select first 1 ID
  from WARES
  where ORIG_IZG_ID = :ORIG_IZG_ID
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where ORIG_IZG_ID = :ORIG_IZG_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if ((WARE_ID is null) and
     (IZG_ID is not null)) then
 begin
  select first 1 ID
  from WARES
  where IZG_ID = :IZG_ID
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where IZG_ID = :IZG_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if (WARE_ID is null) then
  GOODIZG = SIZG;
 else
 begin
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
  select W.IZG_ID,
         V.SVALUE
  from WARES W
  left join VALS V on W.IZG_ID = V.ID
  where W.ID = :WARE_ID
  into :IZG_ID,
       :GOODIZG;
 end

 WARE_ID = null;
 if (ORIG_COUNTRY_ID is not null) then
 begin
  select first 1 ID
  from WARES
  where ORIG_COUNTRY_ID = :ORIG_COUNTRY_ID
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where ORIG_COUNTRY_ID = :ORIG_COUNTRY_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if ((WARE_ID is null) and
     (COUNTRY_ID is not null)) then
 begin
  select first 1 ID
  from WARES
  where COUNTRY_ID = :COUNTRY_ID
  into :WARE_ID;
  if (WARE_ID is null) then
   select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
   from WARES_LOG
   where COUNTRY_ID = :COUNTRY_ID
   order by LOG_ID desc
   into :WARE_ID;
 end
 if (WARE_ID is null) then
  GOODCOUNTRY = SCOUNTRY;
 else
 begin
  /* select actualware_id from PR_GETACTUALWAREID(:ware_id) into :ware_id; */
  select W.COUNTRY_ID,
         V.SVALUE
  from WARES W
  left join VALS V on W.COUNTRY_ID = V.ID
  where W.ID = :WARE_ID
  into :COUNTRY_ID,
       :GOODCOUNTRY;
 end

 /* if ((active_id>0) and ((goodname<>sname) or (goodizg<>sizg) or (goodcountry<>scountry))) then */
 /*   insert into doc_detail_active_log(id,sname,sizg,scountry, part_type) values (:active_id,:sname,:sizg,:scountry,:alttype); */

 /* goodname='3'; */
 GOODBARCODE = BARCODE;

 if (GOODNAME is null) then
  GOODNAME = SNAME;
 if (GOODIZG is null) then
  GOODIZG = SIZG;
 if (GOODCOUNTRY is null) then
  GOODCOUNTRY = SCOUNTRY;
 if (GOODNAME is null) then
  GOODNAME = SORIG_NAME;
 if (GOODIZG is null) then
  GOODIZG = SORIG_IZG;
 if (GOODCOUNTRY is null) then
  GOODCOUNTRY = SORIG_COUNTRY;

 suspend;
end;

create or alter procedure PR_GET_WARE (
    SNAME type of DM_TEXT,
    SIZG type of DM_TEXT,
    SCOUNTRY type of DM_TEXT,
    ORIG_CODE type of DM_TEXT = '',
    SORIG_NAME type of DM_TEXT = '',
    SORIG_IZG type of DM_TEXT = '',
    SORIG_COUNTRY type of DM_TEXT = '',
    BARCODE type of DM_TEXT = '',
    Z_ID type of DM_ID = 0,
    SKLAD_ID DM_TEXT = '',
    ALTTYPE DM_STATUS = 0,
    MNN DM_TEXT = '')
returns (
    W_ID type of DM_UUID_NULL)
as
declare variable NAME_ID type of DM_UUID_NULL;
declare variable IZG_ID type of DM_UUID_NULL;
declare variable COUNTRY_ID type of DM_UUID_NULL;
declare variable ORIG_NAME_ID type of DM_UUID_NULL;
declare variable ORIG_IZG_ID type of DM_UUID_NULL;
declare variable ORIG_COUNTRY_ID type of DM_UUID_NULL;
begin

 select VAL_ID
 from PR_GETVAL_ID(:SNAME, 0, :ALTTYPE, :MNN)
 into :NAME_ID;
 select VAL_ID
 from PR_GETVAL_ID(:SIZG, 3, :ALTTYPE)
 into :IZG_ID;
 select VAL_ID
 from PR_GETVAL_ID(:SCOUNTRY, 2, :ALTTYPE)
 into :COUNTRY_ID;
 select VAL_ID
 from PR_GETVAL_ID(:SORIG_NAME, 1, :ALTTYPE)
 into :ORIG_NAME_ID;
 select VAL_ID
 from PR_GETVAL_ID(:SORIG_IZG, 6, :ALTTYPE)
 into :ORIG_IZG_ID;
 select VAL_ID
 from PR_GETVAL_ID(:SORIG_COUNTRY, 5, :ALTTYPE)
 into :ORIG_COUNTRY_ID;
 /*
  select first 1 id from wares where NAME_ID=:name_id and IZG_ID=:izg_id and COUNTRY_ID=:country_id and
  ORIG_CODE=:orig_code and ORIG_NAME_ID=:orig_name_id and
  ORIG_IZG_ID=:orig_izg_id and ORIG_COUNTRY_ID=:orig_country_id -- into :w_id;
  and barcode=:barcode into :w_id;
  */
 select ID
 from WARES
 where NAME_ID = :NAME_ID
       and IZG_ID = :IZG_ID
       and COUNTRY_ID = :COUNTRY_ID
       and BARCODE = :BARCODE
 into :W_ID;

 if (W_ID is null) then
 begin
  /* exception EX_WRONG_OPER; */
  /* w_id=gen_id(gen_wares_id,1); */
  W_ID = uuid_to_char(gen_uuid());
  insert into WARES (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, BARCODE,
                     Z_ID, SKLAD_ID)
  values (:W_ID, :NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, :BARCODE,
          :Z_ID, :SKLAD_ID);
 end
 suspend;
end;

create or alter procedure PR_GETWARE_BY_IDS (
    NAME_ID type of DM_UUID_NULL,
    IZG_ID type of DM_UUID_NULL,
    COUNTRY_ID type of DM_UUID_NULL,
    ORIG_CODE type of DM_TEXT,
    ORIG_NAME_ID type of DM_UUID_NULL,
    ORIG_IZG_ID type of DM_UUID_NULL,
    ORIG_COUNTRY_ID type of DM_UUID_NULL,
    DOINSERT type of DM_STATUS,
    SEARCHINLOGS type of DM_STATUS,
    BARCODE DM_TEXT)
returns (
    WARE_ID type of DM_UUID_NULL)
as
begin
 if (ORIG_CODE is null) then
  ORIG_CODE = '';
 if (SEARCHINLOGS is null) then
  SEARCHINLOGS = 0;
 if (SEARCHINLOGS = 1) then
 begin
  select first 1 iif(ACTUAL_WARE_ID is null, ID, ACTUAL_WARE_ID)
  from WARES_LOG
  where NAME_ID = :NAME_ID
        and IZG_ID = :IZG_ID
        and COUNTRY_ID = :COUNTRY_ID
        and BARCODE = :BARCODE
  order by LOG_INSERTDT desc
  into :WARE_ID;
  select ID
  from WARES
  where ID = :WARE_ID
  into :WARE_ID;
 end
 else
 begin
  select first 1 ID
  from WARES
  where NAME_ID = :NAME_ID
        and IZG_ID = :IZG_ID
        and COUNTRY_ID = :COUNTRY_ID
        and BARCODE = :BARCODE
  into :WARE_ID;
 end
 if ((WARE_ID is null) and
     (DOINSERT = 1) and
     (SEARCHINLOGS <> 1)) then
 begin
  /* ware_id=gen_id(gen_wares_id,1); */
  WARE_ID = uuid_to_char(gen_uuid());
  insert into WARES (ID, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID,
                     SKLAD_ID, BARCODE)
  values (:WARE_ID, :NAME_ID, :IZG_ID, :COUNTRY_ID, :ORIG_CODE, :ORIG_NAME_ID, :ORIG_IZG_ID, :ORIG_COUNTRY_ID, 0, '',
          :BARCODE);
 end
 suspend;
end;

create or alter procedure PR_DOC_COMMIT (
    DOC_ID type of DM_ID,
    SESSION_ID type of DM_ID)
as
declare variable WRONG_WARES DM_STATUS;
declare variable BASE_TYPE type of DM_STATUS;
declare variable SUMMA type of DM_DOUBLE;
declare variable SUMMA_O type of DM_DOUBLE;
declare variable SUM_NDSO type of DM_DOUBLE;
declare variable DSCSUMMA type of DM_DOUBLE;
declare variable SUM_DSC type of DM_DOUBLE;
declare variable PRICE_TYPE DM_STATUS;
declare variable CONTRACT_ID DM_ID;
declare variable W_PARAM DM_STATUS;
declare variable WARES_MODE DM_STATUS;
begin
 select DT.BASE_TYPE,
        D.PRICE_TYPE,
        D.CONTRACT_ID
 from DOCS D
 left join DOC_TYPES DT on D.DOC_TYPE = DT.ID
 where D.ID = :DOC_ID
 into :BASE_TYPE,
      :PRICE_TYPE,
      :CONTRACT_ID;
 if (BASE_TYPE in (1, 3, 6, 8)) then
 begin

  WRONG_WARES = 0;
  WARES_MODE = 1;
  select PARAM_VALUE
  from PR_GETPARAMVALUE('WARES_MODE', 0)
  into WARES_MODE;
  if (WARES_MODE = 0) then
  begin
   select count(ID)
   from DOC_DETAIL_ACTIVE
   where DOC_ID = :DOC_ID
         and ((WARE_ID = '0') or (WARE_ID = '-1') or (WARE_ID = ''))
   into WRONG_WARES;
   if (WRONG_WARES > 0) then
    exception EX_WRONG_OPER;
  end

  execute procedure PR_DOC_PRIHOD_COMMIT(:DOC_ID, 0);
 end
 else
 if (BASE_TYPE in (2, 7, 9)) then
  execute procedure PR_DOC_RASHOD_COMMIT(:DOC_ID, 0);
 else
  exception EX_WRONGDOC_BASE_TYPE;
 delete from DOC_DETAIL_ACTIVE
 where DOC_ID = :DOC_ID;
 if (BASE_TYPE = 3) then
 begin
  delete from WAREBASE
  where PART_ID in (select PART_ID
                    from DOC_DETAIL
                    where DOC_ID = :DOC_ID
                          and QUANT < 0
                          and PART_TYPE = 1);
 end
 select sum(SUMMA),
        sum(SUMMA_O),
        sum(SUM_NDSO),
        sum(PRICE * QUANT / 10000),
        sum(SUM_DSC)
 from DOC_DETAIL
 where DOC_ID = :DOC_ID
 into :SUMMA,
      :SUMMA_O,
      :SUM_NDSO,
      :DSCSUMMA,
      :SUM_DSC;
 DSCSUMMA = DSCSUMMA * 10000;
 update DOCS
 set COMMITSESSION_ID = :SESSION_ID,
     STATUS = 1,
     POSTDT = 'now',
     CHECKDATA = (select CHECKDATA
                  from PR_DOCCHECKDATA(:DOC_ID)),
     SUMMA = :SUMMA,
     SUMMA_O = :SUMMA_O,
     SUM_NDSO = :SUM_NDSO,
     CALCSUMMA = :DSCSUMMA,
     SUM_DSC = :SUM_DSC,
     CASHDATA = (iif(:BASE_TYPE = 1, -:SUMMA, null))
 where ID = :DOC_ID;
 execute procedure PR_UPDBLOCKINFO_BY_DOC(:DOC_ID, 1);
 /* if ((base_type = 2) and (contract_id<>0)) then */
 if (CONTRACT_ID <> 0) then
 begin
  if (BASE_TYPE = 2) then
   execute procedure PR_CREDITCOMMIT(:CONTRACT_ID);
  if (BASE_TYPE in (8, 9)) then
   execute procedure PR_CREDITREINITCOMMIT(:CONTRACT_ID, :SESSION_ID);
  /* execute procedure PR_CREDITCOMMIT(:contract_id); */
 end
 execute procedure PR_DOEVENT('EV_DOCCOMMIT');
end;

create or alter trigger DOC_DETAIL_ACTIVE_BI for DOC_DETAIL_ACTIVE
active before insert position 0
as
begin
 if (new.ID is null) then
  new.ID = gen_id(GEN_DOC_DETAIL_ACTIVE_ID, 1);
 new.INSERTDT = 'now';
 if (new.BASE_AGENT_ID is null) then
  new.BASE_AGENT_ID = 0;
 if (new.STATUS is null) then
  new.STATUS = 0;
 if (new.PARENT_ID is null) then
  new.PARENT_ID = 0;
 if (new.PART_TYPE is null) then
  new.PART_TYPE = 0;

 /* первая строчка =  до привидения, оригианльные значения накладной */
 /* select first 1 ID from WARES where NAME_ID = new.NAME_ID and IZG_ID = new.IZG_ID and COUNTRY_ID = new.COUNTRY_ID and BARCODE = new.BCODE_IZG into new.WARE_ID; */

 insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
                                    BARCODE1, BCODE_IZG, GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT,
                                    SDSERT, REGN, NGTD, EDIZM, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID,
                                    ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME,
                                    SORIG_IZG, SORIG_COUNTRY, INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC,
                                    HUMAN_QUANT, CUSTOMDRAW, PART_TYPE, BASE_AGENT_ID)
 values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
         new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z, new.PRICE_R,
         new.BARCODE, new.BARCODE1, new.BCODE_IZG, new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO, new.SERT,
         new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN, new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID, new.COUNTRY_ID,
         new.ORIG_CODE, new.ORIG_NAME_ID, new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID, new.SNAME,
         new.SIZG, new.SCOUNTRY, new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO, new.KOEF,
         new.MOTHERPART_ID, new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE, new.BASE_AGENT_ID);
 /* -- */

 if ((new.PART_ID is null) or (new.PART_ID = 0)) then
 begin
  /* >cf20110923 */
  /* if (new.ware_id<0) then */
  if (new.WARE_ID = '-1') then
  begin
   select GOODNAME,
          GOODIZG,
          GOODCOUNTRY,
          GOODBARCODE
   from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.BCODE_IZG)
   into new.SNAME,
        new.SIZG,
        new.SCOUNTRY,
        new.BCODE_IZG;
  end
  /* <cf20110923 */
  new.PART_ID = 0;
  select ID
  from VALS
  where VTYPE = 0
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SNAME
  into new.NAME_ID;
  select ID
  from VALS
  where VTYPE = 3
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SIZG
  into new.IZG_ID;
  select ID
  from VALS
  where VTYPE = 2
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SCOUNTRY
  into new.COUNTRY_ID;
  select ID
  from VALS
  where VTYPE = 1
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SORIG_NAME
  into new.ORIG_NAME_ID;
  select ID
  from VALS
  where VTYPE = 6
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SORIG_IZG
  into new.ORIG_IZG_ID;
  select ID
  from VALS
  where VTYPE = 5
        and ALTTYPE = new.PART_TYPE
        and SVALUE = new.SORIG_COUNTRY
  into new.ORIG_COUNTRY_ID;
  new.WARE_ID = 0;
  select first 1 ID
  from WARES
  where NAME_ID = new.NAME_ID
        and IZG_ID = new.IZG_ID
        and COUNTRY_ID = new.COUNTRY_ID
        and BARCODE = new.BCODE_IZG
  into new.WARE_ID;

  /* вторая строчка = после приведения */
  insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
                                     DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
                                     BARCODE1, BCODE_IZG, GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT,
                                     SDSERT, REGN, NGTD, EDIZM, NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID,
                                     ORIG_IZG_ID, ORIG_COUNTRY_ID, Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME,
                                     SORIG_IZG, SORIG_COUNTRY, INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC,
                                     HUMAN_QUANT, CUSTOMDRAW, PART_TYPE, BASE_AGENT_ID)
  values (new.ID, new.PARENT_ID, new.DOC_ID, new.PART_ID, new.PART_PARENT_ID, new.DOC_DETAIL_ID, new.PRICE, new.NAC,
          new.QUANT, new.DISCOUNT, new.SUMMA, new.SUMMA_O, new.DCARD, new.WARE_ID, new.PRICE_O, new.PRICE_Z,
          new.PRICE_R, new.BARCODE, new.BARCODE1, new.BCODE_IZG, new.GODENDO, new.SERIA, new.NDS, new.SUM_NDSO,
          new.SERT, new.DATESERT, new.KEMVSERT, new.SDSERT, new.REGN, new.NGTD, new.EDIZM, new.NAME_ID, new.IZG_ID,
          new.COUNTRY_ID, new.ORIG_CODE, new.ORIG_NAME_ID, new.ORIG_IZG_ID, new.ORIG_COUNTRY_ID, new.Z_ID, new.SKLAD_ID,
          new.SNAME, new.SIZG, new.SCOUNTRY, new.SORIG_NAME, new.SORIG_IZG, new.SORIG_COUNTRY, new.INSERTDT, new.INFO,
          new.KOEF, new.MOTHERPART_ID, new.DEP, new.SUM_DSC, new.HUMAN_QUANT, new.CUSTOMDRAW, new.PART_TYPE,
          new.BASE_AGENT_ID);
  /* -- */

 end
 if (new.PART_PARENT_ID is null) then
  new.PART_PARENT_ID = 0;
 if (new.WARE_ID is null) then
  new.WARE_ID = 0;
 if (new.NAME_ID is null) then
  new.NAME_ID = 0;
 if (new.IZG_ID is null) then
  new.IZG_ID = 0;
 if (new.COUNTRY_ID is null) then
  new.COUNTRY_ID = 0;
 if (new.ORIG_NAME_ID is null) then
  new.ORIG_NAME_ID = 0;
 if (new.ORIG_IZG_ID is null) then
  new.ORIG_IZG_ID = 0;
 if (new.ORIG_COUNTRY_ID is null) then
  new.ORIG_COUNTRY_ID = 0;
 if (new.Z_ID is null) then
  new.Z_ID = 0;
 if (new.SKLAD_ID is null) then
  new.SKLAD_ID = '';
 if (new.MOTHERPART_ID is null) then
  new.MOTHERPART_ID = 0;
 if (new.DOC_DETAIL_ID is null) then
  new.DOC_DETAIL_ID = 0;

 if (new.PART_ID > 0) then
 /* заполняем значения партии */
 begin
  select WARE_ID,
         PRICE,
         PRICE_O,
         PRICE_Z,
         PRICE_R,
         /* QUANT, */
         /* BARCODE, */
         BARCODE1,
         /* KRITK, */
         GODENDO,
         SERIA,
         NDS,
         /* SUM_NDSO, */
         SERT,
         DATESERT,
         KEMVSERT,
         SDSERT,
         REGN,
         NGTD,
         EDIZM,
         NAC,
         DEP,
         PART_TYPE,
         BASE_AGENT_ID,
         GROUP_ID
  from PARTS
  where ID = new.PART_ID
  into new.WARE_ID,
       new.PRICE,
       new.PRICE_O,
       new.PRICE_Z,
       new.PRICE_R,

       /* new.QUANT, */
       /* new.BARCODE, */
       new.BARCODE1,

       /* new.KRITK,*/
       new.GODENDO,
       new.SERIA,
       new.NDS,

       /* new.SUM_NDSO, */
       new.SERT,
       new.DATESERT,
       new.KEMVSERT,
       new.SDSERT,
       new.REGN,
       new.NGTD,
       new.EDIZM,
       new.NAC,
       new.DEP,
       new.PART_TYPE,
       new.BASE_AGENT_ID,
       new.GROUP_ID;
 end
 if (new.GROUP_ID is null) then
  new.GROUP_ID = 0;
 /* if (new.ware_id>0) then -- заполняем значения позиции */
 if (new.WARE_ID not in ('-1', '0')) then
 /* заполняем значения позиции */
 begin
  select WW.NAME_ID,
         (select SVALUE
          from VALS
          where ID = WW.NAME_ID),
         WW.IZG_ID,
         (select SVALUE
          from VALS
          where ID = WW.IZG_ID),
         WW.COUNTRY_ID,
         (select SVALUE
          from VALS
          where ID = WW.COUNTRY_ID),
         WW.ORIG_CODE,
         WW.ORIG_NAME_ID,
         (select SVALUE
          from VALS
          where ID = WW.ORIG_NAME_ID),
         WW.ORIG_IZG_ID,
         (select SVALUE
          from VALS
          where ID = WW.ORIG_IZG_ID),
         WW.ORIG_COUNTRY_ID,
         (select SVALUE
          from VALS
          where ID = WW.ORIG_COUNTRY_ID),
         WW.BARCODE,
         WW.Z_ID,
         WW.SKLAD_ID
  from WARES WW
  where WW.ID = new.WARE_ID
  into new.NAME_ID,
       new.SNAME,
       new.IZG_ID,
       new.SIZG,
       new.COUNTRY_ID,
       new.SCOUNTRY,
       new.ORIG_CODE,
       new.ORIG_NAME_ID,
       new.SORIG_NAME,
       new.ORIG_IZG_ID,
       new.SORIG_IZG,
       new.ORIG_COUNTRY_ID,
       new.SORIG_COUNTRY,
       new.BCODE_IZG,
       new.Z_ID,
       new.SKLAD_ID;
 end
 else
 begin
  /* if (new.ware_id<0) then */
  if (new.WARE_ID = '-1') then
  begin
   select GOODNAME,
          GOODIZG,
          GOODCOUNTRY
   from PR_MAKEGOODWAREVALUES(new.SNAME, new.SORIG_NAME, new.SIZG, new.SORIG_IZG, new.SCOUNTRY, new.SORIG_COUNTRY, new.ORIG_CODE, new.ID, new.PART_TYPE, new.BCODE_IZG)
   into new.SNAME,
        new.SIZG,
        new.SCOUNTRY;
   new.WARE_ID = 0;
  end
 end
 if (new.QUANT is null) then
  new.QUANT = 0;
 if (new.PRICE is null) then
  new.PRICE = 0;
 if (new.PRICE_O is null) then
  new.PRICE_O = 0;
 if (new.NDS is null) then
  new.NDS = 0;
 if (new.SUMMA is null) then
  new.SUMMA = new.QUANT * new.PRICE;
 if (new.SUM_DSC is null) then
  new.SUM_DSC = 0;
 if (new.SUMMA_O is null) then
 begin
  new.SUMMA_O = new.QUANT * new.PRICE_O;
 end
 else
 begin
  if (new.QUANT <> 0) then
   new.PRICE_O = new.SUMMA_O / new.QUANT;
 end
 if (new.SUM_NDSO is null) then
  new.SUM_NDSO = new.SUMMA_O * new.NDS / 100;
 if (((new.NAC is null) or (new.NAC = 0)) and
     new.PRICE_O <> 0) then
  new.NAC = (new.PRICE - new.PRICE_O) * 100 / new.PRICE_O;
 if (new.DEP is null) then
 begin
  select first 1 ID
  from DEPS
  where NDS = new.NDS
  order by ID
  into new.DEP;
 end
 if (new.DEP is null) then
  new.DEP = 0;
 /* -> 20130301-ann gen_barcode_by_name */
 if ((new.BARCODE is null) or (trim(new.BARCODE) = '')) then
 begin
  if ((select PARAM_VALUE
       from PR_GETPARAMVALUE('GEN_BARCODE', 0)) = 0) then
   select RES_BCODE
   from UTPR_GETCHECKSUM_EAN13(new.ID)
   into new.BARCODE;

  /* else select res_bcode from utpr_getchecksum_ean13(new.name_id,'88') into new.barcode; */
  else
   select RES_BCODE
   from UTPR_GETCHECKSUM_EAN13((select L_ID
                                from VALS
                                where D$UUID = new.NAME_ID), '88')
   into new.BARCODE;
 end
 /* -> 20130301-ann gen_barcode_by_name */
 if (new.PART_PARENT_ID > 0) then
 begin
  update DOC_DETAIL_ACTIVE
  set QUANT = QUANT - new.QUANT,
      SUMMA = SUMMA - new.SUMMA,
      SUMMA_O = SUMMA_O - new.SUMMA_O,
      SUM_NDSO = SUM_NDSO - new.SUM_NDSO
  where ID = new.PART_PARENT_ID;
  execute procedure PR_COPY_MEMBERSHIP('DOC_DETAIL_ACTIVE', new.PART_PARENT_ID, new.ID);
 end
end;

create or alter trigger DOC_DETAIL_ACTIVE_BU0 for DOC_DETAIL_ACTIVE
active before update position 0
as
begin
 /*20160513
    if ((new.SNAME <> old.SNAME) or (new.NAME_ID is null) or (new.NAME_ID = '0')) then
      select VAL_ID from PR_GETVAL_ID(new.SNAME, 0, new.PART_TYPE, new.MNN) into new.NAME_ID;
    */

 if (new.NAME_ID is null) then
  new.NAME_ID = 0;
 if ((new.SUMMA is null) and
     ((new.QUANT <> 0) and
     (new.PRICE <> 0))) then
 begin
  new.SUMMA = new.QUANT * new.PRICE;
  if (new.SUM_DSC is not null) then
   new.SUMMA = new.SUMMA - new.SUM_DSC;
 end
 if (((new.SUMMA_O = 0) or (new.SUMMA_O is null)) and
     ((new.QUANT <> 0) and
     (new.PRICE_O <> 0))) then
  new.SUMMA_O = new.QUANT * new.PRICE_O;
 if (new.NAME_ID <> old.NAME_ID) then
  execute procedure PR_COPYGROUPS(old.NAME_ID, new.NAME_ID);
 if (new.SUM_DSC is null) then
  if (new.SUMMA <> old.SUMMA) then
  begin
   new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * new.SUMMA / (100 + old.DISCOUNT) - 100 * new.SUMMA / (100 + old.DISCOUNT);
   --new.summa=(1+new.discount/100)*100*old.summa/(100+old.discount);
  end
 if (new.SUM_DSC is null) then
  if (new.DISCOUNT <> old.DISCOUNT) then
  begin
   new.SUM_DSC = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT) - 100 * old.SUMMA / (100 + old.DISCOUNT);
   new.SUMMA = (1 + new.DISCOUNT / 100) * 100 * old.SUMMA / (100 + old.DISCOUNT);
  end

 select ID
 from VALS
 where VTYPE = 0
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SNAME
 into new.NAME_ID;
 if (new.NAME_ID is null) then
  new.NAME_ID = 0;
 select ID
 from VALS
 where VTYPE = 3
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SIZG
 into new.IZG_ID;
 if (new.IZG_ID is null) then
  new.IZG_ID = 0;
 select ID
 from VALS
 where VTYPE = 2
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SCOUNTRY
 into new.COUNTRY_ID;
 if (new.COUNTRY_ID is null) then
  new.COUNTRY_ID = 0;
 select ID
 from VALS
 where VTYPE = 1
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SORIG_NAME
 into new.ORIG_NAME_ID;
 if (new.ORIG_NAME_ID is null) then
  new.ORIG_NAME_ID = 0;
 select ID
 from VALS
 where VTYPE = 6
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SORIG_IZG
 into new.ORIG_IZG_ID;
 if (new.ORIG_IZG_ID is null) then
  new.ORIG_IZG_ID = 0;
 select ID
 from VALS
 where VTYPE = 5
       and ALTTYPE = new.PART_TYPE
       and SVALUE = new.SORIG_COUNTRY
 into new.ORIG_COUNTRY_ID;
 if (new.ORIG_COUNTRY_ID is null) then
  new.ORIG_COUNTRY_ID = 0;

 insert into DOC_DETAIL_ACTIVE_LOG (ID, PARENT_ID, DOC_ID, PART_ID, PART_PARENT_ID, DOC_DETAIL_ID, PRICE, NAC, QUANT,
                                    DISCOUNT, SUMMA, SUMMA_O, DCARD, WARE_ID, PRICE_O, PRICE_Z, PRICE_R, BARCODE,
                                    BARCODE1, BCODE_IZG,
                                    --KRITK,
                                    GODENDO, SERIA, NDS, SUM_NDSO, SERT, DATESERT, KEMVSERT, SDSERT, REGN, NGTD, EDIZM,
                                    NAME_ID, IZG_ID, COUNTRY_ID, ORIG_CODE, ORIG_NAME_ID, ORIG_IZG_ID, ORIG_COUNTRY_ID,
                                    Z_ID, SKLAD_ID, SNAME, SIZG, SCOUNTRY, SORIG_NAME, SORIG_IZG, SORIG_COUNTRY,
                                    INSERTDT, INFO, KOEF, MOTHERPART_ID, DEP, SUM_DSC, HUMAN_QUANT, CUSTOMDRAW,
                                    PART_TYPE, BASE_AGENT_ID)
 values (old.ID, old.PARENT_ID, old.DOC_ID, old.PART_ID, old.PART_PARENT_ID, old.DOC_DETAIL_ID, old.PRICE, old.NAC,
         old.QUANT, old.DISCOUNT, old.SUMMA, old.SUMMA_O, old.DCARD, old.WARE_ID, old.PRICE_O, old.PRICE_Z, old.PRICE_R,
         old.BARCODE, old.BARCODE1, old.BCODE_IZG,
         --old.KRITK,
         old.GODENDO, old.SERIA, old.NDS, old.SUM_NDSO, old.SERT, old.DATESERT, old.KEMVSERT, old.SDSERT, old.REGN,
         old.NGTD, old.EDIZM, old.NAME_ID, old.IZG_ID, old.COUNTRY_ID, old.ORIG_CODE, old.ORIG_NAME_ID, old.ORIG_IZG_ID,
         old.ORIG_COUNTRY_ID, old.Z_ID, old.SKLAD_ID, old.SNAME, old.SIZG, old.SCOUNTRY, old.SORIG_NAME, old.SORIG_IZG,
         old.SORIG_COUNTRY, old.INSERTDT, old.INFO, old.KOEF, old.MOTHERPART_ID, old.DEP, old.SUM_DSC, old.HUMAN_QUANT,
         old.CUSTOMDRAW, old.PART_TYPE, old.BASE_AGENT_ID);
end;

create or alter view VW_WARES (
    ID,
    NAME_ID,
    SNAME,
    IZG_ID,
    SIZG,
    COUNTRY_ID,
    SCOUNTRY,
    ORIG_CODE,
    ORIG_NAME_ID,
    SORIG_NAME,
    ORIG_IZG_ID,
    SORIG_IZG,
    ORIG_COUNTRY_ID,
    SORIG_COUNTRY,
    BARCODE,
    Z_ID,
    SKLAD_ID,
    INSERTDT,
    PACKET,
    MGN_NAME,
    MGN_ID,
    MGN_SOURCE)
as
select W.ID,
       W.NAME_ID,
       W.SNAME, --(select svalue from vals where id = w.name_id),
       W.IZG_ID,
       (select SVALUE
        from VALS
        where ID = W.IZG_ID),
       W.COUNTRY_ID,
       (select SVALUE
        from VALS
        where ID = W.COUNTRY_ID),
       W.ORIG_CODE,
       W.ORIG_NAME_ID,
       (select SVALUE
        from VALS
        where ID = W.ORIG_NAME_ID),
       W.ORIG_IZG_ID,
       (select SVALUE
        from VALS
        where ID = W.ORIG_IZG_ID),
       W.ORIG_COUNTRY_ID,
       (select SVALUE
        from VALS
        where ID = W.ORIG_COUNTRY_ID),
       W.BARCODE,
       W.Z_ID,
       W.SKLAD_ID,
       W.INSERTDT,
       W.PACKET,
       W.MGN_NAME,
       W.MGN_ID,
       W.MGN_SOURCE
from WARES W;

create or alter procedure PR_DOC_DETAIL_ACTIVE_ONCOMMIT (
    ACTIVE_ID type of DM_ID,
    COMMIT_ID type of DM_ID,
    DOC_ID type of DM_ID,
    COMMITDATE type of DM_DATETIME)
as
begin
 /*
  эта процедура выполняется для каждой записи, добавляемой в DOC_DETAIL в процедурах проведения документов
*/
 /* Для вречей*/
 update DOC_DETAIL_DOCTOR
 set DOC_DETAIL_ID = :COMMIT_ID,
     DOC_ID = :DOC_ID,
     COMMITDATE = :COMMITDATE
 where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;
 /* Старые рецепты*/
 update DOC_DETAIL_MAKE
 set DOC_DETAIL_ID = :COMMIT_ID,
     DOC_ID = :DOC_ID,
     COMMITDATE = :COMMITDATE
 where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;
 /* Новые рецепты*/
 update RECEPTS
 set TABLE_ID = :COMMIT_ID,
     TABLE_NAME = 'DOC_DETAIL',
     STATUS = 1
 where DOC_DETAIL_ACTIVE_ID = :ACTIVE_ID;--для рецептов

 execute procedure PR_AUTO_WARESLINK(:DOC_ID, :ACTIVE_ID);

end;