IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_OmsSchetLoadAll_Bulk_3_2_Orenburg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_OmsSchetLoadAll_Bulk_3_2_Orenburg]
GO

CREATE PROCEDURE [dbo].[sp_OmsSchetLoadAll_Bulk_3_2_Orenburg]
@SchetID uniqueidentifier, @type int, @Worker uniqueidentifier
WITH EXEC AS CALLER
AS
BEGIN	
-- ФЛК второго уровня, проверка зависимостей невозможная с помощью XSD
--переменные для старых проверок (некоторые и в новых используются)
declare @ActualDate datetime = (select [DSCHET] from tempdb..TEMP_SCHET)
declare @NSCHET varchar(30) =  (select [NSCHET] from tempdb..TEMP_SCHET)
declare @filename1 varchar(26) = (
select top 1 
[filename]
from tempdb..TEMP_ZGLV
)

--создать таблицу ошибок
create table #Errors
(
	IM_POL varchar(100),
	ZN_POL varchar(1000),
	NSCHET varchar(15),
	BASE_EL varchar(20),
	BAS_EL varchar(10),
	N_ZAP varchar(36),
	ID_PAC varchar(36),
	IDCASE varchar(36),
	SL_ID varchar(36),
	IDSERV varchar(36),
	OSHIB varchar(20),
	COMMENT varchar(1500),
	FIO varchar(120),
	IDDOKT varchar(100),
	FDATE datetime
)


--удаление дублирующих ID_PAC
update tempdb..TEMP_PERS set PersId = newid()

delete t0
from tempdb..TEMP_PERS t0 with(nolock) 
join tempdb..TEMP_PERS t1 with(nolock) on t0.persid < t1.persid and convert(varchar(36), t0.id_pac) = convert(varchar(36), t1.id_pac)


--q015 begin
--q015 preamb begin
declare @year int
declare @month int
declare @yearmonth int
declare @filename varchar(60) 
declare @q019type varchar(1)
declare @dschet date
declare @SchetPeriod uniqueidentifier

select top 1 
@filename = [filename]
from tempdb..TEMP_ZGLV

--if (@filename = 'HM510102111027T51_220101')
--begin
--	declare @RaisErrorMsg1 varchar(2047)
--	set @RaisErrorMsg1 = char(13) + 'ПРОИЗВОДИТСЯ ТЕСТИРОВАНИЕ ХРАНИМОЙ ПРОЦЕДУРЫ' + char(13)
--	raiserror (@RaisErrorMsg1,18,1)
--end

select top 1 
@year = [year]
,@month = [month]
,@yearmonth = [year] * 100 + [month]
,@dschet = dschet
from tempdb..TEMP_SCHET


select @schetperiod = schetperiodid from ies.R_SCHET_PERIOD where State = 1 order by year desc, month desc, StartDate desc
declare @delSchetID uniqueidentifier

--set @delSchetID = (select top 1 s.SchetID
--							from tempdb..TEMP_SCHET schet 
--							join tempdb..TEMP_ZGLV zglv on schet.ID = zglv.ID
--							join ies.T_SCHET s with(nolock) on s.NSCHET = schet.NSCHET
--															and s.DSCHET = schet.DSCHET
--															and s.[YEAR] = schet.[YEAR] 
--															and s.[MONTH] = schet.[MONTH] 
--															and s.IsDelete = 0
--															and s.CODE_MO=schet.CODE_mo
--															and s.[Filename]=zglv.[filename]+'.ZIP'
--															)
--		exec sp_dropschet @delSchetID, @type,1


----if @schetperiod is null
----begin
	
----	declare @RaisErrorMsg varchar(300)
----	set @RaisErrorMsg = char(13) + 'Не найден открытый период приёма реестров счетов (SCHET_PERIOD)'
----	raiserror (@RaisErrorMsg,18,1)
----	return
----end

if @type = 693
	begin
		set @q019type = (
		case
			when @filename like 'H%' then 'H'
			when @filename like 'T%' then 'T'
			when @filename like 'D%' then 'X'
			when @filename like 'C%' then 'C'
			else null
		end	
		)
	end
----else
-- --if @type IN( 554,562)
--	--begin
--	--	set @q019type = (
--	--	case			
--	--		when @filename like 'H%' then 'H'
--	--		when @filename like 'T%' then 'T'
--	--		when @filename like 'D%' then 'X'
--	--		when @filename like 'C%' then 'C'
--	--		else null
--	--	end	
--	--	)
	--end
	declare @sumsluch decimal
	select @sumsluch =  Sum([SUM_M]) from tempdb..TEMP_SLUCH
	declare @sumSanks decimal
	select @sumSanks =  Sum([SANK_IT]) from tempdb..TEMP_Z_SLUCH
	declare @sumschet decimal
	select @sumschet = SUM([SUMMAV]) from tempdb..TEMP_SCHET	

print 'Создание вычислимых полей, полей для FK' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	alter table tempdb..TEMP_SCHET add IsAdditional bit;

	alter table tempdb..TEMP_Z_SLUCH add ZSLType varchar(20),
	RSLT_DId uniqueidentifier;


	alter table tempdb..TEMP_SLUCH add VidPomR varchar(20),
	DialysisCount int,
	NA001Count int,
	StomRequiredUslCount int,
	KsgStomatCount int,
	IsGynecology bit,
	HasSpecsUsl bit,
	HasSession bit,
	SessionCount int,
	ExcludeFromProfile_PRVS_W_Check bit,
	ExcludeFromProfile_PRVS_Age_Check bit,
	PODRId uniqueidentifier,
	K_FR_CRIT varchar(10),
	LEK_PR_Exists_FOR_CRIT bit,
	MR_USL_NCount int,
	HasKsgnUsl bit,
	NameOPMP varchar(5),
	VidPomOPMP varchar(5),
	VidPomForCheck varchar(5), --__ВидПомощиДляПроверки
	ONK_SLRequired bit, --флагТребуетсяONK_SL
	HasOnkDiag bit, --МКБ_ЗНО,
	Age18 bit,
	VD_5_6 int,
	SpecIndependNoReabCount int,
	TempInt int,
	KD_Z_Calc int
	;

	alter table tempdb..TEMP_COMENTSL add MethodId uniqueidentifier;

	alter table tempdb..TEMP_USL add PriceSzId uniqueidentifier,
	N_ZRequired bit
	, StomUslType int
	, StomUslRequired bit
	, SpecsId uniqueidentifier
	, IsSession bit
	, KSGNId uniqueidentifier
	, KSGN_CId uniqueidentifier
	, MR_USL_NCount int
	;

	alter table tempdb..TEMP_LEK_PR_DATE_INJ add
	MNN_LP_LTId uniqueidentifier,
	SHLTId uniqueidentifier,
	ADDIT_CRITId uniqueidentifier

	alter table tempdb..TEMP_PERS add FIO varchar(200)

	alter table tempdb..TEMP_MR_USL_N add SpecIndependNoReab uniqueidentifier
END

if (@q019type = 'X')
begin
update tempdb..TEMP_Z_SLUCH set usl_ok = 3
end


insert into #Errors (
OSHIB,
IM_POL,
ZN_POL,
NSCHET,
BAS_EL,
N_ZAP,
IDCASE,
SL_ID,
IDSERV,
COMMENT,
ID_PAC,
FDATE
)
exec sp_GetRegionalFlkFiltersBulk_56 @type


--q015 preamb end
----q015 rules begin
--000F.00.0001 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_ZAP', convert(varchar(36),t1.N_ZAP) as ZN_POL ,@NSCHET, 'ZAP', t1.N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '000F.00.0001'
, 'Дубль N_ZAP'
from tempdb..TEMP_ZAP t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.N_ZAP = t1.N_ZAP
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
group by t1.N_ZAP
having count(*)>1
--000F.00.0001 end

--000F.00.0002 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDCASE', convert(varchar(36),t0.IDCASE) as ZN_POL ,@NSCHET, 'Z_SL', null as N_ZAP, null as ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '000F.00.0002'
, 'Дубль IDCASE'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
group by IDCASE
having count(*)>1
--000F.00.0002 end

--000F.00.0003 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL_ID', SL_ID as ZN_POL ,@NSCHET, 'SL', null as N_ZAP, null as ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID, null as IDSERV, '000F.00.0003'
, 'Дубль SL_ID в пределах файла'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
group by SL_ID,t1.IDCASE
having count(*)>1
--000F.00.0003 end

--000F.00.0004 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSERV', IDSERV as ZN_POL ,@NSCHET, 'USL', null as N_ZAP, null as ID_PAC, t1.IDCASE, t0.SL_ID, t0.IDSERV as IDSERV, '000F.00.0004'
, 'Дубль IDSERV'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
group by t1.IDCASE, t0.SL_ID, t0.IDSERV
having count(*)>1
--000F.00.0004 end

--003F.00.0401 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0401'
, 'NPR_MO должно быть заполнено при заполнении NPR_DATE'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'X', 'H', 'R', 'T')
and NPR_MO is null
and NPR_DATE is not null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.0401 end

--006F.00.0350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0350'
, 'При DS_ONK=1 должно выполняться RSLT_D <>1'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('X')
and t0.RSLT_D = 1
and DS_ONK=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0350 end

--006F.00.0360 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0350'
, 'При DS_ONK=1 должно выполняться RSLT<>{317, 321, 332, 343, 347}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
((@q019type in ('X', 'R') and DATE_Z_2 >= convert(date,'01.04.2020',104) and DATE_Z_2 <= convert(date,'31.03.2022',104)) 
	or (@q019type in ('R') and DATE_Z_2 >= convert(date,'01.04.2022',104)))
and t0.RSLT in (317, 321, 332, 343, 347)
and DS_ONK=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0360 end

--003F.00.0402 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', convert(varchar(10),t0.NPR_DATE,104) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0402'
, 'Поле NPR_DATE должно быть заполнено при заполнении NPR_MO'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'X', 'H', 'R', 'T')
and NPR_DATE is null
and NPR_MO is not null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.0402 end

--006F.00.0311 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', null as ZN_POL ,@NSCHET, 'Z_SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, t3.sl_id as SL_ID, null as IDSERV,  '006F.00.0311'
, 'Дата направления должна быть больше или равна дате рождения'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
where
@q019type in ('C', 'X', 'H', 'R', 'T')
and NPR_DATE < DR
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0311 end

--002F.00.0140 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VNOV_D', convert(varchar(4), t0.VNOV_D) as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '002F.00.0140'
, 'Поле VNOV_D имеет недопустимое значение (больше чем 200 и VNOV_D меньше чем 2500)'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('C', 'X', 'H', 'R', 'T')
and not (200<VNOV_D and VNOV_D<2500)
and VNOV_D is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0140 end

--004F.00.1511 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', convert(varchar(25), t0.CODE_MD) as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, t0.IDSERV as IDSERV, '004F.00.1511'
, 'Поле CODE_MD Допустимы цифры, строчные и прописные буквы латинского алфавита'
from tempdb..TEMP_MR_USL_N t0 with(nolock)
join tempdb..TEMP_USL t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.IDSERV = t2.IDSERV and t0.SL_ID = t2.SL_ID 
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('H')
and not (dbo.RegExMatch(t0.CODE_MD,'^[0-9A-Za-z]{1,25}$')= 1)
and t0.CODE_MD is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1511 end


----000F.00.0005 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'S_CODE', null as ZN_POL ,'@NSCHET', 'SANK', t1.N_ZAP, null as ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '000F.00.0005'
--, 'Дубль S_CODE в пределах законченного случая'
--from tempdb..TEMP_SANK t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--where @q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--group by t0.S_CODE, t0.idcase, t1.n_zap
--having count(*) > 1
----000F.00.0005 end

----000F.00.0006 begin
--select convert(varchar(36),t0.id_pac) as id_pac
--into #id_pac_dub
--from tempdb..TEMP_PERS t0 with(nolock)
--group by convert(varchar(36),t0.id_pac)
--having count(*) > 1

----000F.00.0007 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'ID_PAC', null as ZN_POL ,'@NSCHET', 'PERS', t0.N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '000F.00.0007'
--, 'Не найдены перс данные пациента'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--left join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t0.ID_PAC) =  convert(varchar(36),t1.ID_PAC)
--where @q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--and t1.ID_PAC is null
----000F.00.0007 end

--001F.00.0030 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MO', CODE_MO as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0030', 'Значение не найдено в справочнике F003'
from tempdb..TEMP_SCHET t0 with(nolock)
left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.CODE_MO 
and ISNULL(t2.D_BEGIN,'1900.01.01')<=@dschet and (t2.D_END is null or t2.D_END>=@dschet)
where
@q019type in ('C', 'H', 'T', 'X')
and t0.CODE_MO is not null
and t2.MCOD is null
and @dschet >= convert(date,'01.11.2019',104)
--001F.00.0030 end

--001F.00.0050 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DISP', DISP as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0050'
, 'Значение не найдено в справочнике V016'
from tempdb..TEMP_SCHET t0 with(nolock)
left join ies.T_V016_DISPT t2 with(nolock) on t2.IDDT = t0.DISP
where
@q019type in ('X')
and t0.DISP is not null
and t2.IDDT is null
and @dschet >= convert(date,'01.11.2019',104)
--001F.00.0050 end

--001F.00.0060 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VPOLIS', convert(varchar(1), t0.VPOLIS) as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0060', 'Значение не найдено в справочнике F008'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t0.N_ZAP
left join ies.T_F008_OMS_TYPE t2 with(nolock) on t2.IDDoc = t0.VPOLIS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.VPOLIS is not null
and t2.IDDoc is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0060 end

--001F.00.0070 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ST_OKATO', ST_OKATO as ZN_POL ,@NSCHET, 'PACIENT', t1.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0070', 'Значение не найдено в справочнике F010'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t0.N_ZAP
left join ies.T_F010_SUBJECT t2 with(nolock) on t2.KOD_OKATO = t0.ST_OKATO and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.ST_OKATO is not null
and t2.KOD_OKATO is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0070 end


----001F.00.0080 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'SMO', t0.SMO as ZN_POL ,@NSCHET, 'PACIENT', t1.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0080', 'Значение не найдено в справочнике F002'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t0.N_ZAP
--left join ies.T_F002_SMO t2 with(nolock) on t2.SMOCOD = t0.SMO and t2.D_BEGIN<=DATE_Z_2 and (t2.D_END is null or D_END>=DATE_Z_2)
--where
--@q019type in ('C', 'H', 'T', 'X')
--and t0.SMO is not null
--and t2.SMOCOD is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0080 end

--001F.00.0100 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SMO_OK', SMO_OK as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0100'
, 'Значение не найдено в справочнике F010'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
left join ies.T_F010_SUBJECT t2 with(nolock) on t2.KOD_OKATO = t0.SMO_OK
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'H', 'T', 'X')
and t0.SMO_OK is not null
and t2.KOD_OKATO is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0100 end

--001F.00.0170 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VIDPOM', convert(varchar(4), t0.VIDPOM) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0170'
, 'Значение не найдено в справочнике V008'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V008_MEDICAL_TYPE t2 with(nolock) on t2.IDVMP = t0.VIDPOM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.VIDPOM is not null
and t2.IDVMP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0170 end

--001F.00.0180 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FOR_POM', convert(varchar(1), t0.FOR_POM) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0180'
, 'Значение не найдено в справочнике V014'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V014_MEDICAL_FORM t2 with(nolock) on t2.IDFRMMP = t0.FOR_POM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.FOR_POM is not null
and t2.IDFRMMP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0180 end

--001F.00.0190 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0190', 'Значение не найдено в справочнике F003'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.NPR_MO
and t2.D_BEGIN<=t0.DATE_Z_2 and (t2.D_END is null or t2.D_END>=t0.DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.NPR_MO is not null
and t2.mcod is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0190 end

----001F.00.0200 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'LPU', LPU as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0200', 'Значение не найдено в справочнике F003'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.LPU
--and t2.D_BEGIN<=t0.DATE_Z_2 and (t2.D_END is null or t2.D_END>=t0.DATE_Z_2)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
--and t0.LPU is not null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0200 end

--001F.00.0210 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', convert(varchar(3), t0.RSLT) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0210'
, 'Значение не найдено в справочнике V009'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V009_RESULT t2 with(nolock) on t2.IDRMP = t0.RSLT
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.RSLT is not null
and t2.IDRMP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0210 end

--001F.00.0220 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', convert(varchar(3), t0.ISHOD) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0220'
, 'Значение не найдено в справочнике V012'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V012_OUTCOME t2 with(nolock) on t2.IDIZ = t0.ISHOD
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.ISHOD is not null
and t2.IDIZ is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0220 end

--001F.00.0230 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSP', convert(varchar(2), t0.IDSP) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0230'
, 'Значение не найдено в справочнике V010'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V010_PAY t2 with(nolock) on t2.IDSP = t0.IDSP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.IDSP is not null
and t2.IDSP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0230 end

--001F.00.0240 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', convert(varchar(2), t0.RSLT_D) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '001F.00.0240'
, 'Значение не найдено в справочнике V017'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
left join ies.T_V017_DISP_RESULT t2 with(nolock) on t2.IDDR = t0.RSLT_D
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.RSLT_D is not null
and t2.IDDR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0240 end

--001F.00.0250 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_HMP', VID_HMP as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0250', 'Значение не найдено в справочнике V018'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.idcase
left join ies.T_V018_VMP_TYPE t2 with(nolock) on t2.IDHVID = t0.VID_HMP
and t2.DATEBEG<=DATE_Z_2 and (t2.DATEEND is null or t2.DATEEND>=DATE_Z_2)
left join ies.T_V018_VMP_TYPE_PREV t3 with(nolock) on t3.V018VmpType= t0.VID_HMP
and t3.DATEBEG<=DATE_Z_2 and (t3.DATEEND is null or t3.DATEEND>=DATE_Z_2)
where
@q019type in ('D', 'R', 'T')
and t0.VID_HMP is not null
and t2.IDHVID is null
and t3.V018VmpType is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0250 end

----001F.00.0260 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'METOD_HMP', convert(varchar(3), t0.METOD_HMP) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0260', 'Значение не найдено в справочнике V019'
--from tempdb..TEMP_SLUCH t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
----join tempdb..TEMP_SLUCH_REG t3  with(nolock) on t0.sl_id = t3.sl_id
--outer apply (
--select top 1 V019VmpMethodId
--from ies.T_V019_VMP_METHOD t100 with(nolock)
--left join ies.T_V022_MODPAC t200 with(nolock) on t200.V022ModpacID = t100.V022Modpac
--where
--t100.idhm = t0.METOD_HMP
--and t100.DATEBEG<=DATE_Z_2 and (t100.DATEEND is null or t100.DATEEND>=DATE_Z_2)
--and (
--	t100.V022Modpac is null	
--	and @year <= 2020
--	)
--) t2
--where
--@q019type in ('D', 'R', 'T')
--and t0.METOD_HMP is not null
--and t2.V019VmpMethodId is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0260 end

--001F.00.0270 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', convert(varchar(3), t0.PROFIL) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0270', 'Значение не найдено в справочнике V002'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V002_PROFILE t2 with(nolock) on t2.IDPR = t0.PROFIL
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.PROFIL is not null
and t2.IDPR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0270 end

--001F.00.0280 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL_K', convert(varchar(3), t0.PROFIL_K) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0280', 'Значение не найдено в справочнике V020'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V020_BED_PROFILE t2 with(nolock) on t2.IDK_PR = t0.PROFIL_K
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.PROFIL_K is not null
and t2.IDK_PR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0280 end

--001F.00.0290 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_CEL', P_CEL as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0290', 'Значение не найдено в справочнике V025'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V025_KPC t2 with(nolock) on t2.IDPC = t0.P_CEL
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R')
and t0.P_CEL is not null
and t2.IDPC is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0290 end

--001F.00.0300 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', convert(varchar(1), t0.C_ZAB) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0300', 'Значение не найдено в справочнике V027'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V027_C_ZAB t2 with(nolock) on t2.IDCZ = t0.C_ZAB
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.C_ZAB is not null
and t2.IDCZ is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0300 end

--001F.00.0310 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0310', 'Значение не найдено в справочнике V021'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.PRVS is not null
and t2.CODE_SPEC is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0310 end

--001F.00.0320 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_SP', convert(varchar(4), t0.NAZ_SP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0320'
, 'Значение не найдено в справочнике V021'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.NAZ_SP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.NAZ_SP is not null
and t2.CODE_SPEC is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0320 end

--001F.00.0330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PMP', convert(varchar(3), t0.NAZ_PMP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0330'
, 'Значение не найдено в справочнике V002'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V002_PROFILE t2 with(nolock) on t2.IDPR = t0.NAZ_PMP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.NAZ_PMP is not null
and t2.IDPR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0330 end

--001F.00.0340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PK', convert(varchar(3), t0.NAZ_PK) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0340'
, 'Значение не найдено в справочнике V020'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V020_BED_PROFILE t2 with(nolock) on t2.IDK_PR = t0.NAZ_PK
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.NAZ_PK is not null
and t2.IDK_PR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0340 end

--001F.00.0350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_V', convert(varchar(1), t0.NAZ_V) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0350'
, 'Значение не найдено в справочнике V029'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V029_MET_ISSL t2 with(nolock) on t2.IDMET = t0.NAZ_V
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.NAZ_V is not null
and t2.IDMET is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0350 end

--001F.00.0360 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', NAPR_MO as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0360', 'Значение не найдено в справочнике F003'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.NAPR_MO
and t2.D_BEGIN<=t1.DATE_Z_2 and (t2.D_END is null or t2.D_END>=t1.DATE_Z_2)
where
@q019type in ('X')
and t0.NAPR_MO is not null
and (t2.mcod is null  or t2.mcod in ('510001', '510002'))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0360 end

--001F.00.0370 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', NAPR_MO as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0370', 'Значение не найдено в справочнике F003'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.NAPR_MO 
and t2.D_BEGIN<=t1.DATE_Z_2 and (t2.D_END is null or t2.D_END>=t1.DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.NAPR_MO is not null
and (t2.mcod is null  or t2.mcod in ('510001', '510002'))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0370 end

--001F.00.0380 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_V', convert(varchar(2), t0.NAPR_V) as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0380'
, 'Значение не найдено в справочнике V028'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V028_NAPR_V t2 with(nolock) on t2.IDVN = t0.NAPR_V
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.NAPR_V is not null
and t2.IDVN is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0380 end

--001F.00.0390 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MET_ISSL', convert(varchar(2), t0.MET_ISSL) as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0390'
, 'Значение не найдено в справочнике V029'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V029_MET_ISSL t2 with(nolock) on t2.IDMET = t0.MET_ISSL
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.MET_ISSL is not null
and t2.IDMET is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0390 end

--001F.00.0400 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PR_CONS', convert(varchar(1), t0.PR_CONS) as ZN_POL ,@NSCHET, 'CONS', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0400'
, 'Значение не найдено в справочнике N019'
from tempdb..TEMP_CONS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N019_ONK_CONS t2 with(nolock) on t2.ID_CONS = t0.PR_CONS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.PR_CONS is not null
and t2.ID_CONS is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0400 end

--001F.00.0410 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1_T', convert(varchar(2), t0.DS1_T) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0410'
, 'Значение не найдено в справочнике N018'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N018_ONK_REAS t2 with(nolock) on t2.ID_REAS = t0.DS1_T
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.DS1_T is not null
and t2.ID_REAS is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0410 end

--001F.00.0420 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STAD', convert(varchar(3), t0.STAD) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0420'
, 'Значение не найдено в справочнике N002'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 on t3.IDCASE = t1.IDCASE
left join ies.T_N002_STADIUM t2 with(nolock) on t2.ID_St = t0.STAD
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
and t3.DS1 like t2.DS_St + '%'
where
@q019type in ('C', 'D', 'R', 'T')
and t0.STAD is not null
and t2.ID_St is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0420 end

--001F.00.0430 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_T', convert(varchar(4), t0.ONK_T) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0430'
, 'Значение не найдено в справочнике N003'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 on t3.IDCASE = t1.IDCASE
left join ies.T_N003_TUMOR t2 with(nolock) on t2.ID_T = t0.ONK_T
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
and t3.DS1 like t2.DS_T+ '%'
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_T is not null
and t2.ID_T is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0430 end

--001F.00.0440 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_N', convert(varchar(4), t0.ONK_N) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0440'
, 'Значение не найдено в справочнике N004'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 on t3.IDCASE = t1.IDCASE
left join ies.T_N004_NODUS t2 with(nolock) on t2.ID_N = t0.ONK_N
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
and t3.DS1 like t2.DS_N + '%'
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_N is not null
and t2.ID_N is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0440 end

--001F.00.0450 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_M', convert(varchar(4), t0.ONK_M) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0450'
, 'Значение не найдено в справочнике N005'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 on t3.IDCASE = t1.IDCASE
left join ies.T_N005_METASTASIS t2 with(nolock) on t2.ID_M = t0.ONK_M
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
and t3.DS1 like t2.DS_M + '%'
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_M is not null
and t2.ID_M is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0450 end

--001F.00.0460 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_CODE', convert(varchar(3), t0.DIAG_CODE) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0460'
, 'Значение не найдено в справочнике N007'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N007_MRF t2 with(nolock) on t2.ID_Mrf = t0.DIAG_CODE
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=1
and t2.ID_Mrf is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0460 end

--001F.00.0470 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_CODE', convert(varchar(3), t0.DIAG_CODE) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0470'
, 'Значение не найдено в справочнике N010'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N010_MARK t2 with(nolock) on t2.ID_Igh = t0.DIAG_CODE
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=2
and t2.ID_Igh is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0470 end

--001F.00.0480 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_RSLT', convert(varchar(3), t0.DIAG_RSLT) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0480', 'Значение не найдено в справочнике N008'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N008_MRF_RT t2 with(nolock) on t2.ID_R_M = t0.DIAG_RSLT
and ((DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)) or t0.DIAG_DATE < '01.09.2018')
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=1
and t0.DIAG_RSLT is not null
and t2.ID_R_M is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0480 end

--001F.00.0490 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_RSLT', convert(varchar(3), t0.DIAG_RSLT) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0490', 'Значение не найдено в справочнике N011'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N011_MARK_VALUE t2 with(nolock) on t2.ID_R_I = t0.DIAG_RSLT
and ((DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)) or t0.DIAG_DATE < '01.09.2018')
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=2
and t2.ID_R_I is null
and t0.DIAG_RSLT is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0490 end

