>> context already in use (BLR error).

DY> Думаешь, тут все телепаты? Триггер показать слабо?


    Сорри.
    FB 2.0       на порту 3050
    FB 2.1.16309 на порту 3052

    клиентская либа от FB 2.1.16309 (Embeded)

    Коннект локальный через Embeded или через localhost/3052

    В IBE попатка перекомпилировать теггер на view
    На другой машине (только FB 2.1.16309) - всё ОК
    БД только после Backup/Restre

CREATE TRIGGER VW_DET_BI FOR VW_DET
ACTIVE BEFORE INSERT POSITION 0
AS
declare variable sale_id bigint;
declare variable val_id bigint;
declare variable rg_id bigint;
declare variable koef double precision;
declare variable kurs_price numeric(15,3);
declare variable mq numeric(15,3);
declare variable t bigint;
declare variable cen smallint;
declare variable tname varchar(128);
BEGIN
  if (new.doc_id is not null) then
  begin
    if (Exists (select id from vw_doc0 where id = new.doc_id
        and getacl(coalesce(state, 0), 7) = 1)) then exception err_doc_blocked;
  end 

  if ((new.id is not null) and (exists (select id from det where id = new.id)))
   then t = -999;    -- Флаг обновления
    else
  begin
    if (new.id is null) then new.id = gen_id(gen_global_id, 1);
    select ver from log_change(new.id, 'C') into :t;
  end

  if (new.uslovie_id is not null and new.tovar_id is not null and not 
(coalesce(new.tovar_id, 0) < 0)) then
  begin
    select koef from getdef_doc_property((select TYPE_ID from vw_doc where id = 
new.doc_id)) into :cen;

    select sale_id, rg_id,
     case :cen
       when  1 then koef_opt
       when -1 then koef_in
       else 1
     end
     from vw_condition_all
      where id = new.uslovie_id
       into :sale_id, :rg_id, :koef;

    select first 1 COST, val_id from vw_costs_Full_history
     where TOV_ID = new.tovar_id and RG_ID = :RG_ID
       and ((SALE_ID = :sale_id) or (SALE_ID = -5))
       and (CHANGE >= (select date_doc from vw_doc where id = new.doc_id) or 
CHANGE is null)
     order by sale_id desc, CHANGE desc NULLS FIRST into new.price, :val_id;

     if (row_count = 0) then
     begin
       select first 1 COST, val_id from vw_costs
        where TOV_ID = new.tovar_id and RG_ID = :RG_ID
          and ((SALE_ID = :sale_id) or (SALE_ID = -5))
       order by sale_id desc NULLS last into new.price, :val_id;
     end

    select kurs from GET_KURS(:val_id) into :kurs_price;
    new.price = cast(new.price * :koef * :kurs_price as numeric(15,2));
  end
/*  if (coalesce(koef) = 0) then
    select koef_opt from vw_condition where id = new.uslovie_id into :koef;
*/
  if (new.price is null) then new.price = 0;
  if (new.uslovie_id is null) then new.uslovie_id = -1;
  if (new.card_id is null) then new.card_id = -1;
  if (new.pack_id is null) then new.pack_id = 107;

if (new.tovar_id < 0) then
begin
  select vw_doc.kurs from vw_doc where id = new.doc_id into :kurs_price;
  if (row_count = 0) then kurs_price = 1;
  new.price = coalesce(new.cost, 0) * kurs_price;
end -- else
  if (coalesce(new.cost, 0) = 0) then new.cost = coalesce(new.price, 0);
/*
  select QUANTITY from GET_CARDS((select type_id from vw_doc where id = 
new.doc_id), new.id)
    where DET_ID = new.card_id into :mq;
  if (coalesce(new.quantity) > coalesce(mq)) then new.quantity = coalesce(mq);
*/

  if ((new.card_id != -1) and (coalesce(new.tovar_id, 0) > 0)) then
  begin
    tname = trim(coalesce(new.tovar_name, ''));
    if (tname = '') then
      select CODE from tovar where id = new.tovar_id and ver = 0 into :tname;

    select sum(quantity)
     from get_cards((select type_id from vw_doc where id = new.doc_id), 
/*iif(:t = - 999, */new.id/*, null) */,
                    new.doc_id, coalesce(:tname, ''))
      where Det_ID = new.card_id into :mq;

    if (coalesce(new.quantity, 0) > coalesce(:mq, 0)) then new.quantity = 
coalesce(:mq, 0);
  end

  new.quantity = coalesce(new.quantity, 0);

  if (t = -999) then
   update det set ver = (select ver from LOG_CHANGE(new.id,'M')) where id = 
new.id and ver = 0;

  update changes set dt = current_timestamp where id = new.doc_id and ver = -1;

  INSERT INTO DET (ID, DOC_ID, TOVAR_ID, USLOVIE_ID, PACK_ID, CARD_ID, 
QUANTITY, COST, PRICE, notes)
   VALUES ( NEW.ID, NEW.DOC_ID, NEW.TOVAR_ID, NEW.USLOVIE_ID, NEW.PACK_ID, 
NEW.CARD_ID, NEW.QUANTITY, NEW.COST, NEW.PRICE, new.notes);
END    
    




С уважением,
Константин Григорьевич.
===============


Ответить