Скрипт FIR_EMD_TREATPLACESIGN_D
create or alter trigger FIR_EMD_TREATPLACESIGN_D for TREATPLACESIGN
active after insert position 10
as
declare variable acdid type of column accident.acdid;
declare variable treatcode type of column treatplace.treatcode;
declare variable dirid type of column treatplace.dirid;
declare variable GRPID type of column EXCHANGEGUID.GRPID;
declare variable TABLENAME type of column EXCHANGEGUID.TABLENAME;
declare variable epicrisisacdid type of column treatplace.epicrisisacdid;
declare cdacode ticode;
declare noacd ticode;
declare variable KIND integer;
declare PROFID type of column profession.er_profcode;
declare chairman type of column doctor.chairman;
declare pcode type of column clients.pcode;
declare fir_mid type of column fir_emd_arc.mid;
declare fir_status type of column fir_emd_arc.status;
declare fir_errors type of column fir_emd_arc.errors;
declare mid_det type of column treatplacesigndet.mid;
declare ps_uid_det type of column treatplacesigndet.uid;
begin
grpid = 9924;
tablename = 'FIR_MC_DOCUMENT';
-- Версия 20191118 введм признак необходимости наличия случая для документов
-- Версия 20910812. Лебеденко. Переделка с exchangeguid на fir_emd_arc
if (exists(select * from repl$getaccess where repl$access = 'USER')) then
begin
--if (coalesce(new.mid,0)=0) then exit;
--select cdacode, noacd from fir_emd_get_cdacode(new.protocolid) into cdacode, noacd;
--select extcode from dicinfo where refid = -508 and dicid = :cdacode into kind;
select first 1 wd.recid, di.rekvint6, di.extcode
from treatplace tr
left join workplacedoc w on tr.placeid = w.placeid
left join workplacedocdet wd on wd.placeid = w.placeid
left join dicinfo di on di.refid = -508 and di.dicid = wd.recid
where tr.protocolid = new.protocolid
and wd.rectype = 97 and di.rekvint1 in (2, 5) --3
and di.rekvint2 is distinct from 2 --без xml
into :cdacode, :noacd, :kind;
if ((:cdacode > 0) and not(:kind = 8 and new.signtype = 2)) then --не подпись МО для телеконсультации --2022.03.14
begin
select treatcode, dirid, epicrisisacdid, pcode
from treatplace where protocolid = new.protocolid
into treatcode, dirid, epicrisisacdid, pcode;
if (coalesce(new.mid, 0) = 0) then
begin
if (not exists (select * from fir_emd_arc where protocolid = new.protocolid and ver_no = new.ver_no)) then
UPDATE OR INSERT INTO FIR_EMD_ARC (PROTOCOLID, VER_NO, pcode, MID, STATUS, DOCNUM, createdate
, mo_oid, errors)
VALUES (new.protocolid, new.ver_no, :pcode, new.mid, 3, new.protocolid, current_timestamp
,(select jp.fir_oid from filials f
left join jpersons jp on jp.jid=f.jid
where f.filid=(select keyvalue from m_config where upper(keyname) = 'FILIAL')),
'Ошибка формирования информации о подписи: отсутствует ЭЦП')
matching (protocolid, ver_no);
end
else
begin
select first 1 dc.acdid from diagclients dc
left join accident a on a.acdid = dc.acdid
where dc.objtype = 1 and dc.objcode = :treatcode and dgtypecode = 1
--and a.finaldate is not null
order by dc.modifydate desc
into acdid;
if (acdid is null and dirid > 0) then
select first 1 acdid from accident where dirid = :dirid --and finaldate is not null
into acdid;
if (acdid is null and epicrisisacdid > 0) then
acdid = epicrisisacdid;
if ((acdid > 0) or (coalesce(:noacd, 0) = 1)) then
-- проверка есть в FIR_EMD_REGDOC_EVENT
begin
if (not exists (select * from fir_emd_arc where protocolid = new.protocolid and ver_no = new.ver_no)) then
UPDATE OR INSERT INTO FIR_EMD_ARC (PROTOCOLID,VER_NO,MID,STATUS,DOCNUM, createdate
,mo_oid)
VALUES (new.protocolid,new.ver_no,new.mid,0,new.protocolid, current_timestamp
,(select jp.fir_oid from filials f left join jpersons jp on jp.jid=f.jid where f.filid=(select keyvalue from m_config where upper(keyname)='FILIAL')))
matching (protocolid,ver_no);
else
begin
select mid, status, errors
from fir_emd_arc f
where f.protocolid = new.protocolid and f.ver_no = new.ver_no
into :fir_mid, :fir_status, :fir_errors;
-- если уже есть запись со статусом 3 и с внутренней ошибкой про отсутствие ЭЦП или про не указан случай,
-- то перезапись
if ((coalesce(:fir_mid, 0) = 0) and (:fir_status = 3)
and (:fir_errors = 'Ошибка формирования информации о подписи: отсутствует ЭЦП')
or
(:fir_status = 3) and (:fir_errors = 'Для данного протокола не указан случай оказания мед.помощи.')
) then
UPDATE OR INSERT INTO FIR_EMD_ARC (PROTOCOLID, VER_NO, MID, STATUS, DOCNUM, createdate, mo_oid, errors)
VALUES (new.protocolid, new.ver_no, new.mid, 0, new.protocolid, current_timestamp
,(select jp.fir_oid from filials f
left join jpersons jp on jp.jid=f.jid
where f.filid=(select keyvalue from m_config where upper(keyname)='FILIAL')), null)
matching (protocolid, ver_no);
end
--для документов, которые подписывает глав.врач или пред.ВК спустя время
if(:kind in (34,109,33,35,11,13,59,76,58)
and ((select f.status from fir_emd_arc f where f.protocolid = new.protocolid
and f.ver_no = new.ver_no
and (not (coalesce(f.errors, '') = 'Ошибка формирования информации о подписи: отсутствует ЭЦП')) and
(not (coalesce(f.errors, '') = 'Для данного протокола не указан случай оказания мед.помощи.'))
) = 3)) then
begin
select d.chairman, p.er_profcode from doctor d
left join profession p on p.profid = d.profid
where d.dcode = new.ps_uid
into chairman, profid;
if(
(
(:kind in (34, 109) and profid in (4, 5, 427, 428, 6, 7, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 8) and :chairman=1) or
(:kind in (33,35,11,13,59,76,58) and profid in (4, 5, 427, 428, 6, 7, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 8))
)
or (:fir_errors containing 'но не подписан доктором')) --2021.11.02
then
update fir_emd_arc set status = 0, errors = null, modifydate = current_timestamp
where protocolid=new.protocolid and ver_no=new.ver_no;
end
end
if ((coalesce(:acdid, 0) = 0) and (coalesce(:noacd, 0) = 0)) then -- 2021.07.30 внутренняя ошибка
begin
if (not exists (select * from fir_emd_arc where protocolid = new.protocolid and ver_no = new.ver_no)) then
UPDATE OR INSERT INTO FIR_EMD_ARC (PROTOCOLID,VER_NO, pcode, MID,STATUS,DOCNUM, createdate
, mo_oid, errors)
VALUES (new.protocolid, new.ver_no, :pcode, new.mid, 3, new.protocolid, current_timestamp
,(select jp.fir_oid from filials f
left join jpersons jp on jp.jid = f.jid
where f.filid = (select keyvalue from m_config where upper(keyname) = 'FILIAL')),
'Для данного протокола не указан случай оказания мед.помощи.')
matching (protocolid,ver_no);
end
end
end
end
end
sqlБыла ли статья полезна?