--001F.00.0500 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROT', convert(varchar(1), t0.PROT) as ZN_POL ,@NSCHET, 'B_PROT', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0500'
, 'Значение не найдено в справочнике N001'
from tempdb..TEMP_B_PROT t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N001_PrOt t2 with(nolock) on t2.ID_PrOt = t0.PROT
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.PROT is not null
and t2.ID_PrOt is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0500 end

--001F.00.0510 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_TIP', convert(varchar(1), t0.USL_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0510', 'Значение не найдено в справочнике N013'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N013_TREAT_TYPE t2 with(nolock) on t2.ID_TLech = t0.USL_TIP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('C', 'D', 'R', 'T')
and t0.USL_TIP is not null
and t2.ID_TLech is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0510 end

--001F.00.0520 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'HIR_TIP', convert(varchar(1), t0.HIR_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0520'
, 'Значение не найдено в справочнике N014'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N014_SURG_TREAT t2 with(nolock) on t2.ID_THir = t0.HIR_TIP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.HIR_TIP is not null
and t2.ID_THir is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0520 end

--001F.00.0530 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_L', convert(varchar(1), t0.LEK_TIP_L) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0530'
, 'Значение не найдено в справочнике N015'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N015_DRUG_THERAPY_LINES t2 with(nolock) on t2.ID_TLek_L = t0.LEK_TIP_L
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.LEK_TIP_L is not null
and t2.ID_TLek_L is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0530 end

--001F.00.0540 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_V', convert(varchar(1), t0.LEK_TIP_V) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0540'
, 'Значение не найдено в справочнике N016'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N016_DRUG_THERAPY_CYCLES t2 with(nolock) on t2.ID_TLek_V = t0.LEK_TIP_V
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.LEK_TIP_V is not null
and t2.ID_TLek_V is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0540 end

--001F.00.0550 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LUCH_TIP', convert(varchar(1), t0.LUCH_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0550'
, 'Значение не найдено в справочнике N017'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N017_RADIATION_THERAPY_TYPES t2 with(nolock) on t2.ID_TLuch = t0.LUCH_TIP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.LUCH_TIP is not null
and t2.ID_TLuch is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0550 end

--001F.00.0560 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0560'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0560 end

--001F.00.0570 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0570', 'Значение не найдено в справочнике V024'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH is not null
and dbo.DateTimeDiff('year',dr,date_z_2) >= 18
and ((t3.ds1 >= 'C00' and t3.ds1 < 'C81') or t3.ds1 like 'C97%' or t3.ds1 like 'D0%')
and t4.IDDKK is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--001F.00.0570 end

--001F.00.0571 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0571', 'Значение не найдено в справочнике V024'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH and DATEBEG<=t1.DATE_Z_2 and (DATEEND is null or DATEEND>=t1.DATE_Z_2) 
left join  [IES].[R_NSI_SHLT] rs with(nolock) on rs.CODE = t0.CODE_SH
left join  [IES].[R_NSI_ADDIT_CRIT] ra with(nolock) on  ra.CODE = t0.CODE_SH
left join  [IES].[R_NSI_MNN_LP_LT] rm with(nolock) on  rm.CODE = t0.CODE_SH
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH is not null
and dbo.DateTimeDiff('year',dr,t1.date_z_2) >= 18
and (t4.IDDKK is null and rs.CODE is null and ra.CODE is null and rm.CODE is null)
and (
	(t3.ds1 >= 'C00' and t3.ds1 < 'C81')
	or (t3.ds1 >= 'C81' and t3.ds1 < 'C97' and t0.USL_TIP = 2) 
	or (t3.ds1 >= 'C96' and t3.ds1 < 'C99')
	or t3.ds1 like 'C97%' 
	or t3.ds1 like 'D0%'
	or (t3.ds1 >= 'D45' and t3.ds1 < 'D48' and t0.USL_TIP = 2) 
)
and t1.DATE_Z_2 >= convert(date,'01.01.2021',104)
--001F.00.0571 end

----001F.00.0580 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'N_KSG', t0.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0580'
--, 'Значение не найдено в справочнике V023'
--from tempdb..TEMP_KSG_KPG t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join ies.T_V023_KSG t2 with(nolock) on t2.K_KSG = t0.N_KSG
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--@q019type in ('C', 'D', 'H', 'R')
--and KSG_PG=0
--and t0.N_KSG is not null
--and t2.K_KSG is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0580 end

--001F.00.0590 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KPG', t0.N_KPG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0590'
, 'Значение не найдено в справочнике V026'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V026_KPG t2 with(nolock) on t2.K_KPG = t0.N_KPG
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R')
and t0.N_KPG is not null
and t2.K_KPG is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0590 end

----001F.00.0600 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'KOEF_Z', convert(varchar, cast(t0.KOEF_Z as money)) as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0600'
--, 'Значение KOEF_Z должно совпадать со значением V023.KOEF_Z записи, соответствующей номеру КСГ'
--from tempdb..TEMP_KSG_KPG t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join ies.T_V023_KSG t2 with(nolock) on t2.KOEF_Z = t0.KOEF_Z and t0.n_ksg =t2.k_ksg
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--@q019type in ('C', 'D', 'H', 'R')
--and KSG_PG=0
--and t2.v023ksgid is null
--and t0.n_ksg is not null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0600 end

----001F.00.0610 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'CRIT', CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0610'
--, 'Значение не найдено в справочнике V024'
--from tempdb..TEMP_CRIT t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join ies.T_V024_DOP_KR t2 with(nolock) on t2.IDDKK = t0.CRIT
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
--join tempdb..TEMP_KSG_KPG t3 on t1.IDCASE = t3.IDCASE
--where
--@q019type in ('C', 'D', 'H', 'R')
--and KSG_PG=0
--and t0.CRIT is not null
--and t2.IDDKK is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0610 end

--001F.00.0621 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LPU', t0.LPU as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0621', 'Значение не найдено в справочнике F003'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_F003_MO t2 with(nolock) on t2.mcod = t0.LPU  
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.LPU is not null
and t2.mcod is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0621 end

--001F.00.0630 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', convert(varchar(3), t0.PROFIL) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0630'
, 'Значение не найдено в справочнике V002'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V002_PROFILE t2 with(nolock) on t2.IDPR = t0.PROFIL
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.PROFIL is not null
and t2.IDPR is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0630 end

--001F.00.0640 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', VID_VME as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0640'
, 'Значение не найдено в справочнике V019'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V019_VMP_METHOD t2 with(nolock) on convert(varchar,t2.IDHM) = t0.VID_VME
and t2.DATEBEG<=DATE_Z_2 and (t2.DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('T')
and t0.VID_VME is not null
and t2.V019VmpMethodId is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0640 end

--001F.00.0660 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0660'
, 'Значение не найдено в справочнике V021'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.PRVS is not null
and t2.CODE_SPEC is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--001F.00.0660 end

--001F.00.0661 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0661'
, 'Значение не найдено в справочнике V021'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.PRVS is not null
and t2.CODE_SPEC is null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--001F.00.0661 end

--001F.00.0662 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0662'
, 'Значение не найдено в справочнике V021'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t0.PRVS is not null
and t2.CODE_SPEC is null
and t0.P_OTK=0
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 < convert(date,'01.10.2021',104)
--001F.00.0662 end

--001F.00.0662 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0662'
, 'Значение не найдено в справочнике V021'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.idserv and t2.sl_id = t0.sl_id and t2.idcase = t0.idcase
left join ies.T_V021_MED_SPEC t3 with(nolock) on t3.CODE_SPEC = t2.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('X')
and t2.PRVS is not null
and t3.CODE_SPEC is null
and t0.P_OTK=0
and DATE_Z_2 >= convert(date,'01.10.2021',104)
--001F.00.0662 end

--001F.00.0670 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'S_TIP', convert(varchar(2), t0.S_TIP) as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0670'
, 'Значение не найдено в справочнике F006'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_F006_CONTROL_TYPE t2 with(nolock) on t2.IDVID = t0.S_TIP
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.S_TIP is not null
and t2.IDVID is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0670 end

----001F.00.0680 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'S_OSN', convert(varchar(3), t0.S_OSN) as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0680'
--, 'Значение не найдено в справочнике F014'
--from tempdb..TEMP_SANK t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join ies.T_F014_DENY_REASON t2 with(nolock) on t2.Kod = t0.S_OSN
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--and S_OSN is not null
--and t2.Kod is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0680 end

--001F.00.0720 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCTYPE', DOCTYPE as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0720'
, 'Значение не найдено в справочнике F011'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t1.N_ZAP= t2.N_ZAP 
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
left join ies.T_F011_DOCUMENT_TYPE t4 with(nolock) on t4.IDDoc = t0.DOCTYPE
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
t0.DOCTYPE is not null
and t4.IDDoc is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0720 end

----001F.00.0730 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'OKATOG', OKATOG as ZN_POL ,@NSCHET, 'PERS', null as N_ZAP, t0.ID_PAC as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0730', 'Значение не найдено в справочнике O002'
--from tempdb..TEMP_PERS t0 with(nolock)
--join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
--join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
--left join ies.T_O002_OKATO t4 with(nolock) on t4.CODE = t0.OKATOG  
--where
--t0.OKATOG is not null
--and t4.CODE is null
----ВНИМАНИЕ ПРОВЕРЬ НАЛИЧИЕ индекса на T_002_OKATO.Code
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----001F.00.0730 end

--001F.00.0740 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OKATOP', OKATOP as ZN_POL ,@NSCHET, 'PERS', null as N_ZAP, t0.ID_PAC as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '001F.00.0740', 'Значение не найдено в справочнике O002'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
left join ies.T_O002_OKATO t4 with(nolock) on t4.CODE = t0.OKATOP
where
t0.OKATOP is not null
and t4.CODE is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--001F.00.0740 end



----002F.00.0012 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select top 1 'VERSION', VERSION as ZN_POL ,@NSCHET, 'ZGLV', null, null, null, null, null, '002F.00.0012','Версия счёта отлично от версии 3.1 	Для типа передаваемых данных "H" до отчетного периода "декабрь 2021" включительно и "C" до отчетного периода "декабрь 2023" включительно.'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_ZGLV t3 with(nolock) on 1 = 1
join tempdb..Temp_schet s  with(nolock) on 1 = 1
where 
(( VERSION ='3.1' and @q019type = 'H' and s.year>=2022 )
or( VERSION ='3.1' and @q019type = 'C' and s.year>=2023 ))
and DATE_Z_2 >= convert(date,'01.01.2024',104)
----002F.00.0012 end


----002F.00.0013 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VERSION', VERSION as ZN_POL ,@NSCHET, 'ZGLV', null, null, null, null, null, '002F.00.0013','Версия счёта отлично от версии 4.0 при YEAR>=2024'
from tempdb..TEMP_ZGLV t3
join tempdb..Temp_schet s  with(nolock) on 1 = 1
where  @q019type in ('H','C')
and VERSION <>'4.0' 
and s.[year]>=2024
----002F.00.0013 end



--002F.00.0080 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PR_NOV', convert(varchar(1), t0.PR_NOV) as ZN_POL ,@NSCHET, 'ZAP', t0.N_ZAP, t1.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '002F.00.0080'
, 'Поле PR_NOV имеет недопустимое значение'
from tempdb..TEMP_ZAP t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('C', 'H', 'T', 'X')
and t0.PR_NOV not in (0, 1)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0080 end

--002F.00.0130 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NOVOR', t0.NOVOR as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '002F.00.0130'
, 'Поле NOVOR имеет недопустимое значение'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.NOVOR != '0'
and [dbo].[RegExMatch](t0.NOVOR,'^[12](([012][0-9])|(3[01]))((1[0-2])|(0[1-9]))(19|[23][0-9])((0{0,1}[0-9]{0,1})|10)$') != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0130 end

----002F.00.0140 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'VNOV_D', convert(varchar(4), t0.VNOV_D) as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '002F.00.0140'
--, 'Поле VNOV_D имеет недопустимое значение (300<VNOV_D<2500)'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and not (300<VNOV_D and VNOV_D<2500)
--and VNOV_D is not null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----002F.00.0140 end

----002F.00.0150 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'VNOV_M', convert(varchar(4), t0.VNOV_M) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '002F.00.0150'
--, 'Поле VNOV_M имеет недопустимое значение (300<VNOV_M<2500)'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and not (300<VNOV_M and VNOV_M<2500)
--and VNOV_M is not null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----002F.00.0150 end

--002F.00.0160 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OS_SLUCH', convert(varchar(1), t0.OS_SLUCH) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '002F.00.0160'
, 'Поле OS_SLUCH имеет недопустимое значение'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.OS_SLUCH,1) not in ('1','2')
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0160 end

--002F.00.0180 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OPLATA', convert(varchar(1), t0.OPLATA) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '002F.00.0180'
, 'Поле OPLATA имеет недопустимое значение'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and 
(
	(isnull(t0.OPLATA, -1) not in (0, 1, 2, 3) and @q019type in ('A'))
	or 
	(isnull(t0.OPLATA, 0) not in (0, 1, 2, 3) and @q019type in ('C', 'D', 'H', 'R', 'T', 'X'))
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0180 end

--002F.00.0190 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VBR', convert(varchar(1), t0.VBR) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '002F.00.0190'
, 'Поле VBR имеет недопустимое значение'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('X')
and not (t0.VBR in (0, 1))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0190 end

--002F.00.0200 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_OTK', convert(varchar(1), t0.P_OTK) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '002F.00.0200'
, 'Поле P_OTK имеет недопустимое значение'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('X')
and not (t0.P_OTK in (0,1))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0200 end

--002F.00.0220 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', convert(varchar(1), t0.DET) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0220'
, 'Поле DET имеет недопустимое значение'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and not (DET in (0,1))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0220 end

--002F.00.0250 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS_ONK', convert(varchar(1), t0.DS_ONK) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0250'
, 'Поле DS_ONK имеет недопустимое значение'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'D', 'R', 'T', 'X')
and not ( t0.DS_ONK in (0,1) )
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0250 end

--002F.00.0260 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', DS1 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0260'
, 'Данный случай должен отсутствовать в этом типе реестров'
FROM tempdb..TEMP_SLUCH t0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
WHERE @q019type IN ('H')
	AND (
		DS1 LIKE 'C%'
		OR DS1 LIKE 'D0%'
		OR (
			DS1 = 'D70'
			AND EXISTS (
							SELECT TOP 1 1
							FROM tempdb..TEMP_DS2 t1
							WHERE (
									(
										DS2 >= 'C00'
										AND DS2 < 'C81'
									)
									OR DS2 LIKE 'c97%'
								)
								AND t1.SL_ID = t0.SL_ID
								AND t1.IDCASE = t0.IDCASE
				)
			)
		)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0260 end

--002F.00.0310 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PR_D_N', convert(varchar(1), t0.PR_D_N) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0310'
, 'Поле PR_D_N имеет недопустимое значение'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('X')
and not( isnull(PR_D_N,0) in(1, 2, 3) )
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0310 end

--002F.00.0320 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VERS_SPEC', VERS_SPEC as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0320'
, 'Текущий справочник мед. специальностей - V021'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and VERS_SPEC != 'V021'
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0320 end

--002F.00.0330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2_PR', convert(varchar(1), t0.DS2_PR) as ZN_POL ,@NSCHET, 'DS2_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0330'
, 'Поле DS2_PR имеет недопустимое значение'
from tempdb..TEMP_DS2_N t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and not (isnull(t0.DS2_PR,1) = 1)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0330 end

--002F.00.0340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PR_DS2_N', convert(varchar(1), t0.PR_DS2_N) as ZN_POL ,@NSCHET, 'DS2_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0340'
, 'Поле PR_DS2_N имеет недопустимое значение'
from tempdb..TEMP_DS2_N t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and not (isnull(t0.PR_DS2_N,0) in (1, 2, 3))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0340 end

--002F.00.0350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_R', convert(varchar(2), t0.NAZ_R) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0350'
, 'Поле NAZ_R имеет недопустимое значение'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and not
(
 isnull(t0.NAZ_R,0) in (1, 2, 3, 4, 5, 6)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0350 end

--002F.00.0360 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MTSTZ', convert(varchar(1), t0.MTSTZ) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0360'
, 'Поле MTSTZ имеет недопустимое значение'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and MTSTZ is not null
and MTSTZ != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0360 end

--002F.00.0370 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'WEI', convert(varchar, cast(t0.WEI as money)) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0370'
, 'Поле WEI имеет недопустимое значение'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and WEI is not null
and WEI >= 500
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0370 end

--002F.00.0380 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'HEI', convert(varchar(3), t0.HEI) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0380'
, 'Поле HEI имеет недопустимое значение'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and HEI is not null
and HEI >= 260
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0380 end

--002F.00.0390 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'BSA', convert(varchar, cast(t0.BSA as money)) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0390'
, 'Поле BSA имеет недопустимое значение'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and BSA is not null
and BSA >= 6
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0390 end

--002F.00.0400 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_TIP', convert(varchar(1), t0.DIAG_TIP) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0400'
, 'Поле DIAG_TIP имеет недопустимое значение'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and not (DIAG_TIP in (1,2))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0400 end

--002F.00.0410 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REC_RSLT', convert(varchar(1), t0.REC_RSLT) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0410'
, 'Поле REC_RSLT имеет недопустимое значение'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and REC_RSLT is not null
and REC_RSLT != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0410 end

--002F.00.0420 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PPTR', convert(varchar(1), t0.PPTR) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0420'
, 'Поле PPTR имеет недопустимое значение'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and PPTR is not null
and PPTR != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0420 end

--002F.00.0430 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0430'
, 'CODE_SH должно содержать значение НЕТ'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH != 'нет'
and (dbo.DateTimeDiff('year',dr,date_z_1) < 18 or  (t3.ds1 >= 'C81' and t3.ds1 < 'C97'))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--002F.00.0430 end

--002F.00.0431 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0571'
, 'Пул значений: нет'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and isnull(t0.CODE_SH,'') != 'нет'
and dbo.DateTimeDiff('year',dr,date_z_1) < 18
and t0.USL_TIP > 4
and (
	(t3.ds1 >= 'C81' and t3.ds1 < 'C97') 
	or (t3.ds1 >= 'D45' and t3.ds1 < 'D48') 	
)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--002F.00.0431 end


--002F.00.0465 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'K_ZP', N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0465'
, 'K_ZP должно быть заполнено'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.N_KSG is not null -- мб элемент обязателен
and t0.K_ZP<cast('1.00000' as decimal (7,5))
and DATE_Z_2 >= convert(date,'01.11.2024',104)
--002F.00.0465 end

--003F.00.2755 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'K_ZP', N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2755'
, 'K_ZP должно быть заполнено'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_ZGLV t3 with(nolock) on 1 = 1
where
@q019type in ('C',  'H')
and t0.N_KSG is not null -- мб элемент обязателен
and t0.K_ZP is null
and t3.Version='4.0'
and DATE_Z_2 >= convert(date,'01.11.2024',104)
--003F.00.2755 end


--002F.00.0480 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'S_IST', convert(varchar(1), t0.S_IST) as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0480'
, 'Поле S_IST имеет недопустимое значение'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

where
@q019type in ('C', 'H', 'T', 'X')
and isnull(t0.S_IST,0) != 1
and @type = 693
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0480 end

--002F.00.0510 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', convert(varchar(1), t0.DET) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0510'
, 'Поле DET имеет недопустимое значение'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and DET is not null
and not (DET in (0,1))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0510 end

--002F.00.0530 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_OTK', convert(varchar(1), t0.P_OTK) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '002F.00.0530'
, 'Поле P_OTK имеет недопустимое значение'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and not
(
 isnull(t0.P_OTK,-1) in (0, 1)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--002F.00.0530 end


--002F.00.0560 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0560'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID

where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('002120')
and t3.DS1 in ('C07', 'C08', 'C15', 'C16', 'C54')
and t4.DIAG_CODE=1
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=1
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0560 end


--002F.00.0561 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0561'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001235', '001705', '002120')
and t3.DS1 in ('C19', 'C18')
and t4.DIAG_CODE=1
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=1
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0561 end


--002F.00.0562 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0562'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001653')
and t3.DS1 in ('C19', 'C18')
and t4.DIAG_CODE=4
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=10
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0562 end



--002F.00.0563 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0563'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001614')
and t3.DS1 in ('C25', 'C48.0', 'C48.1', 'C48.2', 'C56', 'C57', 'C61')
and t4.DIAG_CODE=12
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=25
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0563 end

--002F.00.0564 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0564'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('000700','002117')
and t3.DS1 in ('C34')
and t4.DIAG_CODE=2
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=4
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0564 end


--002F.00.0565 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0565'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('000609','001637')
and t3.DS1 in ('C34')
and t4.DIAG_CODE=5
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=11
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0565 end

--002F.00.0566 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0566'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('000074','001203','002297')
and t3.DS1 in ('C34')
and t4.DIAG_CODE=6
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=13
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0566 end

--002F.00.0567 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0567'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('000527', '000700', '001161', '002117')
and t3.DS1 in ('C34')
and t4.DIAG_CODE=2
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=4
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0567 end



--002F.00.0568 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0568'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001235')
and t3.DS1 in ('C20')
and t4.DIAG_CODE=1
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=1
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0568 end



--002F.00.0569 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0569'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001235', '001705', '002120', '002121')
and t3.DS1 in ('C50')
and t4.DIAG_CODE=1
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=1
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0569 end



--002F.00.0570 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0570'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001646', '001881', '002474', '002963') 
and t3.DS1 in ('C50')
and t4.DIAG_CODE=1
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=2
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0570 end


--002F.00.0571 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0571'
, 'Значение не найдено в справочнике N020'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_N020_ONK_LEKP t2 with(nolock) on t2.ID_LEKP = t0.REGNUM
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_B_DIAG t4 on t4.IDCASE=t3.IDCASE and t4.SL_ID=t3.SL_ID
where
@q019type in ('C', 'R', 'T')
and t0.REGNUM is not null
and t2.ID_LEKP IN ('001614', '002963') 
and t3.DS1 in ('C50')
and t4.DIAG_CODE=12
and t4.DIAG_TIP=2
and t4.DIAG_RSLT=25
and DATE_Z_2 >= convert(date,'01.01.2024',104)
--002F.00.0571 end



--003F.00.0030 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEK', convert(varchar, cast(t0.SANK_MEK as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0030'
, 'Поле SANK_MEK заполнено при отсутствии санкций'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEK,0) <=0
and exists (select top 1 1 from tempdb..TEMP_SANK where S_TIP in (1, 10,11,12) and isnull(S_SUM,0)<>0 and S_IST=1)
and @dschet >= convert(date,'01.11.2019',104)
--003F.00.0030 end

--003F.00.0040 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEE', convert(varchar, cast(t0.SANK_MEE as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0040'
, 'Поле SANK_MEE заполнено при отсутствии санкции МЭЭ'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEE,0) <=0
and exists (select top 1 1 from tempdb..TEMP_SANK where S_TIP>19 and S_TIP<30 and isnull(S_SUM,0)<>0 and S_IST=1)
and @dschet >= convert(date,'01.11.2019',104)
and @dschet <= convert(date,'31.05.2021',104)
--003F.00.0040 end

--003F.00.0041 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEE', convert(varchar(36), cast(t0.SANK_MEE as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0040'
, ''
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEE,0) <=0
and exists (select top 1 1 from tempdb..TEMP_SANK where S_TIP>49 and S_TIP<70 and isnull(S_SUM,0)<>0 and S_IST=1)
and @dschet >= convert(date,'01.06.2021',104)
--003F.00.0041 end

--003F.00.0050 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_EKMP', convert(varchar, cast(t0.SANK_EKMP as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0050'
, 'Поле SANK_EKMP заполнено при отсутствии санкций ЭКМП'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_EKMP,0) <=0
and exists (select top 1 1 from tempdb..TEMP_SANK where S_TIP>29 and S_TIP<49 and isnull(S_SUM,0)<>0 and S_IST=1)
and @dschet >= convert(date,'01.11.2019',104)
and @dschet <= convert(date,'31.05.2021',104)
--003F.00.0050 end

--003F.00.0051 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_EKMP', convert(varchar(36), cast(t0.SANK_EKMP as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0051'
, ''
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_EKMP,0) <=0
and exists (select top 1 1 from tempdb..TEMP_SANK where S_TIP>69 and S_TIP<=89 and isnull(S_SUM,0)<>0 and S_IST=1)
and @dschet >= convert(date,'01.06.2021',104)
--003F.00.0051 end

--003F.00.0060 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'Z_SL', null as ZN_POL ,@NSCHET, 'ZAP', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0060'
, 'Зак. случай не может быть включен в этот тип реестров счетов'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C')
and not exists
(
	select top 1 1
	from tempdb..TEMP_SLUCH t1 with(nolock) 
	where t0.idcase = t1.idcase
	and
	(isnull(DS_ONK,0) = 1
	or DS1 like 'C%'  
	or DS1 like 'D0%'  
	or (
			DS1 = 'D70'
			and exists ( 
							select top 1 1 
							from tempdb..TEMP_DS2 t2
							where((DS2>='C00' and DS2<'C81') or DS2 like 'C97%')
							and t2.SL_ID = t1.SL_ID
							and t2.IDCASE= t1.IDCASE
						)
		)
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0060 end

--003F.00.0061 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'Z_SL', null as ZN_POL ,@NSCHET, 'ZAP', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0061'
, 'Зак. случай не может быть включен в этот тип реестров счетов'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C')
and not exists 
(
	select top 1 1
	from tempdb..TEMP_SLUCH t1 with(nolock) 
	where t0.idcase = t1.idcase
	and
		(
			isnull(DS_ONK,0) = 1
			or DS1 like 'C%'  
			or DS1 like 'D0%'  
		)
)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0061 end

--003F.00.0062 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'Z_SL', null as ZN_POL ,@NSCHET, 'ZAP', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0062'
, 'Зак. случай не может быть включен в этот тип реестров счетов'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C')
and not exists 
(
	select top 1 1
	from tempdb..TEMP_SLUCH t1 with(nolock) 
	where t0.idcase = t1.idcase
	and
		(
			isnull(DS_ONK,0) = 1
			or DS1 like 'C%'  
			or DS1 like 'D0%'
			or (DS1 >= 'D45' and DS1 < 'D48')
		)
)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0062 end

----003F.00.0080 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'SPOLIS', t0.SPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0080'
--, 'Поле SPOLIS заполняется только для старых полисов'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
--where
--@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--and SPOLIS is not null
--and VPOLIS<>1
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----003F.00.0080 end

--003F.00.0110 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ST_OKATO', t0.ST_OKATO as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0110'
, 'Поле ST_OKATO должно быть заполнено для старых полисов'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_SCHET t2 with(nolock) on 1 = 1
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t0.n_zap = t3.n_zap
where
@q019type in ('C', 'H', 'T', 'X')
and PLAT is not null
and VPOLIS=1
and ST_OKATO is null
and @type = 693 --это строка выражает условие "ЕСЛИ PLAT не пусто в имени файла"
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0110 end


----006F.00.0220 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'SMO', t0.SMO as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0220'
--, 'Значения SCHET.PLAT отсутствует'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_SCHET t2 with(nolock) on 1 = 1
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t0.n_zap = t3.n_zap
--where
--@q019type in ('C', 'H', 'T', 'X')
--and PLAT is  null
--and t0.SMO is not null
--and @type = 693 --это строка выражает условие "ЕСЛИ PLAT не пусто в имени файла"
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----006F.00.0220 end

----003F.00.0140 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'SMO_OK', t0.SMO_OK as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0140'
--, 'Поле SMO_OK должно быть заполнено при отсутствии SMO'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_SCHET t2 with(nolock) on 1 = 1
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t0.n_zap = t3.n_zap
--where
--@q019type in ('C', 'H', 'T', 'X')
--and t0.SMO is null
--and t0.SMO_OK is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----003F.00.0140 end

------003F.00.0150 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL,  ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'SMO_NAM', SMO_NAM as ZN_POL ,@NSCHET, 'PACIENT', t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0150', 
--'Заполняется при невозможности указать ни реестровый номер, ни ОГРН СМО'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--where
--@q019type in ('H','C','T','X')
--and t0.SMO is null 
--and t0.SMO_OGRN is null
--and ISNULL(t0.SMO_NAM,'')=''
------003F.00.0150 end

--003F.00.0330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCDATE', convert(varchar(10),t0.DOCDATE,104) as ZN_POL ,@NSCHET, 'PERS', null , t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0330'
, 'Поле DOCDATE должно быть заполнено, если VPOLIS <> 3'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t1.N_ZAP= t2.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t1.VPOLIS<>3
and t0.docdate is null
and @type = 554
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0330 end

--003F.00.0340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCORG', t0.docorg as ZN_POL ,@NSCHET, 'PERS', null , t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0340'
, 'Поле DOCORG должно быть заполнено, если VPOLIS <> 3'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t1.N_ZAP= t2.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t1.VPOLIS<>3
and nullif(rtrim(t0.docorg),'') is null
and @type = 554
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0340 end

--003F.00.0390 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VNOV_D', convert(varchar(4), t0.VNOV_D) as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0390'
, 'VNOV_D и VNOV_M не могут быть заполнены одновременно'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.N_ZAP = t1.N_ZAP
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and VNOV_D is not null
and VNOV_M is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0390 end

----003F.00.0400 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPR_MO', NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0400'
--, 'Поле NPR_MO должно быть заполнено при: (FOR_POM=3 и USL_OK=1) или USL_OK=2'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and ((FOR_POM=3 and USL_OK=1) or USL_OK=2)
--and NPR_MO is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----003F.00.0400 end

----003F.00.0401 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPR_MO', NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0401'
--, 'NPR_MO должно быть заполнено при заполнении NPR_DATE'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and NPR_MO is null
--and NPR_DATE is not null
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----003F.00.0401 end

--003F.00.0402 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', convert(varchar(10),t0.NPR_DATE,104) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0402'
, 'Поле NPR_DATE должно быть заполнено при заполнении NPR_MO'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and NPR_DATE is null
and NPR_MO is not null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.0402 end

----003F.00.0410 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPR_DATE', convert(varchar(10),t0.NPR_DATE,104) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0410'
--, 'Поле NPR_DATE должно быть заполнено при: ((FOR_POM=3 and USL_OK=1) or USL_OK=2)'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and ((FOR_POM=3 and USL_OK=1) or USL_OK=2)
--and NPR_DATE is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----003F.00.0410 end

--003F.00.0430 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KD_Z', convert(varchar(3), t0.KD_Z) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0430'
, 'Поле KD_Z должно быть заполнено при USL_OK = (1, 2)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R')
and USL_OK in (1, 2)
and kd_z is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0430 end

--003F.00.0440 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KD_Z', convert(varchar(3), t0.KD_Z) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0440'
, 'Поле KD_Z должно отсутствовать при USL_OK not in (1, 2)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R')
and KD_Z is not null
and not (USL_OK in (1, 2))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0440 end

--003F.00.0450 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VNOV_M', convert(varchar(4), t0.VNOV_M) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0450'
, 'VNOV_D и VNOV_M не могут быть заполнены одновременно'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and VNOV_D is not null
and VNOV_M is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0450 end

--003F.00.0460 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OS_SLUCH', convert(varchar(1), t0.OS_SLUCH) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0460'
, 'Поле OS_SLUCH не заполнено в особом случае'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar,t0.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on  t2.n_zap = t0.n_zap
where
@q019type in ('C', 'H', 'T', 'X')
and t0.OS_SLUCH is null -- мб элемент обязателен
and ((isnull(t1.OT,'') = '' and NOVOR=0) or (isnull(t1.OT_P ,'') = '' and NOVOR!=0) or (NOVOR!=0 and right(novor,1) != '1'))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0460 end

--003F.00.0480 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OS_SLUCH', convert(varchar(1), t0.OS_SLUCH) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0480'
, 'Поле OS_SLUCH должно отсутствовать'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar,t0.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on  t2.n_zap = t0.n_zap
where
@q019type in ('C', 'H', 'T', 'X')
and t0.OS_SLUCH is not null -- элемент должен отсуствовать
and NOVOR=0 and isnull(t1.OT,'') != ''
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0480 end

--003F.00.0500 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VB_P', convert(varchar(1), t0.VB_P) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0500'
, 'Не заполнено поле VB_P'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where
@q019type in ('C', 'H')
and isnull (VB_P,1) != 1-- мб элемент обязателен
and IDSP=33 and P_PER=4
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0500 end

--003F.00.0510 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VB_P', convert(varchar(1), t0.VB_P) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0510'
, 'Поле VB_P должно отсутствовать'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'H')
and t0.VB_P is not null -- элемент должен отсуствовать
and (IDSP != 33
or not exists (
	select top 1 1 from tempdb..TEMP_SLUCH t1 with(nolock)
	where t1.p_per = 4 and t1.idcase = t0.idcase
))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0510 end

--003F.00.0530 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OPLATA', convert(varchar(1), t0.OPLATA) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0530'
, 'Поле OPLATA должно быть заполнено при наличии санкций МЭК'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'H', 'T', 'X')
and exists (select top 1 1 from tempdb..TEMP_SANK where S_IST=1)
and t0.OPLATA is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0530 end

--003F.00.0550 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OPLATA', convert(varchar(1), t0.OPLATA) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0550'
, 'Поле OPLATA должно отсутствовать если нет от ТФОМС/СМО'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'H', 'T', 'X')
and exists (select top 1 1 from tempdb..TEMP_SANK where S_IST!=1)
and t0.OPLATA is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0550 end

--003F.00.0580 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK',null  as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0580'
, 'При наличии SANK_IT должен присутствовать хотя бы один элемент SANK'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.SANK_IT is not null
and not exists (select top 1 1 from tempdb..TEMP_SANK t1 where t1.IDCASE = t0.IDCASE)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0580 end

--003F.00.0590 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_IT', convert(varchar, cast(t0.SANK_IT as money)) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0590'
, 'Поле SANK_IT имеет недопустимое значение'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.SANK_IT is null
and exists (select top 1 1 from tempdb..TEMP_SANK t1 with(nolock) where t0.IDCASE = t1.IDCASE and isnull(s_sum,0) >0 )
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0590 end

--003F.00.0640 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL_K', convert(varchar(3), t0.PROFIL_K) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0640'
, 'Поле PROFIL_K должно быть заполнено при USL_OK = (1, 2)'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.PROFIL_K is null -- мб элемент обязателен
and isnull(USL_OK,0) in (1, 2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0640 end

--003F.00.0650 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL_K', convert(varchar(3), t0.PROFIL_K) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0650'
, 'Поле PROFIL_K должно отсутствовать '
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.PROFIL_K is not null -- элемент должен отсуствовать
and isnull(USL_OK,0) not in (1, 2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0650 end

--003F.00.0660 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_CEL', convert(varchar(3), t0.P_CEL) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID as SL_ID, null as IDSERV,  '003F.00.0660'
, 'Поле P_CEL должно быть заполнено для случаев с USL_OK = 3'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R')--, 'X' похоже убрали
and (isnull(USL_OK,0) = 3)
and t0.P_CEL is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0660 end

--003F.00.0670 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_CEL', convert(varchar(3), t0.P_CEL) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID as SL_ID, null as IDSERV,  '003F.00.0670'
, 'Поле P_CEL должно отсутствовать при USL_OK != 3'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'X')
and (isnull(USL_OK, 0) != 3)
and t0.P_CEL is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0670 end

--003F.00.0700 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_PER', convert(varchar(1), t0.P_PER) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0700'
, 'Поле P_PER обязательно для случаев с USL_OK = (1, 2)'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'H')
and t0.P_PER is null -- мб элемент обязателен
and isnull(USL_OK,0) in (1, 2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0700 end

--003F.00.0710 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_PER', convert(varchar(1), t0.P_PER) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0710'
, 'Поле P_PER должно отсутствовать если USL_OK != (1,2)'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'H')
and t0.P_PER is not null -- элемент должен отсуствовать
and isnull(USL_OK,0) not in (1, 2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0710 end

--003F.00.0740 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KD', convert(varchar(3), t0.KD) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.0740'
, 'Поле KD должно быть заполнено если USL_OK = (1,2)'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R')
and KD is null
and (USL_OK in (1, 2))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0740 end

--003F.00.0750 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KD', convert(varchar(3), t0.KD) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0750'
, 'Поле KD должно отсутствовать если USL_OK != (1,2) '
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R')
and t0.KD is not null -- элемент должен отсуствовать
and USL_OK not in (1, 2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0750 end

--003F.00.0760 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', convert(varchar(1), t0.C_ZAB) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0760'
, 'Поле C_ZAB обязательно при USL_OK = 3 и DS1 != Z'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('H')
and t0.C_ZAB is null -- мб элемент обязателен
and USL_OK=3 
and DS1 not like 'Z%'
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'10.04.2021',104)
--003F.00.0760 end

--003F.00.0761 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', convert(varchar(1), t0.C_ZAB) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0761'
, 'Поле C_ZAB обязательно при USL_OK = 3 и DS1 != {Z*, U11, U11.9)'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('H')
and t0.C_ZAB is null -- мб элемент обязателен
and USL_OK=3 
and not ( DS1 like 'Z%' or DS1 = 'U11' or DS1 = 'U11.9')
and DATE_Z_2 >= convert(date,'04.10.2021',104)
--003F.00.0761 end

--003F.00.0770 begin
INSERT INTO #ERRORS ( IM_POL ,ZN_POL ,NSCHET ,BAS_EL ,N_ZAP ,ID_PAC ,IDCASE ,SL_ID ,IDSERV ,OSHIB ,COMMENT )
SELECT 'C_ZAB' ,CONVERT(VARCHAR(1), T0.C_ZAB) AS ZN_POL ,@NSCHET ,'SL' ,T0.N_ZAP ,T0.ID_PAC ,T0.IDCASE ,T0.SL_ID ,NULL AS IDSERV ,'003F.00.0770'
, 'Поле C_ZAB обязательно при ЗНО и нейтропении'
FROM TEMPDB..TEMP_SLUCH T0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
WHERE @Q019TYPE IN ('T')
	AND (
		DS1 LIKE 'C%'
		OR DS1 LIKE 'D0%'
		OR (
			DS1 = 'D70'
			AND EXISTS (
				SELECT TOP 1 1
				FROM TEMPDB..TEMP_DS2 T1
				WHERE
					((DS2 >= 'C00' AND DS2 < 'C81') OR DS2 LIKE 'C97%')
					AND T1.SL_ID = T0.SL_ID
					AND T1.IDCASE = T0.IDCASE
				)
			)
		)
	AND C_ZAB IS NULL
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0770 end

--003F.00.0771 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0771'
, 'C_ZAB обязательно при этом диагнозе'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.C_ZAB is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0771 end

--003F.00.0772 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0772'
, 'Поля обязательно для данного диагноза'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.C_ZAB is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0772 end

--003F.00.0780 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', convert(varchar(1), t0.C_ZAB) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0780'
, 'Поле C_ZAB обязательно при ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.idcase
where
@q019type in ('C', 'D', 'R')
and t0.C_ZAB is null -- мб элемент обязателен
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
	DS1='D70' 
	and exists
		(
			select top 1 1
			from tempdb..TEMP_DS2 t2 
			where
			((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and t2.SL_ID = t0.SL_ID and t2.IDCASE= t0.IDCASE
		)
	)
)
and isnull(usl_ok,0) != 4
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0780 end

--003F.00.0781 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0781'
, 'C_ZAB обязательно при этом диагнозе'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R')
and t0.C_ZAB is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and isnull(USL_OK,0) != 4
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0781 end

--003F.00.0782 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0782'
, 'Поля обязательно для данного диагноза'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R')
and t0.C_ZAB is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48')
)
and isnull(USL_OK,0) != 4
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0782 end

--003F.00.0790 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DN', convert(varchar(1), t0.DN) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0790'
, 'Поле DN обязательно при цели посещения 1.3'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'D', 'H', 'R')
and t0.DN is null -- мб элемент обязателен
and isnull(P_CEL,'') = '1.3'
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0790 end


--003F.00.0810 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_KPG', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0810'
, 'Элемент KSG_KPG должен отсутствовать если IDSP != 33'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R')
and exists (select top 1 1 from tempdb..TEMP_KSG_KPG t01 with(nolock) where t0.sl_id = t01.sl_id and t0.idcase = t01.idcase)
and IDSP!=33
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0810 end

--003F.00.0830 begin
INSERT INTO #Errors ( IM_POL ,ZN_POL ,NSCHET ,BAS_EL ,N_ZAP ,ID_PAC ,IDCASE ,SL_ID ,IDSERV ,OSHIB ,COMMENT )
SELECT 'CONS' ,NULL AS ZN_POL ,@NSCHET ,'SL' ,t0.N_ZAP ,t0.ID_PAC ,t0.IDCASE ,t0.SL_ID ,NULL AS IDSERV ,'003F.00.0830'
, 'Элемент CONS обязателен при подозрении на ЗНО, ЗНО или нейтропении.'
FROM tempdb..TEMP_SLUCH t0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
WHERE @q019type IN ('D', 'R', 'T')
	AND NOT EXISTS (
		SELECT TOP 1 1
		FROM tempdb..TEMP_CONS t1
		WHERE t1.SL_ID = t0.SL_ID
			AND t1.IDCASE = t0.IDCASE
		)
	AND (
		DS_ONK = 1
		OR (
			DS1 LIKE 'C%'
			OR DS1 LIKE 'D0%'
			OR (
				DS1 = 'D70'
				AND EXISTS (
					SELECT TOP 1 1
					FROM tempdb..TEMP_DS2 t1
					WHERE (
								(
								DS2 >= 'C00'
								AND DS2 < 'C81'
								)
								OR DS2 LIKE 'C97%'
							)
						AND t1.SL_ID = t0.SL_ID
						AND t1.IDCASE = t0.IDCASE
					)
				)
			)
		)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0830 end

--003F.00.0831 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0831'
, 'Консилиум обязателен  если DS_ONK=1 или C00.0<=DS1<D10'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and not exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and (DS_ONK=1 or DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0831 end

--003F.00.0833 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0833'
, 'Консилиум обязателен в данном случае'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and not exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and (DS_ONK=1 or DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0833 end

--003F.00.0840 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0840'
, 'Элемент CONS обязателен при ЗНО или нейтропении.'
FROM tempdb..TEMP_SLUCH t0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
WHERE @q019type IN ('D', 'R', 'T')
	AND NOT EXISTS (
		SELECT TOP 1 1
		FROM tempdb..TEMP_CONS t1
		WHERE t1.SL_ID = t0.SL_ID
			AND t1.IDCASE = t0.IDCASE
		)
	AND (	
		DS1 LIKE 'C%'
		OR DS1 LIKE 'D0%'
		OR (
			DS1 = 'D70'
			AND EXISTS (
				SELECT TOP 1 1
				FROM tempdb..TEMP_DS2 t1
				WHERE ((DS2 >= 'C00' AND DS2 < 'C81') or ds2 like 'C97%')
					AND t1.SL_ID = t0.SL_ID
					AND t1.IDCASE = t0.IDCASE
				)
			)
		)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0840 end

--003F.00.0841 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0841'
, 'Консилиум обязателен для данного диагноза'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C')
and not exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and (DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0841 end

--003F.00.0842 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0842'
, 'Консилиум обязателен для данного диагноза или при подозрении на ЗНО'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C')
and not exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and 
	(
		(DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
		or isnull(DS_ONK,0) = 1
	)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0842 end

--003F.00.0850 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
SELECT 'CONS', NULL AS ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, NULL AS IDSERV, '003F.00.0850'
, 'Элемент CONS должен отсутствовать при отсутствии подозрения на ЗНО, ЗНО или нейтропении'
FROM tempdb..TEMP_SLUCH t0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
WHERE @q019type IN ('D', 'R', 'T')
	AND EXISTS (
		SELECT TOP 1 1
		FROM tempdb..TEMP_CONS t1
		WHERE t1.SL_ID = t0.SL_ID
			AND t1.IDCASE = t0.IDCASE
		)
	AND NOT (
		DS_ONK = 1
		OR (
			DS1 LIKE 'C%'
			OR DS1 LIKE 'D0%'
			OR (
				DS1 = 'D70'
				AND EXISTS (
					SELECT TOP 1 1
					FROM tempdb..TEMP_DS2 t1
					WHERE ((DS2 >= 'C00' AND DS2 < 'C81') or ds2 like 'C97%')
						AND t1.SL_ID = t0.SL_ID
						AND t1.IDCASE = t0.IDCASE
					)
				)
			)
		)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0850 end

--003F.00.0851 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0851'
, 'Консилиумы запрещены при DS_ONK=0 и (DS1<C00 или DS1>D09) '
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and not (DS_ONK=1 or DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0851 end

--003F.00.0853 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0853'
, 'Консилиум запрещён в данном случаев'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
and not (DS_ONK=1 or DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0853 end

--003F.00.0860 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CONS', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0860', 'Элемент CONS должен отсутствовать.'
FROM tempdb..TEMP_SLUCH t0 WITH (NOLOCK)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
WHERE @q019type IN ('C')
AND EXISTS (SELECT TOP 1 1 FROM tempdb..TEMP_CONS t1 WHERE t1.SL_ID = t0.SL_ID AND t1.IDCASE = t0.IDCASE)
AND not 
(	
DS1 LIKE 'C%'
OR DS1 LIKE 'D0%'
OR (
	DS1 = 'D70'
	AND EXISTS 
	(
	SELECT TOP 1 1
	FROM tempdb..TEMP_DS2 t1
	WHERE ((DS2 >= 'C00' AND DS2 < 'C81') or ds2 like 'C97%')
	AND t1.SL_ID = t0.SL_ID
	AND t1.IDCASE = t0.IDCASE
	)
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'02.11.2019',104)
--003F.00.0860 end

--003F.00.0870 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0870'
, 'Элемент ONK_SL должен быть заполнен при ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('T')
and not exists (select top 1 1 from tempdb..TEMP_ONK_SL t10 with(nolock) where t10.sl_id = t0.sl_id and t10.idcase = t0.idcase )
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
	DS1='D70' 
	and exists
		(
			select top 1 1
			from tempdb..TEMP_DS2 t2 
			where
			((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and t2.SL_ID = t0.SL_ID and t2.IDCASE= t0.IDCASE
		)
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0870 end

--003F.00.0871 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0871'
, 'Элемент обязателен при условии C00.0<=DS1<D10'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.DS1_T is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0871 end



--003F.00.0872 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0872'
, 'Элемент обязателен для данного диагноза'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.DS1_T is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0872 end


--003F.00.0872 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, 'EMF.023'
, 'Элемент обязателен для данного диагноза'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('T','C','H','X','D')
and t0.DS1_T is null -- мб элемент обязателен
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0872 end


--003F.00.0880 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0880'
, 'Элемент ONK_SL должен быть заполнен при ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_SL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C')
and t2.DS1_T is null
and (
	(USL_OK != 4 and REAB != 1 and DS_ONK=0)
	and (
	DS1 like 'C%'
	or DS1 like 'D0%'
	or (
	         DS1 = 'D70'
	         and exists ( select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
	    )
	
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0880 end

--003F.00.0881 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0881'
, 'Элемент обязателен при условии C00.0<=DS1<D10 и USL_OK<>4 и REAB<>1 и DS_ONK=0'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('C')
and t0.DS1_T is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and USL_OK != 4
and REAB != 1
and DS_ONK = 0
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0881 end

--003F.00.0882 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0881'
, 'Элемент ONK_SL обязателен в данном случае'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('C')
and t0.DS1_T is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and USL_OK != 4
and REAB != 1
and DS_ONK = 0
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0882 end

--003F.00.0900 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0900'
, 'Элемент ONK_SL должен отсутствовать при отсутствии ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('T')
and exists (select top 1 1 from tempdb..TEMP_ONK_SL t10 with(nolock) where t10.sl_id = t0.sl_id and t10.idcase = t0.idcase )
and not (
DS1 like 'C%'
or DS1 like 'D0%'
or (
	DS1='D70' 
	and exists
		(
			select top 1 1
			from tempdb..TEMP_DS2 t2 
			where
			((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and t2.SL_ID = t0.SL_ID and t2.IDCASE= t0.IDCASE
		)
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0900 end

--003F.00.0901 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0901'
, 'элемент должен отсутствовать при условии DS1<C00 или DS1>D09'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.DS1_T is not null -- элемент должен отсуствовать
and not (DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0901 end

--003F.00.0902 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0902'
, 'элемент должен отсутствовать для данного диагноза'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.DS1_T is not null -- элемент должен отсуствовать
and not (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0902 end

--003F.00.0910 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0910'
, 'Элемент ONK_SL должен отсутствовать при отсутствии ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
join tempdb..TEMP_ONK_SL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C')
AND t2.ds1_t is not null
and not (
	(USL_OK != 4 and REAB != 1)
	and (
	DS1 like 'C%'
	or DS1 like 'D0%'
	or (
	         DS1 = 'D70'
	         and exists ( select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
	    )
	
	)
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0910 end

--003F.00.0911 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0911'
, 'элемент должен отсутствовать при выполнении условия DS1<C00 или DS1>D09 или USL_OK=4 или REAB=1'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('C')
and t0.DS1_T is not null -- элемент должен отсуствовать
and (
not (DS1 like 'C%' or DS1 like 'D0%')
or USL_OK = 4
or REAB = 1
)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0911 end

--003F.00.0912 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_SL', null as ZN_POL ,@NSCHET, 'SL', t2.N_ZAP, t2.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0912'
, 'Элемент должен отсутствовать при выполнении условия'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.IDCASE = t1.IDCASE
where
@q019type in ('C')
and t0.DS1_T is not null -- элемент должен отсуствовать
and (
not (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
or USL_OK = 4
or REAB = 1
)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0912 end

--003F.00.0930 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', convert(varchar, cast(t0.TARIF as money)) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0930'
, 'Поле TARIF не может быть пустым при ЗНО и нейтропении'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
where
@q019type in ('C', 'T')
and t0.TARIF is null -- мб элемент обязателен
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
         DS1 = 'D70'
         and exists ( select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
    )
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0930 end

--003F.00.0931 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0931'
, 'Поле обязательно при условии C00.0<=DS1<D10'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'T')
and t0.TARIF is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0931 end

--003F.00.0932 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0932'
, 'Элемент обязателен согласно диагнозу'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'T')
and t0.TARIF is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0932 end

--003F.00.0940 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0940'
, 'Элемент USL обязателен в данном случае'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('T')
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
         DS1 = 'D70'
         and exists ( select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
    )

)
and isnull(t2.USL_TIP,0) in (1,3,4)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 where t4.sl_id = t0.sl_id and t4.idcase = t0.idcase)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0940 end

--003F.00.0941 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0941'
, 'Если C00.0<=DS1<D10 и USL_TIP={1, 3, 4}, то элемент обязателен'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('T')
and (DS1 like 'C%' or DS1 like 'D0%')
and isnull(t2.USL_TIP,0) in (1,3,4)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 with(nolock) where t4.IDCASE = t0.IDCASE and t4.SL_ID = t0.SL_ID)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0941 end

--003F.00.0942 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0941'
, 'Элеент обязателен'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('T')
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and isnull(t2.USL_TIP,0) in (1,3,4)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 with(nolock) where t4.IDCASE = t0.IDCASE and t4.SL_ID = t0.SL_ID)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0942 end

--003F.00.0950 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t1.SL_ID, null as IDSERV, '003F.00.0950'
, 'Услуги должны отсутствовать при наличии признака отказа'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('X')
and isnull(t0.P_OTK,0) = 1
and exists 
(
select top 1 1 from tempdb..TEMP_USL t2
where t2.IDCASE = t0.IDCASE
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0950 end

--003F.00.0960 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0960'
, 'Элемент USL обязателен в этом случае'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R')
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
         DS1 = 'D70'
         and exists ( select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
    )

)
and isnull(t2.USL_TIP,0) in (1,3,4,6)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 where t4.sl_id = t0.sl_id and t4.idcase = t0.idcase)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.0960 end

--003F.00.0961 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0961'
, 'Если C00.0<=DS1<D10 и USL_TIP={1, 3, 4, 6}, то поле обязательно'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R')
and (DS1 like 'C%' or DS1 like 'D0%')
and isnull(t2.USL_TIP,0) in (1,3,4,6)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 with(nolock) where t4.IDCASE = t0.IDCASE and t4.SL_ID = t0.SL_ID)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.0961 end

--003F.00.0962 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0961'
, 'Элеент обязателен'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R')
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and isnull(t2.USL_TIP,0) in (1,3,4,6)
and not exists (select top 1 1 from tempdb..TEMP_USL t4 with(nolock) where t4.IDCASE = t0.IDCASE and t4.SL_ID = t0.SL_ID)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.0962 end

--003F.00.0970 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STAD', convert(varchar(3), t0.STAD) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0970'
, 'Поле STAD обязателен в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and t0.STAD is null
and t0.ds1_t in (0,1,2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0970 end

--003F.00.0980 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STAD', convert(varchar(3), t0.STAD) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0980'
, 'Поле STAD обязателен в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R')
and t0.STAD is null
and t0.ds1_t in (0,1,2,3,4)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0980 end

--003F.00.0990 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_T', convert(varchar(4), t0.ONK_T) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.0990', 'Обязателен при DS1_T=0 и возраст пациента на DATE_Z_1 больше или равен 18 лет'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_T is null
and t0.ds1_t = 0
and dbo.DateTimeDiff('year',t2.dr,t3.date_1)>=18
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0990 end

--003F.00.1000 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_N', convert(varchar(4), t0.ONK_T) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1000', 'Обязателен при DS1_T=0 и возраст пациента на DATE_Z_1 больше или равен 18 лет'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_N is null
and t0.ds1_t = 0
and dbo.DateTimeDiff('year',t2.dr,t3.date_1)>=18
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1000 end

--003F.00.1010 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_M', convert(varchar(4), t0.ONK_T) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1010', 'Обязателен при DS1_T=0 и возраст пациента на DATE_Z_1 больше или равен 18 лет'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and t0.ONK_M is null
and t0.ds1_t = 0
and dbo.DateTimeDiff('year',t2.dr,t3.date_1)>=18
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1010 end

--003F.00.1020 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MTSTZ', convert(varchar(1), t0.MTSTZ) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1020'
, 'Поле MTSTZ должно отсутствовать если DS1_T != (1,2)'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and t0.MTSTZ is not null
and not (t0.DS1_T in (1,2))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1020 end

--003F.00.1030 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SOD', convert(varchar, cast(t0.SOD as money)) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1030'
, 'Поле SOD должно быть заполнено в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

join tempdb..TEMP_ONK_USL t2 with(nolock) on t2.SL_ID = t0.SL_ID and t2.IDCASE = t0.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and t0.SOD is null
and isnull(t2.USL_TIP,0) in (3,4)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1030 end

--003F.00.1040 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SOD', convert(varchar, cast(t0.SOD as money)) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1040'
, 'Поле SOD должно отсутствовать в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and t0.SOD is not null
and not exists (select top 1 1 from tempdb..TEMP_ONK_USL t2 with(nolock) where t2.SL_ID = t0.SL_ID and t2.IDCASE = t0.IDCASE and isnull(t2.USL_TIP,0) in (3,4))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1040 end

--003F.00.1050 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'K_FR', convert(varchar(2), t0.K_FR) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1050'
, 'Поле K_FR должно быть заполнено в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_ONK_USL t2 with(nolock) on t2.SL_ID = t0.SL_ID and t2.IDCASE = t0.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and t0.K_FR is null
and isnull(t2.USL_TIP,0) in (3,4)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1050 end

--003F.00.1060 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'K_FR', convert(varchar(2), t0.K_FR) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1060'
, 'Поле K_FR должно отсутствовать в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

where
@q019type in ('C', 'D', 'R', 'T')
and t0.K_FR is not null
and not exists (select top 1 1 from tempdb..TEMP_ONK_USL t2 with(nolock) where t2.SL_ID = t0.SL_ID and t2.IDCASE = t0.IDCASE and isnull(t2.USL_TIP,0) in (3,4))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1060 end

--003F.00.1070 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'WEI', convert(varchar, cast(t0.WEI as money)) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1070'
, 'Поле WEI должно быть заполнено, если заполнено BSA'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and BSA is not null
and WEI is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1070 end

--003F.00.1080 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'HEI', convert(varchar(3), t0.HEI) as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1080'
, 'Поле HEI должно быть заполнено, если заполнено поле BSA'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and BSA is not null
and HEI is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1080 end

--003F.00.1090 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_USL', null as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1090'
, 'Элемент ONK_USL обязателен в данном случае'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R')
and not exists (select top 1 1 from tempdb..TEMP_ONK_USL t11 where t11.sl_id = t3.sl_id and t11.idcase = t3.idcase)
and USL_OK in (1, 2) 
and DS1_T in (0,1,2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1090 end

--003F.00.1100 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1100'
, 'N_KSG или N_KPG должно быть заполнено'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.N_KSG is null -- мб элемент обязателен
and t0.N_KPG is null
and IDSP=33
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1100 end

--003F.00.1110 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1110'
, 'N_KSG или N_KPG должно быть заполнено'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.N_KSG is not null -- элемент должен отсуствовать
and t0.N_KPG is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1110 end

--003F.00.1130 begin
--повторяет 003F.00.1110
--003F.00.1130 end

--003F.00.1140 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IT_SL', convert(varchar, cast(t0.IT_SL as money)) as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1140'
, 'Поле IT_SL обязательно при наличии SL_KOEF'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.idcase = t2.idcase and t0.sl_id = t2.sl_id
where
@q019type in ('C', 'D', 'H', 'R')
and t0.IT_SL is null -- мб элемент обязателен
and exists (select top 1 1 from tempdb..TEMP_SL_KOEF t11 with(nolock) where t11.sl_id = t2.sl_id and t11.idcase = t2.idcase)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1140 end

--003F.00.1150 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IT_SL', convert(varchar, cast(t0.IT_SL as money)) as ZN_POL ,@NSCHET, 'KSG_KPG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1150'
, 'IT_SL должен отсуствовать при отсуствии SL_KOEF'
from tempdb..TEMP_KSG_KPG t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE and t0.SL_ID = t1.SL_ID
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'H', 'R')
and t0.IT_SL is not null -- элемент должен отсуствовать
and not exists (select top 1 1 from tempdb..TEMP_SL_KOEF t11 with(nolock) where t11.sl_id = t1.sl_id and t11.idcase = t1.idcase)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1150 end

--003F.00.1180 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', NAPR_MO as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1180', 'Элемент NAPR_MO должен отсуствовать если он равен CODE_MO.'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SCHET t2 on 1 = 1
where
@q019type in ('C', 'D', 'R', 'T')
and NAPR_MO=CODE_MO
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'02.11.2019',104)
--003F.00.1180 end

--003F.00.1190 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MET_ISSL', convert(varchar(2), t0.MET_ISSL) as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1190'
, 'Поле MET_ISSL обязательно при NAPR_V=3'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and  MET_ISSL is null
and NAPR_V=3
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1190 end

--003F.00.1200 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MET_ISSL', convert(varchar(2), t0.MET_ISSL) as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1200'
, 'Поле MET_ISSL должно отсутствовать при NAPR_V != 3'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and MET_ISSL is not null
and NAPR_V != 3
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1200 end

--003F.00.1210 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_USL', NAPR_USL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1210'
, 'Поле NAPR_USL обязательно, если MET_ISSL присутствует'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and NAPR_USL is null
and MET_ISSL is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1210 end

--003F.00.1220 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_USL', NAPR_USL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1220'
, 'Поле NAPR_USL должно отсутствовать, если MET_ISSL отсутствует'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and NAPR_USL is not null
and MET_ISSL is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1220 end

--003F.00.1230 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DT_CONS', convert(varchar(10),t0.DT_CONS,104) as ZN_POL ,@NSCHET, 'CONS', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1230'
, 'Поле DT_CONS обязательно при PR_CONS={1,2,3}'
from tempdb..TEMP_CONS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and PR_CONS in (1,2,3)
and DT_CONS is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1230 end

--003F.00.1240 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DT_CONS', convert(varchar(10),t0.DT_CONS,104) as ZN_POL ,@NSCHET, 'CONS', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1240'
, 'Поле DT_CONS должно отсутствовать при PR_CONS<>{1,2,3}'
from tempdb..TEMP_CONS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and not (PR_CONS in (1,2,3))
and DT_CONS is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1240 end

--003F.00.1250 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_RSLT', convert(varchar(3), t0.DIAG_RSLT) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1250'
, 'Поле DIAG_RSLT обязательно при REC_RSLT =1'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and REC_RSLT =1
and DIAG_RSLT is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1250 end

--003F.00.1260 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REC_RSLT', convert(varchar(1), t0.REC_RSLT) as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1260'
, 'Поле REC_RSLT обязательно при наличии DIAG_RSLT'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and REC_RSLT is null
and DIAG_RSLT is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1260 end

--003F.00.1270 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_SP', convert(varchar(4), t0.NAZ_SP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1270'
, 'Поле NAZ_SP обязательно  при NAZ_R={1, 2}'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and t0.NAZ_SP is null
and t0.NAZ_R in (1,2)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1270 end

--003F.00.1280 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_SP', convert(varchar(4), t0.NAZ_SP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1280'
, 'Поле NAZ_SP должно отсутствовать'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and t0.NAZ_SP is not null
and not (isnull(t0.NAZ_R,0) in (1,2))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1280 end

--003F.00.1290 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_V', convert(varchar(1), t0.NAZ_V) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1290'
, 'Поле NAZ_V обязательно при NAZ_R=3'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V029_MET_ISSL t2 with(nolock) on t2.IDMET = t0.NAZ_V
where
@q019type in ('X')
and t0.NAZ_R=3
and 
(
    t0.NAZ_V is null
    or
    (
        t0.NAZ_V is not null
        and t2.IDMET is null
    )
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1290 end

--003F.00.1300 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_V', convert(varchar(1), t0.NAZ_V) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1300'
, 'Поле NAZ_V должно отсутствовать NAZ_R<>3'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

where
@q019type in ('X')
and isnull(t0.NAZ_R,0) !=3
and t0.NAZ_V is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1300 end

--003F.00.1310 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_USL', NAZ_USL as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1310'
, 'Поле NAZ_USL обязательно при NAZ_R=3 и DS_ONK=1'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and isnull(NAZ_R,0) =3 
and isnull(DS_ONK,0) = 1
and t0.NAZ_USL is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1310 end

--003F.00.1320 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_USL', NAZ_USL as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1320'
, 'Поле NAZ_USL должно отсутствовать если NAZ_R<>3 или DS_ONK=0'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and (
isnull(t0.naz_r,0) != 3
or DS_ONK = 0)
and NAZ_USL is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1320 end

--003F.00.1330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_DATE', convert(varchar(10),t0.NAPR_DATE,104) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1330'
, 'Поле NAPR_DATE обязательно при NAZ_R={2,3} и DS_ONK=1'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and isnull(t0.NAZ_R,0) in (2,3)
and DS_ONK = 1
and NAPR_DATE is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1330 end

--003F.00.1340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_DATE', convert(varchar(10),t0.NAPR_DATE,104) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1340'
, 'Поле NAPR_DATE должно отсутствовать если NAZ_R<>{2,3} или DS_ONK=0'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and t0.NAPR_DATE is not null
and (not (NAZ_R in (2,3))
or 
DS_ONK = 0 )
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1340 end

--003F.00.1350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', NAPR_MO as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1350'
, 'Поле NAPR_MO обязательно при NAZ_R={2,3} и DS_ONK=1'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and isnull(t0.NAZ_R,0) in (2,3)
and DS_ONK = 1
and NAPR_MO is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1350 end

--003F.00.1360 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', NAPR_MO as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1360'
, 'Поле NAPR_MO должно отсутствовать если NAZ_R<>{2,3} или DS_ONK=0'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and t0.NAPR_MO is not null
and (not (NAZ_R in (2,3))
or 
DS_ONK = 0 )
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1360 end

--003F.00.1370 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PMP', convert(varchar(3), t0.NAZ_PMP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1370'
, 'Поле NAZ_PMP обязательно при NAZ_R={4, 5}'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE

join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and NAZ_PMP is null
and NAZ_R in (4, 5)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1370 end

--003F.00.1380 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PMP', convert(varchar(3), t0.NAZ_PMP) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1380'
, 'Поле NAZ_PMP должно отсутствовать если NAZ_R<>{4, 5}'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and not (NAZ_R in (4, 5))
and NAZ_PMP is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1380 end

--003F.00.1390 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PK', convert(varchar(3), t0.NAZ_PK) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1390'
, 'Поле NAZ_PK обязательно при NAZ_R=6'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and NAZ_R=6
and NAZ_PK is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1390 end

--003F.00.1400 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_PK', convert(varchar(3), t0.NAZ_PK) as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1400'
, 'Поле NAZ_PK должен отсутствовать если NAZ_R<>6'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.sl_id = t2.sl_id and t0.idcase = t2.idcase
where
@q019type in ('X')
and NAZ_R<>6
and NAZ_PK is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1400 end

--003F.00.1410 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'HIR_TIP', convert(varchar(1), t0.HIR_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1410'
, 'Поле HIR_TIP обязательно при USL_TIP=1'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP=1
and HIR_TIP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1410 end

--003F.00.1420 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'HIR_TIP', convert(varchar(1), t0.HIR_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1420'
, 'Поле HIR_TIP должно отсутствовать если USL_TIP<>1'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP != 1
and HIR_TIP is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1420 end

--003F.00.1430 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_L', convert(varchar(1), t0.LEK_TIP_L) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1430'
, 'Поле LEK_TIP_L обязательно USL_TIP=2'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP=2
and LEK_TIP_L is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1430 end

--003F.00.1440 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_L', convert(varchar(1), t0.LEK_TIP_L) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1440'
, 'Поле LEK_TIP_L должно отсутствовать если USL_TIP<>2'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP != 2
and LEK_TIP_L is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1440 end

--003F.00.1450 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_V', convert(varchar(1), t0.LEK_TIP_V) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1450'
, 'Поле LEK_TIP_V обязательно если USL_TIP=2'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP=2
and LEK_TIP_V is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1450 end

--003F.00.1460 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_TIP_V', convert(varchar(1), t0.LEK_TIP_V) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1460'
, 'Поле LEK_TIP_V должно отсутствовать если USL_TIP<>2'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP != 2
and LEK_TIP_V is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1460 end

--003F.00.1470 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LUCH_TIP', convert(varchar(1), t0.LUCH_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1470'
, 'Поле LUCH_TIP обязательно если USL_TIP={3, 4}'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP in (3, 4)
and LUCH_TIP is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1470 end

--003F.00.1480 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LUCH_TIP', convert(varchar(1), t0.LUCH_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1480'
, 'Поле LUCH_TIP должно отсутствовать если USL_TIP<>{3, 4}'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and not (USL_TIP in (3, 4))
and LUCH_TIP is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1480 end

--003F.00.1490 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_PR', null as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1490'
, 'Поле LEK_PR обязательно если USL_TIP={2, 4}'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP in (2, 4)
and not exists (select top 1 1 from tempdb..TEMP_LEK_PR_DATE_INJ t2 with(nolock) where t2.SL_ID = t0.SL_ID and t2.IDCASE = t0.IDCASE)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1490 end

--003F.00.1500 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_PR', null as ZN_POL ,@NSCHET, 'ONK_USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1500'
, 'Поле LEK_PR должно отсутствовать если USL_TIP<>{2, 4}'
from tempdb..TEMP_ONK_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and USL_TIP not in (2, 4)
and exists (
	select top 1 1 
	from tempdb..TEMP_LEK_PR_DATE_INJ t2 with(nolock) 
	where t2.SL_ID = t0.SL_ID
	and t2.IDCASE = t0.IDCASE
	)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1500 end

--003F.00.1510 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1510'
, 'Поле VID_VME обязательно если (C00.0<=DS1<D10или (DS1=D70 и (C00.0<=DS2<C81 или DS2=C97))) и USL_TIP={1,3,4}'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1  with(nolock) on t0.idcase = t1.idcase
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
join tempdb..TEMP_USL t3 on t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE
where
@q019type in ('T')
and t3.VID_VME is null
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
         DS1 = 'D70'
         and exists (select top 1 1 from tempdb..TEMP_DS2 t3 where ((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and  t3.SL_ID = t0.SL_ID and t3.IDCASE = t0.IDCASE)
    )
)
and isnull(t2.USL_TIP,0) in (1,3,4)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.1510 end

--003F.00.1511 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1511'
, 'Элемент обязателен, если C00.0<=DS1<D10 и USL_TIP={1,3,4}'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
join tempdb..TEMP_ONK_USL t3 with(nolock) on t3.IDCASE = t2.IDCASE and t3.SL_ID = t2.SL_ID
where
@q019type in ('T')
and t0.VID_VME is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and USL_TIP in (1,3,4)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.1511 end

--003F.00.1512 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1511'
, 'Элеент обязателен'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
join tempdb..TEMP_ONK_USL t3 with(nolock) on t3.IDCASE = t2.IDCASE and t3.SL_ID = t2.SL_ID
where
@q019type in ('T')
and t0.VID_VME is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and USL_TIP in (1,3,4)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.1512 end

--003F.00.1520 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', VID_VME as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1520'
, 'Поле VID_VME обязательно если (C00.0<=DS1<D10или (DS1=D70 и (C00.0<=DS2<C81 или DS2=C97))) и USL_TIP={1,3,4, 6}'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE and t0.sl_id = t1.sl_Id
left join tempdb..TEMP_ONK_USL t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t1.n_zap = t3.n_zap
where
@q019type in ('C', 'D', 'R')
and t0.VID_VME is null -- мб элемент обязателен
and (
DS1 like 'C%'
or DS1 like 'D0%'
or (
	DS1='D70' 
	and exists
		(
			select top 1 1
			from tempdb..TEMP_DS2 t2 
			where
			((DS2>='C00' and DS2<'C81') or DS2 like 'C97%') and t2.SL_ID = t1.SL_ID and t2.IDCASE= t1.IDCASE
		)
	)
)
and isnull(t2.USL_TIP,0) in (1,3,4,6)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.03.2020',104)
--003F.00.1520 end

--003F.00.1521 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1521'
, 'Элемент обязателен, если C00.0<=DS1<D10 и USL_TIP={1,3,4, 6}'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
join tempdb..TEMP_ONK_USL t3 with(nolock) on t3.IDCASE = t2.IDCASE and t3.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'R')
and t0.VID_VME is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%')
and USL_TIP in (1,3,4,6)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--003F.00.1521 end

--003F.00.1522 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1521'
, 'Элемент обязателен'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
join tempdb..TEMP_ONK_USL t3 with(nolock) on t3.IDCASE = t2.IDCASE and t3.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'R')
and t0.VID_VME is null -- мб элемент обязателен
and (DS1 like 'C%' or DS1 like 'D0%' or (DS1 >= 'D45' and DS1 < 'D48'))
and USL_TIP in (1,3,4,6)
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--003F.00.1522 end

--003F.00.1530 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM', t0.FAM as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1530'
, 'Поле FAM обязательно если NOVOR=0 и DOST<>2'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t1.NOVOR=0 and not exists(select top 1 1 from tempdb..TEMP_DOST t3 where t3.dost='2' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) )
and t0.fam is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1530 end

--003F.00.1540 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM', t0.FAM as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1540'
, 'Поле FAM должно отсутствовать если NOVOR<>0 или DOST=2'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t0.fam is not null and
not (t1.NOVOR=0 and not exists(select top 1 1 from tempdb..TEMP_DOST t3 where t3.dost ='2' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) ))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1540 end

--003F.00.1550 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM_P', t0.FAM_P as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1550'
, 'Поле FAM_P обязательно если NOVOR<>0 и DOST_P<>2'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t1.NOVOR!=0 and not exists(select top 1 1 from tempdb..TEMP_DOST_P t3 where t3.dost_p='2' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC)  )
and t0.fam_p is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1550 end

--003F.00.1560 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM_P', t0.FAM_P as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1560'
, 'Поле FAM_P должно отсутствовать если DOST_P=2 или NOVOR=0'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t0.fam_p is not null
and not (t1.NOVOR!=0 and not exists(select top 1 1 from tempdb..TEMP_DOST_P t3 where t3.dost_p='2' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) ))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1560 end

--003F.00.1570 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM', t0.IM as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1570'
, 'Поле IM обязательно если NOVOR=0 и DOST<>3'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t1.NOVOR=0 and not exists(select top 1 1 from tempdb..TEMP_DOST t3 where t3.dost = '3' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) )
and t0.im is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1570 end

--003F.00.1580 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM', t0.IM as ZN_POL ,@NSCHET, 'PERS', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.1580'
, 'Поле IM должно отсутствовать если NOVOR<>0 или DOST=3'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC)= convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_ZAP t2 with(nolock) on t2.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
where
t0.im is not null
and not (t1.NOVOR=0 and not exists(select top 1 1 from tempdb..TEMP_DOST t3 where t3.dost='3' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) ))
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1580 end

--003F.00.1590 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM_P', IM_P as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1590'
, 'Поле IM_P обязательно если NOVOR<>0 и DOST_P<>3'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST_P t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
IM_P is null -- мб элемент обязателен
and NOVOR!=0 and t3.DOST_P!='3'
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1590 end

--003F.00.1600 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM_P', IM_P as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1600'
, 'Поле IM_P должно отсутствовать если DOST_P=3 или NOVOR=0 '
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
IM_P is not null -- элемент должен отсуствовать
and (NOVOR=0 or 
exists(select top 1 1 from tempdb..TEMP_DOST_P t3 where t3.dost_p = '3' and convert(varchar(36),t3.ID_PAC)= convert(varchar(36),t0.ID_PAC) )
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1600 end

--003F.00.1610 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OT', OT as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1610'
, 'Поле OT должно отсутствовать если NOVOR не равно 0 или DOST равно 1'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
OT is not null -- элемент должен отсуствовать
and (NOVOR!=0 or t3.DOST='1')
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1610 end

--003F.00.1620 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OT_P', OT_P as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1620'
, 'Поле OT_P должно отсутствовать если NOVOR=0 или DOST_P=1 '
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST_P t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
OT_P is not null -- элемент должен отсуствовать
and (NOVOR=0 and t3.DOST_P='1')
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1620 end

--003F.00.1630 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOST', convert(varchar(1), t3.DOST) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1630'
, 'Поле DOST обязательно если IM отсутствует и NOVOR=0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
t3.DOST is null -- мб элемент обязателен
and IM is null and NOVOR=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1630 end

--003F.00.1640 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOST', convert(varchar(1), t3.DOST) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1640'
, 'Поле DOST обязательно если FAM отсутствует и NOVOR=0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
t3.DOST is null -- мб элемент обязателен
and FAM is null and NOVOR=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1640 end

--003F.00.1650 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOST_P', convert(varchar(1), t3.DOST_P) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1650'
, 'Поле DOST_P обязательно если IM_P отсутствует и NOVOR<>0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST_P t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
t3.DOST_P is null -- мб элемент обязателен
and IM_P is null and NOVOR!=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1650 end

--003F.00.1660 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOST_P', convert(varchar(1), t3.DOST_P) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1660'
, 'Поле DOST_P обязательно если FAM_P отсутствует и NOVOR<>0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
left join tempdb..TEMP_DOST_P t3 on convert(varchar(36),t3.ID_PAC)=convert(varchar(36),t0.ID_PAC)
where
t3.DOST_P is null -- мб элемент обязателен
and FAM_P  is null and NOVOR!=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1660 end

--003F.00.1670 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'W_P', convert(varchar(1), W_P) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1670'
, 'Поле W_P обязательно если NOVOR<>0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
W_P is null -- мб элемент обязателен
and NOVOR!=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1670 end

--003F.00.1680 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'W_P', convert(varchar(1), W_P) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1680'
, 'Поле W_P должно отсутствовать если NOVOR=0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
W_P is not null -- элемент должен отсуствовать
and NOVOR=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1680 end

--003F.00.1690 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR_P', convert(varchar(10),DR_P,104) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1690'
, 'Поле DR_P обязательно если NOVOR<>0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
DR_P is null -- мб элемент обязателен
and NOVOR!=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1690 end

--003F.00.1700 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR_P', convert(varchar(10),DR_P,104) as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE,null as SL_ID, null as IDSERV, '003F.00.1700'
, 'Поле DR_P должно отсутствовать если NOVOR=0'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
DR_P is not null -- элемент должен отсуствовать
and NOVOR=0
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1700 end

--003F.00.1720 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCTYPE', DOCTYPE as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1720'
, 'Поле DOCTYPE обязательно если VPOLIS<>3'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar,t0.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on  t2.n_zap = t0.n_zap
where
DOCTYPE is null -- мб элемент обязателен
and VPOLIS<>3
and @type = 554
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1720 end

--003F.00.1730 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCSER', DOCSER as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1730'
, 'Поле DOCSER обязательно если VPOLIS<>3'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar,t0.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on  t2.n_zap = t0.n_zap
where
DOCSER is null -- мб элемент обязателен
and VPOLIS<>3
and @type = 554
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1730 end

--003F.00.1740 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCNUM', DOCNUM as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '003F.00.1740'
, 'Поле DOCNUM обязательно если VPOLIS<>3'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar,t0.ID_PAC) = convert(varchar,t1.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on  t2.n_zap = t0.n_zap
where
DOCNUM is null -- мб элемент обязателен
and VPOLIS<>3
and @type = 554
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1740 end

--003F.00.1770 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL_ID', SL_ID as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1770'
, 'Поле SL_ID обязательно если S_SUM<>0'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.s_sum,0) != 0
and t0.sl_id is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1770 end

--003F.00.1780 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'S_OSN', convert(varchar(3), t0.S_OSN) as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1780'
, 'Поле S_OSN обязательно если S_SUM<>0'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.s_sum,0) != 0
and t0.S_OSN is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.1780 end

--003F.00.1790 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_EXP', 'NULL' as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1790'
, 'Не указан эксперт при проведении ЭКМП'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.S_TIP>=30
and not exists (select top 1 1 from tempdb..TEMP_CODE_EXP t2 with(nolock) where t0.IDCASE = t2.IDCASE and t0.s_code = t2.s_code)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
and DATE_Z_2 <= convert(date,'31.05.2021',104)
--003F.00.1790 end

--003F.00.1791 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_EXP', 'NULL' as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.1790'
, 'Не указан эксперт при проведении ЭКМП'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.S_TIP>=70
and not exists (select top 1 1 from tempdb..TEMP_CODE_EXP t2 with(nolock) where t0.IDCASE = t2.IDCASE and t0.s_code = t2.s_code and rtrim(isnull(t2.CODE_EXP,'')) != '')
and DATE_Z_2 >= convert(date,'01.06.2021',104)
--003F.00.1791 end

--003F.00.0090 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPOLIS', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0090'
, 'Поле NPOLIS обязательно если VPOLIS<>3'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('C', 'H', 'T')
and t0.NPOLIS is null and t0.VPOLIS<>3
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0090 end

--003F.00.0100 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ENP', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.0100'
, 'Поле ENP обязательно, если VPOLIS=3'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('A', 'D','R')
and t0.ENP is null and t0.VPOLIS=3
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.0100 end

----003F.00.2170 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPOLIS', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003F.00.2170'
--, 'Поле NPOLIS или ENP должны быть заполнены'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
--where
--@q019type in ('X')
--and t0.NPOLIS is null and t0.enp is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----003F.00.2170 end

--003F.00.2350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KD_Z', convert(varchar(3), t0.KD_Z) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.2350'
, 'Не заполнено количество койко-дней (KD_Z)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('T')
and KD_Z is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.2350 end

--003F.00.2381 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.2381'
, 'RSLT_D обязательно при P_OTK=0'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('X')
and t0.RSLT_D is null -- мб элемент обязателен
and P_OTK=0
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.2381 end

--003F.00.2382 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '003F.00.2382'
, 'При P_OTK=1 RSLT_D должен отсутствовать'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('X')
and t0.RSLT_D is not null -- элемент должен отсуствовать
and P_OTK=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.2382 end

--003F.00.2452 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2452'
, 'DS1 обязателен если P_OTK=0'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and t0.DS1 is null -- мб элемент обязателен
and P_OTK=0
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.2452 end

--003F.00.2571 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PR_D_N', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2571'
, 'PR_D_N обязателен если P_OTK=0 или DS1 присутствует'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and t0.PR_D_N is null -- мб элемент обязателен
and (P_OTK=0 or DS1 is not null)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.2571 end

--003F.00.2600 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_USL', null as ZN_POL ,@NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2600'
, 'Элемент ONK_USL обязательно если тег ONK_SL присутствует'
from tempdb..TEMP_ONK_SL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and not exists (select top 1 1 from tempdb..TEMP_ONK_USL t10 with(nolock) where t10.sl_id = t0.sl_id and t10.idcase = t0.idcase)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.2600 end

--003F.00.2800 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'S_SUM', convert(varchar, cast(t0.S_SUM as money)) as ZN_POL ,@NSCHET, 'SANK', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2800'
, 'Поле S_SUM обязательно'
from tempdb..TEMP_SANK t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.S_SUM is null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--003F.00.2800 end

----003F.00.2961 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'PRVS', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2961'
--, 'Элемент обязателен'
--from tempdb..TEMP_USL t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and t0.PRVS is null -- мб элемент обязателен
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----003F.00.2961 end

----003F.00.2962 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'PRVS', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2962'
--, 'Элемент обязателен, если USL/P_OTK=0'
--from tempdb..TEMP_USL t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join tempdb..TEMP_MR_USL_N mr with(nolock) on mr.IDSERV = t0.IDSERV and t0.IDCASE = mr.IDCASE and t0.SL_ID = mr.SL_ID
--where
--@q019type in ('X')
--and (t0.PRVS is null and mr.PRVS is null) -- мб элемент обязателен
--and t0.P_OTK=0
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----003F.00.2962 end

----003F.00.2963 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'PRVS', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, t0.IDSERV as IDSERV, '003F.00.2963'
--, 'элемент должен отсутствовать, если P_OTK=1'
--from tempdb..TEMP_USL t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join tempdb..TEMP_MR_USL_N mr with(nolock) on mr.IDSERV = t0.IDSERV and t0.IDCASE = mr.IDCASE and t0.SL_ID = mr.SL_ID
--where
--@q019type in ('X')
--and (t0.PRVS is not null or mr.PRVS is not null) -- элемент должен отсуствовать
--and t0.P_OTK=1
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----003F.00.2963 end

----003F.00.2971 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'CODE_MD', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2971'
--, 'Элемент обязателен'
--from tempdb..TEMP_USL t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--where
--@q019type in ('C', 'H', 'T')
--and t0.CODE_MD is null -- мб элемент обязателен
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
--and DATE_Z_2 <= convert(date,'31.12.2021',104)
----003F.00.2971 end

--003F.00.2972 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', null as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2972'
, 'Элемент обязателен, если Тег SL.USL присутствует и ZL_LIST/ZAP/Z_SL/SL/USL/P_OTK=0'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.IDSERV and t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('X')
and t2.CODE_MD is null -- мб элемент обязателен
and t0.P_OTK=0
and DATE_Z_2 >= convert(date,'01.10.2021',104)
and DATE_Z_2 <= convert(date,'31.12.2021',104)
--003F.00.2972 end

--003F.00.2973 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', null as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2973'
, 'элемент должен отсутствовать, если ZL_LIST/ZAP/Z_SL/SL/USL/P_OTK=1'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.IDSERV and t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('X')
and t2.CODE_MD is not null-- элемент должен отсуствовать
and t0.P_OTK=1
and DATE_Z_2 >= convert(date,'01.10.2021',104)
and DATE_Z_2 <= convert(date,'31.12.2021',104)
--003F.00.2973 end

--003F.00.3051 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ_R', null as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.3051'
, 'NAZ_R обязателен при RSLT_D={3,4,5,31,32}'
from tempdb..TEMP_NAZ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('X')
and t0.NAZ_R is null -- мб элемент обязателен
and RSLT_D in (3,4,5,31,32)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--003F.00.3051 end

--003K.51.0020 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FILENAME', 'null' as ZN_POL ,@NSCHET, 'ZGLV', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '003K.51.0020'
, 'Реестр с таким именем уже был загружен в систему. Измените порядковый номер имени файла.'
from tempdb..TEMP_ZGLV t0 with(nolock)
where
@q019type in ('C', 'H', 'T', 'X')
and @type = 554
and exists (
select 1
from ies.t_schet t100 with(nolock) 
where
isnull(t100.FILENAME,'-1') = isnull(t0.FILENAME,'-2')
and t100.IsDelete = 0
and t100.type_ = 554
and isnull(t100.FFOMSCheckStatus,-1) = 8
)
and @dschet >= convert(date,'01.01.2021',104)
--003K.51.0020 end




--004F.00.0230 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPOLIS', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '004F.00.0230'
, 'NPOLIS должно соответствовать маске ХХХХХХХХХХХХХХХХХХХ9 для старых полисов'
from tempdb..TEMP_PACIENT t0 with(nolock)
join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and t0.VPOLIS=1
and [dbo].[RegExMatch](t0.NPOLIS,'^\d{1,20}$') != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.0230 end

--004F.00.0240 begin Отключено по приказу Анны
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPOLIS', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '004F.00.0240'
--, 'Поле NPOLIS должно соответствовать маске 999999999 для временных свидетельств'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
--where
--@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--and t0.VPOLIS=2
--and [dbo].[RegExMatch](t0.NPOLIS,'^\d{9}$') != 1
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.0240 end

----004F.00.0250 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'NPOLIS', NPOLIS as ZN_POL ,@NSCHET, 'PACIENT', t0.N_ZAP, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '004F.00.0250'
--, 'Обязательно если SMO отсуствует'
--from tempdb..TEMP_PACIENT t0 with(nolock)
--join tempdb..TEMP_ZAP t1 with(nolock) on t0.N_ZAP= t1.N_ZAP
--join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.n_zap = t2.n_zap
--where
--@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
--and t0.VPOLIS=3
--and [dbo].[RegExMatch](t0.NPOLIS,'^\d{16}$') != 1
--and t0.NPOLIS is not null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----004F.00.0250 end

--004F.00.0670 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL_ID', SL_ID as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '004F.00.0670'
, 'Допустимы цифры, строчные и прописные буквы латинского алфавита, горизонтальные разделители'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and not (dbo.RegExMatch(t0.SL_ID, '^[0-9A-Za-z\-]{1,36}$') = 1)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.0670 end

--004F.00.0731 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_NUM', t0.TAL_NUM as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '004F.00.0731'
, 'Не соответствует приказу Минздрава России от 30.01.2015 № 29н'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and [dbo].[RegExMatch](t0.TAL_NUM,'^\d{2}.\d{4}.\d{5}.\d{3}$') != 1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--004F.00.0731 end

--004F.00.0750 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NHISTORY', NHISTORY as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '004F.00.0750'
, 'Допустимы цифры, буквы русского и латинского алфавита, пробел, точка, горизонтальные разделители, вертикальные и наклонные разделители'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('A', 'C', 'D', 'H', 'R', 'T', 'X')
and not (dbo.RegExMatch(t0.NHISTORY,'^[0-9А-Яа-яЁёA-Za-z \.\-\|\\\/]{1,50}$') = 1)
--Допустимы цифры, буквы русского и латинского алфавита, пробел, точка, горизонтальные разделители, вертикальные и наклонные разделители
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.0750 end

--004F.00.1340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSERV', IDSERV as ZN_POL ,@NSCHET, 'USL', null as N_ZAP, null as ID_PAC, t1.IDCASE, t0.SL_ID, t0.IDSERV as IDSERV, '004F.00.1340'
, 'Допустимы цифры, строчные и прописные буквы латинского алфавита, горизонтальные разделители'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and dbo.regexmatch(t0.idserv,'^[0-9A-Za-z\-]{1,36}$')!=1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1340 end

--004F.00.1370 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', t0.VID_VME as ZN_POL ,@NSCHET, 'PACIENT', t2.N_ZAP, t2.ID_PAC, t2.IDCASE as IDCASE, t0.sl_id as SL_ID, t0.IDSERV as IDSERV, '004F.00.1370'
, 'Не соответствует шаблону A99.99X.999.XXX'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.sl_id = t1.sl_id and t0.idcase = t1.idcase
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t2.idcase = t1.idcase
where
@q019type in ('C','H')
and t0.VID_VME is not null
and [dbo].[RegExMatch](t0.VID_VME,'^([AB]){1}\d{2}(\.\d{2,3}){2,}$')<> 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1370 end

--004F.00.1580 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM', FAM as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1580'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.FAM, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.FAM is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1580 end

--004F.00.1590 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM', IM as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1590'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.IM, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.IM is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1590 end

--004F.00.1600 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OT', OT as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1600'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.OT, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.OT is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1600 end

--004F.00.1640 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM_P', FAM_P as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1640'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.FAM_P, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.FAM_P is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1640 end

--004F.00.1650 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IM_P', IM_P as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1650'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.IM_P, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.IM_P is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1650 end

--004F.00.1660 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'OT_P', OT_P as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1660'
, 'Допустимы цифры, буквы русского алфавита, пробел, точка, горизонтальные разделители'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar,t0.ID_PAC) = t1.ID_PAC
where
not (dbo.RegExMatch(t0.OT_P, '^[0-9А-Яа-яЁё \.\-]{1,40}$') = 1)
and t0.OT_P is not null
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1660 end

----004F.00.1700 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'DOCSER', t0.DOCSER as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t3.IDCASE,null as SL_ID, null as IDSERV, '004F.00.1700'
--, 'Значение не соответствует маске в справочнике F011'
--from tempdb..TEMP_PERS t0 with(nolock)
--join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),t1.ID_PAC)
--join tempdb..TEMP_ZAP t2 with(nolock) on t1.N_ZAP= t2.N_ZAP 
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
--left join ies.T_F011_DOCUMENT_TYPE t4 with(nolock) on t4.IDDoc = t0.DOCTYPE
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--t0.DOCTYPE is not null
--and t4.IDDoc is not null
-- and t0.docser is not null
--and isnull(t4.docserregex,'') != ''
--and not (dbo.RegExMatch(t0.docser,t4.docserregex) = 1)
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----004F.00.1700 end

----004F.00.1710 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'DOCNUM', t0.DOCNUM as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t3.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1710', 'Не соответсвтует маске'
--from tempdb..TEMP_PERS t0 with(nolock)
--join tempdb..TEMP_PACIENT t1 with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),t1.ID_PAC)
--join tempdb..TEMP_ZAP t2 with(nolock) on t1.N_ZAP= t2.N_ZAP 
--join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t3.n_zap = t1.n_zap
--left join ies.T_F011_DOCUMENT_TYPE t4 with(nolock) on t4.IDDoc = t0.DOCTYPE
--and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--t0.DOCTYPE is not null
--and t4.IDDoc is not null
--and isnull(t4.docnumregex,'') != ''
--and not (dbo.RegExMatch(t0.docnum,t4.docnumregex) = 1)
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----004F.00.1710 end

--004F.00.1720 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SNILS', SNILS as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, null as SL_ID, null as IDSERV, '004F.00.1720'
, 'СНИЛС не соответствует шаблону 999-999-999 99'
from tempdb..TEMP_PERS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on convert(varchar(36),t0.ID_PAC) =  convert(varchar(36),t1.ID_PAC)
where
t0.SNILS is not null
and dbo.RegExMatch(t0.SNILS,'^\d{3}-\d{3}-\d{3} \d{2}$') != 1
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--004F.00.1720 end


--005F.00.0030 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS0', DS0 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.00.0030', 'Значение не найдено в справочнике M001'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
left join ies.T_MKB t2 with(nolock) on t2.MCBCode = t0.DS0
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.DS0 is not null and
(
 t2.MCBCode is null
and isnull(t2.Actual,0) = 0
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--005F.00.0030 end

--005F.00.0040 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', DS1 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.00.0040', 'Значение не найдено в справочнике M001'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t1.n_zap = t0.n_zap
left join ies.T_MKB t2 with(nolock) on t2.MCBCode = t0.DS1
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and 
t0.DS1 is not null and
(
t2.MCBCode is null
and isnull(t2.Actual,0) = 0
)
and DATE_Z_2 >= convert(date,'01.11.2019',104) 

--005F.00.0040 end

--005F.00.0050 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', DS2 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.00.0050', 'Значение не найдено в справочнике M001'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_DS2 t1 with(nolock) on t0.IDCASE = t1.IDCASE and t0.SL_ID = t1.SL_ID
left join ies.T_MKB t2 with(nolock) on t2.MCBCode = t1.DS2
join tempdb..TEMP_Z_SLUCH t3 with(nolock) on t0.IDCASE = t3.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t1.DS2 is not null and
(
 t2.MCBCode is null
and isnull(t2.Actual,0) = 0
)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--005F.00.0050 end


----005F.00.0210 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'VID_VME', VID_VME as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.00.0210', 'Значение не найдено в справочнике V001'
--from tempdb..TEMP_USL t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
--left join ies.T_V001_NOMENCLATURE t2 with(nolock) on t2.Code = t0.VID_VME
--and isnull(DATEBEG,convert(date,'01.01.2010',104))<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
--where
--@q019type in ('C', 'H')
--and t0.VID_VME is not null
--and t2.Code is null
--and DATE_Z_2 >= convert(date,'01.11.2019',104)
----005F.00.0210 end

--005F.00.0230 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS', DS as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.00.0230'
, 'Значение не найдено в справочнике M001'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_MKB t2 with(nolock) on t2.MCBCode = t0.DS
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.DS is not null
and t2.MCBCode is null
and not exists (select 1 from ies.T_MKB t100 where t100.ParentID = t2.MkbID)
and DATE_Z_2 >= convert(date,'01.11.2019',104)
--005F.00.0230 end

--005F.51.0004 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '005F.51.0004', 'SL.PRVS не соответствует VIDPOM'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
left join ies.T_V021_MED_SPEC t2 with(nolock) on t2.CODE_SPEC = t0.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'H', 'T')
and @type = 554
and isnull(t1.USL_OK,0) = 3
and (
	(
		(t2.CODE_SPEC in (39, 49, 76, 95) and t1.vidpom != 12)
		or (t2.CODE_SPEC >= 200 and t2.CODE_SPEC <= 299 and t1.vidpom != 11)
		or ( not (t2.CODE_SPEC in (39, 49, 76, 95) or (t2.CODE_SPEC >= 200 and t2.CODE_SPEC <= 299)) and vidpom != 13 )
	)
)
and DATE_Z_2 >= convert(date,'18.03.2021',104)
--005F.51.0004 end



--006F.00.0010 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATA', null as ZN_POL ,@NSCHET, 'ZGLV', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0010'
, 'Значение поля не должно быть больше текущей даты обработки файла.'
from tempdb..TEMP_ZGLV t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and DATA > getdate()
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.0010 end

--006F.00.0040 begin
--повторяет 006F.00.0030
--006F.00.0040 end

--006F.00.0110 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SD_Z', null as ZN_POL ,@NSCHET, 'ZGLV', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0110'
, 'Значение поля должно быть равно количеству законченных случаев (Z_SL), включенных в реестр.'
from tempdb..TEMP_ZGLV t0 with(nolock)
where
@q019type in ('C', 'H', 'T', 'X')
and SD_Z != (select count(*) from tempdb..TEMP_Z_SLUCH)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.0110 end

--006F.00.0160 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'YEAR', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0160'
, 'Год даты окончания зак. случая больше года реестра счетов'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and exists (select top 1 1 from tempdb..TEMP_Z_SLUCH t1 where year(DATE_Z_2) > t0.YEAR)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.0160 end

--006F.00.0180 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MONTH', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0180'
, 'Месяц случая больше месяца реестра счетов'
from tempdb..TEMP_SCHET t0 with(nolock)
join tempdb..TEMP_Z_SLUCH with(nolock) on 1 = 1
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and year(date_z_2) = t0.year
and t0.month < month(DATE_Z_2)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.0180 end

--006F.00.0200 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DSCHET', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.0200'
, 'Значение поля не должно быть больше DATA'
from tempdb..TEMP_SCHET t0 with(nolock)
join tempdb..TEMP_ZGLV t1 with(nolock) on 1 = 1
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.DSCHET > t1.DATA
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.0200 end

--006F.00.0310 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0310'
, 'Дата направления не может быть больше даты начала случая'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and NPR_DATE > DATE_Z_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0310 end

--006F.00.0311 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', null as ZN_POL ,@NSCHET, 'Z_SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, t3.sl_id as SL_ID, null as IDSERV,  '006F.00.0311'
, 'Дата направления должна быть больше или равна дате рождения'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and NPR_DATE < DR
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0311 end

--006F.00.0312 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_D', null as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t2.ID_PAC, t1.IDCASE, t3.SL_ID, null as IDSERV, '006F.00.0312'
, 'Значение поля не должно быть меньше DR'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
where
@q019type in ('D', 'R', 'T')
and TAL_D < t2.DR
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0312 end

--006F.00.0313 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_D', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0313'
, 'Значение поля не должно быть больше DATE_2'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('D', 'R', 'T')
and TAL_D > DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0313 end

--006F.00.0314 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_P', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0314'
, 'Значение поля не должно быть меньше TAL_D'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('T')
and TAL_P<TAL_D
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0314 end

--006F.00.0315 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_P', null as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t2.ID_PAC, t1.IDCASE, t3.SL_ID, null as IDSERV, '006F.00.0315'
, 'Значение поля не должно быть меньше DR'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
where
@q019type in ('T')
and TAL_P < t2.DR
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0315 end

--006F.00.0330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_Z_1', CONVERT(VARCHAR(10), t0.DATE_Z_1, 126) as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0330'
, 'Значение поля не должно быть больше DATE_Z_2'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and DATE_Z_1 > DATE_Z_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0330 end

----006F.00.0340 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'DATE_Z_2', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0340'
--, 'Значение поля не должно быть больше DSCHET'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SCHET t1 with(nolock) on 1 = 1
--where
--@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
--and t1.dschet < date_z_2
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----006F.00.0340 end

--006F.00.0350 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', RSLT_D as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0350'
, 'При DS_ONK=1 должно выполняться RSLT_D <>1'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('X')
and t0.RSLT_D = 1
and DS_ONK=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0350 end

--006F.00.0370 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMV', SUMV as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.0370'
, 'Значение поля должно быть равно сумме значений SUM_M вложенных элементов SL'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and sumv != (select sum(sum_m) from tempdb..TEMP_SLUCH t1 where t1.IDCASE = t0.IDCASE)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0370 end

--006F.00.0390 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REAB', REAB as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0390'
, 'Указывается при NOVOR=0'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PACIENT t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
where
@q019type in ('C', 'D', 'H', 'R')
and NOVOR!=0
and REAB is not null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0390 end

--006F.00.0395 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', DET as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE, t3.SL_ID, null as IDSERV, '006F.00.0395'
, 'Возраст должен быть меньше 18 лет на дату начала случая'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and dbo.DateTimeDiff('year',dr,date_1) >= 18
and DET = 1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0395 end

--006F.00.0400 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_1', DATE_1 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0400'
, 'Значение поля не должно быть меньше DATE_Z_1'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and DATE_1 < DATE_Z_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0400 end

--006F.00.0410 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_1', CONVERT(VARCHAR(10), t0.date_1, 126) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0410'
, 'Значение поля не должно больше DATE_2'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and not date_1 <= date_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0410 end

--006F.00.0420 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_2', DATE_2 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0420'
, 'Значение поля не должно быть больше DATE_Z_2'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and date_2 > date_z_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0420 end

--006F.00.0430 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', DS1 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0430'
, 'Значение поля не должно быть равным значению поля DS2 или DS3'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and (
exists (select top 1 1 from tempdb..TEMP_DS2 t01 with(nolock) where DS2 = DS1 and t0.sl_id = t01.sl_id and t0.idcase = t01.idcase)
or 
exists (select top 1 1 from tempdb..TEMP_DS3 t02 with(nolock) where DS3 = DS1 and t0.sl_id = t02.sl_id and t0.idcase = t02.idcase)
)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0430 end

--006F.00.0440 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0440'
, 'Значение поля не должно быть равным значению поля DS3'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_DS2 t1 with(nolock) on t0.SL_ID = t1.SL_ID and t0.IDCASE = t1.IDCASE
join tempdb..TEMP_Z_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and exists (select top 1 1 from tempdb..TEMP_DS3 t01 with(nolock)
where t0.SL_ID = t01.SL_ID and t0.IDCASE = t01.IDCASE and t1.DS2 = t01.DS3)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0440 end

--006F.00.0450 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MES1', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0450'
, 'Значение поля не должно быть равным значению поля CODE_MES2'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and CODE_MES1 = CODE_MES2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0450 end

--006F.00.0460 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0460'
, 'Если USL_0K<>{1,2}, то значение поля должно быть не больше SUM_M'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(USL_OK,0) not in (1,2)
and TARIF > SUM_M
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0460 end

--006F.00.0470 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_CODE', null as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0470'
, 'Данный тип исследования запрещён для данного диагноза'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
left join ies.T_N007_MRF n007 with(nolock) on n007.ID_Mrf = t0.DIAG_CODE
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
left join ies.T_N009_MRT_DS n009 with(nolock) on n009.N007Mrf = n007.N007MrfID and DS1 like DS_Mrf+'%'
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=1
and N009MrtDsID is null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0470 end

--006F.00.0480 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_RSLT', null as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0480'
, 'Неверный результат для данного исследования'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
join ies.T_N007_MRF n007 with(nolock) on ID_Mrf = t0.DIAG_CODE
left join ies.T_N008_MRF_RT n008 with(nolock) on N007MrfId = N007Mrf and ID_R_M = DIAG_RSLT
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=1
and N008MrfRtID is null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0480 end

--006F.00.0490 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_CODE', null as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0490'
, 'Данный тип исследования запрещён для данного диагноза'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
left join ies.T_N010_MARK n010 with(nolock) on n010.ID_Igh = t0.DIAG_CODE
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
left join ies.T_N012_MARK_DIAG n012 with(nolock) on n012.N010Mark = n010.N010MarkID and DS1 like DS_Igh+'%'
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=2
and N012MarkDiagID is null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0490 end

--006F.00.0500 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_RSLT', null as ZN_POL ,@NSCHET, 'B_DIAG', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0500'
, 'Неверный результат для данного исследования'
from tempdb..TEMP_B_DIAG t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
join ies.T_N010_MARK n010 with(nolock) on n010.ID_Igh = t0.DIAG_CODE
left join ies.T_N011_MARK_VALUE n011 with(nolock) on N010MarkID = N010Mark and ID_R_I = DIAG_RSLT
where
@q019type in ('C', 'D', 'R', 'T')
and DIAG_TIP=2
and N011MarkValueID is null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0500 end

--006F.00.0510 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'D_PROT', null as ZN_POL ,@NSCHET, 'B_PROT', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0510'
, 'Значение поля должно быть не больше DATE_2'
from tempdb..TEMP_B_PROT t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and D_PROT>DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0510 end

--006F.00.0520 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t3.IDCASE, t3.SL_ID, t4.idserv as IDSERV, '006F.00.0520'
, 'При DET=1 Возраст должен быть меньше 18 лет на дату начала случая'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.idcase = t1.idcase
join tempdb..TEMP_USL t4 with(nolock) on t4.sl_id = t3.sl_id and t4.idcase = t3.idcase
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and dbo.DateTimeDiff('year',t2.dr,date_1) >= 18
and t4.det = 1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0520 end

--006F.00.0530 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_IN', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0530'
, 'Значение поля не должно быть меньше DATE_1'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'H', 'T')
and DATE_IN < DATE_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0530 end

--006F.00.0550 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_IN', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0550'
, 'Значение поля не должно быть больше DATE_OUT'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and DATE_IN > DATE_OUT
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0550 end

--006F.00.0560 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_OUT', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0560'
, 'Значение поля не должно быть больше DATE_2'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and DATE_OUT > DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0560 end

--006F.00.0570 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0570'
, 'Значение поля должно быть не более SUMV_USL'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.TARIF>SUMV_USL
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0570 end

--006F.00.0580 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_DATE', null as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0580'
, 'Значение поля не должно быть больше DATE_2'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t2.IDCASE = t0.IDCASE and t2.SL_ID = t0.SL_ID
where
@q019type in ('C', 'D', 'R', 'T')
and NAPR_DATE>DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0580 end

--006F.00.0590 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_DATE', null as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0590'
, 'Значение поля не должно быть меньше DATE_1'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'R', 'T')
and NAPR_DATE<DATE_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0590 end

--006F.00.0600 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DT_CONS', null as ZN_POL ,@NSCHET, 'CONS', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0600'
, 'Значение поля должно быть не больше DATE_2'
from tempdb..TEMP_CONS t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.IDCASE = t2.IDCASE and t0.SL_ID = t2.SL_ID
where
@q019type in ('C', 'D', 'R', 'T')
and DT_CONS > DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0600 end

--006F.00.0610 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_INJ', convert(varchar(10),DATE_INJ,104) as ZN_POL,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0610'
, 'Значение поля должно быть не меньше DATE_1'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and DATE_INJ<DATE_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0610 end

--006F.00.0620 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_INJ', null as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0620'
, 'Значение поля должно быть не больше DATE_2'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 with(nolock) on t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and DATE_INJ>DATE_2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0620 end

--006F.00.0630 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0630'
, 'CODE_SH должно содержать значение НЕТ'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH != 'нет'
and DS1 >= 'C81' and DS1 < 'C97'
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--006F.00.0630 end

--006F.00.0631 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0631'
, 'Пул значений: нет'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH != 'нет'
and (
	(DS1 >= 'C81' and DS1 < 'C97')
	or (DS1 >= 'D45' and DS1 < 'D48')
)
and t0.USL_TIP > 4
and DATE_Z_2 >= convert(date,'01.01.2021',104)
--006F.00.0631 end

--006F.00.0640 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0640'
, 'CODE_SH должно содержать значение НЕТ'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2) 
where
@q019type in ('C', 'D', 'R', 'T')
and t0.CODE_SH != 'нет'
and dbo.DateTimeDiff('year',dr,date_z_1) < 18
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0640 end

--006F.00.0650 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0650'
, 'Значение не входит в пул sh001<=CODE_SH<=sh904'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and dbo.DateTimeDiff('year',dr,date_z_1) >= 18
and ((t3.ds1 >= 'C00' and t3.ds1 < 'C81') or (t3.ds1 like 'C97%') or t3.ds1 like 'D0%')
and t0.USL_TIP = 2
and isnull(IDDKK,'') not like 'sh%'
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--006F.00.0650 end

--006F.00.0655 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0655'
, 'Пул значений: sh0001<=CODE_SH<=sh9003'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and (
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 21 
			and ((t3.ds1 >= 'C00' and t3.ds1 < 'C81') or t3.ds1 like 'C97%' or (t3.ds1 >= 'D00' and t3.ds1 < 'D10'))
		)
		or
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 18 
			and ((t3.ds1 >= 'C00' and t3.ds1 < 'C81') or t3.ds1 like 'C97%' or (t3.ds1 >= 'D00' and t3.ds1 < 'D10'))
			and t3.DS1 not in ('C22.2' ,'C38.1' ,'C47.3' ,'C47.4' ,'C47.5' ,'C47.6' ,'C47.8' ,'C47.9' ,'C48.0' ,'C74.1' ,'C74.9' ,'C76.0' ,'C76.1' ,'C76.2' ,'C76.3' ,'C76.7' ,'C76.8' ,'C83.3' ,'C83.5' ,'C83.7' ,'C84.6' ,'C84.7' ,'C85.2' ,'C91.0' ,'C91.8' ,'C92.0' ,'C92.3' ,'C92.4' ,'C92.5' ,'C92.6' ,'C92.7' ,'C92.8' ,'C92.9' ,'C93.0' ,'C94.0' ,'C94.2')
			and left(t3.ds1,3) not in ('C40' ,'C49' ,'C62' ,'C64' ,'C70' ,'C71' ,'C72' ,'C81' ,'C95')
		)
)

and t0.USL_TIP = 2
and not (
		left(isnull(CODE_SH,''),6) >= 'sh0001' and left(isnull(CODE_SH,''),6) <= 'sh9003'
	)
--and DATE_Z_2 >= convert(date,'01.01.2021',104)
--006F.00.0655 end

--006F.00.0656 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0656'
, 'Пул значений: gemop1<=CODE_SH<=gemop26 или CODE_SH=gem'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and (
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 21 
			and ((t3.ds1 >= 'C81' and t3.ds1 < 'C97') or (t3.ds1 >= 'D45' and t3.ds1 < 'D48'))
		)
		or
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 18 
			and ((t3.ds1 >= 'C81' and t3.ds1 < 'C97') or (t3.ds1 >= 'D45' and t3.ds1 < 'D48'))
			and t3.DS1 not in ('C22.2' ,'C38.1' ,'C47.3' ,'C47.4' ,'C47.5' ,'C47.6' ,'C47.8' ,'C47.9' ,'C48.0' ,'C74.1' ,'C74.9' ,'C76.0' ,'C76.1' ,'C76.2' ,'C76.3' ,'C76.7' ,'C76.8' ,'C83.3' ,'C83.5' ,'C83.7' ,'C84.6' ,'C84.7' ,'C85.2' ,'C91.0' ,'C91.8' ,'C92.0' ,'C92.3' ,'C92.4' ,'C92.5' ,'C92.6' ,'C92.7' ,'C92.8' ,'C92.9' ,'C93.0' ,'C94.0' ,'C94.2')
			and left(t3.ds1,3) not in ('C40' ,'C49' ,'C62' ,'C64' ,'C70' ,'C71' ,'C72' ,'C81' ,'C95')
		)
)

and t0.USL_TIP = 2
and not (
		isnull(CODE_SH,'') like 'gemop%'
		or
		isnull(CODE_SH,'') = 'gem'
	)
--and DATE_Z_2 >= convert(date,'01.01.2021',104)
--006F.00.0656 end

--006F.00.0660 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0660', 'Значение не найдено в справочнике V024'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('C', 'D', 'R', 'T')
and dbo.DateTimeDiff('year',dr,date_z_1) >= 18
and (
	(DS1 >= 'C00' and DS1 < 'C81')
	or
	DS1 like 'D%'
)
and t0.USL_TIP = 4
and isnull(IDDKK,'') not like 'sh%'
and isnull(IDDKK,'') not like 'mt%'
and DATE_Z_2 >= convert(date,'01.04.2020',104)
and DATE_Z_2 <= convert(date,'31.12.2020',104)
--006F.00.0660 end

--006F.00.0663 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '006F.00.0663'
, 'Пул значений sh0001<=CODE_SH<=sh9003 или mt001<=CODE_SH<=mt024'
from tempdb..TEMP_LEK_PR_DATE_INJ t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_PERS t2 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t2.ID_PAC)
join tempdb..TEMP_SLUCH t3 with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where
@q019type in ('C', 'D', 'R', 'T')
and (
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 21 
			and ((t3.ds1 >= 'C81' and t3.ds1 < 'C97') or (t3.ds1 >= 'D45' and t3.ds1 < 'D48'))
		)
		or
		(
			dbo.DateTimeDiff('year',dr,date_z_1) >= 18 
			and ((t3.ds1 >= 'C81' and t3.ds1 < 'C97') or (t3.ds1 >= 'D45' and t3.ds1 < 'D48'))
			and t3.DS1 not in ('С22.2' ,'С38.1' ,'С47.3' ,'С47.4' ,'С47.5' ,'С47.6' ,'С47.8' ,'С47.9' ,'С48.0' ,'С74.1' ,'С74.9' ,'С76.0' ,'С76.1' ,'С76.2' ,'С76.3' ,'С76.7' ,'С76.8' ,'С83.3' ,'С83.5' ,'С83.7' ,'С84.6' ,'С84.7' ,'С85.2' ,'С91.0' ,'С91.8' ,'С92.0' ,'С92.3' ,'С92.4' ,'С92.5' ,'С92.6' ,'С92.7' ,'С92.8' ,'С92.9' ,'С93.0' ,'С94.0' ,'С94.2')
			and left(t3.ds1,3) not in ('С40' ,'С49' ,'С62' ,'С64' ,'С70' ,'С71' ,'С72' ,'С81' ,'С95')
		)
)
and t0.USL_TIP = 4
and not (
		(left(isnull(CODE_SH,''),6) >= 'sh0001' and left(isnull(CODE_SH,''),6) <= 'sh9003')
		or
		(left(isnull(CODE_SH,''),5) >= 'mt001' and left(isnull(CODE_SH,''),5) <= 'mt024')
	)
--and DATE_Z_2 >= convert(date,'01.01.2021',104)
--006F.00.0663 end

--006F.00.0670 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR', null as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t1.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '006F.00.0670'
, 'DR должно быть <= ZGLV.DATA'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
join tempdb..TEMP_ZGLV t3 with(nolock) on 1 = 1
where
t1.DR > t3.DATA
and t1.DR is not null
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0670 end

--006F.00.0680 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR', null as ZN_POL ,@NSCHET, 'PERS', t2.N_ZAP, t1.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '006F.00.0680'
, 'Дата рождения должна быть меньше или равна DATA_Z_1'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
t1.DR > DATE_Z_1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0680 end

--006F.00.0690 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR_P', null as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '006F.00.0690'
, 'Значение поля должно быть меньше DR не менее, чем на 14 лет'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
where
year(t1.DR)-year(t1.DR_P)<=14
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.0690 end

--006F.00.1010 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR', null as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t1.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '006F.00.1010'
, 'Разность между годом рождения пациента и годом прохождения диспансеризации должна быть 18 лет и более'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
join tempdb..TEMP_SCHET t3 with(nolock) on 1 = 1
where
@q019type in ('X')
and year(DATE_Z_2)-year(dr)<18
and disp in ('ДВ4', 'ДВ2', 'ОПВ')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1010 end

--006F.00.1020 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR', null as ZN_POL ,@NSCHET, 'PERS', t0.N_ZAP, t1.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV, '006F.00.1020'
, 'Разность между годом рождения пациента и годом прохождения диспансеризации должна быть менее 18 лет'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_PERS t1 with(nolock) on convert(varchar(36),t1.ID_PAC) = convert(varchar(36),t0.ID_PAC)
join tempdb..TEMP_PACIENT t2 with(nolock) on t2.n_zap = t0.n_zap
join tempdb..TEMP_SCHET t3 on 1 = 1
where
@q019type in ('X')
and year(DATE_Z_2)-year(dr)>=18
and disp in ('ДС1', 'ДС2', 'ДС3', 'ДС4', 'ПН1', 'ПН2')
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1020 end

--006F.00.1040 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMMAV', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1040'
, 'SUMMAV должна быть равна сумме значений SUMV всех законченных случаев, включенных в счет'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and SUMMAV != (select sum(isnull(sumv,0)) from tempdb..TEMP_Z_SLUCH)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.1040 end

--006F.00.1050 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMMAP', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1050'
, 'SUMMAP должна быть равна сумме значений SUMP всех законченных случаев, включенных в счет'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and SUMMAP != (select sum(isnull(sump,0)) from tempdb..TEMP_Z_SLUCH)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.1050 end

--006F.00.1060 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEK', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1060'
, 'SANK_MEK не равна сумме санкций МЭК по законченным случаям'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEK,0) != isnull((select sum(isnull(s_sum,0)) from tempdb..TEMP_SANK where S_TIP in (1, 10, 11, 12) ),0)
and @dschet >= convert(date,'01.04.2020',104)
--006F.00.1060 end

--006F.00.1070 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEE', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1070'
, 'SANK_MEE не равна сумме санкций МЭЭ по законченным случаям'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEE,0) != isnull((select sum(isnull(s_sum,0)) from tempdb..TEMP_SANK where S_TIP>=20 and S_TIP<30),0)
and @dschet >= convert(date,'01.04.2020',104)
and @dschet <= convert(date,'31.05.2021',104)
--006F.00.1070 end

--006F.00.1071 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_MEE', null as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1071'
, ''
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEE,0) != isnull((select sum(isnull(s_sum,0)) from tempdb..TEMP_SANK where S_TIP>=50 and S_TIP<70),0)
and @dschet >= convert(date,'01.06.2021',104)
--006F.00.1071 end

--006F.00.1080 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_EKMP', convert(varchar, cast(t0.SANK_EKMP as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1080'
, 'SANK_EKMP не равна сумме санкций ЭКМП на законченных случаях'
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_EKMP,0) != isnull((select sum(isnull(s_sum,0)) from tempdb..TEMP_SANK where S_TIP>=30 and S_TIP<50),0)
and @dschet >= convert(date,'01.04.2020',104)
and @dschet <= convert(date,'31.05.2021',104)
--006F.00.1080 end

--006F.00.1081 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_EKMP', convert(varchar(36), cast(t0.SANK_EKMP as money)) as ZN_POL ,@NSCHET, 'SCHET', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1081'
, ''
from tempdb..TEMP_SCHET t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_EKMP,0) != isnull((select sum(isnull(s_sum,0)) from tempdb..TEMP_SANK where S_TIP>=70 and S_TIP<90),0)
and @dschet >= convert(date,'01.06.2021',104)
--006F.00.1081 end

--006F.00.1090 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select top 1 'SANK_IT', null as ZN_POL ,@NSCHET, 'Z_SL', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '006F.00.1090'
, 'Сумма SANK_IT <> SANK_MEK + SANK_MEE + SANK_EKMP'
from tempdb..TEMP_SCHET t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on 1 = 1
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and isnull(t0.SANK_MEK,0)+isnull(t0.SANK_MEE,0)+isnull(t0.SANK_EKMP,0) != isnull((select sum(isnull(SANK_IT,0)) from tempdb..TEMP_Z_SLUCH),0)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1090 end

--006F.00.1110 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1110'
, 'При ISHOD=306, разрешены RSLT = {305, 308, 314, 315, 301}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'H')
and t0.RSLT not in (305, 308, 314, 315, 301)  -- мб элемент обязателен
and ISHOD=306
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1110 end

----006F.00.1120 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1120'
--, 'Для данного результата обращения ISHOD = 402'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--where
--@q019type in ('C', 'D', 'H', 'R', 'T')
--and t0.ISHOD != 402
--and RSLT in (407, 408, 409, 410, 411, 412, 413, 414)
--and DATE_Z_2 >= convert(date,'01.04.2020',104)
----006F.00.1120 end

--006F.00.1130 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1130'
, 'ISHOD = 101 запрещён для данных результатов обращений'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.ISHOD = 101 
and RSLT in (102, 103, 104, 105, 106, 109, 107, 108, 110)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1130 end

--006F.00.1140 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1140'
, 'Исход 201 запрещён для данного результата обращения'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.ISHOD = 201
and RSLT in (202, 203, 204, 205, 206, 207, 208)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1140 end

--006F.00.1150 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSP', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1150'
, 'Разрешены только IDSP = {25, 28, 29, 30, 31, 44}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('X')
and t0.IDSP not in (25, 28, 29, 30, 31, 44)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1150 end

--006F.00.1160 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMP', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1160'
, 'SUMP != SUMV-SANK_IT'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and SUMP!=SUMV-SANK_IT
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1160 end

--006F.00.1170 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMP', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1170'
, 'Если OPLATA=3, то SUMP=SUMV-SANK_IT'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and OPLATA=3
and SUMP!=SUMV-SANK_IT
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1170 end

--006F.00.1171 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_IT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1171'
, 'Если OPLATA=3, то 0<SANK_IT<SUMV'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and not (0<SANK_IT and SANK_IT<SUMV)
and OPLATA=3
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1171 end

--006F.00.1180 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMP', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1180'
, 'Если OPLATA=2, то SUMP=0'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and SUMP!=0
and OPLATA=2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1180 end

--006F.00.1181 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_IT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1181'
, 'SANK_IT=SUMV если OPLATA=2'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.SANK_IT != sumv -- мб элемент обязателен
and OPLATA=2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1181 end

--006F.00.1190 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMP', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1190'
, 'Если OPLATA=1, то SUMP=SUMV'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and SUMP != SUMV
and OPLATA=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1190 end

--006F.00.1191 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SANK_IT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1191'
, 'SANK_IT=0 если OPLATA=1'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T', 'X')
and t0.SANK_IT != 0 -- мб элемент обязателен
and OPLATA=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1191 end

--006F.00.1200 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VIDPOM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1200'
, 'Если USL_OK=4, то VIDPOM = {2, 21, 22, 23}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.VIDPOM not in (2, 21, 22, 23)
and USL_OK=4
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1200 end

--006F.00.1210 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VIDPOM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1210'
, 'Если USL_OK=3, то VIDPOM = {1, 2, 11, 12, 13, 4, 14}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.VIDPOM not in (1, 2, 11, 12, 13, 4, 14)
and USL_OK=3
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1210 end

--006F.00.1220 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VIDPOM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1220'
, 'Если USL_OK=2, то VIDPOM = {12, 13, 14, 31, 32, 33}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.VIDPOM not in (12, 13, 14, 31, 32, 33)
and USL_OK=2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1220 end

--006F.00.1230 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VIDPOM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1230'
, 'Если USL_OK=1, то VIDPOM = {3, 21, 31, 32, 33, 4}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.VIDPOM not in (3, 21, 31, 32, 33, 4)
and USL_OK=1
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1230 end

--006F.00.1240 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1240'
, 'При RSLT={105, 106} допустим только ISHOD=104'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.ISHOD != 104
and RSLT in (105, 106)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1240 end

--006F.00.1250 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1250'
, 'При RSLT={205, 206} допустимо только ISHOD=204'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and ISHOD!=204
and RSLT in (205, 206)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1250 end

--006F.00.1260 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1260'
, 'При RSLT={313} допустимо только ISHOD=305'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.ISHOD != 305
and RSLT=313
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1260 end

--006F.00.1270 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ISHOD', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1270'
, 'При RSLT={405, 406} допустимо только ISHOD=403'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and ISHOD != 403
and RSLT in (405, 406)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1270 end

--006F.00.1280 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FOR_POM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1280'
, 'При USL_OK=2 FOR_POM={2, 3}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.FOR_POM not in (2,3) -- мб элемент обязателен
and isnull(USL_OK,0)=2
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1280 end

--006F.00.1290 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FOR_POM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1290'
, 'При USL_OK=3 FOR_POM={2, 3}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.FOR_POM not in (2,3) -- мб элемент обязателен
and isnull(USL_OK,0)=3
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1290 end

--006F.00.1300 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FOR_POM', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1300'
, ''
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and t0.FOR_POM not in (1,2) -- мб элемент обязателен
and isnull(USL_OK,0)=4
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1300 end

--006F.00.1310 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1310'
, 'Неверный результат обращения для данного условия оказания МП'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and isnull(USL_OK,0)=1
and not (RSLT>100 and RSLT<200)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1310 end

--006F.00.1320 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1320'
, 'Неверный результат обращения для данного условия оказания МП'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and isnull(USL_OK,0)=2
and not (RSLT>200 and RSLT<300)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1320 end

--006F.00.1330 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1330'
, 'Неверный результат обращения для данного условия оказания МП'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and isnull(USL_OK,0)=3
and not (RSLT>300 and RSLT<400)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1330 end

--006F.00.1340 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT', null as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, null as SL_ID, null as IDSERV,  '006F.00.1340'
, 'Неверный результат обращения для данного условия оказания МП'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R', 'T')
and isnull(USL_OK,0)=4
and not (RSLT>400 and RSLT<500)
and DATE_Z_2 >= convert(date,'01.04.2020',104)
--006F.00.1340 end


--q015 rules end
--q015 end

----FLK 2022 begin

--001F.00.0663 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '001F.00.0663'
, 'Специальность врача не найдена в справочнике v021'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.idserv and t2.sl_id = t0.sl_id and t2.idcase = t0.idcase
left join ies.T_V021_MED_SPEC t3 with(nolock) on t3.CODE_SPEC = t2.PRVS
and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('H')
and t3.CODE_SPEC is null
and t2.PRVS is not null
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--001F.00.0663 end

----002F.00.0011 begin

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VERSION', VERSION as ZN_POL ,@NSCHET, 'ZGLV', null, null, null, null, null, '002F.00.0011','Версия счёта отлична от версии 3.2 при YEAR>=2022'
from tempdb..TEMP_ZGLV 
where 
(@filename not like 'D[AB]%')
 and( @type =693  or @type =554) 
 and VERSION <>'3.2' 
 and @q019type = 'H' 
 and @year >= 2022 
 and @year<2024

----002F.00.0011 end

--003F.00.2964 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2964'
, 'Поле обязательно'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.idserv and t2.sl_id = t0.sl_id and t2.idcase = t0.idcase
where
@q019type in ('H')
and t2.PRVS is null
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--003F.00.2964 end

--003F.00.2974 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', convert(varchar(4), t0.PRVS) as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.2974'
, 'Поле обязательно'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join tempdb..TEMP_MR_USL_N t2 with(nolock) on t2.IDSERV = t0.idserv and t2.sl_id = t0.sl_id and t2.idcase = t0.idcase
where
@q019type in ('H')
and t2.CODE_MD is null
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--003F.00.2974 end

----003F.00.3130 begin
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'WEI', convert(varchar, cast(t0.WEI as money)) as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.3130'
--, 'Не указан вес пациента'
--from tempdb..TEMP_SLUCH t0 with(nolock)
--join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
--join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
--where
--@q019type in ('H')
--and isnull(t0.WEI,0) <= 0
--and t0.DS1 in ('U07.1','U07.2')
--and t0.REAB<>1
--and not exists (select 1 from tempdb..TEMP_CRIT t100 where t100.CRIT = 'STT5' and t100.SL_ID = t0.SL_ID and t100.IDCASE = t0.IDCASE)
--and not exists (select top 1 ds.DS2 from tempdb..TEMP_DS2 ds where (ds.DS2 between 'C00' and 'C99' or  ds.ds2 between 'Z34' and 'Z35'))
--and usl_ok=1
--and dbo.DateTimeDiff('year',t2.dr,date_z_1) >= 18
--and DATE_Z_2 >= convert(date,'01.01.2022',104)
----003F.00.3130 end

--003F.00.3140 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'LEK_PR', null as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.3140'
, 'Не указаны сведения о лек. препаратах'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar,t2.ID_PAC) = convert(varchar,t1.ID_PAC)
where
@q019type in ('H')
and t0.DS1 in ('U07.1','U07.2')
and t0.REAB<>1
and not exists (select 1 from tempdb..TEMP_CRIT t100 where t100.CRIT = 'STT5' and t100.SL_ID = t0.SL_ID and t100.IDCASE = t0.IDCASE)
and not exists (select 1 from tempdb..TEMP_SLUCH_LEK_PR t100 where t100.SL_ID = t0.SL_ID and t100.IDCASE = t0.IDCASE)
and not exists (select top 1 ds.DS2 from tempdb..TEMP_DS2 ds where (ds.DS2 between 'C00' and 'C99' or  ds.ds2 between 'Z34' and 'Z35'))
and usl_ok=1
and dbo.DateTimeDiff('year',t2.dr,t0.date_1)>=18
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--003F.00.3140 end

--003F.00.3150 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH ', CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.3150'
, 'Поле обязательно при DS1 = U07.1 или DS1 = U07.2'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
join tempdb..TEMP_SLUCH_LEK_PR t2 with(nolock) on t0.idcase = t2.idcase and t0.SL_ID = t2.SL_ID
left join ies.T_V024_DOP_KR t4 with(nolock) on t4.IDDKK = t2.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
join tempdb..TEMP_PERS t5  with(nolock) on convert(varchar,t5.ID_PAC) = convert(varchar,t1.ID_PAC) 
where
@q019type in ('H')
and t0.DS1 in ('U07.1','U07.2')
and not exists (select top 1 ds.DS2 from tempdb..TEMP_DS2 ds where (ds.DS2 between 'C00' and 'C99' or  ds.ds2 between 'Z34' and 'Z35'))
and dbo.DateTimeDiff('year',t5.dr,t0.date_1)>=18
and usl_ok=1
and t2.CODE_SH is null
and t4.IDDKK is not null
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--003F.00.3150 end

--print '003F.00.3160'
--003F.00.3160 begin
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', t2.REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t0.N_ZAP, t0.ID_PAC, t0.IDCASE, t0.SL_ID, null as IDSERV, '003F.00.3160'
, 'Поле REGNUM  пустое'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
join tempdb..TEMP_SLUCH_LEK_PR t2 with(nolock) on t0.idcase = t2.idcase and t0.SL_ID = t2.SL_ID
left join ies.T_V031_GROUP_DRUG t4 with(nolock) on t4.ManIndMNN = t2.REGNUM and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)
where
@q019type in ('H')
and t2.REGNUM is null
and t4.ManIndMNN is not null
and DATE_Z_2 >= convert(date,'01.01.2022',104)
--003F.00.3160 end


----FLK 2022 end

	


--region output
if (Select COUNT(*) from #Errors)>0
	BEGIN --FLK OUTPUT
		update e
		set fio = ltrim(ltrim(isnull(pers.FAM,'') + ' ') + isnull(pers.IM,'') + ' ')  + isnull(pers.OT,'') + ' ' + isnull(convert(varchar(10), pers.dr, 104),'')
		from #Errors e
		join tempdb..TEMP_PERS pers on pers.ID_PAC = e.ID_PAC
		where 
		e.FIO is null
		and e.ID_PAC is not null

		update e
		set fio = ltrim(ltrim(isnull(pers.FAM,'') + ' ') + isnull(pers.IM,'') + ' ')  + isnull(pers.OT,'') + ' ' + isnull(convert(varchar(10), pers.dr, 104),'')
		from #Errors e
		join tempdb..TEMP_Z_SLUCH z_sl on e.IDCASE = z_sl.IDCASE
		join tempdb..TEMP_PERS pers on pers.ID_PAC = z_sl.ID_PAC
		where 
		e.FIO is null
		and z_sl.IDCASE is not null

		update e
		set IDDOKT = sl.IDDOKT
		from #Errors e
		join tempdb..TEMP_SLUCH sl on e.IDCASE = sl.IDCASE and e.SL_ID = sl.SL_ID
		where 
		e.IDDOKT is null
		and sl.SL_ID is not null

		update e
		set FDATE = GETDATE()
		from #Errors e


		select 
		'<?xml version="1.0" encoding="Windows-1251"?>
		' + (
		select 
			OSHIB AS 'OSHIB',
			IM_POL AS 'IM_POL',
			case 
				when oshib = '904' then ZN_POL
				else isnull(ZN_POL,'Не заполнено (Null)')
			end
			as 'ZN_POL',
			@NSCHET as 'NSCHET',
			isnull(BASE_EL,bas_el) AS 'BAS_EL',
			N_ZAP AS 'N_ZAP',
			ID_PAC as 'ID_PAC',
			IDCASE AS 'IDCASE',
			SL_ID AS 'SL_ID',
			IDSERV AS 'IDSERV',
			FIO as FIO,
			IDDOKT as IDDOKT,
			COMMENT AS 'COMMENT',
			FDATE as 'FDATE'

		FROM #Errors
		FOR XML PATH('PR'),
		ROOT('FLK_P')
		)
	END


ELSE
	begin

		
			
	 --LOAD
				--проверка, что суммы сходятся
				--declare @sumsluch decimal
				--select @sumsluch =  Sum([SUM_M]) from tempdb..TEMP_SLUCH
				--declare @sumSanks decimal
				--select @sumSanks =  Sum([SANK_IT]) from tempdb..TEMP_Z_SLUCH
				--declare @sumschet decimal
				--select @sumschet = SUM([SUMMAV]) from tempdb..TEMP_SCHET
				--if (@sumsluch <> @sumschet)
				--begin
				--	raiserror('ОШИБКА В ФАЙЛЕ СЧЕТ-РЕЕСТРА: Сумма на случаях не равна сумме на счете', 18,1)
				--end

				update tempdb..TEMP_ZGLV set ID = '1'
				update tempdb..TEMP_SCHET set ID = '1'
				update tempdb..TEMP_ZAP set SchetZapID = newid()
				update tempdb..TEMP_Z_SLUCH set [SchetSluchAccomplishedID] = newid()
				update tempdb..TEMP_SANK set [SchetSluchSankID] = newid()
                update tempdb..TEMP_SANK_SL_ID set [SchetSluchSankSLID] = newid()  
				update tempdb..TEMP_SLUCH set [SchetSluchID] = newid()				
				update tempdb..TEMP_ONK_SL set SchetSluchOnkID = newid()
				update tempdb..TEMP_ONK_USL set [SchetOnkUslID] = newid() 
				update tempdb..TEMP_SANK set [SchetSluchSankID] = newid() 
				update tempdb..TEMP_KSG_KPG set [KsgID] = newid() 
				update tempdb..TEMP_USL set [SchetUslID] = newid()
				update tempdb..TEMP_MR_USL_N set [MrUslNId] = newid()
				Update tempdb..TEMP_SLUCH_LEK_PR set [LekPrID] = newid()
				Update tempdb..TEMP_COMENTSL set [SCHETSLUCHCOMENTSLID]=newid()
				Update tempdb..TEMP_COMENTU set [SchetUslComentUID]=newid() 
				Update tempdb..TEMP_KSG_STOMAT set [KSGSTOMATID]=newID()
				update tempdb..TEMP_CODE_MD set [SchetDoctID]=NEWID()
				


		
		 		

 SELECT '[IES].[T_SCHET]' as tableName, 'счета' as operationName, (select count(*) from tempdb..TEMP_SCHET) as tableCount
				 
					 SELECT t2.[VERSION]
					 ,t2.[DATA]
					 ,[CODE]
					 ,[YEAR]
					 ,[MONTH]
					 ,[NSCHET]
					 ,[DSCHET]
					 ,[SUMMAV]
					 ,[COMENTS]
					 ,[SUMMAP]
					 ,[SANK_MEK] as SANK_MEK_R
					 ,[SANK_MEE] AS SANK_MEE_R
					 ,[SANK_EKMP] AS SANK_EKMP_R
					 ,CAST(CAST(CAST(GETDATE() as float) as INT) as datetime) as [ReceivedDate]
					 ,GETDATE() as [ReceivedTime]
					 ,t2.[FILENAME]
					 ,[CODE_MO]
					 ,[PLAT]
					 ,@Worker as [Worker]
					 ,@SchetID as [SchetID]
					 ,@type as [type_]
					 ,0 as [Status]
					 ,0 as [SchetKind]
					-- ,(SELECT V016ID from IES.T_V016_DISPT where IDDT = DISP and DATEBEG<=@ActualDate and (DATEEND is null or DATEEND>=@ActualDate)) AS DISP
					 ,SD_Z	
					 ,@SchetPeriod as SchetPeriodR
					 ,ISNULL(t4.[FILENAME1],t3.FILENAME1) as [FILENAME_1]	
					 ,(Select SUM(t5.SUMMAP_APP) from tempdb..TEMP_COMENTS t5 join tempdb..TEMP_SCHET on 1=1)  as SUMMAP_APP
					 FROM tempdb..TEMP_SCHET t1  
					 JOIN tempdb..TEMP_ZGLV t2 on t2.ID = t1.ID
					 left join tempdb..TEMP_ZGLV_PERS_DOCT t3 on t3.FILENAME1=t2.[FILENAME]
					 left join tempdb..TEMP_ZGLV_PERS t4 on t4.FILENAME1=t2.[FILENAME]

					
					

					SELECT '[IES].[T_SCHET_DOCT]' as tableName, 'Структура реестра медицинских работников' as operationName, (select count(*) from tempdb..TEMP_CODE_MD) as tableCount

					 
					 SELECT  t3.[SchetDoctID]
                            ,t3.[CODE_MD]
							,t3.FAM as FAM
							,t3.IM as IM
							,t3.OT as OT
							,t3.DR as DR
                            ,t3.[SNILS] as SNILS
                            ,@SchetID as [Schet]
                     FROM tempdb..TEMP_CODE_MD t3
					 --JOIN tempdb..TEMP_ZGLV t1 on SUBSTRING(t0.FILENAME,2, LEN(t0.FILENAME))=t1.[FILENAME]
					 --join tempdb..TEMP_SCHET t2 on  t2.ID = t1.ID

	 
				-----------------[IES].T_SCHET_ZAP
				--CREATE NONCLUSTERED INDEX [IX_TEMP_ZAP_N_ZAP] ON tempdb..TEMP_ZAP
				--(
				--	[N_ZAP] ASC
				--)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

				--CREATE NONCLUSTERED INDEX [IX_IESDB_TEMP_TEMP_SLUCH_VERS_SPEC]
				--ON [IESDB_TEMP].[dbo].[TEMP_SLUCH] ([VERS_SPEC])
				--INCLUDE ([N_ZAP],[IDCASE],[PRVS])

				--CREATE NONCLUSTERED INDEX [IX_IESDB_TEMP_TEMP_USL_IDCASE]
				--ON [IESDB_TEMP].[dbo].[TEMP_USL] ([IDCASE])
				--INCLUDE ([IDSERV],[PRVS])

				--CREATE NONCLUSTERED INDEX [IX_IESDB_TEMP_TEMP_SLUCH_IDCASE_VERS_SPEC]
				--ON [IESDB_TEMP].[dbo].[TEMP_SLUCH] ([IDCASE],[VERS_SPEC])
				--INCLUDE ([N_ZAP])

	

				SELECT '[IES].[T_SCHET_ZAP]' as tableName, ' информации о людях' as operationName, (select count(*) from tempdb..TEMP_ZAP) as tableCount

				select t0.ID_PAC, Left(t0.rezult,Len(t0.rezult)-1) As rezult into #temp_dost
					From
					(
						Select distinct cast(ID_PAC as varchar(36)) as ID_PAC, 
							(
								Select cast(t1.DOST as varchar(1)) + ',' AS [text()]
								From tempdb..TEMP_DOST t1
								Where cast(t1.ID_PAC as varchar(36)) = cast(ST2.ID_PAC as varchar(36))
								ORDER BY cast(t1.ID_PAC as varchar(36))
								For XML PATH ('')
							) rezult
						From tempdb..TEMP_DOST ST2
					) t0 

				select t0.ID_PAC, Left(t0.rezult,Len(t0.rezult)-1) As rezult into #temp_dost_p
					From
					(
						Select distinct cast(ID_PAC as varchar(36)) as ID_PAC, 
							(
								Select cast(t1.DOST_P as varchar(1)) + ',' AS [text()]
								From tempdb..TEMP_DOST_P t1
								Where cast(t1.ID_PAC as varchar(36)) = cast(ST2.ID_PAC as varchar(36))
								ORDER BY cast(t1.ID_PAC as varchar(36))
								For XML PATH ('')
							) rezult
						From tempdb..TEMP_DOST_P ST2
					) t0 	

			
				
				 SELECT 
				 [SchetZapID]
				 ,[SPOLIS]
				 ,[NPOLIS]
				 ,[NOVOR]
				 ,zap.[N_ZAP]
				 ,(SELECT TOP 1 f008.IDDOC FROM [IES].T_F008_OMS_TYPE f008 WHERE f008.IDDOC =( Case when[VPOLIS]=9 then 3 else VPOLIS end)) as [VPOLIS]
				 ,cast(pac.ID_PAC as varchar(36)) as [ID_PAC]
				 ,(SELECT TOP 1 mo.SMOCOD FROM [IES].T_F002_SMO mo WHERE mo.SMOCOD = pac.[SMO]) as [SMO]
				 ,[SMO_OGRN]
				 ,[SMO_OK]
				 ,[SMO_NAM]
				 ,[PR_NOV]
				 ,@SchetID as [Schet]
				 ,698 as [type_]
				 ,[ST_OKATO]
				 ,nullif([VNOV_D],0) as [VNOV_D]
				 ,[FAM]
				 ,[IM]
				 ,[OT]
				 ,[W]
				 ,[DR]
				 ,[FAM_P]
				 ,[IM_P]
				 ,[OT_P]
				 ,[W_P]
				 ,[DR_P]
				 ,[MR]
				 ,(case when [DOCTYPE]='03' then '3' 
				        when [DOCTYPE]='Не' then null 
						else [DOCTYPE] end) as [DOCTYPE]
				 ,[DOCSER]
				 ,[DOCNUM]
				 ,[DOCDATE]
				 ,[DOCORG]
				 ,[SNILS]
				 ,[OKATOG]
				 ,[OKATOP]
				 ,[COMENTP]
				 --,[MO] as [AttachmentMo]
				 ----,[MODATE] as [AttachDate]
				 ,dost.rezult as DOST
				 ,dost_p.rezult as DOST_P
				 ,[INV]
				 ,pac.[MSE]
				 ,TEL as [TEL]
				 ,[ENP]
				-- , MO_SK as [MO_SK],
				 FROM tempdb..TEMP_ZAP zap
				 join tempdb..TEMP_PACIENT pac on pac.N_ZAP = zap.N_ZAP
				 left join tempdb..TEMP_PERS pers on cast(pers.ID_PAC as varchar(36)) = cast(pac.ID_PAC as varchar(36))
				 left join #temp_dost   dost   on (CAST(dost.ID_PAC as varchar(36)) = cast(pac.ID_PAC as varchar(36)))
				 left join #temp_dost_p dost_p on (CAST(dost_p.ID_PAC as varchar(36)) = cast(pac.ID_PAC as varchar(36)))
	
if exists (
	select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#temp_dost')
)
DROP TABLE #temp_dost;

if exists (
	select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#temp_dost_p')
)
DROP TABLE #temp_dost_p;




				 ---- Заполняет значение результата для диспансеризации


				  SELECT '[IES].[T_SCHET_SLUCH_ACCOMPLISHED]' as tableName, ' информации о законченных случаях лечения' as operationName, (select count(*) from tempdb..TEMP_Z_SLUCH) as tableCount
  
				  SELECT SchetSluchAccomplishedID
				  , IDCASE
				  ,t1.USL_OK  As [USL_OK]
				  , VIDPOM
				  , t1.[FOR_POM] As [FOR_POM]
				  , NPR_MO
				  , NPR_DATE
				  , LPU
				  , DATE_Z_1
				  , DATE_Z_2
				  , KD_Z
				  , cast(RTRIM(LTRIM(Replace(VNOV_M,';',''))) as int) as VNOV_M
				  , t1.RSLT AS [RSLT]
				  ,[ISHOD] As [ISHOD]
				  , OS_SLUCH
				  , VB_P
				  , IDSP
				  , SUMV
				  , OPLATA
				  , SUMP
				  , SANK_IT
				  , t2.[SchetZapID] as [SchetZap]
				  , VBR
				  , P_OTK
				  , (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t1.RSLT_D = v017.IDDR) as [RSLT_D]
				  , P_DISP2
				  FROM tempdb..TEMP_Z_SLUCH t1 
				  JOIN tempdb..TEMP_ZAP t2 on t2.N_ZAP = t1.N_ZAP


				
				  SELECT '[IES].[T_SCHET_SLUCH]' as tableName, ' информации о случаях лечения' as operationName, (select count(*) from tempdb..TEMP_SLUCH) as tableCount

				  SELECT 
					 t1.[SchetSluchID]
					,t1.[SL_ID]
					,t1.[NHISTORY]
					,t1.[DATE_1]
					,t1.[DATE_2]
					,t1.[DS0]
					,t1.[DS1]
					,t1.[CODE_MES1]
					,t1.[CODE_MES2]
					,t1.[ED_COL]
					,t1.[TARIF] 
					,t1.SUM_M as [SUMV]
					,t1.SUM_M as [SUMP]
					--,t1.[COMENTSL]
					,t1.[PROFIL] as PROFIL
					,(select top 1 V020BedProfileId from IES.T_V020_BED_PROFILE where  t1.[PROFIL_K] = IDK_PR and DATEBEG<=t1.[DATE_1] and (DATEEND is null or DATEEND>=t1.[DATE_2]) ) as [PROFIL_K]
					,(select top 1 V027CZabID from ies.T_V027_C_ZAB zab where IDCZ=t1.C_ZAB and DATEBEG<=t1.[DATE_1] and (DATEEND is null or DATEEND>=t1.[DATE_2])) as [C_ZAB]
					,case when [VERS_SPEC] = 'V004' THEN t1.[PRVS] ELSE null END as [PRVS]
					,case when [VERS_SPEC] = 'V015' THEN t1.[PRVS] ELSE null END as [PRVS2]
					,case when [VERS_SPEC] = 'V021' or [VERS_SPEC] is null THEN 
					(select top 1 IDSPEC from ies.T_V021_MED_SPEC v021 with(nolock) where CODE_SPEC=t1.PRVS and DATEBEG<=@ActualDate and (DATEEND is null or DATEEND>=@ActualDate)) 
					ELSE null END as [PRVS3]
					,t1.[DET] AS [DET]
					--,t1.[DET] AS [DET]
					,[PODR]
					,[LPU_1]
					,[IDDOKT]
					, 700 as [type_]
					, t2.[SchetZapID] as [SchetZap]
					,(CASE WHEN @filename like 'D%' then 1
						else null end) AS [DISP]
					, (select top 1 V025KpcID from IES.T_V025_KPC where IDPC = t1.[P_CEL] and DATEBEG<=t1.[DATE_1] and (DATEEND is null or DATEEND>=t1.[DATE_2])) As [P_CEL]
					, t1.[VID_HMP]
					--, (select top 1 v019.V019VmpMethodID from ies.T_V019_VMP_METHOD v019 with(nolock) where v019.IDHM = t1.METOD_HMP and v019.DATEBEG<=DATE_Z_2 and (v019.DATEEND is null or v019.DATEEND>=DATE_Z_2)) as [METOD_HMP]
					, t5.V019VmpMethodID as [METOD_HMP]
					, t1.HGR
					, t1.[KD]
					, cast(t1.[WEI] as decimal(8,1)) as WEI
					, t1.[P_PER]
					, t1.[TAL_D]
					, t1.[TAL_NUM]
					, t1.[TAL_P]
					, t1.[DS1_PR]
					, t1.[DN]
					, t1.[REAB]
					, t1.[PR_D_N]
					, t3.[SchetSluchAccomplishedID] as [SchetSluchAccomplished]
					, t1.[DS_ONK]
					, t1.[USL_OK_SL]
					,case when t1.ds1 in ('I10','I11','I12','I13','I15','I20.1','I20.8','I20.8','I20.9','I25.0','I25.1','I25.2','I25.5','I25.6','I25.8','I25.9')
					then (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR='1') 
					when t1.ds1 in ('I48')
					then (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR='2') 
					when t1.ds1 in ('Е11')
					then (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR='3') 
					when t1.ds1 in ('J44.0', 'J44.8', 'J44.9')
					then (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR='4')
					when t1.ds1 in ('I69.0', 'I69.1', 'I69.2', 'I69.3', 'I69.4', 'I67')
					then (select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR='5')
					else
					(select top 1 v017.IDDR from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where t3.RSLT_D = v017.IDDR  and v017.IDDR not in ('5','4','3','2','1'))
					end as [RSLT_D]
					,(CASE WHEN @filename like 'D%' then 'V021' else t1.[VERS_SPEC] end) AS [VERS_SPEC]
				  FROM tempdb..TEMP_SLUCH t1    
				  JOIN tempdb..TEMP_ZAP t2 on t2.N_ZAP = t1.N_ZAP
				  JOIN tempdb..TEMP_Z_SLUCH t3 on t1.IDCASE = t3.IDCASE and t2.N_zap=t3.N_zap
				  outer apply (
					select top 1 V019VmpMethodId
					from ies.T_V019_VMP_METHOD t100 with(nolock)
					left join ies.T_V022_MODPAC t200 with(nolock) on t200.V022ModpacID = t100.V022Modpac
					where
					t100.idhm = t1.METOD_HMP and t100.HGR=t1.HGR
					and t100.DATEBEG<=DATE_Z_2 and (t100.DATEEND is null or t100.DATEEND>=DATE_Z_2)
					) t5



				SELECT '[IES].[T_SCHET_SLUCH_COMENTSL]' as tableName, ' информации о Служебное поле' as operationName, (select count(*) from tempdb..TEMP_COMENTSL) as tableCount
					
					SELECT    t0.[SCHETSLUCHCOMENTSLID]
					         ,t0.[ATTACH_MO]
                             ,t0.[ATTACH_MO_HELP]
                             ,t0.[ATTACH_Z]
                             ,t0.[ATTACH_Z_HELP]
                             ,t0.[ATTACH_GIN]
                             ,t0.[ATTACH_GIN_HELP]
                             --,t0.[SPEC_NPR]
                             ,t0.[OPMP]
                            -- ,t0.[FIN_TYPE]
                             ,t0.[VIDMP]
                             ,t0.[ATTACH_SMP] as ATTACH_SMP
                             ,t0.[METHOD]
                             ,t0.[TIME_CALL]
                             ,t0.[TIME_MISSION]
                             ,t0.[NPR_OTHER_MO]
                             ,t0.[STOMAT_TIME]
                             ,t0.[UK_STOMAT_K]
                             ,t0.[VBR]
                             ,t0.[CAOP]
                             ,t0.[TEST_VI]
                             --,t0.[GESTATION]
							 ,t0.[MR_1S]
							 ,t0.[SHRM]
							 ,cast(Ltrim(Rtrim(t0.DG)) as int) as DG
							 ,t0.P_OTK2
                             ,t1.[schetsluchID] as [schetsluch]
							 ,t0.SHRM_N  as [SHRM_N]
							 ,t0.SHRM_K as [SHRM_K]
							 ,t0.DATE_NEXT as [DATE_NEXT]
							 ,t0.DATE_MGI as [DATE_MGI]
							 ,t0.CODE_MGI as [CODE_MGI]
							 ,t0.MO_MGI as [MO_MGI]
							 ,t0.DEPART_OID as [DEPART_OID]
							 ,t0.PRVS_C as [PRVS_C]
							 ,t0.IDDOKT_C as [IDDOKT_C]
							 ,t0.M_PROF as [M_PROF]
                         FROM tempdb..TEMP_COMENTSL t0
						 join tempdb..TEMP_SLUCH t1 on t0.SL_ID=t1.SL_ID  and t0.IDCASE = t1.IDCASE and t1.n_zap=t0.N_ZAP
					--	--left join tempdb..TEMP_KSG_STOMAT t2 on  t0.SL_ID=t2.SL_ID  and t0.IDCASE = t2.IDCASE


					SELECT '[IES].[T_KSG_STOMAT]' as tableName, ' информации об Сведения о КСГ случая стоматологии' as operationName, (select count(*) from tempdb..TEMP_KSG_STOMAT) as tableCount
					
					SELECT  t0.[KSGSTOMATID]
                           ,t0.[KSG_STOMAT_ID]
                           ,t0.[KSG_CODE] 
                           ,t0.NZ as [KSG_NZ]
                           ,t0.[SHORT]
                           ,t0.[FIN_SANK]
                           ,t1.[SCHETSLUCHCOMENTSLID] as [SCHETSLUCHCOMENTSL]
                    FROM tempdb..TEMP_KSG_STOMAT t0
					join tempdb..TEMP_COMENTSL t1 on  t0.SL_ID=t1.SL_ID  and t0.IDCASE = t1.IDCASE and t0.N_ZAP=t1.N_ZAP

				

					
					SELECT '[IES].[T_SCHET_SLUCH_SANK]' as tableName, ' информации об удержаниях' as operationName, (select count(*) from tempdb..TEMP_SANK) as tableCount

					
                      SELECT 
                       ISNULL(sankSL.[SchetSluchSankSLID], sank.SchetSluchSankID) AS [SchetSluchSankID]
                      ,zSL.[SchetSluchAccomplishedID] AS [SchetSluchAccomplished]
                      ,sank.[S_CODE] AS [S_CODE]
                      ,sank.[S_SUM] AS [S_SUM]
                      ,sank.[S_TIP] AS [S_TIP]
                      ,(select top 1 f014.[F014DenyReasonID] from [IES].[T_F014_DENY_REASON] f014 where f014.[kod] = sank.[S_OSN] and 
                      f014.DATEBEG<=zSL.[DATE_Z_2] and (f014.[DATEEND] is null or f014.[DATEEND] >= zSL.[DATE_Z_2])) AS [S_OSN]
                      ,sank.[S_COM] AS [S_COM]
                      ,sank.[S_IST] AS [S_IST]
                      ,sank.[DATE_ACT] AS [DATE_ACT]
                      ,sank.[NUM_ACT] AS [NUM_ACT]
                      ,sl.SchetSluchID AS [SchetSluch]
					,(
						select top 1 f006.[F006ControlTypeID] from ies.T_F006_CONTROL_TYPE f006 with(nolock) where f006.IDVID = sank.[S_TIP]
						and f006.DATEBEG<=isnull(sank.DATE_ACT,zsl.[DATE_Z_2]) and (f006.DATEEND is null or f006.DATEEND>=isnull(sank.DATE_ACT,zsl.[DATE_Z_2]))
					) as [S_TIP_F006]
					, t0.[CODE_EXP] AS [EXPERT]
					FROM tempdb..TEMP_SANK sank
                    JOIN tempdb..TEMP_Z_SLUCH zSL ON (zSL.[IDCASE] = sank.[IDCASE])
                    LEFT JOIN tempdb..TEMP_SANK_SL_ID sankSL ON (sank.[S_CODE] = sankSL.[S_CODE] and sank.[IDCASE] = sankSL.[IDCASE])
                    LEFT JOIN tempdb..TEMP_SLUCH sl ON (sl.[IDCASE] = sank.[IDCASE] and sankSL.[SL_ID] = sl.[SL_ID])
					left join tempdb..TEMP_CODE_EXP t0  ON t0.IDCASE = sank.IDCASE AND t0.S_CODE = sank.S_CODE

				
					select '[IES].[T_SCHET_SLUCH_SANK_EXP]' as tableName, ' информации об удержаниях' as operationName, (select count(*) from tempdb..TEMP_CODE_EXP ) as tableCount

					SELECT NEWID() AS SchetSluchSankExpID
					, isnull(t4.schetsluchsankslid,t1.SchetSluchSankID) AS SchetSluchSank
					, t0.[CODE_EXP] AS [F004Expert]
					from tempdb..TEMP_CODE_EXP t0 
					JOIN tempdb..TEMP_SANK t1 ON t0.IDCASE = t1.IDCASE
						AND t0.S_CODE = t1.S_CODE
					--LEFT JOIN tempdb..TEMP_SANK_SL_ID t4 ON (t4.S_CODE = t0.S_CODE AND t4.IDCASE = t0.IDCASE)
					OUTER APPLY(SELECT TOP (1) * FROM tempdb..TEMP_SANK_SL_ID t4 WHERE t4.S_CODE = t1.S_CODE AND t4.IDCASE = t1.IDCASE ORDER BY CASE t4.SL_ID WHEN t1.SL_ID THEN 1 ELSE 0 END DESC) t4

					SELECT '[IES].[T_KSG]' as tableName, ' информации о КСГ в случаях' as operationName, (select count(*) from tempdb..TEMP_KSG_KPG) as tableCount

					SELECT 
					[KsgID]
					, KSG_PG
					, SL_K
					, IT_SL
					, ts.SchetSluchID as [SchetSluch]
					, (select top 1 V026KpgID from IES.T_V026_KPG where K_KPG = t1.N_KPG) as [N_KPG]
					,  t1.N_KSG as [N_KSG]
					, (select top 1 V024DopKrID from IES.T_V024_DOP_KR where IDDKK = t1.DKK2) as [DKK2]
					, (select top 1 V024DopKrID from IES.T_V024_DOP_KR where IDDKK = t1.DKK1) as [DKK1]
					, VER_KSG
					, KOEF_Z
					, KOEF_UP
					, BZTSZ
					, KOEF_D
					, KOEF_U
					, K_ZP as [K_ZP]
					FROM tempdb..TEMP_KSG_KPG t1 
					JOIN tempdb..TEMP_SLUCH ts on ts.[SL_ID] = t1.[SL_ID] and t1.IDCASE = ts.IDCASE
					join tempdb..TEMP_Z_SLUCH t3 on (t3.IDCASE = ts.IDCASE and t3.n_zap=ts.n_ZAP)


					SELECT '[IES].[T_KSG_CRIT]' as tableName, ' Классификационный критерий (V024)' as operationName, (select count(*) from tempdb..TEMP_CRIT) as tableCount

					SELECT 
					[KsgID] as [Ksg]
					, newid() as [KsgCritID]
					, (select top 1 V024DopKrID from IES.T_V024_DOP_KR where IDDKK = t1.CRIT) as [V024DopKr]
					, (select top 1 dictionaryBaseID from [IES].[R_NSI_SHLT] rs where rs.CODE = t1.CRIT) as [SHLT]
					, (select top 1 dictionaryBaseID from [IES].[R_NSI_ADDIT_CRIT] ra where ra.CODE = t1.CRIT) as [ADDIT_CRIT]
					, (select top 1 dictionaryBaseID from [IES].[R_NSI_MNN_LP_LT] rm where rm.CODE = t1.CRIT) as [MNN_LP_LT]
					,t1.Crit as R_CRIT
					FROM tempdb..TEMP_CRIT t1
					join tempdb..TEMP_KSG_KPG t2 on t2.[SL_ID] = t1.[SL_ID] and t1.IDCASE = t2.IDCASE 
					JOIN tempdb..TEMP_SLUCH ts on ts.[SL_ID] = t2.[SL_ID] and t2.IDCASE = ts.IDCASE

					SELECT '[IES].[T_KSLP]' as tableName, ' информации о КСЛП в случаях' as operationName, (select count(*) from tempdb..TEMP_SL_KOEF) as tableCount

					SELECT 
					newid() as [KslpID]
					, t1.Z_SL as [KOEF]
					, t2.[SchetSluchID] as [OmsSchetSluch]
					, t1.IDSL as [KOEF_TYPE]
					, t3.KsgID as [Ksg] 
					FROM tempdb..TEMP_SL_KOEF t1
					JOIN tempdb..TEMP_SLUCH t2 on t2.[SL_ID] = t1.[SL_ID] and t1.IDCASE = t2.IDCASE
					JOIN tempdb..TEMP_KSG_KPG t3 on t2.[SL_ID] = t3.[SL_ID] and t2.IDCASE = t3.IDCASE 


					

					 SELECT '[IES].[T_SCHET_USL]' as tableName, ' информации об услугах' as operationName, (select count(*) from tempdb..TEMP_USL) as tableCount

					 SELECT 
					  t1.[SchetUslID]
					 ,t1.[IDSERV]
					 ,t1.[LPU]
					 ,t1.[DATE_IN]
					 ,t1.[DATE_OUT]
					 ,t1.[DS] as DS
					 --,(CASE WHEN 
						--	(select top 1 [FILENAME] 
						--	from tempdb..TEMP_ZGLV s WITH(NOLOCK)) not like 'D%'
						--	THEN cast (t1.[KOL_USL] as decimal(6,2)) ELSE 1 END) As [KOL_USL]
					 ,cast(t1.[KOL_USL] as decimal(6,2)) As [KOL_USL]
					 ,cast(t1.[TARIF] as decimal(15,2)) as [TARIF]
					 ,case when t2.Tarif>0 then
					 cast((select COUNT(*) from tempdb..TEMP_SLUCH t3 where t3.SL_ID = t1.SL_ID and t3.IDCASE = t1.IDCASE and t1.N_ZAP=t3.N_ZAP)*t2.tarif as decimal(17,2))
					 else cast(ISNULL(t1.[SUMV_USL],0) as decimal(10,2)) end as [SUMV_USL]
					 --,cast(ISNULL(t1.[SUMV_USL],0) as decimal(10,2)) as [SUMV_USL]
					 ,t1.[COMENTU]
					 ,t1.[PROFIL] as PROFIL
					 ,t1.[NPL]
					 ,t1.[DET]
					 ,t1.[LPU_1]
					 ,t1.[PODR]
					 ,t1.[CODE_USL]
					 ,t1.[CODE_MD]
					 ,t2.[SchetSluchID] as [SchetSluch]	
					 ,(select top 1 v001.name from [IES].[T_V001_NOMENCLATURE] v001 where v001.CODE=t1.CODE_USL and ISNULL(v001.DATEBEG,'19000101')<=@ActualDate and (DATEEND is null or DATEEND>=@ActualDate)) as USL
					 ,710 as [type_]
					 ,t1.[VID_VME]
					 ,case when t2.[VERS_SPEC] = 'V004' THEN t1.[PRVS] else null END as [PRVS]
					 ,case when t2.[VERS_SPEC] = 'V015' THEN (select top 1 RECID from [IES].T_V015_MEDSPEC where CODE = t1.[PRVS]) ELSE NULL END as PRVS2
					 ,case when t2.[VERS_SPEC] = 'V021' or t2.[VERS_SPEC] is null THEN (select top 1 IDSPEC from ies.T_V021_MED_SPEC v021 where CODE_SPEC=t1.PRVS and DATEBEG<=@ActualDate and (DATEEND is null or DATEEND>=@ActualDate)) ELSE null END as [PRVS3]
					 ,t1.UslType_R as UslType_R
					 --,(CASE WHEN 
						--(select top 1 [FILENAME] 
						--from tempdb..TEMP_ZGLV s WITH(NOLOCK)) like 'D%' then 'V021' else t1.[VERS_SPEC] end) AS [VERS_SPEC]
					 FROM tempdb..TEMP_USL t1
					 JOIN tempdb..TEMP_SLUCH t2 on t2.SL_ID = t1.SL_ID and t2.IDCASE = t1.IDCASE and t1.N_ZAP=t2.N_ZAP
					 --JOIN tempdb..TEMP_Z_SLUCH t3 on t2.IDCASE = t3.IDCASE and t2.N_ZAP=t3.N_ZAP
				

	  
					SELECT '[IES].[T_SCHET_USL_COMENTU]' as tableName, ' информации о Служебное поле' as operationName, (select count(*) from tempdb..TEMP_COMENTU) as tableCount

					SELECT t0.[NZ]
                          ,t0.[TELEMED]
                          ,t0.[KOL_TEST]
                          ,t1.schetuslid as  [SCHETUSL]
                          ,t0.[SchetUslComentUID]
                    FROM tempdb..TEMP_COMENTU t0
				    join tempdb..TEMP_USL t1 on t1.SL_ID = t0.SL_ID and t0.IDCASE = t1.IDCASE and t1.IDSERV=t0.IDSERV and t0.N_ZAP=t1.N_ZAP

					

		
	  
					SELECT '[IES].[T_MR_USL_N]' as tableName, ' информации об мед.работниках выполнивших услугу' as operationName, (select count(*) from tempdb..TEMP_MR_USL_N) as tableCount
					SELECT
					t0.MrUslNID
					,t1.[SchetUslID] as [SchetUsl]
					,t0.MR_N
					,(select top 1 IDSPEC from ies.T_V021_MED_SPEC v021 with(nolock) where CODE_SPEC=t0.PRVS and DATEBEG<=@ActualDate and (DATEEND is null or DATEEND>=@ActualDate)) as PRVS
					,t0.CODE_MD
					FROM TEMPDB..TEMP_MR_USL_N t0
					JOIN tempdb..TEMP_USL t1 ON t1.IDSERV = t0.IDSERV and t1.SL_ID = t0.SL_ID and t1.IDCASE = t0.IDCASE  and t1.N_ZAP=t0.N_ZAP
					JOIN tempdb..TEMP_SLUCH t2 on t2.SL_ID = t1.SL_ID and t2.IDCASE = t1.IDCASE and t1.N_ZAP=t2.N_ZAP
					
					
			
					--select top 1 * from ies.T_MR_USL_N

					declare @cnt int

					select @cnt = count(*)
					from
					(
						SELECT NEWID() as [SchetSluchDsID], t1.[DS2] as [MKB], t2.[SchetSluchID] as [SchetSluch],0 as [MKBType]
						FROM 
						tempdb..TEMP_DS2 t1
						JOIN tempdb..TEMP_SLUCH t2 on t2.[IDCASE] = t1.[IDCASE]
						union
						SELECT NEWID() as [SchetSluchDsID], t1.[DS3] as [MKB], t2.[SchetSluchID] as [SchetSluch],1 as [MKBType]
						FROM 
						tempdb..TEMP_DS3 t1
						JOIN tempdb..TEMP_SLUCH t2 on t2.[IDCASE] = t1.[IDCASE]
					)aa

					SELECT '[IES].[T_SCHET_SLUCH_DS]' as tableName, ' информации об диагнозах' as operationName, @cnt as tableCount
					
					SELECT NEWID() as [SchetSluchDsID], t1.[DS2] as [MKB], t2.[SchetSluchID] as [SchetSluch],0 as [MKBType], null as DS2_PR, null as  PR_DS2_N
					FROM 
					tempdb..TEMP_DS2 t1
					JOIN tempdb..TEMP_SLUCH t2 on t2.[IDCASE] = t1.[IDCASE] and t1.SL_ID = t2.SL_ID and t1.N_ZAP=t2.N_ZAP
					union
					SELECT NEWID() as [SchetSluchDsID], t1.[DS3] as [MKB], t2.[SchetSluchID] as [SchetSluch],1 as [MKBType], null as DS2_PR, null as PR_DS2_N
					FROM 
					tempdb..TEMP_DS3 t1
					JOIN tempdb..TEMP_SLUCH t2 on t2.[IDCASE] = t1.[IDCASE] and t1.SL_ID = t2.SL_ID  and t1.N_ZAP=t2.N_ZAP
					union
					SELECT NEWID() as [SchetSluchDsID], t1.[DS2] as [MKB], t2.[SchetSluchID] as [SchetSluch],0 as [MKBType], DS2_PR as [DS2_PR], PR_DS2_N as [PR_DS2_N]
					FROM 
					tempdb..TEMP_DS2_N t1
					JOIN tempdb..TEMP_SLUCH t2 on t2.[IDCASE] = t1.[IDCASE] and t1.SL_ID = t2.SL_ID  and t1.N_ZAP=t2.N_ZAP

						


					SELECT '[IES].[T_SCHET_SLUCH_CONS]' as tableName, ' Сведения о проведении консилиума ' as operationName, (select count(*) from tempdb..TEMP_CONS) as tableCount

					SELECT 
					newid() as [SchetSluchConsID]
					, t2.SchetSluchID as [SchetSluch]
					,(select top 1 n019.N019OnkConsID from [IES].[T_N019_ONK_CONS] N019 where N019.ID_CONS = PR_CONS) as N019OnkCons
					, DT_CONS
					from tempdb..TEMP_CONS t1 
					JOIN tempdb..TEMP_SLUCH t2 on (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.n_zap=t2.N_ZAP)

					SELECT '[IES].[T_SCHET_SLUCH_NAZ]' as tableName, ' информации о назначениях' as operationName, (select count(*) from tempdb..TEMP_NAZ) as tableCount
					--select top 1 * from [IES].[T_SCHET_SLUCH_NAZ]
					SELECT  
					newid() as [SchetSluchNazID]
					, tn.NAZ_N
					, tn.NAZ_R as [NAZR]
					, tn.NAZ_IDDOKT as [NAZ_IDDOKT]
					, tn.NAZ_SPDOKT as [NAZ_SPDOKT]
					, (select top 1 IDSPEC from IES.T_V021_MED_SPEC where IDSPEC = tn.NAZ_SP) as [NAZ_SP]
					, tn.NAZ_PMP
					, (select top 1 V029MetIsslID from IES.T_V029_MET_ISSL where IDMET = tn.NAZ_V) as [NAZ_V]
					, (select top 1 V020BedProfileID from IES.T_V020_BED_PROFILE where IDK_PR = tn.NAZ_PK) as [NAZ_PK]
					, ts.SchetSluchID as [SchetSluch]
					, tn.NAPR_DATE
					, tn.NAPR_MO
					, tn.NAZ_USL 
					, tn.NAZ_DATE as [NAZ_DATE]
					FROM tempdb..TEMP_NAZ tn  
					JOIN tempdb..TEMP_SLUCH  ts on ts.[SL_ID] = tn.[SL_ID] and tn.IDCASE = ts.IDCASE  and ts.N_ZAP=tn.N_ZAP

					

					SELECT '[IES].[T_SCHET_USL_NAPR]' as tableName, ' направления ' as operationName, (select count(*) from tempdb..TEMP_NAPR) as tableCount

					SELECT 
					newid() as [SchetUslNaprID]
					, t2.SchetSluchID as [SchetSluch]
					, NAPR_DATE
					, NAPR_V
					, MET_ISSL
					, NAPR_USL,  
					(case when NAPR_MO = t3.LPU and NAPR_MO is not null then null 
						else NAPR_MO
						end) as NAPR_MO
					from tempdb..TEMP_NAPR t1 
					JOIN tempdb..TEMP_SLUCH t2 on (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.N_ZAP=t2.N_ZAP) 
					JOIN tempdb..TEMP_Z_SLUCH t3 on (t2.IDCASE = t3.IDCASE and t2.N_zap=t3.N_zap)

					SELECT '[IES].[T_SCHET_SLUCH_ONK]' as tableName, ' онкологические заболевания ' as operationName, (select count(*) from tempdb..TEMP_ONK_SL) as tableCount

					SELECT 
					SchetSluchOnkID
					, t2.SchetSluchID as [SchetSluch]
					, (select top 1 N018.N018OnkReasID from [IES].[T_N018_ONK_REAS] N018 where n018.ID_REAS = t1.DS1_T) as [DS1_T]
					, (select top 1 N002.N002StadiumID from [IES].[T_N002_STADIUM] N002 where N002.ID_St = t1.STAD) as [STAD]
					, (select top 1 N003.N003TumorID from [IES].[T_N003_TUMOR] N003 where N003.ID_T = t1.ONK_T) as [ONK_T]
					, (select top 1 N004.N004NodusID from [IES].[T_N004_NODUS] N004 where N004.ID_N = t1.ONK_N) as [ONK_N]
					, (select top 1 N005.N005MetastasisID from [IES].[T_N005_METASTASIS] N005 where N005.ID_M = t1.ONK_M) as [ONK_M]
					, MTSTZ
					, SOD
					, K_FR
					, cast(t1.WEI as decimal(8,1)) as WEI
					, HEI
					, BSA
					from tempdb..TEMP_ONK_SL t1   
					JOIN tempdb..TEMP_SLUCH t2 on (t2.[IDCASE] = t1.[IDCASE] and t2.SL_ID = t1.SL_ID and t1.N_ZAP=t2.N_ZAP)

							
					SELECT '[IES].[T_SCHET_SLUCH_ONK_DIAG]' as tableName, ' диагностический блок ' as operationName, (select count(*) from tempdb..TEMP_B_DIAG) as tableCount

					SELECT 
					newid() as [SchetSluchOnkDiagID]
					, t2.SchetSluchOnkID as [SchetSluchOnk]
					, DIAG_TIP
					, DIAG_CODE
					, DIAG_RSLT
					, REC_RSLT
					, DIAG_DATE
					from tempdb..TEMP_B_DIAG t1 
					JOIN tempdb..TEMP_ONK_SL t2 on (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.n_ZAP=t2.N_ZAP)

					SELECT '[IES].[T_SCHET_SLUCH_ONK_PROT]' as tableName, ' сведения об имеющихся противопоказаниях ' as operationName, (select count(*) from tempdb..TEMP_B_PROT) as tableCount

					SELECT 
					newid() as [SchetSluchOnkProtID]
					, t2.SchetSluchOnkID as [SchetSluchOnk]
					, D_PROT
					,(select top 1 N001.N001PrOtID from [IES].[T_N001_PrOt] N001 where N001.ID_PrOt = t1.PROT) as [PROT]
					from tempdb..TEMP_B_PROT t1
					JOIN tempdb..TEMP_ONK_SL t2 on (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.n_ZAP=t2.N_ZAP)

					SELECT '[IES].[T_SCHET_USL_ONK]' as tableName, ' сведения об услуге при лечении онкологического заболевания ' as operationName, (select count(*) from tempdb..TEMP_ONK_USL) as tableCount

					SELECT 
					t1.SchetOnkUslID as [SchetUslOnkID]
					, t2.SchetSluchOnkID as [SchetSluchOnk]
					, (select top 1 N013.N013TreatTypeID from [IES].[T_N013_TREAT_TYPE] N013 where N013.ID_TLech = USL_TIP) as [USL_TIP]
					, (select top 1 N014.N014SurgTreatID from [IES].[T_N014_SURG_TREAT] N014 where N014.ID_THir = HIR_TIP) as [HIR_TIP]
					, (select top 1 N015.N015DrugTherapyLinesID from [IES].[T_N015_DRUG_THERAPY_LINES] N015 where N015.ID_TLek_L = LEK_TIP_L) as [LEK_TIP_L]
					, (select top 1 N016.N016DrugTherapyCyclesID from [IES].[T_N016_DRUG_THERAPY_CYCLES] N016 where N016.ID_TLek_V = LEK_TIP_V) as [LEK_TIP_V]
					, (select top 1 N017.N017RadiationTherapyTypesID from [IES].[T_N017_RADIATION_THERAPY_TYPES] N017 where N017.ID_TLuch = LUCH_TIP) as [LUCH_TIP]
					, nullif(PPTR,0) as PPTR
					from tempdb..TEMP_ONK_USL t1
					JOIN tempdb..TEMP_ONK_SL t2 on (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.n_ZAP=t2.N_ZAP)
									
					
				

				
					select newid() as ID, t0.IDCASE, t0.SL_ID, t0.REGNUM, t0.CODE_SH, Left(t0.rezult,Len(t0.rezult)-1) As rezult 
					into #temp_lek_pr
						From
						(
							Select distinct cast(t2.IDCASE as varchar(36)) as IDCASE, cast(t2.SL_ID as varchar(36)) as SL_ID, cast(t2.REGNUM as varchar(6)) as REGNUM, cast(t2.CODE_SH as varchar(10)) as CODE_SH,
								(
									Select cast(t1.DATE_INJ as varchar(40)) + ',' AS [text()]
									From tempdb..TEMP_LEK_PR_DATE_INJ t1
									Where (t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and t1.REGNUM = t2.REGNUM and t1.CODE_SH = t2.CODE_SH )
									ORDER BY t1.REGNUM, t1.CODE_SH
									For XML PATH ('')
								) rezult
							From tempdb..TEMP_LEK_PR_DATE_INJ t2
						) t0 	

				
			

					SELECT '[IES].[T_SCHET_USL_ONK_LEK_PR]' as tableName, ' сведения о лекарственных средствах' as operationName, (select count(*) from tempdb..TEMP_LEK_PR_DATE_INJ) as tableCount

					SELECT 
					ID as [SchetUslOnkLekPrID]
					--, t1.rezult as [DATE_INJ]
					, (select top 1 t2.N020OnkLekpID from IES.T_N020_ONK_LEKP t2 where t1.REGNUM = t2.ID_LEKP) as REGNUM
					,(select top 1 v024.V024DopKrID from ies.T_V024_DOP_KR v024 where t1.CODE_SH = v024.IDDKK) as [V024DopKr]
					, aa.SchetOnkUslID as [SchetUslOnk]
					, t1.REGNUM as N020_R
					from #temp_lek_pr t1
					cross apply (select top 1 * from tempdb..TEMP_ONK_USL t2 where t1.IDCASE = t2.IDCASE and t1.SL_ID = t2.SL_ID and (t2.USL_TIP = 2 or t2.USL_TIP = 4)) aa

	
					SELECT '[IES].[T_SCHET_USL_ONK_LEK_PR_DATE]' as tableName, ' сведения о датах лекарственных средств' as operationName, (select count(*) from tempdb..TEMP_LEK_PR_DATE_INJ) as tableCount

					select 
					newid() as [SchetUslOnkLekPrDateID]
					, t2.ID as [SchetUslOnkLekPr]
					, dateLek.DATE_INJ as [DATE_INJ]
					from tempdb..TEMP_LEK_PR_DATE_INJ dateLek
					join #temp_lek_pr t2 on (dateLek.IDCASE = t2.IDCASE and dateLek.SL_ID = t2.SL_ID and dateLek.REGNUM = t2.REGNUM and dateLek.CODE_SH = t2.CODE_SH)

					  
	
		

					SELECT '[IES].[T_SCHET_SLUCH_LEK_PR]' as tableName, ' Сведения о введенном лекарственном препарате' as operationName, (select count(*) from tempdb..TEMP_SLUCH_LEK_PR) as tableCount
          SELECT
                      t1.LekPrID as SchetSluchLekPrID
                     ,ts.SchetSluchID as SchetSluch
                     ,t1.DATA_INJ as DATA_INJ
                     ,(select top 1 v032.V032DrugGrRegID from ies.T_V032_DRUG_GR_REG v032 with(nolock) where v032.ScheDrugGrCd = t1.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)) as CODE_SH
                     ,(select top 1 n020.N020OnkLekpID from ies.T_N020_ONK_LEKP n020 with(nolock) where n020.ID_LEKP = t1.REGNUM and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2)) as REGNUM
                     ,t1.COD_MARK as COD_MARK
                     ,(select top 1 unit.EhrUnitTypeID from ies.T_EHR_UNIT_TYPE unit with(nolock) where unit.ID = t1.ED_IZM) as ED_IZM
                     ,t1.DOSE_INJ as DOSE_INJ
                     ,(select top 1 intro.EhrMetIntroID from ies.T_EHR_MET_INTRO intro with(nolock) where intro.ID = t1.METHOD_INJ) as METHOD_INJ
                     ,t1.COL_INJ as COL_INJ
          FROM tempdb..TEMP_SLUCH_LEK_PR t1
          JOIN tempdb..TEMP_SLUCH ts on ts.[SL_ID] = t1.[SL_ID] and t1.IDCASE = ts.IDCASE
          join tempdb..TEMP_Z_SLUCH tzs on ts.idcase = tzs.idcase and tzs.N_ZAP=ts.N_ZAP

		

		
	    SELECT '[IES].[T_SCHET_USL_MED_DEV]' as tableName, ' cведений о медицинских изделиях, имплантируемых в организм человека' as operationName, (select count(*) from tempdb..TEMP_USL_MED_DEV) as tableCount
    SELECT
    newid() as [SchetUslMedDevID]
    ,t1.[SchetUslID] as [SchetUsl]
    ,t0.DATE_MED as [DATE_MED]
    ,t0.[CODE_MEDDEV] as [CODE_MEDDEV]
    ,t0.[NUMBER_SER] as [NUMBER_SER]
    FROM tempdb..TEMP_USL_MED_DEV t0
    JOIN tempdb..TEMP_USL t1 ON t1.IDSERV = t0.IDSERV and t1.SL_ID = t0.SL_ID and t1.IDCASE = t0.IDCASE and t1.n_ZAP=t0.N_ZAP
	JOIN tempdb..TEMP_SLUCH ts on ts.[SL_ID] = t1.[SL_ID] and t1.IDCASE = ts.IDCASE
    join tempdb..TEMP_Z_SLUCH t2 on t1.IDCASE = t2.IDCASE and ts.n_zap=t2.N_zap


		
	end

	
			
--region output

DROP TABLE #Errors
DROP TABLE tempdb..TEMP_SCHET
DROP TABLE tempdb..TEMP_ZAP
DROP TABLE tempdb..TEMP_Z_SLUCH
DROP TABLE tempdb..TEMP_SLUCH
DROP TABLE tempdb..TEMP_USL
DROP TABLE tempdb..TEMP_SANK
DROP TABLE tempdb..TEMP_SANK_SL_ID
DROP TABLE tempdb..TEMP_CODE_EXP
DROP TABLE tempdb..TEMP_KSG_KPG
DROP TABLE tempdb..TEMP_CRIT
DROP TABLE tempdb..TEMP_SL_KOEF
DROP TABLE tempdb..TEMP_DS2_N
DROP TABLE tempdb..TEMP_DS2
DROP TABLE tempdb..TEMP_DS3
DROP TABLE tempdb..TEMP_CONS
DROP TABLE tempdb..TEMP_NAZ
DROP TABLE tempdb..TEMP_NAPR
DROP TABLE tempdb..TEMP_ONK_SL
DROP TABLE tempdb..TEMP_B_DIAG
DROP TABLE tempdb..TEMP_B_PROT
DROP TABLE tempdb..TEMP_ONK_USL
DROP TABLE tempdb..TEMP_LEK_PR_DATE_INJ
drop table tempdb..TEMP_SLUCH_LEK_PR
drop table tempdb..TEMP_USL_MED_DEV
drop table tempdb..TEMP_DOST
drop table tempdb..TEMP_COMENTSL
drop table tempdb..TEMP_COMENTU
drop table tempdb..TEMP_KSG_STOMAT
drop table tempdb..TEMP_CODE_MD 

if exists (
	select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..TEMP_COMENTS')
)

drop table tempdb..TEMP_COMENTS




if exists (
	select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#temp_lek_pr')
)
DROP TABLE #temp_lek_pr;
			
--	drop table #temp


END
GO
