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

CREATE PROCEDURE [dbo].[sp_GetRegionalFlkFiltersBulk_56]
 @type int
AS 
BEGIN

-- ФЛК второго уровня, проверка зависимостей невозможная с помощью XSD
declare @year int
declare @month int
declare @yearmonth int
declare @dschet date
declare @code_mo varchar(6)

declare @filename varchar(60) 
declare @q019type varchar(1)
declare @nschet varchar(30)
declare @fdom date
declare @ldom date
declare @finyear int

select top 1 
@year = s.[year]
,@month = s.[month]
,@yearmonth = s.[year] * 100 + s.[month]
,@dschet = s.dschet
,@filename = z.filename
,@nschet = s.nschet
,@fdom = convert(date, '01.' + right('0' + convert(varchar(2),@month),2) + '.' + convert(varchar(4), @year),104)
,@code_mo = s.code_mo
from tempdb..TEMP_SCHET s
join tempdb..TEMP_ZGLV z on 1 = 1

--возможно стоит финансовый год определять по открытому периоду счетов
set @finyear = (select max(year) from ies.T_SCHET_PERIOD)

set @ldom = EOMONTH(@fdom)

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),
	FDATE datetime
)

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

print 'Расчёт НаименованиеОПМП, ВидПомощи, ВидПомощиОПМП, на случае' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	UPDATE sl
	SET NameOPMP = dbo.SetSectionOPMP(z_sl.USL_OK, z_sl.IDSP, sl.PRVS, ISNULL(ksg_kpg.N_KSG, ksg_stomat.KSG_CODE), sl.DS1, sl.PROFIL_K
	, STUFF((
		SELECT ', ' + usl.VID_VME
		FROM tempdb..TEMP_USL usl
		WHERE usl.ID_PAC = SL.ID_PAC
		FOR XML path('')
		), 1, 1, '')
	, comentsl.Method, sl.Det, z_sl.DATE_Z_2)
	FROM tempdb..TEMP_Z_SLUCH z_sl
	JOIN tempdb..TEMP_SLUCH sl ON z_sl.idcase = sl.idcase
	LEFT JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
	JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	LEFT JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID

	update sl set VidPomOPMP=gr.Rasdel_OPMP, VidPomR=gr.Rasdel_OPMP, VidPomForCheck = gr.Rasdel_OPMP 
	from tempdb..TEMP_SLUCH sl
	join ies.R_NSI_OPMP_GROUPING gr on sl.NameOPMP = isnull(gr.Rasdel_OPMP,'') + isnull(gr.MP_type,'')
END

CREATE NONCLUSTERED INDEX [IX_ZSLType] ON tempdb..TEMP_Z_SLUCH
(
	ZSLType 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, FILLFACTOR = 90) ON [PRIMARY]

update tempdb..TEMP_Z_SLUCH set ZSLType = 'Стационар' where USL_OK = '1';
update tempdb..TEMP_Z_SLUCH set ZSLType = 'ДневнойСтационар' where USL_OK = '2';
update tempdb..TEMP_Z_SLUCH set ZSLType = 'Поликлиника' where USL_OK = '3';
--update tempdb..TEMP_Z_SLUCH set ZSLType = 'Стоматология' where USL_OK = '3' and IDSP = '25'; --new

update z_sl
set ZSLType = 'Стоматология' 
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.IDCASE = sl.IDCASE
where USL_OK = '3' and  VidPomOPMP = 'Z';

update tempdb..TEMP_Z_SLUCH set ZSLType = 'СкораяПомощь' where USL_OK = '4';

update tempdb..TEMP_USL set
CODE_USL = rtrim(isnull(CODE_USL,''))
--, VID_VME = rtrim(isnull(VID_VME,''))

update tempdb..TEMP_SLUCH set
IsGynecology = 0


--стоматологические доп поля
UPDATE usl
SET N_ZRequired =  stomat.Z  
	, StomUslType = null
	, StomUslRequired = stomat.EXIST
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join ies.R_NSI_STOMAT stomat on stomat.CODE = usl.VID_VME
and stomat.[START_DATE]<=sl.DATE_2 and (stomat.FINAL_DATE is null or stomat.FINAL_DATE>=sl.DATE_2)

UPDATE sl
SET StomRequiredUslCount = (
	select count(*)
	from tempdb..TEMP_USL usl 
	where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	and StomUslRequired = 1
)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase

--ссылка на метод
update comentsl set MethodId = method.DictionaryBaseID
from tempdb..TEMP_SLUCH sl
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join ies.R_NSI_METHODS method on method.CODE = isnull(comentsl.METHOD,'null')
and method.[START_DATE]<=sl.DATE_2 and (method.FINAL_DATE is null or method.FINAL_DATE>=sl.DATE_2)

--ссылка на PRICE_SZ 
update usl set PriceSzId = price_sz.DictionaryBaseID
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join ies.R_NSI_PRICE_SZ price_sz on usl.CODE_USL = price_sz.CODE
and price_sz.[START_DATE]<=sl.DATE_2 and (price_sz.FINAL_DATE is null or price_sz.FINAL_DATE>=sl.DATE_2)
where z_sl.USL_OK in (1,2)


print 'Создаём ссылку на R_NSI_SPECS и сопуствующие поля' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN

	update usl set SpecsID = specs.DictionaryBaseID
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	join ies.R_NSI_SPECS specs on usl.CODE_USL = specs.CODE
	and specs.[START_DATE]<=sl.DATE_2 and (specs.FINAL_DATE is null or specs.FINAL_DATE>=sl.DATE_2)
	where z_sl.USL_OK in (1,2)

	update sl set HasSpecsUsl = 1
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	where z_sl.USL_OK in (1,2)
	and exists (
		select 1 
		from tempdb..TEMP_USL usl
		where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
		and usl.SpecsId is not null
	)

	update usl set IsSession = 1
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	join ies.R_NSI_SPECS specs on specs.DictionaryBaseID = usl.SpecsId
	where usl.SpecsId is not null and specs.C_TYPE = 1
	
	update sl set HasSession = 1
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	where
	HasSpecsUsl = 1 or comentsl.METHOD = 'CB'

	update sl set SessionCount = (select count(*) from tempdb..TEMP_USL usl where usl.IsSession = 1)
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	where
	sl.HasSession = 1
END

print 'Заполняем признак исключения из проверок Профиль-Специальность-Пол/Возраст' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	update sl set ExcludeFromProfile_PRVS_Age_Check = 1
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	where 
	(
		sl.DS1 in ('U07.1', 'U07.2')
		or
		left(sl.DS1,3) in ('J12' ,'J13' ,'J14' ,'J15' ,'J16' ,'J17' ,'J18')
	)
	and @code_mo in ('560220' ,'560064' ,'560024' ,'560070' ,'560070' ,'560055' ,'560047' ,'560275')

	update sl set ExcludeFromProfile_PRVS_W_Check = 1
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	where 
	(
		sl.DS1 in ('U07.1', 'U07.2')
		or
		left(sl.DS1,3) in ('J12' ,'J13' ,'J14' ,'J15' ,'J16' ,'J17' ,'J18')
	)
	and @code_mo in ('560002' ,'560264' ,'560018' ,'560265')
END

print 'FK KSGN, KSGN_C' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	update usl set KSGNId = ksgn.DictionaryBaseID
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	join ies.R_NSI_KSGN ksgn on usl.VID_VME = ksgn.CODE
	and ksgn.[START_DATE]<=sl.DATE_2 and (ksgn.FINAL_DATE is null or ksgn.FINAL_DATE>=sl.DATE_2)
	where z_sl.USL_OK = 1
	and @q019type <> 'X'

	update usl set KSGN_CId = ksgn_c.DictionaryBaseID
	from tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	join ies.R_NSI_KSGN_C ksgn_c on usl.VID_VME = ksgn_c.CODE
	and ksgn_c.[START_DATE]<=sl.DATE_2 and (ksgn_c.FINAL_DATE is null or ksgn_c.FINAL_DATE>=sl.DATE_2)
	where z_sl.USL_OK = 2
END

--ссылку не создаю из-за 4 последовательных проверок
--если ссылка необходима, то можно заполнить, пока не ясно, нужна или нет
print 'Создаём ссылку PODR на DEPART' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
--BEGIN
	--update sl set PODRId = (
	--	select top 1 depart.DictionaryBaseId
	--	from ies.R_NSI_DEPART depart
	--	where depart.CODE_D = sl.PODR
	--	and depart.[START_DATE]<=sl.DATE_2 and (depart.FINAL_DATE is null or depart.FINAL_DATE>=sl.DATE_2
	--	and (depart.USL_OK is null or depart.USL_OK = z_sl.USL_OK)
	--	and @code_mo  
	--	)
	--)
	--from tempdb..TEMP_Z_SLUCH z_sl
	--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	--where
	--(z_sl.ZSLType = 'ДневнойСтационар'
	--or (z_sl.ZSLType = 'Поликлиника' and comentsl.METHOD = 'NA')
	--or (z_sl.ZSLType = 'Стационар' and z_sl.VIDPOM <> '32'))
--END

print 'Рассчёт CRIT на основе K_FR' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
update sl set K_FR_CRIT = 'fr01-05'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 1 and onk_sl.K_FR <= 5
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr06-07'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 6 and onk_sl.K_FR <= 7
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr08-10'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 8 and onk_sl.K_FR <= 10
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr11-20'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 11 and onk_sl.K_FR <= 20
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr21-29'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 21 and onk_sl.K_FR <=29
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr30-32'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 30 and onk_sl.K_FR <= 32
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')

update sl set K_FR_CRIT = 'fr33-99'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.idcase and onk_sl.SL_ID = sl.SL_ID
where
@q019type in ('C')
and onk_sl.K_FR >= 33
and exists (select 1 from  tempdb..TEMP_CRIT crit where crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID and crit.CRIT like 'fr%')
END

print 'Рассчёт MNN_LP_LTId, SHLTId, ADDIT_CRITId' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	update onk_usl_lek_pr
	set MNN_LP_LTId = (
		select top 1 addit_crit.DictionaryBaseID
		FROM IES.[R_NSI_MNN_LP_LT] addit_crit
		where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = onk_usl_lek_pr.CODE_SH 
		and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2)
	)
	, SHLTId = (
		select top 1 addit_crit.DictionaryBaseID
		FROM IES.[R_NSI_SHLT] addit_crit
		where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = onk_usl_lek_pr.CODE_SH 
		and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2)
	)
	, ADDIT_CRITId = (
		select top 1 addit_crit.DictionaryBaseID
		FROM IES.R_NSI_ADDIT_CRIT addit_crit
		where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = onk_usl_lek_pr.CODE_SH 
		and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2)
	)
	from tempdb..TEMP_Z_SLUCH z_sl
	JOIN tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
	JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on sl.idcase = onk_usl_lek_pr.idcase and sl.SL_ID = onk_usl_lek_pr.SL_ID
	where onk_usl_lek_pr.code_sh <> 'НЕТ'
END

print 'Поиск LEK_PR для CRIT' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
update sl set LEK_PR_Exists_FOR_CRIT = 1
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
where
exists (
	select 1
	from tempdb..TEMP_LEK_PR_DATE_INJ lek_pr 
	where sl.idcase = lek_pr.idcase and sl.SL_ID = lek_pr.SL_ID
	and crit.CRIT = lek_pr.CODE_SH
)
END

print 'Подсчёт MR_USL_NCount' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	update sl set MR_USL_NCount = (select count(*) from tempdb..TEMP_MR_USL_N mr where sl.idcase = mr.idcase and sl.SL_ID = mr.SL_ID)
	from tempdb..TEMP_SLUCH sl

	update usl set MR_USL_NCount = (select count(*) from tempdb..TEMP_MR_USL_N mr where usl.idcase = mr.idcase and usl.SL_ID = mr.SL_ID and usl.IDSERV = mr.IDSERV)
	from tempdb..TEMP_USL usl
END

print 'Определяем есть ли услуга из KSGN или KSGN_C для проверки ECR.368 ' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
update sl set
NA001Count = (
	select sum(usl.KOL_USL)
	from tempdb..TEMP_USL usl 
	where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	and isnull(CODE_USL,'') in ('NA001')
)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.USL_OK = 3
and @q019type <> 'X'

update sl set
DialysisCount = (
	select sum(usl.KOL_USL)
	from tempdb..TEMP_USL usl 
	where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	and isnull(CODE_USL,'') in ('A18.05.002', 'A18.05.011', 'A18.05.002.002', 'A18.05.002.001')
)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.USL_OK = 3
and @q019type <> 'X'

update sl set KsgStomatCount = (select count(*) from tempdb..TEMP_KSG_STOMAT ksg_stomat where sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.ZSLType in ('Стоматология')

update sl set IsGynecology = 1
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
z_sl.ZSLType in ('Поликлиника')
and sl.PRVS in (2,207) 
and pers.W = 2

print 'Ссылки на федеральные справочники' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
update z_sl set RSLT_DId = (select top 1 V017DispResultID from [IESDB].[IES].[T_V017_DISP_RESULT] v017 where z_sl.RSLT_D = v017.IDDR)
from tempdb..TEMP_Z_SLUCH z_sl 
where @q019type = 'X'
END

print 'Расчёт __ВидПомощиДляПроверки на случае' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	update sl
	set VidPomForCheck = 'D'
	FROM tempdb..TEMP_Z_SLUCH z_sl
	JOIN tempdb..TEMP_SLUCH sl ON z_sl.idcase = sl.idcase
	JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
	where z_sl.zsltype = 'Поликлиника' and method.FIN_TYPE = 2

	update sl
	set VidPomForCheck = 'E'
	FROM tempdb..TEMP_Z_SLUCH z_sl
	JOIN tempdb..TEMP_SLUCH sl ON z_sl.idcase = sl.idcase
	JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
	JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
	where z_sl.zsltype = 'Поликлиника' and method.FIN_TYPE = 3
	
	update sl
	set VidPomForCheck = 'U'
	FROM tempdb..TEMP_SLUCH sl
	where VidPomForCheck = 'P'
END

print 'Расчёт флагТребуетсяONK_SL, МКБ_ЗНО' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	UPDATE sl
	set HasOnkDiag = 1
	FROM tempdb..TEMP_SLUCH sl
	where 
	DS1 like 'C%'
	or (left(DS1,3) >= 'D00' and left(DS1,3) <= 'D09')
	or (left(DS1,3) >= 'D45' and left(DS1,3) <= 'D47')

	UPDATE sl
	set ONK_SLRequired = 1
	FROM tempdb..TEMP_Z_SLUCH z_sl
	JOIN tempdb..TEMP_SLUCH sl ON z_sl.idcase = sl.idcase
	where 
	HasOnkDiag = 1
	and @q019type <> 'X'
	and z_sl.ZSLType <> 'Стоматология'
	and z_sl.ZSLType <> 'СкораяПомощь'
	and sl.REAB <> 1
	and sl.DS_ONK <> 1
END


print 'SpecIndependNoReab Код специалиста ведущего самостоятельный приём, не реаблитация' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
update mr_usl_n set SpecIndependNoReab = (
	select top 1 nsi.DictionaryBaseId from ies.R_NSI_SPEC_INDEPEND nsi 
	where mr_usl_n.PRVS = nsi.CODE_SPEC
	and isnull(nsi.IS_REAB,0) = 0
)
from  tempdb..TEMP_MR_USL_N mr_usl_n

update sl set SpecIndependNoReabCount = (
	select count(*)
	from tempdb..TEMP_MR_USL_N mr_usl_n
	where sl.idcase = mr_usl_n.idcase and sl.SL_ID = mr_usl_n.SL_ID and SpecIndependNoReab is not null
)
from  tempdb..TEMP_SLUCH sl

END

print 'PERS.FIO' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	UPDATE pers
	set FIO = iif(isnull(FAM,'')='','',FAM +' ') + iif(isnull(IM,'')='','',IM +' ') + isnull(OT, '')
	FROM tempdb..TEMP_PERS pers
END

print 'PERS.FIO' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	UPDATE sl
	set Age18 = 0
	FROM tempdb..TEMP_SLUCH sl

	UPDATE sl
	set Age18 = 1
	FROM tempdb..TEMP_SLUCH sl
	JOIN tempdb..TEMP_PERS pers on sl.ID_PAC = pers.ID_PAC
	where dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
	
	UPDATE sl
	set VD_5_6 = 6
	FROM tempdb..TEMP_SLUCH sl
	where Age18 = 1

	UPDATE sl
	set VD_5_6 = 5
	FROM tempdb..TEMP_SLUCH sl
	where Age18 = 0
END

print 'Расчёт KD_Z_Calc' + ' ' + convert(varchar(10), getdate(), 104) + ' ' + convert(varchar(8), getdate(), 114)
BEGIN
	UPDATE sl
	set KD_Z_Calc = dbo.DateTimeDiff('day',sl.DATE_1,sl.DATE_2)
	FROM tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.IDCASE = sl.IDCASE
	where z_sl.USL_OK = 1

	UPDATE sl
	set KD_Z_Calc = 1
	FROM tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.IDCASE = sl.IDCASE
	where z_sl.USL_OK = 1 and KD_Z_Calc = 0

	UPDATE sl
	set KD_Z_Calc = dbo.DateTimeDiff('day',sl.DATE_1,sl.DATE_2)+1
	FROM tempdb..TEMP_Z_SLUCH z_sl
	join tempdb..TEMP_SLUCH sl on z_sl.IDCASE = sl.IDCASE
	where z_sl.USL_OK = 2
END


--old_flk begin
--1273732D-32BC-4BFE-973B-278334CD84F4	ECR.069 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.069'
, 'Метод оплаты 4.3.1 и 4.3.2 Допустим  только для МО : 560002/560035/560214 /560264'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
t2.[METHOD] in ('4.3.1', '4.3.2')
and t0.LPU in ('560002','560035','560214','560264')
--1273732D-32BC-4BFE-973B-278334CD84F4	ECR.069 END

/* --- Закрыта 01.01.2024, заменина ECR.408
--5F696CA5-FFFC-46FF-94BA-F4B49B772B1E 65 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FIN_TYPE', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '65'
, 'Не подлежат оплате в рамках   (FIN_TYPE = 2) случаи  в ГБУЗ «ОДКБ» (560220) по специальностям:  3/9/10/36/38/59/91'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
t2.FIN_TYPE in (2)
and t0.LPU in ('560220')
and t1.PRVS in ('3','9','10','36','38','59','91')
--5F696CA5-FFFC-46FF-94BA-F4B49B772B1E 65 END
*/
--0F8CF28C-B946-4BCA-9C33-15BF3DDF9327 ECR.461 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '101'
, 'Метод оплаты СА должен соответствовать профилю 137 и виду помощи 31/13'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('СА')
and (t0.VIDPOM <> '31' or t0.VIDPOM <> '13' or t1.PROFIL <> '137')
--0F8CF28C-B946-4BCA-9C33-15BF3DDF9327 ECR.461 END

--F8036816-4E6C-4C2E-8530-03ED783CC2A9 ECR.012 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, t2.IDSERV as IDSERV, 'ECR.012'
, 'Случаи СМП 560109 с кодом услуги 02003 не принимать позиции у которых код профиля != 5 или код вида медицинской помощи  != 2"'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_USL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t0.USL_OK in (4)
and t0.LPU in ('560109')
and t2.CODE_USL in ('02003')
and (t2.PROFIL  != '5'  or  VIDPOM != '2' )
--F8036816-4E6C-4C2E-8530-03ED783CC2A9 ECR.012 END

--ACCEE22D-F895-4D8A-8F1A-81E2D9437A0D ECR.102 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, t3.IDSERV as IDSERV, 'ECR.102'
, 'Метод оплаты DA код 003 только для 560007 /560008 (для случаев с 03.07.2023)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_USL t3 with(nolock) on t3.idcase = t0.idcase and t3.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t3.CODE_USL = 'DA003'
and t0.LPU not in('560007', '560008')
--ACCEE22D-F895-4D8A-8F1A-81E2D9437A0D ECR.102 END

--4B886737-91F2-40F5-84CB-463852FA43A5 ECR.144 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.144'
, 'Не принимать случаи с методом оплаты AF у 560238'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('AF')
and t0.LPU in('560238')
--4B886737-91F2-40F5-84CB-463852FA43A5 ECR.144 END

--C7F82877-E6AD-4C8E-BACE-25641AFDDA05 ECR.416 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.416'
, 'COVID: для МО = 560253 допустимо выставление случаев КС только с КСГ =st12.019.'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t0.LPU = '560253'
and t2.N_KSG not like 'st12.019%'
--C7F82877-E6AD-4C8E-BACE-25641AFDDA05 ECR.416 END

--C5BF9253-7054-4908-933D-7E166812078A 107 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '107'
, 'Код МКБ DS1 должен соответствовать справочнику МКБ10 = код ОМС = самостоятельный код'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
LEFT JOIN ies.T_MKB t2 with(nolock) on t2.MCBCode = t1.ds1
where 
--@q019type in ('H')
t2.MCBCode is null
--C5BF9253-7054-4908-933D-7E166812078A 107 END

--B5810FD7-71FE-42DF-B151-875F9E757558 ECR.406 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.406'
, 'Отклонять случай с методом оплаты 2.1 / 2.2  при условии FOR_POM = 3 "плановая помощь"'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('2.1', '2.2')
and t0.FOR_POM = 3
--B5810FD7-71FE-42DF-B151-875F9E757558 ECR.406 END

--72F9C1C0-7773-4EAE-8A08-0C0D4E2EACF0 ECR.003 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.003'
, 'МО 560261 может выставлять только ксг  Группы st12.019 ,    или КСГ st12.015/st12.016/st12.017/st12.0182. st12.015/st12.016/st12.017/st12.018 можно выставлять только при налии     DS2 относящ. к группе F'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t0.LPU = '560261'
and t0.USL_OK = 1
and (
	(left(t2.N_KSG,8) not in ('st12.019' , 'st12.015' ,'st12.016' ,'st12.017' ,'st12.018' ) )
	or
	(Left(t2.N_KSG,8) in ( 'st12.015' ,'st12.016' ,'st12.017' ,'st12.018' ) 
	and not exists(select top 1 1 from tempdb..TEMP_DS2 ds2 where ds2.idcase = t0.idcase and ds2.SL_ID = t1.SL_ID and ds2.[DS2] like 'F%'))
)
--72F9C1C0-7773-4EAE-8A08-0C0D4E2EACF0 ECR.003 END


--38D7B6A7-AA6F-43AE-812D-918A11B3D19E ECR.418 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '115'
, 'Отклонять КСГ ds02.007 с DS1 не равно O04-O06, O02'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.N_KSG in ('ds02.007')
and not (
	t1.DS1 like 'O02%'
	or (t1.DS1 >= 'O04' and t1.DS1 < 'O07')
)
--38D7B6A7-AA6F-43AE-812D-918A11B3D19E ECR.418 END

----4A273509-2974-4566-8952-0AC156387FE1 ECR.066 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.066'
--, 'Метод оплаты 3.3.1 обязательное заполнение кол-во недель'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
--where
----@q019type in ('H')
--t2.[METHOD] in ('3.3.1')
--and t2.GESTATION is null
----4A273509-2974-4566-8952-0AC156387FE1 ECR.066 END

----05EB1486-45FB-4149-B2A3-E3C8630D54B1 ECR.067 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.067'
--, 'Поле GESTATION (Срок беременности в неделях) заполняется ТОЛЬКО для метода оплаты 3.3.1 '
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
--where
----@q019type in ('H')
--t2.[METHOD] not in ('3.3.1')
--and t2.GESTATION is not null
----05EB1486-45FB-4149-B2A3-E3C8630D54B1 ECR.067 END


--5C7ABB42-BBB3-44F6-AAB3-1CE59007EBA7 ECR.221 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.221'
, 'По результатам 2 этапа углубленной диспансеризации не может быть 1 группы здоровья'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_SLUCH t1 on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId = t0.RSLT_DId
where
t0.RSLT_DId is not null
and t2.[METHOD] like '6.4%'
and v017.GZ = '1'
and @q019type in ('X')
--5C7ABB42-BBB3-44F6-AAB3-1CE59007EBA7 ECR.221 END

--A7965C66-2DAD-4258-BA81-310A9DA96C0B ECR.486 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '129'
, 'Отклонять st38.001 при условии профиль койки не равно 16'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.N_KSG in ('st38.001')
and t1.PROFIL_K <> 16
--A7965C66-2DAD-4258-BA81-310A9DA96C0B ECR.486 END

--7FF0E977-5E4B-423C-892D-965697579561 ECR.409 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.409'
, 'Метод оплаты 4.5.1. возраст 40 лет и более'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_ZAP sz with(nolock) on sz.N_ZAP = t0.N_ZAP
join tempdb..TEMP_PERS tp with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),tp.ID_PAC)
where
--@q019type in ('H')
t2.[METHOD] in ('4.5.1')
and dbo.DateTimeDiff('year',tp.dr,t1.date_2) < 40
--7FF0E977-5E4B-423C-892D-965697579561 ECR.409 END

--D0C7C8FD-9B33-470B-96B8-990DB1A5965C ECP.002 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NZ', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECP.002'
, 'Значение элемента  NZ (Номер зуба ) не соответствует шаблону [1-4][1-8] и [5-8][1-5]'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_COMENTU t1 with(nolock) on t1.idcase = t0.idcase
where
--@q019type in ('H')
t1.NZ NOT LIKE '[1-4][1-8]' AND NZ NOT LIKE '[5-8][1-5]' and t1.NZ is not null
--D0C7C8FD-9B33-470B-96B8-990DB1A5965C ECP.002 END

--2E07A8DA-1BB8-47C8-99A2-2DD3235804E6 ECP.003 BEGIN

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TAL_NUM', cast(TAL_NUM as varchar(200)) as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECP.003'
, 'Значение элемента  TAL_NUM (Номер зуба ) в соответствии с приказом Минздрава России от 30.01.2015 № 29н 01.04.2020 г не соответствует шаблону [0-9][0-9].[0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9]'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where
--@q019type in ('H')
t1.TAL_NUM NOT LIKE '[0-9][0-9].[0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9]' and t1.TAL_NUM is not null
--2E07A8DA-1BB8-47C8-99A2-2DD3235804E6 ECP.003 END

--D0C1AAF7-F5FF-4B46-86A0-96CAFFC22584 ECR.243 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.243'
, 'Для случаев DATE_2 > 28,02,2023  P_CEL = "1.3" и не (МетодОплаты= "3.1"    или  МетодОплаты = "8."  или  МетодОплаты = "4.6"  )'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t1.P_CEL = '1.3'
and not (t2.[METHOD] in ('3.1', '4.6') or t2.METHOD like '8.%')
and t1.DATE_2 > convert(date,'28.02.2023',104)
--D0C1AAF7-F5FF-4B46-86A0-96CAFFC22584 ECR.243 END

--C9AB7E60-0D17-406E-861E-58FA87A3C9C8 ECR.244 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.244'
, 'Для случаев DATE_2 > 28.02.2023  P_CEL <> "1.3" и (МетодОплаты= "3.1"    или  МетодОплаты = "8."  или  МетодОплаты = "4.6"  )'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t1.P_CEL <> '1.3'
and (t2.[METHOD] in ('3.1', '4.6') or t2.METHOD like '8.%')
and t1.DATE_2 > convert(date,'28.02.2023',104)
--C9AB7E60-0D17-406E-861E-58FA87A3C9C8 ECR.244 END

--88A8DE6A-A9C7-4CD1-B6DB-1B15AADCC615 ECR.505 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.505'
, 'код МКБ E30.1 возможен только для женского пола'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_ZAP sz with(nolock) on sz.N_ZAP = t0.N_ZAP
join tempdb..TEMP_PERS tp with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),tp.ID_PAC)
where
--@q019type in ('H')
t1.DS1 = 'E30.1'
and tp.W <> 2
--88A8DE6A-A9C7-4CD1-B6DB-1B15AADCC615 ECR.505 END

--3C548A46-9257-465F-BF7E-363CF9E5A009 ECR.506 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.506'
, 'Методы оплаты 1.3* могут подавать только МО={560007, 560008}'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] like '1.3%'
and t0.lpu not in ('560007', '560008')
--3C548A46-9257-465F-BF7E-363CF9E5A009 ECR.506 END

--B3139B14-4011-49CB-8295-83C3ADA80D2F ECR.507 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.507'
, 'Метод оплаты 4.8 может подавать только МО=560023'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('4.8')
and t0.lpu not in ('560023')
--B3139B14-4011-49CB-8295-83C3ADA80D2F ECR.507 END

--03041F51-375A-4E33-9AB3-617F5D55496E EMF.004 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_STOMAT', @NSCHET as ZN_POL ,@NSCHET, 'KSG_STOMAT', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.004'
, 'Для стоматологии, обязательно  использовать КСГ (KSG_STOMAT)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
LEFT JOIN tempdb..TEMP_KSG_STOMAT t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t0.USL_OK = '3' and  t0.IDSP = '25' and t2.idcase is null
--03041F51-375A-4E33-9AB3-617F5D55496E EMF.004 END

--CF495096-E31B-42BD-9179-9B6D126F9A51 ECR.001 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, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.001'
, 'Файлы с ПЦР предоставляются только определёнными МО (560014, 560023, 560035, 560101, 560144, 560220, 560214, 560001, 560268, 560101)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('AP')
and t0.LPU not in ('560014', '560023', '560035', '560101', '560144', '560220', '560214', '560001', '560268', '560101')
--CF495096-E31B-42BD-9179-9B6D126F9A51 ECR.001 END

--B4251172-422D-4312-8FCE-61B98DE9FE22 ECR.014 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', TempInt as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.014'
, 'Для метода оплаты 1.2 услуги (USL) должны содержать более 1-го специалиста(PRVS)/услуги(VID_VME)'
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_SLUCH t1 on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('1.2')
and t0.ID_PAC in (
	select ID_PAC
	from (
			select u.ID_PAC , un.PRVS , u.VID_VME  
			from tempdb..TEMP_USL u 
			join tempdb..TEMP_MR_USL_N un on u.IDSERV = un.IDSERV
			where u.ID_PAC in 
				(
					select t100.ID_PAC from 
					tempdb..TEMP_SLUCH t100
					JOIN tempdb..TEMP_COMENTSL t101 on t100.idcase = t101.idcase and t100.SL_ID = t101.SL_ID
					where t101.[METHOD] in ('1.2')
				)
			group by u.ID_PAC , un.PRVS , u.VID_VME
		) as tab 
		group by ID_PAC
		HAVING COUNT(*) <2
)
--B4251172-422D-4312-8FCE-61B98DE9FE22 ECR.014 END

--D4AED5EB-CA83-4587-9A39-6F50B8283CBF ECR.065 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.065'
, 'Отклонять случай с методом оплаты 1.2, предъявленный МО ≠ 560001/560220/560264/560007/560035/560214/560255'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('1.2')
and t0.LPU not in ('560001','560220','560264','560007','560035','560214','560255')
--D4AED5EB-CA83-4587-9A39-6F50B8283CBF ECR.065 END

--973DFE3E-F17A-489E-9BF4-55F6DAC58DB9 ECR.407 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.407'
, 'Для метода оплаты 1.1. форма оказания медицинской помощи только плановая (= 3)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('1.1')
and t0.FOR_POM <> 3
--973DFE3E-F17A-489E-9BF4-55F6DAC58DB9 ECR.407 END

--C40BE769-1CE6-4829-9BD1-BF6604F77B07 79 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '79'
, 'Отклонять случай с кодом услуги A.11.12.003.002  при МКБ≠ I21.0., I21.1., I21.2., I21.3., I21.4., I22.8.0:'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_USL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.CODE_USL = 'A.11.12.003.002'
and t1.ds1 not like ('I21.0.%')
and t1.ds1 not like ('I21.1.%')
and t1.ds1 not like ('I21.2.%')
and t1.ds1 not like ('I21.3.%')
and t1.ds1 not like ('I21.4.%')
and t1.ds1 not like ('I22.8.0')
--C40BE769-1CE6-4829-9BD1-BF6604F77B07 79 END

--89C9FD9A-69B6-4B2B-8C20-03651BB95735 ECR.068 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.068'
, 'Метод оплаты 4.4 Допустим  только для МО : 560002/560264'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('4.4')
and t0.LPU not in ('560002', '560264')
--89C9FD9A-69B6-4B2B-8C20-03651BB95735 ECR.068 END

--A6758E62-04A4-4EDD-8738-C2C73DCA4F90 ECR.063 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '61'
, 'Метод оплаты 4.1.1 и 4.1.2 Допустим  только для МО : 560018/560002/560231/560229/560265/560264'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('4.1.1', '4.1.2')
and t0.LPU not in ('560018','560002','560231','560229','560265','560264')
--A6758E62-04A4-4EDD-8738-C2C73DCA4F90 ECR.063 END

--01368524-56B8-4A4D-BB3C-18B6B1A5C7CA ECR.405 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', t1.DS1 as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.405'
, 'В случае оказания неотложной помощи (FOR_POM =2)  диагноз  не может быть из класса Z'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where
--@q019type in ('H')
t0.FOR_POM = 2
and left(t1.DS1,1) = 'Z'
and t0.ZSLType = 'Поликлиника'
--01368524-56B8-4A4D-BB3C-18B6B1A5C7CA ECR.405 END

--441E90BF-3C39-4510-8C8A-E053A9EC6C3A 146 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '146'
, 'Отклонять случай оказания медицинской помощи по реабилитации в условиях поликлиники, дневного и круглосуточного стационара при условии МОнаправления = МОоказания для следующих МО:560263,560239,560177,560091"'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where
--@q019type in ('H')
t0.LPU in ('560263','560239','560177','560091')
and isnull(t0.NPR_MO,'')=t0.LPU and t1.profil=158
--441E90BF-3C39-4510-8C8A-E053A9EC6C3A 146 END

--E49C3144-A05F-4F90-AD0E-6C8E46B3840D 141 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, '141'
, 'Отклонять случай с методом оплаты 6.1. и возраст меньше или равно 39 лет и меньше 18лет'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_ZAP sz with(nolock) on sz.N_ZAP = t0.N_ZAP
join tempdb..TEMP_PERS tp with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),tp.ID_PAC)
where
--@q019type in ('H')
t2.[METHOD] in ('6.1')
and DATEDIFF(YEAR,tp.dr,t1.date_2)+(SIGN(DATEDIFF(DAY,tp.dr,DATEADD(YEAR,YEAR(tp.dr)-YEAR(t1.date_2),t1.date_2)))-1)/2 not between 18 and 39
--E49C3144-A05F-4F90-AD0E-6C8E46B3840D 141 END

--DB0B824B-692C-490A-81D8-9C2FCB2E0BC8 ECR.011 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', @NSCHET as ZN_POL ,@NSCHET, 'USL', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.011'
, 'Не допустим перитониальный диализ:... более 1 раза в день:  ... в файле случаев  раздел USL! '
from 
(select usl.ID_PAC ,DATE_IN , CODE_USL from tempdb..TEMP_USL usl where CODE_USL in ('A18.30.001','A18.30.001.001','A18.30.001.002') GROUP BY  usl.ID_PAC ,  DATE_IN, CODE_USL   HAVING COUNT(*)>1)aa
--DB0B824B-692C-490A-81D8-9C2FCB2E0BC8 ECR.011 END

--25AB971A-932A-4D62-92A4-E3A689EA08AD ECR.012 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', t2.CODE_USL as ZN_POL ,@NSCHET, 'USL', null, t1.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.012'
, 'Для методы оплаты AF услуги (USL) должны содержать только один код исследования (CODE_USL) в файле случаев  раздел USL'
FROM tempdb..TEMP_SLUCH t1
join tempdb..TEMP_USL t2 on t2.idcase = t2.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_COMENTSL t3 on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
WHERE t3.METHOD = 'AF'
GROUP BY t1.ID_PAC, t2.CODE_USL
HAVING COUNT(*) > 1
--25AB971A-932A-4D62-92A4-E3A689EA08AD ECR.012 END

--3FBC9EE2-3C8F-4CD0-B946-2E855FBBDDF6 ECR.013 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.013'
, 'Для методы оплаты AF услуги (USL) KOL_USL не должен превышать 1'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_USL t3 with(nolock) on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('AF')
and t3.KOL_USL>1
--3FBC9EE2-3C8F-4CD0-B946-2E855FBBDDF6 ECR.013 END

--EEB5C057-5133-4206-9F4A-55734747FC10 ECR.015 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', null as ZN_POL ,@NSCHET, 'SL', null, t0.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.015'
, 'Для метода оплаты 3.1 услуги (USL) должны содержать минимум  1-го специалиста(PRVS)/услуги(VID_VME)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_USL t3 with(nolock) on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_MR_USL_N t4 with(nolock) on t3.idcase = t4.idcase and t3.SL_ID = t4.SL_ID and t3.IDSERV = t4.IDSERV
where t2.[METHOD] in ('3.1')
group by t0.ID_PAC , t4.PRVS , t3.VID_VME
HAVING COUNT(*) < 1
--EEB5C057-5133-4206-9F4A-55734747FC10 ECR.015 END

--6E7534FD-D424-4589-9A5A-0DED59667007 ECR.027 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_STOMAT', @NSCHET as ZN_POL ,@NSCHET, 'KSG_STOMAT', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.027'
, 'Проверка KSG_STOMAT_ID на уникальность KSG_STOMAT'
FROM (select t1.ID_PAC , KSG_STOMAT_ID  from tempdb..TEMP_KSG_STOMAT t0 join tempdb..TEMP_COMENTSL t1 on  t0.SL_ID=t1.SL_ID and t0.IDCASE = t1.IDCASE GROUP BY t1.ID_PAC ,KSG_STOMAT_ID   HAVING COUNT(*)>1) aa
--6E7534FD-D424-4589-9A5A-0DED59667007 ECR.027 END

--08C2646D-FCAF-4C2E-97D9-65DD882D60A3 ECR.029 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_STOMAT', @NSCHET as ZN_POL ,@NSCHET, 'KSG_STOMAT', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.029'
, 'Проверка KSG_CODE, NZ на уникальность'
FROM (select t1.ID_PAC , KSG_CODE , NZ from tempdb..TEMP_KSG_STOMAT t0 join tempdb..TEMP_COMENTSL t1 on t0.SL_ID=t1.SL_ID and t0.IDCASE = t1.IDCASE GROUP BY t1.ID_PAC , KSG_CODE , NZ   HAVING COUNT(*)>1) aa
--08C2646D-FCAF-4C2E-97D9-65DD882D60A3 ECR.029 END

--5B6CD8AB-E558-41F7-B883-3550F981FF46 ECR.030 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MR_N', @NSCHET as ZN_POL ,@NSCHET, 'MR_USL_N', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.030'
, 'Проверка сочетания  ID_PAC , MR_N ,IDSERV на уникальность'
FROM (select t1.ID_PAC , MR_N ,IDSERV  from tempdb..TEMP_MR_USL_N mr join tempdb..TEMP_SLUCH t1 on  mr.SL_ID=t1.SL_ID and mr.IDCASE = t1.IDCASE  GROUP BY t1.ID_PAC ,MR_N  ,IDSERV  HAVING COUNT(*)>1) aa
--5B6CD8AB-E558-41F7-B883-3550F981FF46 ECR.030 END

--C754DD63-AF8C-4BC6-9DA4-2B0AA0EE8573 ECR.025 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_TIP', @NSCHET as ZN_POL ,@NSCHET, 'ONK_USL', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.025'
, 'Проверка на дубли USL_TIP в ONK_USL'
FROM (select t1.ID_PAC , USL_TIP from tempdb..TEMP_ONK_USL mr join tempdb..TEMP_SLUCH t1 on  mr.SL_ID=t1.SL_ID and mr.IDCASE = t1.IDCASE  GROUP BY ID_PAC ,USL_TIP HAVING COUNT(*)>1) aa
--C754DD63-AF8C-4BC6-9DA4-2B0AA0EE8573 ECR.025 END

--357138FB-34AA-4C78-9E69-3258A49772A4 ECR.026 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_TIP', @NSCHET as ZN_POL ,@NSCHET, 'ONK_USL', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.026'
, 'Проверка наличия USL_TIP = 5 и USL_TIP не равен 5'
FROM (
select tab_1.ID_PAC as ID_PAC from 
(select 
t1.ID_PAC from 
tempdb..TEMP_ONK_USL t0
join tempdb..TEMP_SLUCH t1 on  t0.SL_ID=t1.SL_ID and t0.IDCASE = t1.IDCASE
where USL_TIP = '5') as tab_1 
inner join 
(select 
t1.ID_PAC from 
tempdb..TEMP_ONK_USL t0
join tempdb..TEMP_SLUCH t1 on  t0.SL_ID=t1.SL_ID and t0.IDCASE = t1.IDCASE
where USL_TIP <> '5') as tab_2 
ON convert(varchar(36),tab_1.ID_PAC) = convert(varchar(36),tab_2.ID_PAC)
) aa
--357138FB-34AA-4C78-9E69-3258A49772A4 ECR.026 END

--DB0283CE-2825-406A-AE20-ED540C482828 ECR.006 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.006'
, 'Не заполнена ШРМ (поле REAB) при PROFIL = 158 и КСГ по реабилитации'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_COMENTSL t3 with(nolock) on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
where 
(isnull(REAB,0) = 0)
and (PROFIL =  158)
and (
		(USL_OK= 1 and t2.N_KSG like 'st37.%') 
		or (USL_OK = 2 and t2.N_KSG like 'ds37.%')
		or (USL_OK = 3 and (t3.METHOD  like '1.3.%' or t3.METHOD  like '7.%'))
	)
--DB0283CE-2825-406A-AE20-ED540C482828 ECR.006 END

--E79C5C71-14F3-468F-B6FA-95FB04DC4A4A ECR.006 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REAB', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.006'
, 'ШРМ заполнен правильно, но PROFIL не равен  158'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_COMENTSL t3 with(nolock) on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
where 
(isnull(REAB,0) = 1)
and (PROFIL !=  158)
--E79C5C71-14F3-468F-B6FA-95FB04DC4A4A ECR.006 END

--144E10F1-160D-498B-B50E-8CB20F173B9F ECR.006 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REAB', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.006'
, 'Поле REAB не нулевой, но КСГ не соответствует USL_OK'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_COMENTSL t3 with(nolock) on t3.idcase = t1.idcase and t3.SL_ID = t1.SL_ID
where 
(isnull(REAB,0) = 1)
and (PROFIL =  158)
and (
		(USL_OK= 1 and t2.N_KSG not like 'st37.%') 
		or (USL_OK = 2 and t2.N_KSG not like 'ds37.%')
		or (USL_OK = 3 and not (t3.METHOD  like '1.3.%' or t3.METHOD  like '7.%'))
	)
--144E10F1-160D-498B-B50E-8CB20F173B9F ECR.006 END

--0B927E61-87DE-4EFA-9D68-58DA6F2B4D09 ECR.514 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDDOKT', @NSCHET as ZN_POL ,@NSCHET, 'SL', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.514'
, 'Для методов оплаты 1.3,1.3.1 кол-во различных специальностей должно быть не менее 2 и врачей - не менее 3,RS.Fields("ФИОДР").Value, RS.Fields("IDDOKT").Value)'
FROM
(select zs.ID_PAC,t2.method, IDDOKT, tp.FAM, tp.IM, tp.OT
from tempdb..TEMP_SLUCH t0
join tempdb..TEMP_Z_SLUCH zs on zs.idcase=t0.idcase
join tempdb..TEMP_ZAP sz with(nolock) on sz.N_ZAP = zs.N_ZAP
join tempdb..TEMP_PERS tp with(nolock) on convert(varchar(36),zs.ID_PAC) = convert(varchar(36),tp.ID_PAC)
left JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = zs.idcase and t2.SL_ID = t0.SL_ID
where substring(t2.method,1,3)='1.3'
and zs.ID_PAC IN 
( select u.ID_PAC from tempdb..TEMP_USL u 
left join tempdb..TEMP_MR_USL_N un on u.IDSERV = un.IDSERV and u.SL_ID = un.SL_ID and u.IDCASE = un.IDCASE
group by u.ID_PAC
having count(distinct isnull(un.PRVS,u.PRVS))<2 or count(distinct isnull(un.CODE_MD, u.CODE_MD))<3)
)aa
--0B927E61-87DE-4EFA-9D68-58DA6F2B4D09 ECR.514 END

----0ED06EF9-6361-436A-95A9-AC9335081B00 ECR.518 BEGIN

--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'IDDOKT', @NSCHET as ZN_POL ,@NSCHET, 'SL', null, aa.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.518'
--, 'Для методов оплаты AA,АВ,AD услуга анестезии отдельно не подается", RS.Fields("ФИОДР").Value, RS.Fields("IDDOKT").Value)'
--FROM
--(
--select zs.ID_PAC, t2.METHOD, IDDOKT--, ФИОДР
--from tempdb..TEMP_SLUCH t0
--join tempdb..TEMP_Z_SLUCH zs on zs.idcase=t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = zs.idcase and t2.SL_ID = t0.SL_ID
--where t2.method in ('AA', 'AB', 'AD')
--and zs.ID_PAC in 
--(select u.ID_PAC from tempdb..TEMP_USL u
--join tempdb..TEMP_USL an on u.ID_PAC=an.ID_PAC and an.code_usl like 'B01.003.004%'
--group by u.ID_PAC
--having count(*)=1))aa
----0ED06EF9-6361-436A-95A9-AC9335081B00 ECR.518 END

--DCA99CF2-2701-4184-8417-2FBCB9F1DA62 ESI.001 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,  'ESI.001'
, 'Недопустимый вид помощи (VIDPOM) для ВМП)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('T')
and t0.VIDPOM not in (32)
--DCA99CF2-2701-4184-8417-2FBCB9F1DA62 ESI.001 END

--A520665F-93E5-4FFA-86C5-E371A0071FE0 ESI.008 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,  'ESI.008'
, 'Пакеты файлов H, D, C, R не могут содержать VIDPOM=32)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where
@q019type in ('C', 'D', 'H', 'R')
and t0.VIDPOM in (32)
--A520665F-93E5-4FFA-86C5-E371A0071FE0 ESI.008 END

--4C90A841-AB17-4A46-AA0B-ABC6A539B643 ECR.493 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.493'
, 'Отсутствует направление МО при реабилитации (st37, ds37, метод оплаты 7*)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_COMENTSL t3 with(nolock) on t3.idcase = t0.idcase and t3.SL_ID = t1.SL_ID
where (N_KSG like 'st37.%' or N_KSG like 'ds37.%' or t3.METHOD  like '7.%') and isnull(NPR_MO,'') in ('','000000')
--4C90A841-AB17-4A46-AA0B-ABC6A539B643 ECR.493 END

--DBB577FE-0A49-45D7-B14C-08BA5C13ECB4 EMF.002 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TIME_CALL', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.002'
, 'Поле время вызова СМП - TIME_CALL  / Время прибытия СМП -  TIME_MISSION  не заполнено ! обязательно для СМП'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
(t2.TIME_CALL is null or t2.TIME_MISSION is null)
and t0.USL_OK = 4
--DBB577FE-0A49-45D7-B14C-08BA5C13ECB4 EMF.002 END

--EAD4BA9F-B9EE-4DA6-B707-02682B261D86 EMF.003 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.003'
, 'Не заполнено КСГ для Случаев стационара'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
LEFT JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where 
@q019type not in ('T')
and (t0.USL_OK = 1 or t0.USL_OK = 2)
and t2.N_KSG is null
--EAD4BA9F-B9EE-4DA6-B707-02682B261D86 EMF.003 END

--EE93370E-79AF-4F6B-84A2-7E9886916366 ECR.054 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', t0.NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.054'
, 'Не указано направления для  USL_OK = 3 и  FOR_POM = 3'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with
(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
@q019type in ('H')
and t0.USL_OK = 3 and t0.FOR_POM = 3
and t0.IDSP<>'31'
and t0.LPU not in ('560008')
and t2.VIDMP not in ('W','X','Y','G','AT','AN','H','AS','ANZ','AND','ANS','ANP')
and t2.[METHOD] not in ('4.6') 
and ISNULL(t0.NPR_MO,'' ) not in ('','000000')
and t1.PRVS NOT IN  (11, 17, 79)
--and (t1.ds1 not like 'N46%' and t1.ds1 not like 'N97%')
and t1.DS1 not like 'N46%' and t1.DS1 not like 'N97%'
--PRVS (cпециальность) NOT IN  (11, 17, 79)
--NPR_MO(Направление) NOT IN  ("", "000000")
--EE93370E-79AF-4F6B-84A2-7E9886916366 ECR.054 END

--01FFB84A-E055-48C6-84E1-17AA83286691 ECR.064 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.064'
, 'Метод оплаты 1.3.3  Допустим  только для МО : 560263 '
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('1.3.3')
and t0.LPU not in ('560263')
--01FFB84A-E055-48C6-84E1-17AA83286691 ECR.064 END

--88F7D280-8279-4438-B09E-862D7382D8A1 ECR.070 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', @NSCHET as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.070'
, 'Недопустимое МО направления=+ пНаправление+"для метода оплаты"+ пМетодОплаты +"(направлять может только другая МО)"'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
where
--@q019type in ('H')
t2.[METHOD] in ('1.2', 'DA')
and (t0.NPR_MO = t0.LPU or isnull(t0.NPR_MO,'') in ('', '000000'))
--88F7D280-8279-4438-B09E-862D7382D8A1 ECR.070 END

--7E7538DB-CED4-4B8E-8898-86B5C522F5A6 ECR.071 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.071'
, 'Для ИССЛЕДОВАНИЙ направление ОБЯЗАТЕЛЬНО'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
left join ies.R_NSI_METHODS method on method.DictionaryBaseId = t2.MethodId
where
--@q019type in ('H')
method.TARIF_TYPE = 2
and (method.CODE not like '8.%' and method.CODE not like '6.%' and method.code <> '6.5.2.2')
and (isnull(t0.NPR_MO,'') in ('', '000000'))
--7E7538DB-CED4-4B8E-8898-86B5C522F5A6 ECR.071 END

--5A1F9782-318E-4181-AE16-1ED6012CC82F ECR.092 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.092'
, 'Специальность не входит в систему ОМС'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_USL t1 with(nolock) on t1.idcase = t0.idcase
left join tempdb..TEMP_MR_USL_N mr with(nolock) on t1.idcase = mr.idcase and t1.SL_ID = mr.SL_ID and t1.IDSERV = mr.IDSERV
where 
--@q019type in ('H')
(
	isnull(t1.PRVS,mr.PRVS) in ('1','7','14','16','74','29','30','34','42','51','52','63','64','67','73','74','82','83','85','86','88','97','98','99','234','280')
	or (isnull(t1.PRVS,mr.PRVS) in ('53','54') and t0.LPU != '560253') 
)
--5A1F9782-318E-4181-AE16-1ED6012CC82F ECR.092 END

--AB930455-18A7-4DCB-80EE-187AAA41E874 ECR.101 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.101'
, 'DA001 услуга  допустима  только для МО : 560265/560267/560214/560033/560144'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_USL t1 with(nolock) on t1.idcase = t0.idcase
where --@q019type in ('H')
t1.CODE_USL = 'DA001'
and t0.LPU not in ('560265','560267','560214','560033','560144')
--AB930455-18A7-4DCB-80EE-187AAA41E874 ECR.101 END

--17F9FB35-2672-41C8-98C1-15474C4C5780 ECR.105 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_OK', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.105'
, 'Для СМП можно выставлять не более 1 услуги в случае!'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where --@q019type in ('H')
t0.USL_OK = 4
and (select count(*) from tempdb..TEMP_USL t100 where t100.idcase = t1.idcase and t100.sl_id = t1.sl_id)>1
--17F9FB35-2672-41C8-98C1-15474C4C5780 ECR.105 END

--5811CD61-B610-4C69-99B1-148E2297D6D2 ECR.107 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', @NSCHET as ZN_POL ,@NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.107'
, 'Не заполнено поле TARIF в узле USL! При CODE_USL из PRICE_SZ обязательно к заполнению!'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_USL t1 with(nolock) on t1.idcase = t0.idcase
where --@q019type in ('H')
t0.USL_OK in (1,2)
and isnull(t1.TARIF,0) = 0
and t1.PriceSzId is not null
--5811CD61-B610-4C69-99B1-148E2297D6D2 ECR.107 END

--70BC5837-4C36-4FFA-A9E6-67E08A229B1A ECR.112 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TELEMED', @NSCHET as ZN_POL ,@NSCHET, 'COMENTU', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.112'
, 'TELEMED (Применение телемедицинских технологий)  может быть только в файле основных случаев'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_COMENTU t1 with(nolock) on t1.idcase = t0.idcase
where
@q019type not in ('H')
and t1.TELEMED = 1
--70BC5837-4C36-4FFA-A9E6-67E08A229B1A ECR.112 END

--EBAF030C-ABF8-445C-8926-4723F9BB4D2E ECR.113 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TELEMED', @NSCHET as ZN_POL ,@NSCHET, 'COMENTU', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.113'
, 'TELEMED (Применение телемедицинских технологий)  может быть только в поликлинике'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_COMENTU t1 with(nolock) on t1.idcase = t0.idcase
where
--@q019type not in ('H')
t1.TELEMED = 1
and t0.USL_OK <> 3
--EBAF030C-ABF8-445C-8926-4723F9BB4D2E ECR.113 END

--CDAAE0DA-8B06-4587-B56C-27D99338777B ECR.127 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', @NSCHET as ZN_POL ,@NSCHET, 'KSG_KPG', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.127'
, 'Для случая стационара / ДневногоСтационара  не УКАЗАНО КСГ'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
LEFT JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where @q019type not in ('T')
and t0.USL_OK in (1,2)
and t2.N_KSG is null
--CDAAE0DA-8B06-4587-B56C-27D99338777B ECR.127 END

--7EA1340C-729C-4D84-97EB-F230F103F963 ECR.130 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KOL_USL', @NSCHET as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.idcase as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.130'
, 'Для случаев АПП с диагностическими и иными услугами поле  ED_COL :+ СтрокаТЗСлуч.ED_COL+"не равна сумме услуг по полю  KOL_USL"'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where --@q019type in ('H')
z_sl.IDSP = '28'
and (
		sl.ED_COL <> isnull((
		select sum(usl.KOL_USL) 
		from tempdb..TEMP_USL usl 
		where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
		),0)
		or sl.ED_COL = 0
	)
--7EA1340C-729C-4D84-97EB-F230F103F963 ECR.130 END

--405908D2-074F-4CD4-944B-1E311A2DA23B ECR.137 BEGIN	
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'USL_OK', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.137'
--, 'Для случая стационара неверно указанно IDSP =+СтрокаТЗСлуч.IDSP+", Должно быть 33"'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--where 
--@q019type not in ('T')
--and t0.USL_OK = 1
--and t0.IDSP <> 33
--405908D2-074F-4CD4-944B-1E311A2DA23B ECR.137 END

----2F8BB669-B4A2-4753-BFEF-ED48028FE11A ECR.138 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'USL_OK', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.138'
--, 'Для случая стационара неверно указанно IDSP =+СтрокаТЗСлуч.IDSP+", Должно быть 32"'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--where 
--@q019type in ('T')
--and t0.USL_OK = 1
--and t0.IDSP <> '32'
----2F8BB669-B4A2-4753-BFEF-ED48028FE11A ECR.138 END

----19F0E592-A9CA-4CD1-BBB1-E1B35891700C ECR.139 BEGIN	
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'USL_OK', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.139'
--, 'Для случая дневного стационара неверно указанно IDSP =+СтрокаТЗСлуч.IDSP+", Должно быть 33/28"'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--where 
--t0.USL_OK = 2
--and t0.IDSP <> '33'
--and t0.IDSP <> '28'
----19F0E592-A9CA-4CD1-BBB1-E1B35891700C ECR.139 END

----136128B3-7E42-4D86-8128-289A7ADC68F2 ECR.142 BEGIN	
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.142'
--, 'При PRVS = 2/207 W =2  FIN_TYPE должен быть = 3'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--join tempdb..TEMP_ZAP sz on sz.N_ZAP = t0.N_ZAP
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
--join tempdb..TEMP_PERS tp on t0.ID_PAC = tp.ID_PAC
--where
--t1.IsGynecology = 1
--and t2.FIN_TYPE <> 3
----136128B3-7E42-4D86-8128-289A7ADC68F2 ECR.142 END

--CD096C74-2675-4C5A-AD4D-972A44A1E1A6 ECR.143 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FIN_TYPE', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.143'
, 'При FIN_TYPE = 3  должны быть следующие значения  PRVS = 2/207 W =2'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
join tempdb..TEMP_ZAP sz on sz.N_ZAP = t0.N_ZAP
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
join tempdb..TEMP_PERS tp with(nolock) on convert(varchar(36),t0.ID_PAC) = convert(varchar(36),tp.ID_PAC)
where
t1.IsGynecology <> 1
and t2.FIN_TYPE = 3
--CD096C74-2675-4C5A-AD4D-972A44A1E1A6 ECR.143 END

----81F45E96-2C41-4D1D-A7A5-EB8CA38F8011 ECR.145 BEGIN	
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.145'
--, 'Неверный IDSP :+СтрокаТЗСлуч.IDSP +"Для  FIN_TYPE :"+ СтрокаТЗСлуч.FIN_TYPE +"допустим IDSP только 31"'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
--where
----@q019type in ('H')
--t2.FIN_TYPE = 2 and t0.IDSP<>31
--and t0.ZSLType = 'Поликлиника'
----81F45E96-2C41-4D1D-A7A5-EB8CA38F8011 ECR.145 END

--C51137C4-3899-412D-B9BC-058B7772EB8D ECR.152 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FIN_TYPE', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'ECR.152'
, 'Для Диспансеризации  поле FIN_TYPE(+ СтрокаТЗСлуч.FIN_TYPE  +")  для диспансеризации должно быть равно 1 или 2"'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t1.idcase and t2.SL_ID = t1.SL_ID
join tempdb..TEMP_PERS pers on t0.ID_PAC = pers.ID_PAC
where
@q019type in ('X')
and (t2.FIN_TYPE = 3 or t1.IsGynecology = 1)
--C51137C4-3899-412D-B9BC-058B7772EB8D ECR.152 END

--5045CBC8-4B67-4274-B8BE-1DD678DE0269 ECR.516 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FOR_POM', @NSCHET as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.516'
, 'Неверно указана форма оказания медицинской помощи FOR_POM для случаев медицинской реабилитации (метод оплаты 7*, st37, ds37), RS.Fields("ФИОДР").Value, RS.Fields("IDDOKT").Value)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
LEFT JOIN tempdb..TEMP_KSG_KPG t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
LEFT JOIN tempdb..TEMP_COMENTSL c with(nolock) on c.idcase = t0.idcase and c.SL_ID = t1.SL_ID
where (t2.N_KSG like 'st37.%' or t2.N_KSG like 'ds37.%' or c.METHOD  like '7.%') and t0.FOR_POM<>3
--5045CBC8-4B67-4274-B8BE-1DD678DE0269 ECR.516 END

--EBD88A0A-42E1-4262-9319-294E476DCD4C ECR.034 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SNILS', @NSCHET as ZN_POL ,@NSCHET, 'CODE_MD', null, null, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.034'
,'У СНИЛС '+t3.SNILS+'не проходит проверка по шаблону! Укажите верный снилс медицинского работника с кодом = '+t3.CODE_MD+')'
FROM tempdb..TEMP_CODE_MD t3
where dbo.RegExMatch(t3.SNILS,'^\d{3}-\d{3}-\d{3} \d{2}$') != 1
--EBD88A0A-42E1-4262-9319-294E476DCD4C ECR.034 END

--7F56293A-1477-4011-91AC-94CFFFB9FCDB ECR.034 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SNILS', @NSCHET as ZN_POL ,@NSCHET, 'CODE_MD', null, null, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.034'
,'У СНИЛС '+t3.SNILS+'не проходит проверка по контрольному числу! Укажите верный снилс медицинского работника с кодом = '+t3.CODE_MD+')'
FROM tempdb..TEMP_CODE_MD t3
where dbo.RegExMatch(t3.SNILS,'^\d{3}-\d{3}-\d{3} \d{2}$') = 1
and dbo.fn_snils14_valid(t3.SNILS) = 0
--7F56293A-1477-4011-91AC-94CFFFB9FCDB ECR.034 END

----E724C9D5-6B3A-49C7-8606-F3B7A3859183 ECR.035 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'CODE_MD', t3.CODE_MD as ZN_POL ,@NSCHET, 'CODE_MD', null, null, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.035'
--,'Не уникальный CODE_MD в файле персональных данных медицинских работников'
--FROM tempdb..TEMP_CODE_MD t3
--GROUP BY t3.CODE_MD HAVING COUNT(*)>1
----E724C9D5-6B3A-49C7-8606-F3B7A3859183 ECR.035 END

--4C724D89-CFDD-4150-9CB8-408CFDB70D63 EST.021 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', CODE_MD as ZN_POL ,@NSCHET, 'CODE_MD', null, null, null as IDCASE, null as SL_ID, null as IDSERV, 'EST.021'
, 'Не уникальный CODE_MD'
from
(
select CODE_MD from tempdb..TEMP_CODE_MD GROUP BY CODE_MD HAVING COUNT(*)>1
)aa
--4C724D89-CFDD-4150-9CB8-408CFDB70D63 EST.021 END

--23810B87-B97F-4205-B568-D9886F65D4A6 ECR.037 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 as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.037'
, 'В случае указан диагноз который не входит в систему ОМС")'
from
tempdb..TEMP_SLUCH t0
JOIN tempdb..TEMP_COMENTSL c with(nolock) on c.idcase = t0.idcase and c.SL_ID = t0.SL_ID
where dbo.MKB_OMS(DS1 ,CONVERT(DATE,DATE_2),CONVERT(INT,DET) ,c.METHOD ) = 1
--23810B87-B97F-4205-B568-D9886F65D4A6 ECR.037 END

--6A20CEF8-A352-4A9F-A154-6DB0779B80C6 ECR.039 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', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, t2.IDSERV as IDSERV, 'ECR.039'
, 'В услуге указан диагноз который не входит в систему ОМС")'
from
tempdb..TEMP_SLUCH t0
JOIN tempdb..TEMP_COMENTSL c with(nolock) on c.idcase = t0.idcase and c.SL_ID = t0.SL_ID
join tempdb..TEMP_USL t2 on t2.idcase = t0.idcase and t2.SL_ID = t0.SL_ID
where dbo.MKB_OMS(DS ,DATE_OUT,CONVERT(INT,t2.DET) , c.METHOD) = 1
--6A20CEF8-A352-4A9F-A154-6DB0779B80C6 ECR.039 END

--C70BBF63-0762-4EEA-A9FC-B0599F13E32D ECR.042 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', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.042'
, 'В случае указан диагноз который запрещено сдавать в ОМС, согласно служебной записки Чевычаловой С.А.")'
from
tempdb..TEMP_USL t0
where t0.DS = 'K02'
--C70BBF63-0762-4EEA-A9FC-B0599F13E32D ECR.042 END

--0E36F0D8-668B-42ED-AF99-3CA202F9BF90 ECR.043 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDDOKT', t0.IDDOKT as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.043'
, 'В случае указан IDDOKT = +RS.Fields("IDDOKT").Value+"отсутствующий в файле персональных данных медицинских работников")'
from tempdb..TEMP_SLUCH t0
left join tempdb..TEMP_COMENTSL t1 on t1.SL_ID=t0.SL_ID and t1.IDCASE=t0.IDCASE
where not exists(select * from tempdb..TEMP_CODE_MD md where md.CODE_MD = ISNULL(t0.IDDOKT,t1.IDDOKT_C))
--0E36F0D8-668B-42ED-AF99-3CA202F9BF90 ECR.043 END

--60940216-2E72-42E3-9AFE-DD215C14512F ECR.045 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', mr.CODE_MD as ZN_POL ,@NSCHET, 'MR_USL_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.045'
, 'В услугах указан CODE_MD отсутствующий в файле персональных данных медицинских работников")'
from tempdb..TEMP_SLUCH sl
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join tempdb..TEMP_MR_USL_N mr on sl.idcase = mr.idcase and sl.SL_ID = mr.SL_ID and usl.IDSERV=mr.IDSERV
where not exists(select * from tempdb..TEMP_CODE_MD md where md.CODE_MD = mr.CODE_MD)
--60940216-2E72-42E3-9AFE-DD215C14512F ECR.045 END

--64C4B792-65B7-4DD9-8F25-0BF89E213731 ECR.044 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', mr.CODE_MD as ZN_POL ,@NSCHET, 'MR_USL_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.044'
, 'В услугах указан CODE_MD = +RS.Fields("IDDOKT").Value+"отсутствующий в файле персональных данных медицинских работников")'
from tempdb..TEMP_SLUCH sl
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join tempdb..TEMP_MR_USL_N mr on sl.idcase = mr.idcase and sl.SL_ID = mr.SL_ID
where not exists(select * from tempdb..TEMP_CODE_MD md where md.CODE_MD = mr.CODE_MD)
--64C4B792-65B7-4DD9-8F25-0BF89E213731 ECR.044 END


--E775F34D-21B5-4786-AE61-2344271F7298 ECR.201 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', sl.DET as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.202'
, 'Поле DET (признак детского профиля) в разделе SLUCH указано неверно (в случае указан представитель, представитель может быть только у детей )'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
--@q019type in ('H')
pers.FAM_P <> ''
and sl.DET<>1
--E775F34D-21B5-4786-AE61-2344271F7298 ECR.201 END

--2FFC18E6-F40B-46F1-A7E3-01508B7954BD ECR.202 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DET', sl.DET as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.202'
, 'Поле DET (признак детского профиля) в разделе USL указано неверно (в случае указан представитель, представитель может быть только у детей )'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
@q019type not in ('X')
and pers.FAM_P <> ''
and usl.DET<>1
--2FFC18E6-F40B-46F1-A7E3-01508B7954BD ECR.202 END


--939B1E65-1A9B-4246-AE39-BA9F1EDFE5B3 ECP.004 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TIME_CALL', comentsl.TIME_CALL as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECP.004'
, 'Поле время вызова СМП - TIME_CALL не соответствует шаблону ([01][0-9]|2[0-3]):([0-5][0-9])'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
--@q019type not in ('X')
z_sl.USL_OK = 4
and dbo.RegExMatch(comentsl.TIME_CALL,'^([01][0-9]|2[0-3]):([0-5][0-9])$') != 1
--939B1E65-1A9B-4246-AE39-BA9F1EDFE5B3 ECP.004 END

--939B1E65-1A9B-4246-AE39-BA9F1EDFE5B3 ECP.004 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TIME_MISSION', comentsl.TIME_MISSION as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECP.004'
, 'Поле время вызова СМП - TIME_MISSION не соответствует шаблону ([01][0-9]|2[0-3]):([0-5][0-9])'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
--@q019type not in ('X')
z_sl.USL_OK = 4	
and dbo.RegExMatch(comentsl.TIME_MISSION,'^([01][0-9]|2[0-3]):([0-5][0-9])$') != 1
--939B1E65-1A9B-4246-AE39-BA9F1EDFE5B3 ECP.004 END


--D89646EC-2C10-441F-A62C-27C9A1716D5A ECR.002 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ID_PAC', z_sl.ID_PAC as ZN_POL ,@NSCHET, 'PERS', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.002'
, 'В файле персональных данных пациентов отсутствуют данные по случаю'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where not exists(select 1 from tempdb..TEMP_PERS pers where z_sl.ID_PAC = pers.ID_PAC)
--D89646EC-2C10-441F-A62C-27C9A1716D5A ECR.002 END

--4EAE635A-C017-4BC6-B5A9-12AB5D0AAEDC ECR.003 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ID_PAC', ID_PAC as ZN_POL ,@NSCHET, 'PACIENT', null as N_ZAP, null as ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.003'
, 'Не уникальный ID_PAC в файле случаев!'
from tempdb..TEMP_SLUCH t0
where
@q019type in ('C', 'H', 'T', 'X')
group by t0.ID_PAC
having count(*)>1
--4EAE635A-C017-4BC6-B5A9-12AB5D0AAEDC ECR.003 END

--D7042C2D-D3BD-4D4C-B840-CCCA812B11F4 ECR.008 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', usl.VID_VME as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.008'
, 'Значение элемента  VID_VME ' + ISNULL(usl.VID_VME,'')+' не найден в справочнике STOMAT (NSI_STOMAT)  на дату :' + CONVERT(VARCHAR (10) , sl.DATE_2, 104)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
WHERE 
ZSLType = 'Стоматология'
  AND NOT EXISTS (
    SELECT 1
    FROM ies.R_NSI_STOMAT AS s 
    WHERE s.CODE = ISNULL(VID_VME ,'') 
    AND sl.DATE_2 BETWEEN s.START_DATE AND s.FINAL_DATE
  ) and @type<>562
--D7042C2D-D3BD-4D4C-B840-CCCA812B11F4 ECR.008 END

--B1566CE4-D4AE-45AB-A827-8E9C00EC8C6E ECR.009 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.009'
, 'Значение элемента  CODE_USL ' + ISNULL(usl.CODE_USL,'')+' не найден в справочнике STOMAT (NSI_STOMAT)  на дату :' + CONVERT(VARCHAR (10) , sl.DATE_2, 104)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
WHERE 
ZSLType = 'Стоматология'
  AND NOT EXISTS (
    SELECT 1
    FROM ies.R_NSI_STOMAT AS s 
    WHERE s.CODE = ISNULL(CODE_USL ,'') 
    AND sl.DATE_2 BETWEEN s.START_DATE AND s.FINAL_DATE
  )
--B1566CE4-D4AE-45AB-A827-8E9C00EC8C6E ECR.009 END


--02075C86-59B1-4B73-A25A-C417E30A64A5 ECR.018 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_OUT', convert(varchar(10),usl.DATE_OUT,104) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.018'
, 'DATE_OUT < DATE_IN'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
WHERE 
usl.DATE_OUT < usl.DATE_IN
--02075C86-59B1-4B73-A25A-C417E30A64A5 ECR.018 END

--600329E5-E084-466D-A7F9-E5567AEA6A4E ECR.021 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_OUT', convert(varchar(10),usl.DATE_OUT,104) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.018'
, 'DATE_IN менее (DATE_1 -1 Месяц) для метода оплаты : 5.1.1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
WHERE 
@q019type in ('X')
and comentsl.[METHOD] in ('5.1.1')
and usl.DATE_IN < DATEADD(MM, -1, sl.DATE_1)
--600329E5-E084-466D-A7F9-E5567AEA6A4E ECR.021 END

--6E7534FD-D424-4589-9A5A-0DED59667007 ECR.027 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_STOMAT_ID', convert(varchar(10), KSG_STOMAT_ID) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.027'
, 'KSG_STOMAT_ID более 99'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
WHERE 
ksg_stomat.KSG_STOMAT_ID > 99
--6E7534FD-D424-4589-9A5A-0DED59667007 ECR.027 END

--219B622E-B9F3-45FA-9ED8-5FD82578D873 ECR.038 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 as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.038'
, 'В случае указан диагноз который не входит в систему ОМС'
from
tempdb..TEMP_SLUCH t0
JOIN tempdb..TEMP_COMENTSL c on c.idcase = t0.idcase and c.SL_ID = t0.SL_ID
where dbo.MKB_OMS(t0.DS1, t0.DATE_2, CONVERT(INT,t0.DET), c.METHOD) = 1
--219B622E-B9F3-45FA-9ED8-5FD82578D873 ECR.038 END

--DE8CFBCA-2D20-4630-9767-3B64901FF670 ECR.041 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.041'
, 'В случае указан диагноз который запрещено сдавать в ОМС, согласно служебной записки Чевычаловой С.А.'
from tempdb..TEMP_SLUCH sl
where sl.DS1 = 'K02'
--DE8CFBCA-2D20-4630-9767-3B64901FF670 ECR.041 END

--F83E522D-6DA5-4D8F-BEE2-43CEA8A0BD87 ECR.046 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.046'
,'Неудалось определить тип случая лечения'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where ZSLType is null
--F83E522D-6DA5-4D8F-BEE2-43CEA8A0BD87 ECR.046 END

----09FF66F7-179A-4262-8ECC-27B3967AB405 ECR.062 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'GESTATION', convert(varchar(100),comentsl.GESTATION) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.062'
--,'Указанный срок беременности в неделях должен в интервале [1;41] недели'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--where comentsl.GESTATION < 1 or comentsl.GESTATION > 41
----09FF66F7-179A-4262-8ECC-27B3967AB405 ECR.062 END

--FC681BC4-84A4-4943-AC47-7B083EA5F8D0 ECR.077 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.METHOD as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.077'
,'Не заполнено поле Метод оплаты для Поликлиники'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where isnull(comentsl.METHOD,'') = ''
and ZSLType = 'Поликлиника'
and VidPomOPMP != 'Z'
--FC681BC4-84A4-4943-AC47-7B083EA5F8D0 ECR.077 END

--6FBBD6F7-FFC5-4F7A-98EF-E72A88750D03 ECR.078 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'null', null as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.078'
,'Не раcсчитано поле Блок ОПМП'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where sl.VidPomOPMP is null
--6FBBD6F7-FFC5-4F7A-98EF-E72A88750D03 ECR.078 END

--4C47FB6F-2A11-418B-83DF-52E5C24E7A0D ECR.079 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMV', z_sl.SUMV as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.079'
,'Не заполнено поле SUMV'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where isnull(z_sl.SUMV,0) = 0
--4C47FB6F-2A11-418B-83DF-52E5C24E7A0D ECR.079 END

--53290BEF-835D-4570-9FB4-59D96140D07D ECR.081 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(100),usl.PROFIL) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.081'
,'В услуге профиль медицинской помощи не соответствует возрасту пациента. Код Q016 003K.00.0780'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where usl.PROFIL in (17, 18, 19, 20, 21, 68, 86)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
--53290BEF-835D-4570-9FB4-59D96140D07D ECR.081 END

--43014437-5BA4-468A-A675-D55F51BAE622 ECR.082 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(100),usl.PROFIL) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.082'
,'Профиль медицинской помощи не соответствует полу. Код Q016 003K.00.0790'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where usl.PROFIL in (3, 136, 137, 184)
and pers.W <> 2
--43014437-5BA4-468A-A675-D55F51BAE622 ECR.082 END

--C5D6CDA0-9BE7-4935-80EC-E3BB6EDF8AE7 ECR.083 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(100),usl.PROFIL) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.083'
,'В услуге Профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть меньше 1 года). Код Q016 003K.00.0800'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where usl.PROFIL in (55)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >=1
--C5D6CDA0-9BE7-4935-80EC-E3BB6EDF8AE7 ECR.083 END

--007347C2-F149-4AF4-8734-B54C93E99B9F ECR.084 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(100),usl.PROFIL) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.084'
,'В услуге профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть  60 лет или более). Код Q016 003K.00.0810'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where usl.PROFIL in (14)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 60
--007347C2-F149-4AF4-8734-B54C93E99B9F ECR.084 END

--C77D69B5-755D-4216-8F00-A698898CB83F ECR.085 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(100),usl.PROFIL) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.085'
,'В услуге профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть больше 18 лет). Код Q016 003K.00.0820'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where usl.PROFIL in (29, 60, 108, 122)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18
--C77D69B5-755D-4216-8F00-A698898CB83F ECR.085 END

--88E228F2-E87F-4E99-B7D6-A5A943ACC30B ECR.086 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.086'
,'В услуге Специальность врача не соответствует возрасту пациента (должно применяться только для пациента до 18 лет). Код Q016 003K.00.0890'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where (
	usl.PRVS in (18, 19, 20, 21, 22, 68)
or (usl.PRVS = 49 and ExcludeFromProfile_PRVS_Age_Check <> 1)
)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
--88E228F2-E87F-4E99-B7D6-A5A943ACC30B ECR.086 END

--609FD9BF-F165-49FF-A871-AF4FFB7B2A35 ECR.087 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.087'
,'В УСЛУГЕ Специальность врача не соответствует полу пациента (003K.00.0900)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
usl.PRVS in (2)
and ExcludeFromProfile_PRVS_W_Check <> 1
and pers.W <> 2
--609FD9BF-F165-49FF-A871-AF4FFB7B2A35 ECR.087 END

--8A8AF533-C367-4AE1-936E-D0B7680AA771 ECR.088 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.088'
,'В услуге Специальность врача не соответствует возрасту пациента (должно применяться только для пациента до 1 года). Код Q016 003K.00.0910'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
usl.PRVS in (37)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 1
--8A8AF533-C367-4AE1-936E-D0B7680AA771 ECR.088 END

--726A63CE-CE9F-4CF8-A1FA-94E84E062AC8 ECR.089 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.089'
,'В УСЛУГЕ Специальность врача не соответствует возрасту пациента (должно применяться только для пациента старше 18 лет). Код Q016 003K.00.0920'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
usl.PRVS in (25, 41, 84, 92)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18
and @q019type <> 'X'
--726A63CE-CE9F-4CF8-A1FA-94E84E062AC8 ECR.089 END

--D18DFE2D-BB47-411E-8A89-255184F4D14A ECR.090 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.090'
,'Специальность врача в услуге не соответствует возрасту пациента (для пациента от 15 лет и старше). Код Q016 003K.00.0930'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
usl.PRVS in (76)
and ExcludeFromProfile_PRVS_Age_Check <> 1
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 15
--D18DFE2D-BB47-411E-8A89-255184F4D14A ECR.090 END

--679463A6-61BB-46C1-BF7B-AAA09DB6C9E6 ECR.091 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.091'
,'Специальность врача в услуге не соответствует возрасту пациента (должно применяться только для пациента от 60 лет и старше). Код Q016 003K.00.0940'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
usl.PRVS in (11)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 60
--679463A6-61BB-46C1-BF7B-AAA09DB6C9E6 ECR.091 END

--3B9465C4-BB79-44B2-AEB7-F10452F245DD ECR.096 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.096'
,'В случае Стационар, ДневнойСтационар CODE_USL <> VID_VME'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
z_sl.ZSLType in ('Стационар', 'ДневнойСтационар')
and usl.CODE_USL <> ''
and usl.VID_VME <> ''
and usl.CODE_USL  <> usl.VID_VME
--3B9465C4-BB79-44B2-AEB7-F10452F245DD ECR.096 END

--C59011D0-7688-4C8F-ADE6-FF1CBCD1E350 ECR.103 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.096'
--,'В случае Стационар, ДневнойСтационар CODE_USL <> VID_VME'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
--left join ies.T_V001_NOMENCLATURE v001 on v001.Code = t0.VID_VME
--and isnull(DATEBEG,convert(date,'01.01.2010',104))<=DATE_2 and (DATEEND is null or DATEEND>=DATE_2)
--where 
--z_sl.IDSP = 28
--C59011D0-7688-4C8F-ADE6-FF1CBCD1E350 ECR.103 END

--B4856352-4AFF-4118-9BF5-359F8493C002 ECR.108 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(100),usl.PRVS) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.108'
,'Не заполнено поле TARIF в узле USL для ТипТарифа (2,3), обязательно к заполнению!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join ies.R_NSI_METHODS method on method.DictionaryBaseId = comentsl.MethodId
where method.TARIF_TYPE in (2,3)
and z_sl.ZSLType = 'Поликлиника'
and usl.TARIF = 0
--B4856352-4AFF-4118-9BF5-359F8493C002 ECR.108 END

--0C0E7CA3-594F-45FB-B2FE-6376C05D7D72 ECR.109 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TARIF', cast(usl.TARIF as varchar(200)) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.109'
,'Заполнен тег TARIF в узле USL! для Типа тарифа=1 при отсутствии Кода услуги (Вида вмешательства)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join ies.R_NSI_METHODS method on method.DictionaryBaseId = comentsl.MethodId
where method.TARIF_TYPE in (1)
and z_sl.ZSLType = 'Поликлиника'
and usl.TARIF <> 0
and isnull(usl.CODE_USL,'') = ''
and isnull(usl.VID_VME,'') = ''
--0C0E7CA3-594F-45FB-B2FE-6376C05D7D72 ECR.109 END

--17A2306C-FF87-47FC-8855-446BEA1BB02A ECR.115 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL', null as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.115'
,'Для поликлинического случая количество NA001 (доставка): '+convert(varchar(3),sl.NA001Count)+', не совпадет с количеством диализов: ' + convert(varchar(3),sl.DialysisCount)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where z_sl.ZSLType = 'Поликлиника'
and sl.DialysisCount <> sl.NA001Count
--17A2306C-FF87-47FC-8855-446BEA1BB02A ECR.115 END

--CBBF694E-1A00-45A3-B6CA-180B6EB9004C ECR.116 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL', null as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.116'
,'Для стоматологического случая отсутствуют обязательные услуги'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where z_sl.ZSLType = 'Стоматология'
and sl.StomRequiredUslCount = 0
--CBBF694E-1A00-45A3-B6CA-180B6EB9004C ECR.116 END

select sl.sl_id,sl.IDCASE into #stom
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join ies.R_NSI_SOOTV_STOMAT sootv_stomat on sootv_stomat.MANIPULATION=isnull(usl.VID_VME,'') 
where
z_sl.ZSLType = 'Стоматология'




--3F91976F-98E4-49FF-BBCD-71D03FC37C6A ECR.117 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', convert(varchar(100),usl.VID_VME) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.117'
,'В услугах не найден код номенклатуры осмотра, соответствующий специальности'
--,'В услугах не найдена манипуляция, соответствующая специальности по Справочнику "Стоматологии соответствие манипуляции специальности"'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
left join #stom st on st.sl_id=sl.sl_id and st.IDCASE=sl.IDCASE
where
z_sl.ZSLType = 'Стоматология'
and st.sl_id is null



--and not exists (
--select 1 
--from ies.R_NSI_SOOTV_STOMAT sootv_stomat
--where isnull(usl.VID_VME,'') = sootv_stomat.MANIPULATION
--and sootv_stomat.CODE_SPEC = usl.PRVS
--)
--3F91976F-98E4-49FF-BBCD-71D03FC37C6A ECR.117 END

drop table #stom

--29EBD58A-6475-4779-B577-2B5966BCC753 ECR.119 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.119'
,'Не заполнены Услуги (нет узлов USL). Для Поликлиники и Стоматологии  и  обязательны'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.ZSLType in ('Стоматология' , 'Поликлиника')
and not exists (
	select 1 
	from tempdb..TEMP_USL usl 
	where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
)
--29EBD58A-6475-4779-B577-2B5966BCC753 ECR.119 END

--D4E06BF0-5BF7-4026-AFEE-580BD5F5E3FF ECR.121 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_2', convert(varchar(10),sl.DATE_2,104) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.121'
,'Дата оказания МП не соответствует отчетному периоду.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
(sl.DATE_2 < @fdom or sl.DATE_2 > @ldom) 
--D4E06BF0-5BF7-4026-AFEE-580BD5F5E3FF ECR.121 END

--BB9176A8-85F4-4A06-ADCF-870986D6BBDF ECR.128 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ED_COL', convert(varchar(3), sl.ED_COL) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.128'
,'Для Метода NA ED_COL содержит 0'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
z_sl.ZSLType in ('Поликлиника')
and sl.ED_COL = 0
and comentsl.METHOD = 'NA'
and sl.DATE_2 >= convert(date,'01.06.2016',104)
--BB9176A8-85F4-4A06-ADCF-870986D6BBDF ECR.128 END

--A2B3C86A-FADA-415C-A2FD-5AC8D747DFDA ECR.129 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ED_COL', convert(varchar(3), sl.ED_COL) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.129'
,'Для неотложной помощи ED_COL содержит 0'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
z_sl.ZSLType in ('Поликлиника')
and sl.ED_COL = 0
and comentsl.METHOD = 'N'
--A2B3C86A-FADA-415C-A2FD-5AC8D747DFDA ECR.129 END


--9696680A-5C49-41F6-B9B1-489A8B85A9A5 ECR.131 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ED_COL', convert(varchar(3), sl.ED_COL) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.131'
,'Для случаев стоматологии поле ED_COL: '+ convert(varchar(3), sl.ED_COL)+' должно соответствовать количеству КСГ стоматлогии: '+ convert(varchar(3), sl.KsgStomatCount)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
where
z_sl.ZSLType in ('Стоматология')
and sl.ED_COL <> sl.KsgStomatCount
and sl.KsgStomatCount > 0
--9696680A-5C49-41F6-B9B1-489A8B85A9A5 ECR.131 END

--66356701-3CB5-4B08-AAA2-A8557611B48D ECR.133 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_OK', convert(varchar(3), z_sl.USL_OK) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.133'
,'В счетах ВМП может быть только Стационар'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.ZSLType <> 'Стационар'
and @q019type = 'T'
--66356701-3CB5-4B08-AAA2-A8557611B48D ECR.133 END

--12F22AB7-338D-42C9-AE10-C881FBF41FA7 ECP.009 BEGIN	
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select distinct 'CODE_MD', t0.CODE_MD as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC as ID_PAC, t1.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.014'
, 'Код мед. работника CODE_MD в разделе MR_USL_N не соответствует шаблону'
from tempdb..TEMP_MR_USL_N t0
join tempdb..TEMP_SLUCH t1 on t1.SL_ID=t0.SL_ID and t0.IDCASE=t1.IDCASE
where t0.CODE_MD like '%[^a-Z0-9]%'
--12F22AB7-338D-42C9-AE10-C881FBF41FA7 ECP.009 END

----4B0202DB-1C8C-4DDA-83D9-0046474D30DC ECR.146 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(3), z_sl.IDSP) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.146'
--,'Для методов оплаты имеющих тип тарифа (2) IDSP должен быть равен 28'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and method.TARIF_TYPE = 2
--and z_sl.IDSP <> 28
----4B0202DB-1C8C-4DDA-83D9-0046474D30DC ECR.146 END

----074C1943-5E79-4FC1-A85F-10FA100790C3 ECR.147 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(3), z_sl.IDSP) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.147'
--,'Для методов оплаты имеющих тип тарифа (3) IDSP должен быть равен 30'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and method.TARIF_TYPE = 3
--and z_sl.IDSP <> 30
----074C1943-5E79-4FC1-A85F-10FA100790C3 ECR.147 END

----3F7B42E0-04BF-40BD-90C7-25805DC8D973 ECR.148 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.148'
--,'Для методов оплаты имеющих тип финансирования=1 значение FIN_TYPE должно быть равно 1'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and method.FIN_TYPE = 1
--and comentsl.FIN_TYPE <> 1
----3F7B42E0-04BF-40BD-90C7-25805DC8D973 ECR.148 END

----DE58FC87-5EF4-49FF-A482-CE85C55F8007 ECR.149 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.149'
--,'Для методов оплаты имеющих тип финансирования=2 значение FIN_TYPE должно быть равно 2,3'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and (
--		(method.FIN_TYPE = 2 and comentsl.FIN_TYPE <> 2)
--		or
--		(comentsl.FIN_TYPE <> 3 and sl.IsGynecology = 1)
--	)
----DE58FC87-5EF4-49FF-A482-CE85C55F8007 ECR.149 END

----A1C67B97-02B1-48C3-8073-F237117E9D7D ECR.150 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.150'
--,'Для методов оплаты имеющих тип финансирования ( ' + convert(varchar(3), method.FIN_TYPE) +' ) поле FIN_TYPE('+convert(varchar(3), comentsl.FIN_TYPE)+')должно быть равно 1,2,3'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and @q019type <> 'X'
--and method.FIN_TYPE in (1, 2, 3)
--and not comentsl.FIN_TYPE in (1, 2, 3)
----A1C67B97-02B1-48C3-8073-F237117E9D7D ECR.150 END

----E70E07E1-3891-49C2-8F0A-5A41BA781014 ECR.140 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.140'
--,'Для случаев вид помощи V ISDP должен быть равен 29'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and comentsl.FIN_TYPE in (1)
--and sl.VidPomR = 'V'
--and z_sl.IDSP <> 29
----E70E07E1-3891-49C2-8F0A-5A41BA781014 ECR.140 END

----3DD795C1-DB6D-405C-B30B-053B34CB93F9 ECR.141 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.141'
--,'Для случаев вид помощи A ISDP должен быть равен 30'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and comentsl.FIN_TYPE in (1)
--and sl.VidPomR = 'A'
--and z_sl.IDSP <> 30
----3DD795C1-DB6D-405C-B30B-053B34CB93F9 ECR.141 END

----A50C436A-0C6D-4317-8B3B-79F096357722 ECR.151 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.151'
--,'Для методов оплаты имеющих тип финансирования ('+convert(varchar(3), method.FIN_TYPE)+')  поле FIN_TYPE('+convert(varchar(3), comentsl.FIN_TYPE)+') для диспансеризации должен быть равен 1 или 2'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and @q019type = 'X'
--and (
--	(method.FIN_TYPE = 1 and comentsl.FIN_TYPE <> 1)
--	or 
--	(method.FIN_TYPE = 2 and comentsl.FIN_TYPE <> 2)
--)
----A50C436A-0C6D-4317-8B3B-79F096357722 ECR.151 END

----C51137C4-3899-412D-B9BC-058B7772EB8D ECR.152 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.152'
--,'Для Диспансеризации поле FIN_TYPE('+convert(varchar(3), comentsl.FIN_TYPE)+') должно быть равно 1 или 2'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and @q019type = 'X'
--and (comentsl.FIN_TYPE = 3 or sl.IsGynecology = 1)
----C51137C4-3899-412D-B9BC-058B7772EB8D ECR.152 END

--978344D2-E847-4794-A8E3-32CDB0979650 ECR.153 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.153'
--,'Для методов оплаты имеющих тип финансирования = 0  поле FIN_TYPE не должно заполняться'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
--where
--z_sl.ZSLType in ('Поликлиника')
--and method.FIN_TYPE is null
--and comentsl.FIN_TYPE is null
--978344D2-E847-4794-A8E3-32CDB0979650 ECR.153 END

--E8871823-2716-424B-80FF-83AA66D3AC77 ECR.156 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.156'
,'С профилем 158 (Медреабилитация) должна использоваться КСГ - МЕДРЕАБИЛИТАЦИЯ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
z_sl.ZSLType in ('Стационар')
and sl.PROFIL = 158
and ksg_kpg.N_KSG not like 'st37.%'
--E8871823-2716-424B-80FF-83AA66D3AC77 ECR.156 END

--12DE700B-D098-4329-B77F-7BF579CDCE11 ECR.157 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.157'
,'КСГ - МЕДРЕАБИЛИТАЦИЯ должна использоваться профиль 158'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
z_sl.ZSLType in ('Стационар')
and sl.PROFIL <> 158
and ksg_kpg.N_KSG like 'st37.%'
--12DE700B-D098-4329-B77F-7BF579CDCE11 ECR.157 END

--17955962-7357-430C-A2D8-88D65C2B0D84 ECR.158 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.158'
,'С профилем 158 (Медреабилитация) должна использоваться КСГ - МЕДРЕАБИЛИТАЦИЯ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
z_sl.ZSLType in ('ДневнойСтационар')
and sl.PROFIL = 158
and ksg_kpg.N_KSG not like 'ds37.%'
--17955962-7357-430C-A2D8-88D65C2B0D84 ECR.158 END

--2BA3DE3B-1C7D-46AC-8029-C50626EFD612 ECR.159 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.159'
,'КСГ - МЕДРЕАБИЛИТАЦИЯ должна использоваться профиль 158'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
z_sl.ZSLType in ('ДневнойСтационар')
and sl.PROFIL <> 158
and ksg_kpg.N_KSG like 'ds37.%'
--2BA3DE3B-1C7D-46AC-8029-C50626EFD612 ECR.159 END

--0C536D83-B200-4B52-8C47-0D1722A86F21 ECR.160 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FIN_TYPE', convert(varchar(3), comentsl.FIN_TYPE) as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.160'
,'Для данных случаев поле FIN_TYPE не заполняется'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
z_sl.ZSLType <> 'Поликлиника'
and @q019type <> 'T'
and comentsl.FIN_TYPE is not null
--0C536D83-B200-4B52-8C47-0D1722A86F21 ECR.160 END
--old_flk end

--new_flk begin
--9613E6F0-63A8-48AD-8BE1-0B75F06925B7 ECR.008 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ID_PAC', convert(varchar(36), pers.ID_PAC) as ZN_POL ,@NSCHET, 'PERS', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.008'
,'Не уникальный ID_PAC в БД уже имеется в другом счете'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
exists (
	select 1
	from ies.t_schet s 
	join ies.t_schet_zap sz on sz.schet = s.schetid 
	where s.CODE_MO = @code_mo
	and sz.ID_PAC = pers.ID_PAC
	and s.IsDelete = 0
) and @type<>562
and not exists (
	select 1
	from ies.t_schet s 
	where s.[YEAR] = @year
	and s.[FILENAME] = @filename
	--and s.[Status] = 0
	and s.IsDelete = 0
)
--9613E6F0-63A8-48AD-8BE1-0B75F06925B7 ECR.008 END

--20D12C40-3983-4D29-9A8F-4E0B1EB78F8C ECR.023 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.023'
,'Несоответствие  МКБ и Пола в разделе SLUCH'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where exists
(select 1 from ies.R_NSI_SOOTV_MKB_SEX mkb_sex 
where mkb_sex.Code = sl.DS1 and pers.w <> mkb_sex.sex
and mkb_sex.DATEBEG<=sl.DATE_2 and (mkb_sex.DATEEND is null or mkb_sex.DATEEND>=sl.DATE_2))
--20D12C40-3983-4D29-9A8F-4E0B1EB78F8C ECR.023 END

--C50FAD18-072E-472F-AA3A-9AD91AD7EBBD ECR.024 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS', usl.DS as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.024'
,'Несоответствие  МКБ и Пола в разделе USL'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where exists
(select 1 from ies.R_NSI_SOOTV_MKB_SEX mkb_sex
where mkb_sex.Code = usl.DS and pers.w <> mkb_sex.sex
and mkb_sex.DATEBEG<=sl.DATE_2 and (mkb_sex.DATEEND is null or mkb_sex.DATEEND>=sl.DATE_2))
--C50FAD18-072E-472F-AA3A-9AD91AD7EBBD ECR.024 END

--3D841FBC-37AB-4B4D-B149-396E84553E30 ECR.031 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.031'
,'SL/LEK_PR/CODE_SH не найдена в справочнике V032 на дату окончания оказания МП'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_SLUCH_LEK_PR lek_pr on sl.idcase = lek_pr.idcase and sl.SL_ID = lek_pr.SL_ID
where
left(lek_pr.CODE_SH,3) not in ('thc', 'gsh', 'amt')
and not exists (select 1 from ies.T_V032_DRUG_GR_REG v032 where v032.ScheDrugGrCd = lek_pr.CODE_SH)

--14DBCF29-5EFB-4FB6-B411-6E38D52C5786 ECR.032 BEGIN

--14DBCF29-5EFB-4FB6-B411-6E38D52C5786 ECR.032 END

--2D7F5CD4-6185-4261-A73E-63B5AE2901AD ECR.469 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.469'
,'LEK_PR/CODE_SH Некорректно заполнено поле (последняя цифра CODE_SH не найдена в спраовчнике V031)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_SLUCH_LEK_PR lek_pr on sl.idcase = lek_pr.idcase and sl.SL_ID = lek_pr.SL_ID
where
left(lek_pr.CODE_SH,3) not in ('thc', 'gsh', 'amt')
and not exists (select * from ies.T_V031_GROUP_DRUG v031 where v031.DrugGroupCode = convert(int, right(lek_pr.CODE_SH,1)) and right(lek_pr.CODE_SH,1) like '[0-9]'
and v031.DATEBEG<=sl.DATE_2 and (v031.DATEEND is null or v031.DATEEND>=sl.DATE_2)
)
--2D7F5CD4-6185-4261-A73E-63B5AE2901AD ECR.469 END

--91A222D6-EE34-47E2-BA23-5D4392E96516 ECR.162 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'CODE_SH', lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.162'
--,'Случаи ВМП МО='+@code_mo+' с Группой ВМП='+HGR+' НЕ ВХОДЯТ В ПЛАН ЗАКАЗ!'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--where
--not exists (
--	SELECT t0.OpmpItemPlanID, t0.MO, t0.VMP_METHOD, t0.PeriodOpmpDate
--	select VMP_METHOD, VMP_PROFIL, VMP_METHOD_ID
--	FROM IES.R_OPMP_ITEM_PLAN t0  where VMP_METHOD is not null
--	where  t0.MO = @code_mo
--	and не хватает HGR в справочнике!
--)

--91A222D6-EE34-47E2-BA23-5D4392E96516 ECR.162 END


--0B5E3FBB-C00C-4D3F-9540-9897625AB0E5 ECR.095 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.095'
,'Неверный код лечения '+usl.CODE_USL+' в поле CODE_USL, не найден в справочниках SPECS!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
z_sl.USL_OK in (1, 2)
and usl.SpecsId is null
and usl.CODE_USL <> ''
--0B5E3FBB-C00C-4D3F-9540-9897625AB0E5 ECR.095 END

--BA42300C-32AC-4453-8F20-ECC579D17171 ECR.098 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', usl.VID_VME as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.098'
,'Неверный код лечения в поле VID_VME, не найден в справочниках KSGN, KSGN_C!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
(
	(z_sl.USL_OK in (1) and usl.KSGNId is null)
	or
	(z_sl.USL_OK in (2) and usl.KSGN_CId is null)
)
and usl.CODE_USL = ''
and @q019type <> 'T'
--BA42300C-32AC-4453-8F20-ECC579D17171 ECR.098 END

--D53B068F-6837-4F0F-A1D4-5345CEF55A1B ECR.099 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', usl.VID_VME as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.098'
,'Неверный код лечения в поле VID_VME. Для случаев ВМП должен соответствовать METOD_HMP'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
VID_VME <> cast(sl.METOD_HMP as varchar(10))
and @q019type = 'T'
--D53B068F-6837-4F0F-A1D4-5345CEF55A1B ECR.099 END

--089BF193-A039-4854-BD3E-B709E4C9417D ECR.106 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NZ', convert(varchar(2),comentu.NZ) as ZN_POL ,@NSCHET, 'COMENTU', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.106'
,'Неверный код лечения в поле VID_VME. Для случаев ВМП должен соответствовать METOD_HMP'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
join tempdb..TEMP_COMENTU comentu on usl.idcase = comentu.idcase and usl.SL_ID = comentu.SL_ID and usl.IDSERV = comentu.IDSERV
where
ZSLType = 'Стоматология'
and usl.N_ZRequired = 1
and comentu.NZ is null
--089BF193-A039-4854-BD3E-B709E4C9417D ECR.106 END

--4D58A87A-D50D-43B9-8009-55FA2D3E56CB ECR.132 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_HMP', sl.VID_HMP as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID,null as IDSERV, 'ECR.132'
,'Для USL_OK 1/2 поле  ED_COL : '+ convert(varchar(10),sl.ED_COL) +' должно быть равно  количеству сеансов диализа :' + convert(varchar(10), sl.SessionCount)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join ies.T_V019_VMP_METHOD v019 on sl.METOD_HMP = v019.IDHM
and sl.VID_HMP = v019.HVID
and sl.HGR = v019.HGR
and v019.DATEBEG<=sl.DATE_2 and (v019.DATEEND is null or v019.DATEEND>=sl.DATE_2)
where
@q019type != 'T'
and (ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and sl.SessionCount != sl.ED_COL
and sl.HasSession =  1
--4D58A87A-D50D-43B9-8009-55FA2D3E56CB ECR.132 END

--51868078-665A-49FA-BE69-689B544F601B ECR.134 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_HMP', sl.VID_HMP as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID,null as IDSERV, 'ECR.134'
,'Не найдена запись в V019 с соответсвующими METOD_HMP, VID_HMP, HGR, DS1 на дату окончания случая!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join ies.T_V019_VMP_METHOD v019 on sl.METOD_HMP = v019.IDHM
and sl.VID_HMP = v019.HVID
and sl.HGR = v019.HGR
and v019.DATEBEG<=sl.DATE_2 and (v019.DATEEND is null or v019.DATEEND>=sl.DATE_2)
where
@q019type = 'T'
and not exists (
	select 1 from ies.R_V019_MKB v019_mkb where v019_mkb.V019VmpMethod = v019.V019VmpMethodID
	and v019_mkb.MkbCode <> ''
	and sl.ds1 like v019_mkb.MkbCode + '%'
)
--51868078-665A-49FA-BE69-689B544F601B ECR.134 END

--5C2957E8-8CE7-4765-A276-9FE219CACD5E ECR.203 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),sl.DET) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID,null as IDSERV, 'ECR.203'
,'Поле DET (признак детского профиля) в разделе SLUCH указано неверно (не соответствует возрасту пациента на дату начала оказания МП)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
(
	(dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18 and sl.DET = 0)
	or
	(dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18 and sl.DET = 1)
)
and @q019type <> 'X'
--5C2957E8-8CE7-4765-A276-9FE219CACD5E ECR.203 END

--6FEDA479-8284-4147-BC6D-A3C2D14D5993 ECR.204 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),usl.DET) as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.204'
,'Поле DET (признак детского профиля) в разделе USL указано неверно (не соответствует возрасту пациента на дату начала оказания МП)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
(
	(dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18 and usl.DET = 0)
	or
	(dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18 and usl.DET = 1)
)
and @q019type <> 'X'
--6FEDA479-8284-4147-BC6D-A3C2D14D5993 ECR.204 END

--D42ECF25-74D9-49DF-93AB-96A5A9EDAA88 ECR.161 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_HMP', sl.VID_HMP as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID,null as IDSERV, 'ECR.161'
,'Не найдена запись в V019 с соответсвующими METOD_HMP, VID_HMP, HGR на дату окончания случая!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
left join ies.T_V019_VMP_METHOD v019 on sl.METOD_HMP = v019.IDHM
and sl.VID_HMP = v019.HVID
and sl.HGR = v019.HGR
and v019.DATEBEG<=sl.DATE_2 and (v019.DATEEND is null or v019.DATEEND>=sl.DATE_2)
where
v019.V019VmpMethodID is null
and @q019type = 'T'
--D42ECF25-74D9-49DF-93AB-96A5A9EDAA88 ECR.161 END

--7A3EE61F-6690-4C42-BAF7-DC5C37B65E2D ECR.176 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_CODE', sl.VID_HMP as ZN_POL ,@NSCHET, 'KSG_STOMAT', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID,null as IDSERV, 'ECR.176'
,'КСГ a001 недопустима для данной МО'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
where
z_sl.ZSLType = 'Стоматология'
and ksg_stomat.KSG_CODE = 'a001'
and @code_mo not in ('560005','560266')
--7A3EE61F-6690-4C42-BAF7-DC5C37B65E2D ECR.176 END

--3C58E1F9-7DDE-42B5-8D23-6758D0CB94AA ECR.185 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.185'
,'CODE_USL 02001/02002/02003 может использовать только МО 560001/560002/560109/560264'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
where
z_sl.ZSLType = 'СкораяПомощь'
and usl.CODE_USL in ('02001', '02002', '02003')
and @code_mo not in ('560001', '560002', '560264', '560109')


insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.185'
,'МО в случаях СМП может использовать только CODE_USL 02001/02002/02003'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
where
z_sl.ZSLType = 'СкораяПомощь'
and usl.CODE_USL not in ('02001', '02002', '02003')
and @code_mo in ('560001', '560002', '560264')
--3C58E1F9-7DDE-42B5-8D23-6758D0CB94AA ECR.185 END

--select * from ies.R_NSI_PRICE_P
--559358EF-958E-4BC4-A732-8117685494B6 ECR.186 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.186'
,'МО в случаях СМП может использовать только CODE_USL 02001/02002/02003'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_KSG_STOMAT ksg_stomat on sl.idcase = ksg_stomat.idcase and sl.SL_ID = ksg_stomat.SL_ID
where
z_sl.ZSLType = 'СкораяПомощь'
and 
(
exists (select 1 
		from ies.R_NSI_PRICE_P price_p 
		where price_p.SPEC_CODE = sl.PRVS
		and iif(sl.DET<>1,6,5) = price_p.AGE
		and price_p.[START_DATE]<=sl.DATE_2 and (price_p.FINAL_DATE is null or price_p.FINAL_DATE>=sl.DATE_2))
or @code_mo in ('560001', '560002', '560264')
)
--559358EF-958E-4BC4-A732-8117685494B6 ECR.186 END

--F09304C0-D6DB-4776-8F4A-19441C5CDA23 ECR.529 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PODR', sl.PODR as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.529'
,'Необходимо указать код отделения в соответствии с справочником DEPART!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
(z_sl.ZSLType = 'ДневнойСтационар'
or (z_sl.ZSLType = 'Поликлиника' and comentsl.METHOD = 'NA')
or (z_sl.ZSLType = 'Стационар' and z_sl.VIDPOM <> '32'))
and not exists (
	select top 1 depart.DictionaryBaseId
	from ies.R_NSI_DEPART depart
	where depart.CODE_D = sl.PODR
	and depart.[START_DATE]<=sl.DATE_2 and (depart.FINAL_DATE is null or depart.FINAL_DATE>=sl.DATE_2)
)
--F09304C0-D6DB-4776-8F4A-19441C5CDA23 ECR.529 END

--A634CC75-BBF8-4609-B0AE-240B2A5F1DCD ECR.191 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PODR', sl.PODR as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.191'
,'Не найдена запись DEPART с соответствующим кодом МО или USL_OK!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
(z_sl.ZSLType = 'ДневнойСтационар'
or (z_sl.ZSLType = 'Поликлиника' and comentsl.METHOD = 'NA')
or (z_sl.ZSLType = 'Стационар' and z_sl.VIDPOM <> '32'))
and not exists (
	select top 1 depart.DictionaryBaseId
	from ies.R_NSI_DEPART depart
	where depart.CODE_D = sl.PODR
	and depart.[START_DATE]<=sl.DATE_2 and (depart.FINAL_DATE is null or depart.FINAL_DATE>=sl.DATE_2)
	and (z_sl.USL_OK = depart.USL_OK or depart.USL_OK is null)
	and @code_mo = depart.MO_CODE
)

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PODR', sl.PODR as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.191'
,'КСГ не соответствует коду отделения DEPART'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
(z_sl.ZSLType = 'ДневнойСтационар'
or (z_sl.ZSLType = 'Поликлиника' and comentsl.METHOD = 'NA')
or (z_sl.ZSLType = 'Стационар' and z_sl.VIDPOM <> '32'))
and not exists (
	select top 1 depart.DictionaryBaseId
	--select * 
	from ies.R_NSI_DEPART depart
	where depart.CODE_D = sl.PODR
	and depart.[START_DATE]<=sl.DATE_2 and (depart.FINAL_DATE is null or depart.FINAL_DATE>=sl.DATE_2)
	and (z_sl.USL_OK = depart.USL_OK or depart.USL_OK is null)
	and @code_mo = depart.MO_CODE
	and (depart.KSG_YES like '%;' + ksg_kpg.N_KSG + ';%' or depart.KSG_YES is null)
)
and not exists (select 1 from #Errors e where e.idcase = sl.idcase and e.SL_ID = sl.SL_ID and e.OSHIB = 'ECR.191')

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PODR', sl.PODR as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.191'
,'Не найдена запись DEPART с исключением данного КСГ!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
(z_sl.ZSLType = 'ДневнойСтационар'
or (z_sl.ZSLType = 'Поликлиника' and comentsl.METHOD = 'NA')
or (z_sl.ZSLType = 'Стационар' and z_sl.VIDPOM <> '32'))
and not exists (
	select top 1 depart.DictionaryBaseId
	--select * 
	from ies.R_NSI_DEPART depart
	where depart.CODE_D = sl.PODR
	and depart.[START_DATE]<=sl.DATE_2 and (depart.FINAL_DATE is null or depart.FINAL_DATE>=sl.DATE_2)
	and (z_sl.USL_OK = depart.USL_OK or depart.USL_OK is null)
	and @code_mo = depart.MO_CODE
	and (depart.KSG_YES like '%;' + ksg_kpg.N_KSG + ';%' or depart.KSG_YES is null)
	and (depart.KSG_NO not like '%;' + ksg_kpg.N_KSG + ';%' or depart.KSG_NO is null)
)
and not exists (select 1 from #Errors e where e.idcase = sl.idcase and e.SL_ID = sl.SL_ID and e.OSHIB = 'ECR.191')
--A634CC75-BBF8-4609-B0AE-240B2A5F1DCD ECR.191 END


--3C64B025-B0F8-46A5-8D5F-E5B048AE9C6C ECR.227 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.NAZ_USL as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.227'
,'NAZ/NAZ_USL заполняется только при заполненнии NAZ_V!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and naz.NAZ_V is null
and naz.NAZ_USL is not null
--3C64B025-B0F8-46A5-8D5F-E5B048AE9C6C ECR.227 END

--740FE589-A880-419F-83E8-FE11A9216D93 ECR.228 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.NAZ_USL as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.228'
,'NAZ/NAZ_USL не соответствует NAZ/NAZ_V по справочнику NAPR_V001!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and NAZ_R = 3 
and sl.DS_ONK = 1
and not exists (
	select *
	from ies.R_NSI_NAPR_V001 napr_v001
	where (napr_v001.MET_ISSL = naz.NAZ_V or napr_v001.MET_ISSL is null) and napr_v001.CODE = naz.NAZ_USL
	and napr_v001.[START_DATE]<=sl.DATE_2 and (napr_v001.FINAL_DATE is null or napr_v001.FINAL_DATE>=sl.DATE_2)
)

--740FE589-A880-419F-83E8-FE11A9216D93 ECR.228 END

--9633933E-95DF-4F9A-A0EA-5486B366746B ECR.229 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, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.229'
,'NAZ/NAPR_DATE обязательно при DS_ONK = 1 и NAZ_R={2,3}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and sl.DS_ONK = 1
and naz.NAZ_R in (2,3)
and naz.NAPR_DATE is null
--9633933E-95DF-4F9A-A0EA-5486B366746B ECR.229 END

--CFA4EE83-0E51-4C5A-BC7F-FDDD3F3EE5D7 ECR.230 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', null as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.230'
,'NAZ/NAPR_MO обязательно при DS_ONK = 1 и NAZ_R={2,3}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and sl.DS_ONK = 1
and naz.NAZ_R in (2,3)
and naz.NAPR_MO is null
--CFA4EE83-0E51-4C5A-BC7F-FDDD3F3EE5D7 ECR.230 END

--6102FAA6-C06C-40CA-9BE4-13078A8D7BC6 ECR.231 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, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.231'
,'NAZ/NAPR_DATE заполняется только при DS_ONK = 1 и NAZ_R={2,3}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and not(sl.DS_ONK = 1 and naz.NAZ_R in (2,3))
and naz.NAPR_DATE is not null
--6102FAA6-C06C-40CA-9BE4-13078A8D7BC6 ECR.231 END

--6E290574-4409-49A5-9287-AE5E8253C57F ECR.232 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_MO', null as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.232'
,'NAZ/NAPR_MO заполняется только при DS_ONK = 1 и NAZ_R={2,3}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and not(sl.DS_ONK = 1 and naz.NAZ_R in (2,3))
and naz.NAPR_MO is not null
--6E290574-4409-49A5-9287-AE5E8253C57F ECR.232 END

--F64BAE21-BD97-4DB0-8167-91FF0949CD4B ESI.010 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), naz.NAPR_DATE, 104) as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ESI.010'
,'NAZ/NAPR_DATE меньше даты начала случая DATE_1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and naz.NAPR_DATE < sl.DATE_1
--F64BAE21-BD97-4DB0-8167-91FF0949CD4B ESI.010 END

--1DECFB85-045D-43BF-8939-A43F86FDD27E ESI.011 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), naz.NAPR_DATE, 104) as ZN_POL ,@NSCHET, 'NAZ', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ESI.011'
,'NAZ/NAPR_DATE меньше даты начала случая DATE_1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_NAZ naz on naz.idcase = sl.idcase and naz.SL_ID = sl.SL_ID
where
@q019type in ('X')
and naz.NAPR_DATE > sl.DATE_2
--1DECFB85-045D-43BF-8939-A43F86FDD27E ESI.011 END

--B27F1318-2C64-49E9-9D0A-B9C1F65C2C85 ECR.236 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), z_sl.DATE_Z_1, 104) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.236'
,'DATE_Z_1 <> DATE_1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.DATE_Z_1 <> sl.DATE_1
--B27F1318-2C64-49E9-9D0A-B9C1F65C2C85 ECR.236 END
--F78A9832-11D4-408C-AF3A-7F1EFAEE07C2 ECR.237 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', convert(varchar(10), z_sl.DATE_Z_2, 104) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.237'
,'DATE_Z_1 <> DATE_1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.DATE_Z_2 <> sl.DATE_2
--F78A9832-11D4-408C-AF3A-7F1EFAEE07C2 ECR.237 END

--098BB8BF-32C3-4A8B-A772-65C9207190B5 ECR.238 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SUMV', convert(varchar(19), z_sl.sumv) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.238'
,'SUMV <> SUM_M'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.sumv <> sl.sum_m
--098BB8BF-32C3-4A8B-A772-65C9207190B5 ECR.238 END

--7190EA79-645F-4E8F-88CA-A7BD8E006ADB ECR.239 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), KD_Z) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.239'
,'SUMV <> SUM_M'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
(z_sl.ZSLType = 'ДневнойСтационар' or (z_sl.ZSLType = 'Стационар' and @q019type not in ('T')))
and z_sl.KD_Z <> sl.KD
--7190EA79-645F-4E8F-88CA-A7BD8E006ADB ECR.239 END

--4865E628-E792-4654-9251-105E3CC2260E ECR.240 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), KD_Z) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.240'
,'Расчетное количество койко-дней/пациентов дней ('+convert(varchar(3), KD_Z_Calc)+') не равно выставленному'+convert(varchar(3), KD_Z)+'!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
(z_sl.ZSLType = 'ДневнойСтационар' or z_sl.ZSLType = 'Стационар')
and z_sl.KD_Z <> KD_Z_Calc
--4865E628-E792-4654-9251-105E3CC2260E ECR.240 END

--E52A0418-B9B1-40E3-86E9-4A3863C930EB ECR.247 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.247'
,'Значение поля P_CEL=2.2 допусимо только для случаев диспансеризации'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type not in ('X')
and isnull(sl.p_cel,'') = '2.2'
--E52A0418-B9B1-40E3-86E9-4A3863C930EB ECR.247 END

--19CC9C59-DF48-45C1-BA3A-D0CF715E31ED ECR.249 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.249'
,'Некорректно заполнено поле CRIT, не найденно значение в справочниках для USL_OK=' + convert(varchar(2), z_sl.USL_OK)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
where
not exists (select * FROM IES.R_NSI_ADDIT_CRIT addit_crit where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = crit.CRIT
and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2))

and not exists (select * FROM IES.[R_NSI_MNN_LP_LT] addit_crit where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = crit.CRIT
and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2))

and not exists (select * FROM IES.[R_NSI_SHLT] addit_crit where z_sl.USL_OK = addit_crit.USL_OK and addit_crit.CODE = crit.CRIT
and addit_crit.[START_DATE]<=sl.DATE_2 and (addit_crit.FINAL_DATE is null or addit_crit.FINAL_DATE>=sl.DATE_2))
--19CC9C59-DF48-45C1-BA3A-D0CF715E31ED ECR.249 END

--406E9D92-9A85-4B49-95AC-A5D439D4683B ECR.250 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.249'
,'Некорректно заполнено поле CRIT, не найденно значение в справочниках для USL_OK=' + convert(varchar(2), z_sl.USL_OK)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
where
@q019type in ('C')
and left(crit.CRIT,2) = 'fr' and sl.K_FR_CRIT <> crit.CRIT
--406E9D92-9A85-4B49-95AC-A5D439D4683B ECR.250 END

--7991D796-00EE-41E5-981A-D90BC0D0B203 ECR.251 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.251'
,'Схема из CRIT: не найдена в лекарственных препаратах!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
where
sl.LEK_PR_Exists_FOR_CRIT = 0
--7991D796-00EE-41E5-981A-D90BC0D0B203 ECR.251 END

--0C471EFA-2071-4DF0-BB57-471CA3FB7334 ECR.252 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.252'
,'Схема из CRIT=AGE75 применяется только для пациентов, у которых возраст на дату начала случая более 75 лет!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
crit.CRIT = 'age75'
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 75
--0C471EFA-2071-4DF0-BB57-471CA3FB7334 ECR.252 END

--D5FFAAAB-3AC9-412A-B577-590DA0E9A6D7 ECR.253 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.253'
,'Схема из CRIT=onkgem применяется только для PROFIL 12-18!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
crit.CRIT = 'onkgem'
and sl.PROFIL not in (12, 13, 14, 15, 16, 17, 18)
--D5FFAAAB-3AC9-412A-B577-590DA0E9A6D7 ECR.253 END

--B15F4269-9EAE-46AE-9E20-94A5FB4C14C9 ECR.254 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.254'
,'Дополнительный критерий CRIT не соответствует SHRM_N!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
(left(crit.CRIT,3) = 'rbb' and len(crit.CRIT) = 4 and substring(crit.CRIT, 4,1) <> convert(varchar(1),comentsl.SHRM_N))
or (left(crit.CRIT,2) = 'rb' and (len(crit.CRIT) = 3 or len(crit.CRIT) = 6) and substring(crit.CRIT, 3,1) <> convert(varchar(1),comentsl.SHRM_N))
--select *, substring(IDDKK, 4,1) from ies.T_V024_DOP_KR t0 where t0.IDDKK like 'rbb%' and len(IDDKK) = 4
--select *, substring(IDDKK, 3,1) from ies.T_V024_DOP_KR t0 where t0.IDDKK like 'rb%' and (len(IDDKK) = 3 or len(IDDKK) = 6)
--B15F4269-9EAE-46AE-9E20-94A5FB4C14C9 ECR.254 END

--9A2E836C-9214-420E-BFA9-C880A68F0211 ECR.255 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', crit.CRIT as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.255'
,'Схема из CRIT требует заполнение лекарственных препаратов!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_CRIT crit on crit.idcase = sl.idcase and crit.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
left(crit.CRIT,3) in ('thc', 'gsh')
and (select count(*) from  tempdb..TEMP_SLUCH_LEK_PR lek_pr where sl.idcase = lek_pr.idcase and sl.SL_ID = lek_pr.SL_ID) = 0
--9A2E836C-9214-420E-BFA9-C880A68F0211 ECR.255 END

--869E214E-E120-428B-BB7D-FAEE1717A88C ECR.256 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), KD) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.256'
,'Недопустимо выставление случаев длительностью более 30 койко-дней, требуется разбиение случаев (st19.090-st19.102/ds19.063-ds19.078) согласно метод рекомендациям!'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
((ksg_kpg.N_KSG >= 'st19.090' and ksg_kpg.N_KSG <= 'st19.102')
or (ksg_kpg.N_KSG >= 'ds19.063' and ksg_kpg.N_KSG <= 'ds19.078'))
and sl.KD > 30
and (ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
--869E214E-E120-428B-BB7D-FAEE1717A88C ECR.256 END

--6A3CF4EA-312A-4EAD-8232-3FEFEB01E366 ECR.257 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(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.257'
,'Признак использования КСЛП SL_K не соответствует значению IT_SL'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
((ZSLType = 'Стационар' and @q019type != 'T') or ZSLType = 'ДневнойСтационар')
and 
(
	(ksg_kpg.IT_SL > 0 and ksg_kpg.SL_K = 0)
	or
	(ksg_kpg.IT_SL = 0 and ksg_kpg.SL_K = 1)
)
--6A3CF4EA-312A-4EAD-8232-3FEFEB01E366 ECR.257 END

--A9FCDEB7-5891-4513-A1D0-A7CBDF1A9852 ECR.265 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REAB', convert(varchar(3), KD) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.265'
,'Элемент должен отсутствовать при выполнении условия FOR_POM<>3 (003F.00.0820).'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
z_sl.FOR_POM <> 3 and sl.REAB = 1
--A9FCDEB7-5891-4513-A1D0-A7CBDF1A9852 ECR.265 END

--18274EA5-564F-4B25-95DA-9EBFB37FFF11 ESI.006 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REAB', convert(varchar(3), KD) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ESI.006'
,'REAB не допустим для NOVOR='+pacient.novor+'(002K.00.0390)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
sl.REAB = 1 and pacient.novor <> '' and pacient.novor <> '0'
--18274EA5-564F-4B25-95DA-9EBFB37FFF11 ESI.006 END

--CF3F5AC4-0A49-4986-8C27-8F737353F8A0 ECR.267 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(3), KD) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.267'
,'Для случаев относящихся к ЗНО поле TARIF: должно соответствовать полю SUM_M:'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
@q019type = 'C'
and sl.DS_ONK <> 1
and sl.TARIF <> sl.SUM_M
--CF3F5AC4-0A49-4986-8C27-8F737353F8A0 ECR.267 END

--1EC03E0E-F75E-4BE8-BF5E-F0CA6334AF5E ECR.288 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.288'
,'ONK_SL может присутствовать только в счетах ВМП/ЗНО'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type not in ('C', 'T')
and exists (select 1 from tempdb..TEMP_ONK_SL onk_sl where onk_sl.IDCASE = sl.IDCASE and onk_sl.SL_ID = sl.SL_ID)
--1EC03E0E-F75E-4BE8-BF5E-F0CA6334AF5E ECR.288 END

--A1BE9BBF-C41A-49B8-8260-4981CA641506 EMF.022 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EMF.022'
,'DS_ONK не заполняется при заполнении ONK_SL'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
DS_ONK = 1
and exists (select 1 from tempdb..TEMP_ONK_SL onk_sl where onk_sl.IDCASE = sl.IDCASE and onk_sl.SL_ID = sl.SL_ID)
--A1BE9BBF-C41A-49B8-8260-4981CA641506 EMF.022 END

--49AAC78B-FA90-42AD-9D9F-280A6F49B892 ECR.310 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_DATE', null as ZN_POL ,@NSCHET, 'B_DIAG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.310'
,'DIAG_DATE (Дата взятия материала) более даты окончания случая DATE_2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_B_DIAG b_diag on b_diag.IDCASE = sl.idcase and b_diag.SL_ID = sl.SL_ID
where
b_diag.DIAG_DATE > sl.DATE_2
--49AAC78B-FA90-42AD-9D9F-280A6F49B892 ECR.310 END

--EE7D6CDE-FA29-43FD-A110-A186DA54B012 ECR.323 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_DATE', null as ZN_POL ,@NSCHET, 'B_DIAG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.323'
,'DIAG_RSLT Указывается при наличии сведений о получении результата диагностики (REC_RSLT=1)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_B_DIAG b_diag on b_diag.IDCASE = sl.idcase and b_diag.SL_ID = sl.SL_ID
where
b_diag.REC_RSLT <> 1
and DIAG_RSLT is not null
--EE7D6CDE-FA29-43FD-A110-A186DA54B012 ECR.323 END

--25DA95C8-3635-49BB-967D-B169B355DE02 ECR.329 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_TIP', onk_usl.USL_TIP as ZN_POL ,@NSCHET, 'ONK_USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.329'
,'ONK_USL/USL_TIP Для случае поликлиники может равняться только 5/6'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_ONK_USL onk_usl on onk_usl.IDCASE = sl.IDCASE and onk_usl.SL_ID = sl.SL_ID
where
ZSLType = 'Поликлиника'
and onk_usl.USL_TIP not in (5, 6)
--25DA95C8-3635-49BB-967D-B169B355DE02 ECR.329 END

--09A49833-7ABF-49A5-9DF6-D7864798AFD8 ECR.330 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'USL_TIP', onk_usl.USL_TIP as ZN_POL ,@NSCHET, 'ONK_USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.330'
,'Для методов оплаты (''AA'' ,''AB'' ,''AC'' ,''AD'' ,''AE'' ,''AF'') ONK_USL/USL_TIP Для случае поликлиники может равняться только 6'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_ONK_USL onk_usl on onk_usl.IDCASE = sl.IDCASE and onk_usl.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
ZSLType = 'Поликлиника'
and comentsl.method in ('AA' ,'AB' ,'AC' ,'AD' ,'AE' ,'AF')
and onk_usl.USL_TIP not in (6)
--09A49833-7ABF-49A5-9DF6-D7864798AFD8 ECR.330 END

--03263D4D-6258-42C6-9ECC-89E9D4AD9DF8 ECR.341 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', onk_usl_lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.341'
,'LEK_PR/CODE_SH не соответствует справочнику SHLT, MNN_LP_LT, ADDIT_CRIT'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
where
onk_usl_lek_pr.CODE_SH <> 'НЕТ'
and onk_usl_lek_pr.MNN_LP_LTId is null
and onk_usl_lek_pr.SHLTId is null
and onk_usl_lek_pr.ADDIT_CRITId is null
--03263D4D-6258-42C6-9ECC-89E9D4AD9DF8 ECR.341 END

--4403416F-C3AA-41FD-98F6-19B0CAE13871 ECR.342 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', onk_usl_lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.342'
,'Схема лекарственного препарата не соответствует лекарственному препарату по справочнику N021 '
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
left JOIN ies.T_N021_ONK_LPSH n021 on onk_usl_lek_pr.CODE_SH = n021.CODE_SH
and n021.DATEBEG<=sl.DATE_2 and (n021.DATEEND is null or n021.DATEEND>=sl.DATE_2)
join ies.T_N020_ONK_LEKP n020 on n021.ID_LEKP = n020.N020OnkLekpID and onk_usl_lek_pr.REGNUM = n020.ID_LEKP
and n020.DATEBEG<=sl.DATE_2 and (n020.DATEEND is null or n020.DATEEND>=sl.DATE_2)
where
onk_usl_lek_pr.CODE_SH <> 'НЕТ'
and onk_usl_lek_pr.MNN_LP_LTId is not null
and n020.N020OnkLekpID is null
and onk_usl_lek_pr.CODE_SH not in ('SH9001', 'SH9002')
--4403416F-C3AA-41FD-98F6-19B0CAE13871 ECR.342 END

--A75020F3-6CC4-4718-90A7-9E8F61B01151 ECR.343 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', onk_usl_lek_pr.CODE_SH as ZN_POL ,@NSCHET, 'LEK_PR', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.342'
,'Схема лекарственного препарата не соответствует лекарственному препарату по справочнику N021'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
onk_usl_lek_pr.CODE_SH = 'НЕТ'
and @q019type <> 'T'
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
and (
	(left(ksg_kpg.N_KSG, 8) >= 'st19.105' and  left(ksg_kpg.N_KSG, 8) <= 'st19.121')
	or
	(left(ksg_kpg.N_KSG, 8) >= 'ds19.037' and  left(ksg_kpg.N_KSG, 8) <= 'ds19.049')
	)
--A75020F3-6CC4-4718-90A7-9E8F61B01151 ECR.343 END

--232FEB6A-0F25-4D2D-857A-E382B2267984 ECR.353 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.353'
,'USL/VID_VME  обязательно заполняется для случаев с - USL_TIP = 1/3/4/6 или IDSP = 28'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
z_sl.USL_OK <> 2
and @q019type <> 'X'
and (z_sl.IDSP = 28 or onk_usl_lek_pr.USL_TIP in (1, 3, 4, 6))
and exists (select 1 FROM tempdb..TEMP_USL usl where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID and isnull(usl.VID_VME,'') = '')
and comentsl.method <> 'NA'
--232FEB6A-0F25-4D2D-857A-E382B2267984 ECR.353 END

--EB204BC2-6137-4C5A-935D-7DB5DD983214 EMF.043 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EMF.043'
,'USL обязательно заполняется для случаев с - USL_TIP = 1/3/4/6 или IDSP = 28'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
z_sl.USL_OK <> 2
and @q019type <> 'X'
and (z_sl.IDSP = 28 or onk_usl_lek_pr.USL_TIP in (1, 3, 4, 6))
and not exists (select 1 FROM tempdb..TEMP_USL usl where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID)
--EB204BC2-6137-4C5A-935D-7DB5DD983214 EMF.043 END

--C17509E9-F73E-428B-B40F-47E75E6D206F ECR.355 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),onk_usl_lek_pr.USL_TIP) as ZN_POL ,@NSCHET, 'ONK_USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.355'
,'ONK_USL/USL_TIP  не соответствует КСГ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_LEK_PR_DATE_INJ onk_usl_lek_pr on onk_usl_lek_pr.IDCASE = sl.IDCASE and onk_usl_lek_pr.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
CROSS APPLY (
	select top 1 sootv_ksg_onk.TIP_USL_ONK
	from ies.R_NSI_SOOTV_KSG_ONK sootv_ksg_onk
	where sootv_ksg_onk.KSG = ksg_kpg.N_KSG
	and sootv_ksg_onk.DATEBEG<=sl.DATE_2 and (sootv_ksg_onk.DATEEND is null or sootv_ksg_onk.DATEEND>=sl.DATE_2)
) as t0
where
@q019type = 'C'
and (ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and onk_usl_lek_pr.USL_TIP <> t0.TIP_USL_ONK
--C17509E9-F73E-428B-B40F-47E75E6D206F ECR.355 END

--D56DB180-C3D9-4C92-A916-B68356A288BC ECR.365 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, 'ECR.365'
, 'Значения элемента ONK_SL/HEI Рост должно быть больше 99 (см)'
from tempdb..TEMP_ONK_SL t0
join tempdb..TEMP_Z_SLUCH t1 on t0.IDCASE = t1.IDCASE
join tempdb..TEMP_SLUCH t2 on t1.IDCASE = t2.IDCASE
where
@q019type in ('C', 'D', 'R', 'T')
and HEI < 99 
and t2.DET = 1 
--D56DB180-C3D9-4C92-A916-B68356A288BC ECR.365 END


/* -- Перенести в МЭК 2 Цинцадзе А.П.
--DC9D41C5-F57D-48D7-9583-1061710A5CA9 ECR.367 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),onk_sl.K_FR) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.355'
,'ONK_SL/K_FR более количество дней госпитализации'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_ONK_SL onk_sl on onk_sl.IDCASE = sl.IDCASE and onk_sl.SL_ID = sl.SL_ID
where
onk_sl.K_FR > iif(dbo.DateTimeDiff('day',sl.DATE_1,sl.DATE_2)=0,1,dbo.DateTimeDiff('day',sl.DATE_1,sl.DATE_2))
--DC9D41C5-F57D-48D7-9583-1061710A5CA9 ECR.367 END
*/


--336AAF52-C11E-4DDE-B6BE-3C79E87CE73A ECR.369 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', ds2.DS2 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.369'
,'Несоответствие диагноза, класса O не может быть у мужчин.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS2 ds2 on ds2.SL_ID = sl.SL_ID and ds2.IDCASE = sl.IDCASE
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
ds2.DS2 like 'O%'
and pers.W <> 2 
--336AAF52-C11E-4DDE-B6BE-3C79E87CE73A ECR.369 END

--28D07941-7065-4F1C-BCD5-32BE0020639B ECR.370 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', ds2.DS2 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.370'
,'Несоответствие диагноза, класс P не может быть у пациентов старше 1 года.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS2 ds2 on ds2.SL_ID = sl.SL_ID and ds2.IDCASE = sl.IDCASE
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
ds2.DS2 like 'P%'
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_2) > 1
--28D07941-7065-4F1C-BCD5-32BE0020639B ECR.370 END

--A687B457-0498-4CB5-ADA8-2D9ADA62BC77 ECR.372 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', ds2.DS2 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.371'
,'Код МКБ должен быть указан до подрубрики (003K.00.0670).'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS2 ds2 on ds2.SL_ID = sl.SL_ID and ds2.IDCASE = sl.IDCASE
where 
z_sl.usl_ok <> 4
and len(ds2.DS2) = 3
and exists (select 1 from ies.T_MKB mkb where mkb.MCBCode like '[A-z][0-9][0-9].%' and left(mkb.MCBCode,3) = ds2.DS2 and mkb.Actual = 1)
--A687B457-0498-4CB5-ADA8-2D9ADA62BC77 ECR.372 END

--037F68F2-9E24-468C-BE19-A8B2B2CAF85E ECR.494 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', null as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.494'
,'Для  услуги анестезии должны быть услуги исследований'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
cross apply(select count(*) as cnt from tempdb..TEMP_USL usl where usl.idcase = sl.idcase and usl.SL_ID = sl.SL_ID and usl.code_usl = 'B01.003.004')usl
where (ZSLType = 'Поликлиника' or comentsl.METHOD = 'DA')
and sl.DATE_2 >= '20230301'
and usl.cnt > 1
--037F68F2-9E24-468C-BE19-A8B2B2CAF85E ECR.494 END

--E4CEC743-E245-49E7-B628-D5E800F59BA8 ECR.372 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', ds2.DS2 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.372'
,'DS2 не должен быть равен DS1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS2 ds2 on ds2.SL_ID = sl.SL_ID and ds2.IDCASE = sl.IDCASE
where 
ds2.DS2 = sl.DS1
--E4CEC743-E245-49E7-B628-D5E800F59BA8 ECR.372 END

--DDC5CEA7-A371-4D27-94E9-E26E248C9DB2 ECR.376 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS3', ds3.DS3 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.376'
,'DS3 не должен быть равен DS1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS3 ds3 on ds3.SL_ID = sl.SL_ID and ds3.IDCASE = sl.IDCASE
where 
ds3.DS3 = sl.DS1
--DDC5CEA7-A371-4D27-94E9-E26E248C9DB2 ECR.376 END

--F898EFAA-20DF-43ED-99BD-B1C2ACA63CF1 ECR.377 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS3', ds3.DS3 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.377'
,'Несоответствие диагноза, класса O не может быть у мужчин.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS3 ds3 on ds3.SL_ID = sl.SL_ID and ds3.IDCASE = sl.IDCASE
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
ds3.DS3 like 'O%'
and pers.W <> 2 
--F898EFAA-20DF-43ED-99BD-B1C2ACA63CF1 ECR.377 END

--FE1030FA-BEB7-4709-AD47-BDDA3DCA4366 ECR.378 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS3', ds3.DS3 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.370'
,'Несоответствие диагноза, класс P не может быть у пациентов старше 1 года.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS3 ds3 on ds3.SL_ID = sl.SL_ID and ds3.IDCASE = sl.IDCASE
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
ds3.DS3 like 'P%'
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_2) > 1
--FE1030FA-BEB7-4709-AD47-BDDA3DCA4366 ECR.378 END

--2E520ABB-E20B-4A6C-B9B5-997C8F70C03A ECR.379 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS3', ds3.DS3 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.372'
,'Код МКБ должен быть указан до подрубрики (003K.00.0680).'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS3 ds3 on ds3.SL_ID = sl.SL_ID and ds3.IDCASE = sl.IDCASE
where 
z_sl.usl_ok <> 4
and len(ds3.DS3) = 3
and exists (select 1 from ies.T_MKB mkb where mkb.MCBCode like '[A-z][0-9][0-9].%' and left(mkb.MCBCode,3) = ds3.DS3)
--2E520ABB-E20B-4A6C-B9B5-997C8F70C03A ECR.379 END

--4B6DB8D8-75A4-4E30-B39A-6029A384988E ECR.387 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),pers.DR_P,104) as ZN_POL ,@NSCHET, 'PERS', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.387'
,'Значение поля DR_P должно быть меньше DR не менее, чем на 14 лет.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
dbo.DateTimeDiff('year',pers.DR_P, pers.dr) < 14
--4B6DB8D8-75A4-4E30-B39A-6029A384988E ECR.387 END

--DF427CEF-E076-4AC2-BCAA-C3B331E58960 ECR.408 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),  z_sl.IDSP) as ZN_POL ,@NSCHET, 'Z_SL', z_sl.N_ZAP, z_sl.ID_PAC, z_sl.IDCASE as IDCASE, null as SL_ID, null as IDSERV, 'ECR.408'
,'Не подлежат оплате случаи при IDSP = 31'
from tempdb..TEMP_Z_SLUCH z_sl
where 
@code_mo in ('560001', '560220')
and z_sl.IDSP = 31
--DF427CEF-E076-4AC2-BCAA-C3B331E58960 ECR.408 END

--F00CC548-AFF1-49A3-9647-7757559017E5 ECR.410 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.[METHOD] as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.410'
,'Код МКБ должен быть указан до подрубрики (003K.00.0680).'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where comentsl.[METHOD] in ('4.5.2')
and dbo.DateTimeDiff('year',pers.dr,sl.date_2) < 18
--F00CC548-AFF1-49A3-9647-7757559017E5 ECR.410 END

--CB50E39C-B99D-4491-A5F1-3D85A875487E ECR.411 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', ksg_kpg.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.411'
,'N_KSG не соответствует полу'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and not (
	((select count(*) from ies.R_NSI_SOOTV_KSG_SEX ksg_sex where ksg_sex.KSG = ksg_kpg.N_KSG) = 0)
	or 
	(
		(select count(*) from ies.R_NSI_SOOTV_KSG_SEX ksg_sex where ksg_sex.KSG = ksg_kpg.N_KSG) > 0
		and exists (select 1 from ies.R_NSI_SOOTV_KSG_SEX ksg_sex where ksg_sex.KSG = ksg_kpg.N_KSG and pers.W = ksg_sex.SEX)
	)
)
--CB50E39C-B99D-4491-A5F1-3D85A875487E ECR.411 END

--C52CE16C-1B57-4ADA-8400-A2F4F6053464 ECR.412 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.412'
,'Диагноз не является допустимым для кода услуги'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
ZSLType = 'СкораяПомощь'
and exists (select 1 from tempdb..TEMP_USL usl 
where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
and isnull(usl.CODE_USL,'') in ('01001' ,'01002' ,'01003' ,'01004' ,'01005' ,'01006' ,'01007' ,'01008'))
and sl.DS1 not in ('I21.0' ,'I21.1' ,'I21.2' ,'I21.3' ,'I21.4' ,'I22.8' ,'I22.0' ,'I22.1')
--C52CE16C-1B57-4ADA-8400-A2F4F6053464 ECR.412 END

--5ED01B81-FB07-46B7-B5B9-D07D06EE3186 ECR.413 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', ksg_kpg.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.413'
,'Диагноз не является допустимым для N_KSG'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_2) < 12
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_2) > 55
and ksg_kpg.N_KSG in ('st02.003', 'st02.003', 'st02.004')
--5ED01B81-FB07-46B7-B5B9-D07D06EE3186 ECR.413 END

--B9F290A4-EDD0-4D59-B42A-C7932B3F82B2 ECR.414 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', ksg_kpg.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.414'
,'Диагноз не является допустимым для N_KSG'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and ksg_kpg.N_KSG in ('st36.001' ,'st36.017' ,'st36.018' ,'st36.019' ,'ds36.008' ,'ds36.009' ,'ds36.010')
and sl.DS1 in ('D69.3', 'M08.2')
--B9F290A4-EDD0-4D59-B42A-C7932B3F82B2 ECR.414 END

--0909164E-EB5E-4945-9C86-9208F9F96F45 ECR.415 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', ksg_kpg.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.415'
,'Для МО не подлежат оплате случаи ксг st12.015/st12.016/st12.017/st12.018'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and ksg_kpg.N_KSG in ('st12.015' ,'st12.016' ,'st12.017' ,'st12.018')
and @code_mo in ('560263', '560008')
--0909164E-EB5E-4945-9C86-9208F9F96F45 ECR.415 END

--4DDB6913-10DA-4119-B5A6-E8C01888C452 ECR.417 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'N_KSG', ksg_kpg.N_KSG as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.417'
,'Для МО 560263 не допустимо выставление случаев КС с КСГ=st12.015'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and ksg_kpg.N_KSG like 'st12.015%'
and @code_mo in ('560263')
--4DDB6913-10DA-4119-B5A6-E8C01888C452 ECR.417 END

--32E1B75B-FF3B-4D94-86E7-9ED098721D0D ECR.422 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(3), sl.PRVS) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.422'
,'Несоответствие специальности виду оказываемой медицинской помощи'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.VIDPOM in (11 ,12 ,13 ,31 ,32 ,33)
and 
	(
		sl.PRVS in (42 ,67 ,82 ,83 ,1 ,7 ,14 ,16 ,29 ,30 ,34 ,42 ,51 ,52 ,63 ,64 ,67 ,73 ,74 ,82 ,83 ,85 ,86 ,88 ,96 ,97 ,98 ,99 ,234 ,280)
		or (sl.PRVS = 53 and @code_mo <> '560253')
		or (sl.PRVS = 54 and @code_mo <> '560253')
	)
--32E1B75B-FF3B-4D94-86E7-9ED098721D0D ECR.422 END

--5F3B41B6-EFD4-4147-A86E-A829D2D9013A ECR.423 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(3), sl.PRVS) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.423'
,'Несоответствие специальности виду оказываемой медицинской помощи'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.VIDPOM in (21, 22, 23)
and 
	(
		sl.PRVS not in (66, 283, 4, 49)
	)
--5F3B41B6-EFD4-4147-A86E-A829D2D9013A ECR.423 END

--E10CC89F-21AF-4937-A10F-541DB436B240 ECR.424 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.424'
,'Несоответствие диагноза, класса O не может быть у мужчин.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where sl.DS1 like 'O%'
and pers.W <> 2 
--E10CC89F-21AF-4937-A10F-541DB436B240 ECR.424 END

--3BB21E15-E674-4FAC-886F-3908F96F2AD8 ECR.425 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.425'
,'Несоответствие диагноза, класс P не может быть у пациентов старше 1 года.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
sl.DS1 like 'P%'
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_2) > 1
--3BB21E15-E674-4FAC-886F-3908F96F2AD8 ECR.425 END

--EEFE68A3-9846-44AE-AEC6-794FB23CCDBF ECR.426 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.426'
,'Недопустимый код диагноза DS1. Код МКБ должен быть указан до подрубрики (003K.00.0660)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.usl_ok <> 4
and len(sl.DS1) = 3
and exists (select 1 from ies.T_MKB mkb where mkb.MCBCode like '[A-z][0-9][0-9].%' and left(mkb.MCBCode,3) = sl.DS1 and mkb.Actual = 1)
--EEFE68A3-9846-44AE-AEC6-794FB23CCDBF ECR.426 END

--3C473D33-C2E9-4381-AB7E-ED4F657A68FE ECR.427 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS0', sl.DS0 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.427'
,'Недопустимый код диагноза DS0. Код МКБ должен быть указан до подрубрики (003K.00.0650)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where 
z_sl.usl_ok <> 4
and len(sl.DS0) = 3
and exists (select 1 from ies.T_MKB mkb where mkb.MCBCode like '[A-z][0-9][0-9].%' and left(mkb.MCBCode,3) = sl.DS0 and mkb.Actual = 1)
--3C473D33-C2E9-4381-AB7E-ED4F657A68FE ECR.427 END

--56545446-639B-4F93-9309-9AF3B4AF5625 ECR.428 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', ds2_n.DS2 as ZN_POL ,@NSCHET, 'DS2_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.428'
,'Недопустимый код диагноза DS2_N/DS2. Код МКБ должен быть указан до подрубрики (003K.00.0690)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_DS2_N ds2_n on z_sl.idcase = sl.idcase and ds2_n.sl_id = sl.sl_id
where 
len(ds2_n.DS2) = 3
and exists (select 1 from ies.T_MKB mkb where mkb.MCBCode like '[A-z][0-9][0-9].%' and left(mkb.MCBCode,3) = ds2_n.DS2)
--56545446-639B-4F93-9309-9AF3B4AF5625 ECR.428 END

--DCD2932A-B45E-492A-9687-A18F4F989E07 ECR.429 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.429'
,'Профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть меньше 18 лет)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
sl.PROFIL in (17 ,18 ,19 ,20 ,21 ,68 ,86)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
and @q019type <> 'X'
--DCD2932A-B45E-492A-9687-A18F4F989E07 ECR.429 END

--BFD29677-50FF-49D2-AC35-E4AD6EFB087C ECR.430 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.430'
,'Профиль медицинской помощи не соответствует полу (003K.00.0741)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
sl.PROFIL in (136, 137, 184)
and pers.W <> 2
--BFD29677-50FF-49D2-AC35-E4AD6EFB087C ECR.430 END

--6DD6EAE0-6AD9-4CD3-B098-9B469E03E0AF ECR.431 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.431'
,'Профиль медицинской помощи не соответствует полу и возрасту(003K.00.0742)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
sl.PROFIL in (3)
and pers.W <> 2
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 1
--6DD6EAE0-6AD9-4CD3-B098-9B469E03E0AF ECR.431 END

--4B5ABE52-2EFA-4F57-92AB-2C7E4134D74D ECR.432 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.432'
,'Профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть меньше 1 года) (003K.00.0750)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where 
sl.PROFIL in (55)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 1
--4B5ABE52-2EFA-4F57-92AB-2C7E4134D74D ECR.432 END

--8C8D1F24-3B16-4D6E-A117-BB5B1E7F191B ECR.433 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.433'
,'Профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть  60 лет или более) (003K.00.0760)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where sl.PROFIL in (14)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 60
--8C8D1F24-3B16-4D6E-A117-BB5B1E7F191B ECR.433 END

--4D4AEFC6-98B2-44A3-8837-2A6998B519DC ECR.434 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.PROFIL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.434'
,'Профиль медицинской помощи не соответствует возрасту пациента (Возраст пациента должен быть больше 18 лет) (003K.00.0770)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where sl.PROFIL in (29, 60, 108, 122)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18
--4D4AEFC6-98B2-44A3-8837-2A6998B519DC ECR.434 END

--2D0C74C7-4E2D-4350-AC5B-5A09FCE3D37D ECR.435 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.435'
,'Специальность врача не соответствует возрасту пациента (003K.00.0830)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where (
	sl.PRVS in (18, 19, 20, 21, 22, 68)
or (sl.PRVS = 49 and ExcludeFromProfile_PRVS_Age_Check <> 1)
)
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
--2D0C74C7-4E2D-4350-AC5B-5A09FCE3D37D ECR.435 END

--D6F6320F-2C09-4AFA-BB08-6F76A9CFB994 ECR.436 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.436'
,'Специальность врача не соответствует полу пациента PRVS (003K.00.0840)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
sl.PRVS in (2)
and ExcludeFromProfile_PRVS_W_Check <> 1
and pers.W <> 2
--возраст на дату начала sl
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 1
--D6F6320F-2C09-4AFA-BB08-6F76A9CFB994 ECR.436 END

--DC554BCD-AFFC-42EB-8F42-D16E5945268B ECR.437 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.437'
,'Специальность врача не соответствует возрасту пациента (003K.00.0850)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
sl.PRVS in (37)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 1
--DC554BCD-AFFC-42EB-8F42-D16E5945268B ECR.437 END

--D9EDD97D-480E-436F-AE6A-59C93A782769 ECR.438 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.438'
,'Специальность врача не соответствует возрасту пациента PRVS (003K.00.0860)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
sl.PRVS in (25, 41, 84, 92)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 18
and @q019type <> 'X'
--D9EDD97D-480E-436F-AE6A-59C93A782769 ECR.438 END

--1F63FC79-3196-4AD5-A8F5-A136F24655CD ECR.439 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.439'
,'Специальность врача не соответствует возрасту пациента (003K.00.0870)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
sl.PRVS in (76)
and ExcludeFromProfile_PRVS_Age_Check <> 1
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 15
--1F63FC79-3196-4AD5-A8F5-A136F24655CD ECR.439 END

--FBD0967B-DF22-4A9A-8E9D-23DA4DCF0A17 ECR.440 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', sl.PRVS as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.440'
,'Специальность врача не соответствует возрасту пациента (должно применяться только для пациента от 60 лет и старше 003K.00.0880)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
sl.PRVS in (11)
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 60
--FBD0967B-DF22-4A9A-8E9D-23DA4DCF0A17 ECR.440 END

--078B5069-307D-4C14-BA4F-DCB0138A47FB ECR.441 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOCTYPE', convert(varchar(10), pers.DOCTYPE) as ZN_POL ,@NSCHET, 'PERS', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.441'
,'Несоответствие типа УДЛ возрасту пациента (должен быть больше 14 лет) (003K.00.1021)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
pers.DOCTYPE in (1, 2, 4, 5, 6, 7, 8, 14, 16, 17, 26, 29)
and pers.FAM_P is null
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) < 14
--078B5069-307D-4C14-BA4F-DCB0138A47FB ECR.441 END

--DC787574-8A17-4002-A12B-D08EF4542169 ECR.442 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'FAM_P', convert(varchar(10), pers.DOCTYPE) as ZN_POL ,@NSCHET, 'PERS', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.442'
,'Указан представитель FAM_P для пациента старше 2х месяцев на дату начала лечения DR'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
pers.FAM_P is not null
and dbo.DateTimeDiff('month',pers.dr,sl.DATE_1) >= 2
--DC787574-8A17-4002-A12B-D08EF4542169 ECR.442 END

--F744BFCB-867A-4C15-B712-92B8C5D988EC ECR.443 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(3), sl.PRVS) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.443'
,'Специальность в СЛУЧАЕ не входит в систему ОМС'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
(
	sl.PRVS in (1, 7, 14, 16, 74, 29, 30, 34, 42, 51, 52, 63, 64, 67, 73, 74, 82, 83, 85, 86, 88, 97, 98, 99, 234, 280)
	or (sl.PRVS in (53, 54) and @code_mo <> 560253) 
)
--F744BFCB-867A-4C15-B712-92B8C5D988EC ECR.443 END

--83A7A09F-FB9E-4418-BCAF-CFC624C3D289 ECR.445 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.METHOD as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.445'
,'Посещения в один день к одному специалисту не являются обращением'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
ZSLType = 'Поликлиника'
and comentsl.METHOD in ('1', '10.4')
and sl.DATE_1 = sl.DATE_2
and sl.MR_USL_NCount < 2
and sl.MR_USL_NCount > 0
--83A7A09F-FB9E-4418-BCAF-CFC624C3D289 ECR.445 END

--B2A2E4B1-C2F7-4ECF-BB84-9A145B1C4536 ECR.446 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.METHOD as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.446'
,'Посещение должно быть одним днём'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
ZSLType = 'Поликлиника'
and comentsl.METHOD in ('1.1', '10.3')
and sl.DATE_1 <> sl.DATE_2
--B2A2E4B1-C2F7-4ECF-BB84-9A145B1C4536 ECR.446 END

--E0C3CC6A-FB68-42FB-B3C0-EEE885BD5462 ECR.451 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), pacient.VNOV_D) as ZN_POL ,@NSCHET, 'PACIENT', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.446'
,'Не указан вес недоношенного/маловесного ребенка. Для ВМП '+ convert(varchar(4),sl.VID_HMP) +' связанных с выхаживанием детей с экстремально низким весом, должен быть указан VNOV_D'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
@q019type = 'T'
and sl.VID_HMP = 469
and pers.DR_P is null
and pacient.VNOV_D is null
--E0C3CC6A-FB68-42FB-B3C0-EEE885BD5462 ECR.451 END

--16EC3F31-E182-4ED8-A7A9-4381074A9956 ECR.452 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), pacient.VNOV_D) as ZN_POL ,@NSCHET, 'PACIENT', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.452'
,'Не указан вес недоношенного/маловесного ребенка. Для КСГ: st17.002, связанных с выхаживанием детей с экстремально низким весом, должен быть указан VNOV_D'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
JOIN tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where
@q019type = 'T'
and (ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and ksg_kpg.N_KSG = 'st17.002'
and isnull(pacient.VNOV_D, 0) = 0
--16EC3F31-E182-4ED8-A7A9-4381074A9956 ECR.452 END

--68DBFBBE-66A3-4FDE-93B2-49A66104A830 ECR.453 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STOMAT_TIME', comentsl.STOMAT_TIME as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.453'
,'Поле время начала оказания МП - STOMAT_TIME обязательно для заполнения при FOR_POM=2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.STOMAT_TIME is null
and z_sl.FOR_POM=2
and ZSLType = 'Стоматология' 
--68DBFBBE-66A3-4FDE-93B2-49A66104A830 ECR.453 END

--F22C4100-9B44-4832-B69D-36C4474C9CBF ECR.454 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STOMAT_TIME', comentsl.STOMAT_TIME as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.453'
,'Поле время начала оказания МП - STOMAT_TIME заполняется только для FOR_POM=2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.STOMAT_TIME is not null
and z_sl.FOR_POM<>2
--F22C4100-9B44-4832-B69D-36C4474C9CBF ECR.454 END

--9E711E49-AA3D-4D4C-8F5B-CDFCFE16A120 ECR.455 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'STOMAT_TIME', comentsl.STOMAT_TIME as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.453'
,'Поле время начала оказания МП - STOMAT_TIME содержит ошибку.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.STOMAT_TIME is not null
and dbo.RegExMatch(comentsl.STOMAT_TIME,'^([01][0-9]|2[0-3]):([0-5][0-9])$') != 1
--9E711E49-AA3D-4D4C-8F5B-CDFCFE16A120 ECR.455 END

--359D8D6B-2FEB-433E-974E-360D6DF9B64C ECR.456 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.456'
,'МО не является ЦАОП, в случае оказан признак указания помощи в ЦАОП (CAOP)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.CAOP <> 0
and not exists (select 1 from ies.R_NSI_MO_CAOP mo_coap where mo_coap.CODE = @code_mo 
and mo_coap.DATEBEG<=sl.DATE_2 and (mo_coap.DATEEND is null or mo_coap.DATEEND>=sl.DATE_2))
--359D8D6B-2FEB-433E-974E-360D6DF9B64C ECR.456 END

--D5AFEF5A-90FD-4E3A-A7E7-4FE8D626FEC0 ECR.457 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.457'
,'МО не является Мобильной бригадой, в случае указан признак оказания помощи в Мобильной бригаде (VBR_COMENTSL/VBR)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.VBR= 1 and z_sl.VBR = 1
and not exists (select 1 from ies.R_NSI_MO_MOBILE_BRIGS mo where mo.CODE = @code_mo 
and mo.DATEBEG<=sl.DATE_2 and (mo.DATEEND is null or mo.DATEEND>=sl.DATE_2))
--D5AFEF5A-90FD-4E3A-A7E7-4FE8D626FEC0 ECR.457 END

--58071A2A-907E-4349-A61B-F196D7421A92 ECR.460 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(3), z_sl.IDSP) as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.460'
,'Не допустим метод оплаты = 1 для областных больниц'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.METHOD = '1'
and @code_mo in ('560001', '560264', '560220')
and z_sl.IDSP <> 31
--58071A2A-907E-4349-A61B-F196D7421A92 ECR.460 END

--FB4F892B-051B-4F55-A178-C7711009B312 ECR.122 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), sl.DATE_1, 104) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.122'
,'Год(DATE_1) < ГодФинансирования-1'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
year(sl.DATE_1) < @finyear - 1
--FB4F892B-051B-4F55-A178-C7711009B312 ECR.122 END

--D2D3666D-1DBA-4565-BCD4-22E338554409 ECR.283 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.DS1 as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.283'
,'Случаи  с диагнозами ОНКОЛОГИЯ могут только входить в файлы ЗНО/ВМП'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type not in ('C', 'T')
and sl.ONK_SLRequired <> 0 
--D2D3666D-1DBA-4565-BCD4-22E338554409 ECR.283 END

--73D66A77-D69C-4FB6-AEBD-D4BBC73C216B ECR.284 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), sl.DS_ONK) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.283'
,'Случаи с подозрением на ОНКОЛОГИЮ могут быть только в файлах ЗНО/ВМП/Диспансеризация'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type not in ('C', 'T', 'X')
and sl.DS_ONK = 1
--73D66A77-D69C-4FB6-AEBD-D4BBC73C216B ECR.284 END

--BB94248A-F776-4817-A011-903A94322B8E ECR.285 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SL', null as ZN_POL ,@NSCHET, 'Z_SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.283'
,'Cлучай счета ЗНО не относится к ЗНО/подозрению на ЗНО'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type in ('C')
and sl.HasOnkDiag <> 1
and sl.DS_ONK <> 1
--BB94248A-F776-4817-A011-903A94322B8E ECR.285 END

--17DDF958-E8C5-4C75-A15A-1E81E2FA2291 ECR.125 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', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.125'
,'Данное ЛПУ не может предоставлять услуги для раздела ОПМП код раздела ОПМП '+cast (VidPomOPMP as varchar(200))+''
--,'Не найдено запись в Справочнике соответствия ЛПУ - Вид помощи. ВидПомощи='+VidPomForCheck+', БлокОПМП='+NameOPMP+''
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
where VidPomForCheck is not null
and not exists (
	select 1 from ies.R_NSI_SOOTV_LPU_VID_POM nsi
	where nsi.moer_code = @code_mo
	and VidPomOPMP = nsi.vid_pom
	and nsi.beg_date<=sl.DATE_2 and (nsi.end_date is null or nsi.end_date>=sl.DATE_2)
)
--17DDF958-E8C5-4C75-A15A-1E81E2FA2291 ECR.125 END

--FC522E51-1D76-4E2F-BEE7-7346A54B9D3F ECR.135 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.Method as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.135'
,'В файле не являющемся файлом диспансеризации найден случай диспансеризации с методом оплаты'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
where
@q019type <> ('X')
and ZSLType = 'Поликлиника'
and isnull(method.DISP_TIP,'') != ''
--FC522E51-1D76-4E2F-BEE7-7346A54B9D3F ECR.135 END

--68B954AC-A1A1-476F-8F74-41E9269D7207 ECR.136 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.Method as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.136'
,'В файле диспансеризации найден случай не являющейся случаем диспансеризации (по методу оплаты)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
where
@q019type = ('X')
and isnull(method.DISP_TIP,'') = ''
--68B954AC-A1A1-476F-8F74-41E9269D7207 ECR.136 END

--61EDE45B-6421-4318-BCBE-E4AD552F6CAA ECR.309 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DIAG_DATE', convert(varchar(10), b_diag.DIAG_DATE, 104) as ZN_POL ,@NSCHET, 'B_DIAG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.309'
,'DIAG_DATE (Дата взятия материала) более даты окончания случая DATE_2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_B_DIAG b_diag on b_diag.IDCASE = sl.idcase and b_diag.SL_ID = sl.SL_ID
where
b_diag.DIAG_DATE > sl.DATE_2
--61EDE45B-6421-4318-BCBE-E4AD552F6CAA ECR.309 END

--86F41CFB-B3B6-40A3-BA2E-2D88CCBCCB9F ECR.103 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', usl.VID_VME as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.103'
,'VID_VME не найден в KSGN_C и V001'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
@q019type != 'X'
and ZSLType = 'Поликлиника'
and isnull(usl.VID_VME,'') != ''
and not exists (
	select 1 from ies.R_NSI_KSGN_C ksg where ksg.CODE = usl.VID_VME
	and ksg.[START_DATE]<=sl.DATE_2 and (ksg.FINAL_DATE is null or ksg.FINAL_DATE>=sl.DATE_2)
)
and z_sl.IDSP = 28
and not exists (
	select 1 from ies.T_V001_NOMENCLATURE v001 where v001.CODE = usl.VID_VME
	and (v001.DATEBEG is null or v001.DATEBEG<=sl.DATE_2) and (v001.DATEEND is null or v001.DATEEND>=sl.DATE_2)
)
--86F41CFB-B3B6-40A3-BA2E-2D88CCBCCB9F ECR.103 END

--B9B59B08-83FF-41E4-A36B-A99885CF0B02 ECR.104 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VID_VME', usl.VID_VME as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.104'
,'VID_VME не найден в NAPR_V001 для метода 1.2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
@q019type != 'X'
and ZSLType = 'Поликлиника'
and isnull(usl.VID_VME,'') != ''
and not exists (
	select 1 from ies.R_NSI_KSGN_C ksg where ksg.CODE = usl.VID_VME
	and ksg.[START_DATE]<=sl.DATE_2 and (ksg.FINAL_DATE is null or ksg.FINAL_DATE>=sl.DATE_2)
)
and z_sl.IDSP != 28
and comentsl.Method = '1.2'
and not exists (
	select 1 from ies.R_NSI_NAPR_V001 napr_v001 where napr_v001.CODE = usl.VID_VME
	and napr_v001.[START_DATE]<=sl.DATE_2 and (napr_v001.FINAL_DATE is null or napr_v001.FINAL_DATE>=sl.DATE_2)
)
--B9B59B08-83FF-41E4-A36B-A99885CF0B02 ECR.104 END

--11E2B050-97EE-4860-A405-3273669350DC ECR.217 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), sl.PR_D_N) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.217'
,'Для группы здоровья 3а (по RSLT_D) PR_D_N должно быть 1 или 2.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId = z_sl.RSLT_DId
where
@q019type = 'X'
and z_sl.RSLT_DId is not null
and sl.PR_D_N not in (1, 2)
and v017.GZ = '3а'
--11E2B050-97EE-4860-A405-3273669350DC ECR.217 END

--38081CAF-8AAC-497A-8E1E-ADA7B873B506 ECR.218 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), sl.PR_D_N) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.218'
,'Для группы здоровья 1 (по RSLT_D) PR_D_N не должно быть 1 или 2.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId = z_sl.RSLT_DId
where
@q019type = 'X'
and z_sl.RSLT_DId is not null
and sl.PR_D_N in (1, 2)
and v017.GZ = '1'
--38081CAF-8AAC-497A-8E1E-ADA7B873B506 ECR.218 END

--9BF7DB3A-E7AF-479F-BE68-8EB567A9BCB3 ECR.246 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'P_CEL', sl.P_CEL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.246'
,'Для диспансерного наблюдения P_CEL должно быть 1.3'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
sl.VidPomOPMP = 'AN'
and comentsl.METHOD like '8.%'
and sl.P_CEL != '1.3'
--9BF7DB3A-E7AF-479F-BE68-8EB567A9BCB3 ECR.246 END

--B504605C-935B-42C8-883A-22ECBB912446 ECR.248 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.P_CEL as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.248'
,'Недопустимое сочетание МКБ='+DS1+' для Д-Учета '+P_CEL+' при данном  методе оплаты :' + METHOD
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
sl.VidPomOPMP = 'AN'
and not exists (
	select * 
	from ies.R_NSI_EXCEPTIONS_ERRORS ee
	left join ies.R_NSI_EXCEPTIONS_ERRORS_MKB eem on ee.DictionaryBaseId = eem.ref
	where ee.is_actual = 1
	and ee.DeletionMark= 0
	and ee.is_use  = 1
	and ee.USL_OK = 3
	and (ee.METHOD = comentsl.METHOD or ee.METHOD is null)
	and eem.IsCheck = 1
	and sl.DS1 = eem.DS
	and ee.OSN2 = 'МКБ_для_ДН168'
)
--B504605C-935B-42C8-883A-22ECBB912446 ECR.248 END

--AA8E5401-23B8-463B-8FC0-701DF718FE38 138 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.method as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EM.138'
,'Для метода оплаты недопустима длительность лечения менее 11 дней'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where
comentsl.method in ('7.2.2','7.2.3','7.3.2','7.3.3','7.4.2','7.4.3','7.5.2','7.5.3','7.6.2','7.6.3','7.7.2','7.7.3','7.8.2','7.8.3','7.9.2','7.9.3','7.10','7.11','7.12' )
and datediff(dd, DATE_1, DATE_2)<10 
and profil=158
--AA8E5401-23B8-463B-8FC0-701DF718FE38 138 END

--175B9381-D922-4A5A-A4A0-4675F7AA5641 137 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'METHOD', comentsl.method as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EM.138'
--,'Для метода оплаты недопустима длительность лечения менее 11 дней'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
--join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
--where
--comentsl.method in ('7.2.2','7.2.3','7.3.2','7.3.3','7.4.2','7.4.3','7.5.2','7.5.3','7.6.2','7.6.3','7.7.2','7.7.3','7.8.2','7.8.3','7.9.2','7.9.3','7.10','7.11','7.12' )
--and isnull(pacient.NPOLIS,'')<>''
--and exists (
--	select 1 
--	from ies.t_schet t0
--	where МетодОплаты In ('7.2.2','7.2.3','7.3.2','7.3.3','7.4.2','7.4.3','7.5.2','7.5.3','7.6.2','7.6.3','7.7.2','7.7.3','7.8.2','7.8.3','7.9.2','7.9.3','7.10','7.11','7.12') 
--	and НомерПолиса=sl.NPOLIS and ТипОплаты IN (1,3)
--	group by НомерПолиса
--	having count(*)>=2
--)
--175B9381-D922-4A5A-A4A0-4675F7AA5641 137 END


--A0117374-01A9-4F3A-B20D-A5521310483F 140 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', usl.CODE_USL as ZN_POL ,@NSCHET, 'USL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EM.140'
,'Cрок проведения исследования для метода оплаты 6.2 более 1 года'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where
comentsl.method='6.2' and dateadd(mm,12,usl.date_out)<sl.date_2
--A0117374-01A9-4F3A-B20D-A5521310483F 140 END

--CB7C672D-A6B2-40BD-AD67-20E5DA83DFEE 143 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select null, null as ZN_POL ,@NSCHET, null, null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'EM.140'
,'Количество специалистов не соответствует методу оплаты'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where left(comentsl.method,4)='6.2.' 
group by sl.ID_PAC, comentsl.method, sl.IDDOKT, pers.FIO
having count(distinct usl.prvs)<right(comentsl.method,1)
--CB7C672D-A6B2-40BD-AD67-20E5DA83DFEE 143 END

--5EC7A511-4D61-4F5C-A085-00B16DE052EB 75 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select null, null as ZN_POL ,@NSCHET, null, null, ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'EM.075'
,'Для методы оплаты 1.1 Разовое обращение по заболеванию. Должно состоять из 1 посещения'
FROM (
	SELECT ID_PAC
		,DATE_IN
		,PRVS
	FROM tempdb..TEMP_USL usl
	WHERE usl.ID_PAC IN (
			SELECT sl.ID_PAC
			FROM tempdb..TEMP_SLUCH sl
			JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
			WHERE comentsl.method = '1.1'
			)
		AND usl.PRVS IN (select CODE_SPEC from ies.R_NSI_SPEC_INDEPEND v021 where isnull(v021.IS_REAB,0) = 0)
	GROUP BY usl.ID_PAC
		,usl.DATE_IN
		,usl.PRVS
	) AS tab
GROUP BY ID_PAC
HAVING COUNT(*) > 1
--5EC7A511-4D61-4F5C-A085-00B16DE052EB 75 END

print 'ECE2E2D7-C061-4A0B-923C-F59AF3C3534B ECR.216'

--ECE2E2D7-C061-4A0B-923C-F59AF3C3534B ECR.216 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'RSLT_D', cast(RSLT_D as varchar(200))as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.216'
, 'Неверно указан результат диспансеризации. '+ isnull(cast(RSLT_D as varchar(200)),'') + ' Группа здоровья 3 не соответствует методу оплаты ' + isnull(cast(comentsl.Method as varchar(20)) ,'')
from tempdb..TEMP_Z_SLUCH t0
join tempdb..TEMP_SLUCH t1 on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = t1.idcase and comentsl.SL_ID = t1.SL_ID
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId = t0.RSLT_DId
where
@q019type = 'X'
and t0.RSLT_DId is not null
and not exists (
	select 1
	from ies.R_NSI_METHODS method
	where comentsl.MethodId = method.DictionaryBaseID
	and ','+ ltrim(rtrim(cast(method.GZ_ZNACH as varchar(200)))) + ',' like '%,'+cast(v017.GZ as varchar(200))+',%'
)
--ECE2E2D7-C061-4A0B-923C-F59AF3C3534B ECR.216 END

--E973D10C-8C42-44EF-92AA-1E52406BA050 ECR.219 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.ds1 as ZN_POL ,@NSCHET, 'SL', null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.219'
, 'Неверный диагноз для группы здоровья 3а по результату диспансеризации (нет соответствия с Справочником ХНИЗ).'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId =z_sl.RSLT_DId
where
@q019type = 'X'
and z_sl.RSLT_DId is not null
and v017.GZ = '3а'
and not exists (select 1 from ies.R_NSI_CHRON_NEINF_ZAB mkb where sl.ds1 like mkb.name + '%'
and mkb.begin_date<=sl.DATE_2 and (mkb.end_date is null or mkb.end_date>=sl.DATE_2))
--E973D10C-8C42-44EF-92AA-1E52406BA050 ECR.219 END

--25AF7852-1E85-4C44-8B31-0341671BA612 ECR.220 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', sl.ds1 as ZN_POL ,@NSCHET, 'SL', null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.220'
,'Группа здоровья '+ cast (v017.GZ as varchar (200)) +' не соответствует диагнозу из перечня ХНИЗ.' 
--'Неверная группа здоровья 3а по результату диспансеризации для диагноза из Спаравочника ХНИЗ.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join [IES].[T_V017_DISP_RESULT] v017 on v017.V017DispResultId =z_sl.RSLT_DId
where
@q019type = 'X'
and z_sl.RSLT_DId is not null
and v017.GZ != '3а'
and exists (select 1 from ies.R_NSI_CHRON_NEINF_ZAB mkb where sl.ds1 like mkb.name + '%'
and mkb.begin_date<=sl.DATE_2 and (mkb.end_date is null or mkb.end_date>=sl.DATE_2))
and dbo.DateTimeDiff('year',pers.dr,sl.DATE_1) >= 18
--25AF7852-1E85-4C44-8B31-0341671BA612 ECR.220 END

--175B9381-D922-4A5A-A4A0-4675F7AA5641 137 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', comentsl.method as ZN_POL ,@NSCHET, 'COMENTSL', null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, '137'
, 'Отклонять случай с методом оплаты из списка 7*, предъявленный на оплату более 2 раз в текущем году'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
@q019type = 'X'
and comentsl.method in ('7.2.2','7.2.3','7.3.2','7.3.3','7.4.2','7.4.3','7.5.2','7.5.3','7.6.2','7.6.3','7.7.2','7.7.3','7.8.2','7.8.3','7.9.2','7.9.3','7.10','7.11','7.12' )
and rtrim(isnull(pacient.npolis,'')) != ''
and exists (
select sz0.npolis
from ies.t_schet s0
join ies.t_schet_zap sz0 on s0.schetid = sz0.schet
join ies.t_schet_sluch sl0 on sl0.schetzap = sz0.schetzapid
join ies.t_schet_sluch_accomplished z_sl0 on z_sl0.schetsluchaccomplishedid = sl0.schetsluchaccomplished
join ies.t_SCHET_SLUCH_COMENTSL comentsl0 on comentsl0.schetsluch = sl0.schetsluchid
join ies.R_NSI_METHODS method on method.CODE = isnull(comentsl0.METHOD,'null')
and method.[START_DATE]<=sl0.DATE_2 and (method.FINAL_DATE is null or method.FINAL_DATE>=sl0.DATE_2)
where
s0.isdelete = 0
and comentsl0.method in ('7.2.2','7.2.3','7.3.2','7.3.3','7.4.2','7.4.3','7.5.2','7.5.3','7.6.2','7.6.3','7.7.2','7.7.3','7.8.2','7.8.3','7.9.2','7.9.3','7.10','7.11','7.12' )
and sz0.npolis = pacient.npolis
and rtrim(isnull(sz0.npolis,'')) != ''
and s0.year = @year
and method.FIN_TYPE in (1,3)
and z_sl0.USL_OK = 3
group by sz0.npolis
having count(*)>=2
)
--175B9381-D922-4A5A-A4A0-4675F7AA5641 137 END

--89426596-95CC-4659-ABF3-C2C850ACE89F ECR.017 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select null, null as ZN_POL ,@NSCHET, 'SCHET', z_sl.N_ZAP, z_sl.ID_PAC, z_sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'EM.137'
, 'Отсутствует ЛИЦЕНЗИЯ  у МО на оказания медицинской помощи по виду МП(VIDPOM)='+convert(varchar(4), z_sl.VIDPOM) +' и ПРОФИЛЮ(PROFIL)=' + convert(varchar(3), sl.PROFIL)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
sl.PROFIL != 186
-- Виды помощи и Профили , для Диспансеризации профили не проверяем 
and NOT EXISTS (
		select D_START, DATA_E, D_TERM, luo.MedicalType, lp.V002Profile
		from ies.T_MO_LICENSE l
		join ies.t_MO_LICENSE_USL_OK luo on luo.MoLicense = l.MoLicenseId
		join ies.t_MO_LICENSE_PROFILE lp on lp.MoLicenseUslOk = luo.MoLicenseUslOkId
		where l.F003Mo = @code_mo
		and luo.MedicalType = z_sl.VIDPOM
		AND ((lp.V002Profile = sl.PROFIL and @q019type != 'X' ) or @q019type = 'X' )
		AND CONVERT(DATE, sl.DATE_2) >= l.D_START AND (isnull(D_TERM,DATA_E) is null or isnull(D_TERM,DATA_E) >= sl.DATE_2)
		)
-- Виды помощи только Для 'U07', 'J12'
AND NOT EXISTS (
		select D_START, DATA_E, D_TERM, luo.MedicalType, lp.V002Profile
		from ies.T_MO_LICENSE l
		join ies.t_MO_LICENSE_USL_OK luo on luo.MoLicense = l.MoLicenseId
		join ies.t_MO_LICENSE_PROFILE lp on lp.MoLicenseUslOk = luo.MoLicenseUslOkId
		where l.F003Mo = @code_mo
		AND LEFT(sl.DS1,3) IN ('U07', 'J12')
		and luo.MedicalType = z_sl.VIDPOM
		AND CONVERT(DATE, sl.DATE_2) >= l.D_START AND (isnull(D_TERM,DATA_E) is null or isnull(D_TERM,DATA_E) >= sl.DATE_2)
		)
-- Виды помощи только , для Диспансеризации профили не проверяем считать ВидыМП 2 и 21 одним и тем же
AND NOT EXISTS (
		select D_START, DATA_E, D_TERM, luo.MedicalType, lp.V002Profile
		from ies.T_MO_LICENSE l
		join ies.t_MO_LICENSE_USL_OK luo on luo.MoLicense = l.MoLicenseId
		join ies.t_MO_LICENSE_PROFILE lp on lp.MoLicenseUslOk = luo.MoLicenseUslOkId
		where l.F003Mo = @code_mo
		AND ((lp.V002Profile = sl.PROFIL and @q019type != 'X' ) or @q019type = 'X' )
		AND luo.MedicalType IN ('2', '21')   AND z_sl.VIDPOM IN ('2', '21')
		AND CONVERT(DATE, sl.DATE_2) >= l.D_START AND (isnull(D_TERM,DATA_E) is null or isnull(D_TERM,DATA_E) >= sl.DATE_2))
--89426596-95CC-4659-ABF3-C2C850ACE89F ECR.017 END

--CF4E3119-8D71-4D06-ACB5-EA6CB6752BDA ECR.073 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', null, sl.ID_PAC, null as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.073'
, 'Недопустимое сочетание МКБ='+DS1+' для '+iif(DET=0, 'взрослых', 'детей')+' и SHRM_N='+convert(varchar(10), SHRM_N)+' для метода оплаты='+comentsl.METHOD
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join tempdb..TEMP_PACIENT pacient on pacient.N_ZAP = z_sl.N_ZAP
where
comentsl.method is not null
and comentsl.SHRM_N is not null
and not exists (
	select ee.DictionaryBaseID
	from ies.R_NSI_EXCEPTIONS_ERRORS ee
	left join ies.R_NSI_EXCEPTIONS_ERRORS_MKB eem on ee.DictionaryBaseId = eem.ref
	where ee.is_actual = 1
	and ee.DeletionMark= 0
	and (ee.VD = sl.VD_5_6 or ee.VD='')
	and ee.is_use  = 1
	and ee.USL_OK = 3
	and ee.PR_REAB = comentsl.SHRM_N
	and (ee.METHOD = comentsl.METHOD or ee.METHOD is null)
	and eem.IsCheck = 1
	and sl.DS1 = eem.DS
	and ee.OSN2 = 'МКБ_Для_ШРМ'
)
--CF4E3119-8D71-4D06-ACB5-EA6CB6752BDA ECR.073 END

--587944D0-BDA9-4DE0-884E-03C947E6F784 ECR.261 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSL', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'SL_KOEF', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.261'
,'Значение IDSL=1273 применятся только для PROFIL не равен {12, 18}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
--join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
left join tempdb..TEMP_SL_KOEF sl_koef on sl.idcase = sl_koef.idcase and sl.SL_ID = sl_koef.SL_ID
where
((ZSLType = 'Стационар' and @q019type != 'T') or ZSLType = 'ДневнойСтационар')
and sl_koef.sl_ID is not null
and sl_koef.IDSL = 1273 
and sl.profil in (12, 18)
--587944D0-BDA9-4DE0-884E-03C947E6F784 ECR.261 END

--CB91E9F7-7978-49A8-A704-402A926A6101 ECR.075 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', null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.075'
, 'При сеансах диализа и т.п. должны быть указаны услуги.'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and sl.HasSession = 1
and not exists (select 1 from tempdb..TEMP_USL usl where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID)
--CB91E9F7-7978-49A8-A704-402A926A6101 ECR.075 END

--9FAF85E5-B12F-459D-BDA1-2560B67C28FC ECR.097 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', null, sl.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.075'
--, 'При сеансах диализа и т.п. должны быть указаны услуги.'
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
--where
--usl.SpecsId is not null
--and not exists (
--	select 1 
--	from 
--)


--Результат = истина;  //- по умолчанию действует
	
--	//Сверим со справочником теги коэфициенты значения
--	Запрос = Новый Запрос;
--	Запрос.Текст = "ВЫБРАТЬ
--	|	ТэгиСКоэфициентами.ТЭГ,
--	|	ТэгиСКоэфициентами.Значение,
--	|	ТэгиСКоэфициентами.НачалоДействия,
--	|	ТэгиСКоэфициентами.КонецДействия
--	|ИЗ
--	|	Справочник.ТэгиСКоэфициентами КАК ТэгиСКоэфициентами
--	|Где ТэгиСКоэфициентами.ТЭГ = &tag и   ( &data между ТэгиСКоэфициентами.НачалоДействия и ТэгиСКоэфициентами.КонецДействия) 
--	| и  ТэгиСКоэфициентами.Значение =  &znach";
--	Запрос.УстановитьПараметр("tag","CODE_MES1");
--	Запрос.УстановитьПараметр("znach",Код);	
--	Запрос.УстановитьПараметр("data",ДатаОкончания);
--	Выборка = Запрос.Выполнить().Выбрать();
	
--	Если  Выборка.Количество() = 0 Тогда 
--		Результат = Ложь;
--	КонецЕсли;
	
--	//Сверим со справочником специальные коды для случаев стационара
	
	
--	Если  Результат = Ложь Тогда		
--		Запрос = Новый Запрос;
--		Запрос.Текст = "ВЫБРАТЬ
--		|	ТарифыСтационараСпециальные.Код,
--		|	ТарифыСтационараСпециальные.НачалоДействия,
--		|	ТарифыСтационараСпециальные.КонецДействия
--		|ИЗ
--		|	Справочник.ТарифыСтационараСпециальные КАК ТарифыСтационараСпециальные
--		| Где  ТарифыСтационараСпециальные.Код = &kode 
--		|	и  &data между ТарифыСтационараСпециальные.НачалоДействия и ТарифыСтационараСпециальные.КонецДействия ";
--		Запрос.УстановитьПараметр("kode",Код);
--		Запрос.УстановитьПараметр("data",ДатаОкончания);
--		Выборка = Запрос.Выполнить().Выбрать();
--		Если  Выборка.Количество() = 0 Тогда 
--			Результат = Ложь;
--		Иначе 
--			Результат = Истина;
--		КонецЕсли; 		
--	КонецЕсли;
	
--	Возврат Результат;

--	update usl set IsSession = 1
--	from tempdb..TEMP_Z_SLUCH z_sl
--	join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--	join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
--	join ies.R_NSI_SPECS specs on specs.DictionaryBaseID = usl.SpecsId
--	where usl.SpecsId is not null and specs.C_TYPE = 1
--9FAF85E5-B12F-459D-BDA1-2560B67C28FC ECR.097 END

--669FC027-5483-4693-A861-AC029814DA43 ECR.124 BEGIN
--update usl set IsSession = 1
--from tempdb..TEMP_Z_SLUCH z_sl
--join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
--join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
--join ies.R_NSI_SPECS specs on specs.DictionaryBaseID = usl.SpecsId
--where usl.SpecsId is not null and specs.C_TYPE = 1
--669FC027-5483-4693-A861-AC029814DA43 ECR.124 END

--2961CE78-26BA-41CE-8353-FF474334EA46 ECR.126 BEGIN

--2961CE78-26BA-41CE-8353-FF474334EA46 ECR.126 END

--B80ACBE4-A939-4BBA-8892-23AAB3B70242 ECR.165 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DG', null as ZN_POL ,@NSCHET, 'COMENTSL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.165'
,'Для метода '+comentsl.method+' Неверно расчитана ДГ='+convert(varchar(10),comentsl.DG)+', Верная='+convert(varchar(10), dbo.RASCHET_DGRUP2017(comentsl.DG, comentsl.method, pers.DR, sl.DATE_1, pers.W)) + '. DATE_1=' + convert(varchar(10), sl.DATE_1, 104)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
JOIN ies.R_NSI_METHODS method on comentsl.MethodId = method.DictionaryBaseID
JOIN tempdb..TEMP_PERS pers on z_sl.ID_PAC = pers.ID_PAC
where
@q019type = 'X'
and comentsl.DG is not null
and comentsl.DG != dbo.RASCHET_DGRUP2017(comentsl.DG, comentsl.method, pers.DR, sl.DATE_1, pers.W)
--B80ACBE4-A939-4BBA-8892-23AAB3B70242 ECR.165 END

--0D7F5A0F-521D-4D04-BAEC-035548F7BE6F ECR.258 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(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.258'
,'Признак использования КСЛП SL_K не определён при поданном CRIT'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
where
((ZSLType = 'Стационар' and @q019type != 'T') or ZSLType = 'ДневнойСтационар')
and ksg_kpg.IT_SL = 0 and ksg_kpg.SL_K = 0
and isnull(crit.CRIT,'') != ''
and exists (
	select *
	from ies.R_NSI_KSLP_G nsi
	where
	nsi.ADD_CRIT = crit.CRIT
	and nsi.[START_DATE]<=sl.DATE_2 and (nsi.FINAL_DATE is null or nsi.FINAL_DATE>=sl.DATE_2)
)
--0D7F5A0F-521D-4D04-BAEC-035548F7BE6F ECR.258 END

--587944D0-BDA9-4DE0-884E-03C947E6F784 ECR.261 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSL', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'SL_KOEF', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.261'
,'Значение IDSL=1273 применятся только для PROFIL не равен {12, 18}'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
--join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
join tempdb..TEMP_SL_KOEF sl_koef on sl.idcase = sl_koef.idcase and sl.SL_ID = sl_koef.SL_ID
where
((ZSLType = 'Стационар' and @q019type != 'T') or ZSLType = 'ДневнойСтационар')
and sl_koef.IDSL = 1273 
and sl.profil in (12, 18)
--587944D0-BDA9-4DE0-884E-03C947E6F784 ECR.261 END

--3573FF53-1BDA-4879-86B2-593FC92BA911 ECR.262 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_PG', convert(varchar(10), ksg_kpg.KSG_PG) as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.262'
,'Неверное значение признака подгруппы КСГ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
join tempdb..TEMP_SL_KOEF sl_koef on sl.idcase = sl_koef.idcase and sl.SL_ID = sl_koef.SL_ID
where
(ZSLType = 'Стационар' and @q019type != 'T')
and ksg_kpg.KSG_PG = 0
--истина когда есть с 1, ложь - иначе
and exists (
	select 1
	from ies.R_NSI_PRICE_S nsi
	where
	nsi.KSG_CODE = ksg_kpg.N_KSG
	and isnull(nsi.KSG_PG, 0) = 1
	and nsi.[START_DATE]<=sl.DATE_2 and (nsi.FINAL_DATE is null or nsi.FINAL_DATE>=sl.DATE_2)
)

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_PG', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.262'
,'Неверное значение признака подгруппы КСГ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
join tempdb..TEMP_SL_KOEF sl_koef on sl.idcase = sl_koef.idcase and sl.SL_ID = sl_koef.SL_ID
where
(ZSLType = 'Стационар' and @q019type != 'T')
and ksg_kpg.KSG_PG = 1
--истина когда есть с 1, ложь - иначе
and not exists (
	select 1
	from ies.R_NSI_PRICE_S nsi
	where
	nsi.KSG_CODE = ksg_kpg.N_KSG
	and isnull(nsi.KSG_PG, 0) = 1
	and nsi.[START_DATE]<=sl.DATE_2 and (nsi.FINAL_DATE is null or nsi.FINAL_DATE>=sl.DATE_2)
)

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_PG', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'KSG_KPG', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.262'
,'Неверное значение признака подгруппы КСГ'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
join tempdb..TEMP_CRIT crit on sl.idcase = crit.idcase and sl.SL_ID = crit.SL_ID
join tempdb..TEMP_SL_KOEF sl_koef on sl.idcase = sl_koef.idcase and sl.SL_ID = sl_koef.SL_ID
where
ZSLType = 'ДневнойСтационар'
and ksg_kpg.KSG_PG = 0
--истина когда есть с 1, ложь - иначе
and exists (
	select 1
	from ies.R_NSI_PRICE_C nsi
	where
	nsi.KSG_CODE = ksg_kpg.N_KSG
	and isnull(nsi.KSG_PG, 0) = 1
	and nsi.[START_DATE]<=sl.DATE_2 and (nsi.FINAL_DATE is null or nsi.FINAL_DATE>=sl.DATE_2)
)
--select * from ies.R_NSI_PRICE_C --dnstac
--select * from ies.R_NSI_PRICE_S --stac
--3573FF53-1BDA-4879-86B2-593FC92BA911 ECR.262 END

--13D38600-8962-4C8F-8B7F-213C2D7D43E7 ECR.368 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.368'
,'МКБ не допустим для случаев USL_OK='+convert(varchar(1), USL_OK)+' (НЕ KSG_USED)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'Стационар') and @q019type != 'T'
and left(sl.ds1, 3) not in ('O82', 'O84')
and sl.ds1 not in ('Z93.2', 'Z93.3')
and not exists (select 1 from tempdb..TEMP_USL usl where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID and usl.ksgnid is not null)
and (select top 1 KSG_USED_1 from ies.T_MKB mkb where mkb.MCBCode = sl.DS1 and Actual = 1) = 0

insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', convert(varchar(10), ksg_kpg.IT_SL) as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.368'
,'МКБ не допустим для случаев USL_OK='+convert(varchar(1), USL_OK)+' (НЕ KSG_USED)'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
join tempdb..TEMP_KSG_KPG ksg_kpg on sl.idcase = ksg_kpg.idcase and sl.SL_ID = ksg_kpg.SL_ID
where 
(ZSLType = 'ДневнойСтационар') and @q019type != 'T'
and left(sl.ds1, 3) not in ('O82', 'O84')
and sl.ds1 not in ('Z93.2', 'Z93.3')
and not exists (select 1 from tempdb..TEMP_USL usl where sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID and usl.ksgn_cid is not null)
and (select top 1 KSG_USED_2 from ies.T_MKB mkb where mkb.MCBCode = sl.DS1 and Actual = 1) = 0
--13D38600-8962-4C8F-8B7F-213C2D7D43E7 ECR.368 END

--3C659C84-2170-44D8-BBC4-529822104C64 ECR.444 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', sl.profil as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.444'
,'Несоответствие Профиля='+ convert(varchar(3), sl.profil) +'и Специальности='+convert(varchar(3), sl.PRVS)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
@q019type != 'X'
and sl.prvs is not null
and not exists (
	select 1
	from [IES].R_NSI_V002_V021_SOOTV nsi
	where nsi.v002 = sl.profil and sl.prvs = nsi.v021
	--and nsi.begin_date<=sl.DATE_2 and (nsi.end_date is null or nsi.end_date>=sl.DATE_2)
)

update t0
set COMMENT += '. Допустимые специальности для данного профиля: ' + isnull((
	SELECT v002s = STUFF(CAST((
	SELECT [text()] = ', ' + convert(varchar(3), v021)
	FROM [IES].R_NSI_V002_V021_SOOTV nsi
	join tempdb..TEMP_SLUCH sl on sl.IDCASE = t0.IDCASE and sl.SL_ID = t0.SL_ID
	where nsi.V002 = sl.profil
	FOR XML PATH(''), TYPE) AS VARCHAR(max)), 1, 2, '')
),'null')
from #Errors t0
where t0.OSHIB = 'ECR.444'
--3C659C84-2170-44D8-BBC4-529822104C64 ECR.444 END

--862F2B4D-7036-463F-B1B4-685B8F3275D3 ECR.459 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', null as ZN_POL ,@NSCHET, 'SL', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.459'
,'Профиль='+ convert(varchar(3), sl.profil) +'не соответствует условия оказания МП='+convert(varchar(1), z_sl.USL_OK)
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
where
(ZSLType = 'Стационар' or ZSLType = 'ДневнойСтационар')
and sl.profil is not null
and not exists (
	select *
	from ies.R_NSI_PROFIL_USL_OK_SOOTV nsi
	where nsi.profil = sl.profil and z_sl.usl_ok = nsi.usl_ok
	and nsi.beg_date<=sl.DATE_2 and (nsi.end_date is null or nsi.end_date>=sl.DATE_2)
)
--862F2B4D-7036-463F-B1B4-685B8F3275D3 ECR.459 END



--0D8B97EA-43CC-4A54-BAEC-D40E6A58DE17 ECR.448 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, 'MR_USL_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.448'
,'Для диспансерного наблюдения в MR_USL_N должен быть специалист ведущий самостоятельный приём'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where SpecIndependNoReabCount = 0
and comentsl.method in ('1', '1.1', '10.3', '10.4')
--and (comentsl.method like '8.%'
--or comentsl.method = '3.1' 
--or sl.VidPomOPMP = 'AN'
--)
and sl.VidPomOPMP = 'AN'
--0D8B97EA-43CC-4A54-BAEC-D40E6A58DE17 ECR.448 END

--C2C2EDCA-3FF5-46AC-9B71-45013D87A0A5 ECR.449 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, 'MR_USL_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, null as IDSERV, 'ECR.449'
,'Обращение, метод оплаты 10.4. / 1, имеет количество посещений к одному специалисту менее 2 в разные дни и/или количество посещений к разным специалистам менее 2'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
where SpecIndependNoReabCount = 1
and comentsl.method in ('1', '10.4')
--C2C2EDCA-3FF5-46AC-9B71-45013D87A0A5 ECR.449 END

	select sl.SL_ID, sl.IDCASE, usl.IDSERV,usl.DATE_IN, usl.DATE_OUT, mr_usl_n.PRVS,  count(*) as kol
	into #temp_mr
	from tempdb..TEMP_MR_USL_N mr_usl_n 
	join tempdb..TEMP_USL usl on mr_usl_n.idcase = usl.idcase and mr_usl_n.SL_ID = usl.SL_ID and usl.IDSERV=mr_usl_n.IDSERV
	join  tempdb..TEMP_SLUCH sl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
	group by  sl.SL_ID, sl.IDCASE, usl.IDSERV,usl.DATE_IN, usl.DATE_OUT, mr_usl_n.PRVS


--8D67E188-1CB3-42E1-9F31-C576EE7F3377 ECR.450 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, 'MR_USL_N', sl.N_ZAP, sl.ID_PAC, sl.IDCASE as IDCASE, sl.SL_ID as SL_ID, usl.IDSERV as IDSERV, 'ECR.450'
,'Посещение,  метод оплаты 10.3/1.1, имеет  количество посещений к специалисту(ам) более  одного'
from tempdb..TEMP_Z_SLUCH z_sl
join tempdb..TEMP_SLUCH sl on z_sl.idcase = sl.idcase
JOIN tempdb..TEMP_COMENTSL comentsl on comentsl.idcase = sl.idcase and comentsl.SL_ID = sl.SL_ID
join tempdb..TEMP_USL usl on sl.idcase = usl.idcase and sl.SL_ID = usl.SL_ID
where 
(select top 1 kol from #temp_mr t
join tempdb..TEMP_MR_USL_N mr_usl_n on mr_usl_n.idcase = t.idcase and mr_usl_n.SL_ID = t.SL_ID and t.IDSERV=mr_usl_n.IDSERV
)>1
and comentsl.method in ('1.1', '10.3')
--8D67E188-1CB3-42E1-9F31-C576EE7F3377 ECR.450 END


--new_flk end

--
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'REG01'
, 'Обязательное поле для D файлов'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
join tempdb..TEMP_ZGLV z on 1=1
where
SUBSTRING(z.[FILENAME],1,1)='D'
and --@q019type in ('H')
t2.PRVS_C is null
and DATE_Z_2 >= convert(date,'01.04.2024',104)


insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDDOKT_C', @NSCHET as ZN_POL ,@NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'REG02'
, 'Обязательное поле для D файлов'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
join tempdb..TEMP_ZGLV z on 1=1
where
SUBSTRING(z.[FILENAME],1,1)='D'
and --@q019type in ('H')
t2.IDDOKT_C is null
and DATE_Z_2 >= convert(date,'01.04.2024',104)

--tim

--B234BA14-4375-463B-9D6F-BDD415038B12 ECR.470 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'REGNUM', t0.REGNUM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.470'
,'LEK_PR/REGNUM не соответствует справочнику N020'
from tempdb..TEMP_SLUCH_LEK_PR 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 t0.REGNUM is not null and t2.ID_LEKP is null
--B234BA14-4375-463B-9D6F-BDD415038B12 ECR.470 END

--167A4501-28B4-433C-9E47-AF403EF67446 ECR.471 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ED_IZM', t0.ED_IZM as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.471'
,'LEK_PR/ED_IZM Обязательно для заполнения, если в справочнике V031 заполнен признак Признак обязательности указания МНН'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
cross apply(select top 1 right(t4.ScheDrugGrCd,1) as DrugGroupCode from ies.T_V032_DRUG_GR_REG t4 with(nolock) where t4.ScheDrugGrCd = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2))t4
join ies.T_V031_GROUP_DRUG v031 with(nolock) on v031.DrugGroupCode = t4.DrugGroupCode and v031.ManIndMNN = 1
where t0.ED_IZM is null
--167A4501-28B4-433C-9E47-AF403EF67446 ECR.471 END

--9491A746-0891-484A-9DB4-C7E1D13F4E08 ECR.472 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DOSE_INJ', t0.DOSE_INJ as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.472'
,'LEK_PR/DOSE_INJ Обязательно для заполнения, если в справочнике V031 заполнен признак Признак обязательности указания МНН'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
cross apply(select top 1 right(t4.ScheDrugGrCd,1) as DrugGroupCode from ies.T_V032_DRUG_GR_REG t4 with(nolock) where t4.ScheDrugGrCd = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2))t4
join ies.T_V031_GROUP_DRUG v031 with(nolock) on v031.DrugGroupCode = t4.DrugGroupCode and v031.ManIndMNN = 1
where t0.DOSE_INJ is null
--9491A746-0891-484A-9DB4-C7E1D13F4E08 ECR.472 END

--9CB7DCBF-7042-4457-9C89-097D10CD42A6 ECR.473 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD_INJ', t0.METHOD_INJ as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.473'
,'LEK_PR/METHOD_INJ Обязательно для заполнения, если в справочнике V031 заполнен признак Признак обязательности указания МНН'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
cross apply(select top 1 right(t4.ScheDrugGrCd,1) as DrugGroupCode from ies.T_V032_DRUG_GR_REG t4 with(nolock) where t4.ScheDrugGrCd = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2))t4
join ies.T_V031_GROUP_DRUG v031 with(nolock) on v031.DrugGroupCode = t4.DrugGroupCode and v031.ManIndMNN = 1
where t0.METHOD_INJ is null
--9CB7DCBF-7042-4457-9C89-097D10CD42A6 ECR.473 END

--8EF79654-2953-4CCC-8CFF-0509D7F21947 ECR.474 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'COL_INJ', t0.COL_INJ as ZN_POL ,@NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.474'
,'LEK_PR/COL_INJ Обязательно для заполнения, если в справочнике V031 заполнен признак Признак обязательности указания МНН'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
cross apply(select top 1 right(t4.ScheDrugGrCd,1) as DrugGroupCode from ies.T_V032_DRUG_GR_REG t4 with(nolock) where t4.ScheDrugGrCd = t0.CODE_SH and DATEBEG<=DATE_Z_2 and (DATEEND is null or DATEEND>=DATE_Z_2))t4
join ies.T_V031_GROUP_DRUG v031 with(nolock) on v031.DrugGroupCode = t4.DrugGroupCode and v031.ManIndMNN = 1
where t0.COL_INJ is null
--8EF79654-2953-4CCC-8CFF-0509D7F21947 ECR.474 END

--59AEAED1-9DB8-4DC3-81CD-FC999D5A9DB8 ECR.475 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MR_USL_N', null as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.475'
,'Раздел SL/MR_USL_N Сведения о медицинских   работниках, выполнивших услугу   должны быть заполнены!'
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 not in ('C', 'T')
and t2.IDSERV is null
--59AEAED1-9DB8-4DC3-81CD-FC999D5A9DB8 ECR.475 END

--59AEAED1-9DB8-4DC3-81CD-FC999D5A9DB8 ECR.475 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MR_USL_N', null as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.475'
,'Раздел SL/MR_USL_N Сведения о медицинских   работниках, выполнивших услугу   должны быть заполнены!'
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 not in ('C', 'T')
and t2.PRVS is null
--59AEAED1-9DB8-4DC3-81CD-FC999D5A9DB8 ECR.475 END

--ECD0A777-1DB1-411C-B8FA-441CC8DB405A ECR.476 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'MR_USL_N', null as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.476'
--,'Раздел SL/MR_USL_N Сведения о медицинских   работниках, выполнивших услугу   должны быть заполнены!'
--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 not in ('C', 'T')
--and t2.PRVS is null
--ECD0A777-1DB1-411C-B8FA-441CC8DB405A ECR.476 END

--E9904CB7-67C7-4106-92C5-AEA8CB892B6A ECR.477 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', t0.PRVS as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.477'
,'Раздел SL/USL Сведения о медицинских   работниках, выполнивших услугу  должны быть заполнены!'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_USL t0 with(nolock) on t0.IDCASE = t1.IDCASE
where @q019type in ('C', 'T')
and t0.PRVS is null
--E9904CB7-67C7-4106-92C5-AEA8CB892B6A ECR.477 END

--C0B61197-A3BE-48FD-889C-44F8C4C184B3 ECR.478 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'PRVS', t0.PRVS as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.477'
--,'Раздел SL/USL Сведения о медицинских   работниках, выполнивших услугу  должны быть заполнены!'
--from tempdb..TEMP_Z_SLUCH t1 with(nolock)
--join tempdb..TEMP_USL t0 with(nolock) on t0.IDCASE = t1.IDCASE
--where @q019type in ('C', 'T')
--and t0.PRVS is null
--C0B61197-A3BE-48FD-889C-44F8C4C184B3 ECR.478 END

--4FD5A450-AB1D-4E2B-A8EA-F919CA78B97E ECR.481 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', t0.PROFIL as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.481'
,'Раздел SL/PROFIL Сведения о профиле не актуальны согласно 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 not in ('X')
--and t0.PROFIL is not null
and t2.IDPR is null
--4FD5A450-AB1D-4E2B-A8EA-F919CA78B97E ECR.481 END

--89A23CA1-A919-4D64-B387-53A2E463FEDE ECR.482 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL', t0.PROFIL as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.482'
,'Раздел SL/USL/PROFIL  Сведения о профиле не актуальны согласно V021'
from tempdb..TEMP_Z_SLUCH t1 with(nolock)
join tempdb..TEMP_USL t0 with(nolock) on t0.IDCASE = t1.IDCASE
where @q019type not in ('X')
and t0.PROFIL is null
--89A23CA1-A919-4D64-B387-53A2E463FEDE ECR.482 END

--2CB61218-53DF-426E-87CE-80B90818F793 ECR.483 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', t0.PRVS as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.483'
,'Раздел SL/PRVS Сведения о специальности не актуальны согласно 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 t0.PRVS is not null
and t2.CODE_SPEC is null
--2CB61218-53DF-426E-87CE-80B90818F793 ECR.483 END

--854CBA9E-B4CF-4E31-951E-3A4FFE73B966 ECR.484 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', t0.PRVS as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.484'
,'Раздел SL/USL/PRVS  Сведения о специальности не актуальны согласно 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 t0.PRVS is not null
and t2.CODE_SPEC is null
--854CBA9E-B4CF-4E31-951E-3A4FFE73B966 ECR.484 END

--6EEE1C73-1954-46EE-9FE4-8C23FC9AD5DD ECR.485 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PRVS', t0.PRVS as ZN_POL ,@NSCHET, 'MR_USL_N', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'ECR.485'
,'Раздел SL/USL/MR_USL_N/PRVS  Сведения о специальности не актуальны согласно 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 t2.PRVS is not null
and t3.CODE_SPEC is null
--and t0.P_OTK=0
--6EEE1C73-1954-46EE-9FE4-8C23FC9AD5DD ECR.485 END

--04499333-D445-4ACA-A7DB-A356A7391EBE ECR.499 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_1', t1.DATE_1 as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.499'
,'Дата начала оказания мед. помощи ранее даты рождения'
from tempdb..TEMP_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar(36),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
where t1.DATE_1 < t2.DR
--04499333-D445-4ACA-A7DB-A356A7391EBE ECR.499 END

--25F35398-D743-4CC9-9C49-CD8EF83131BA EMF.030 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DR', t2.DR as ZN_POL ,@NSCHET, 'PERS', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.030'
,'Дата начала оказания мед. помощи ранее даты рождения'
from tempdb..TEMP_SLUCH t1 with(nolock)
join tempdb..TEMP_PERS t2  with(nolock) on convert(varchar(36),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
where t2.DR is null
--25F35398-D743-4CC9-9C49-CD8EF83131BA EMF.030 END

--7B150713-48A1-4CF9-9E89-7AA4B374C39F ECR.507 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, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.507'
, 'Метод оплаты 4.8 может подавать только МО=560023'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where t2.[METHOD] in ('4.8')
and t0.lpu not in ('560023')
--7B150713-48A1-4CF9-9E89-7AA4B374C39F ECR.507 END

--62339965-C9B7-48BF-B08E-CDBB1B30E708 ECR.523 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'IDSP', t0.IDSP as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.523'
, 'Если МКБ '+ cast(t1.DS1 as varchar(10)) +', то IDSP должно быть равно 31'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
where t0.IDSP <> '31'
and left(t1.DS1,3) in ('Z23','Z24','Z25','Z26','Z27','Z28')
--62339965-C9B7-48BF-B08E-CDBB1B30E708 ECR.523 END

--3557CDFE-C60C-4C99-B2C4-115C9B2D3BEE EMF.001 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_MD', t0.CODE_MD as ZN_POL, @NSCHET, 'CODE_MD', null, null, null as IDCASE,  null as SL_ID, null as IDSERV, 'EMF.001'
, 'Не заполнен CODE_MD в записи в файле персональных данных!'
from tempdb..TEMP_CODE_MD t0 with(nolock)
where t0.CODE_MD is null 
--3557CDFE-C60C-4C99-B2C4-115C9B2D3BEE EMF.001 END

--577D4403-AD73-49C9-AA29-CCFECD92705C EMF.005 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', t0.NPR_DATE as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'EMF.005'
, 'NPR_DATE Поле обязательно к заполнению если присутствует NPR_MO'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where t0.NPR_DATE is null
and t0.NPR_MO is not null
--577D4403-AD73-49C9-AA29-CCFECD92705C EMF.005 END

--716AF5E6-D1EF-4C0F-BB6E-2D97CFC652F4 EMF.006 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', t0.NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'EMF.006'
, 'NPR_MO Поле обязательно к заполнению если присутствует NPR_DATE'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where t0.NPR_MO is null
and t0.NPR_DATE is not null
--716AF5E6-D1EF-4C0F-BB6E-2D97CFC652F4 EMF.006 END

--E915A203-D3DB-4081-A43E-427E5495CC32 EMF.007 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'PROFIL_K', t0.PROFIL_K as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.007'
, 'Не заполненно поле PROFIL_K обязательно для случаев Стационара / Дневного стационара'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where t0.PROFIL_K is null
and t1.USL_OK in (1, 2)
--E915A203-D3DB-4081-A43E-427E5495CC32 EMF.007 END

--92E51F9F-445A-400F-A7E5-0D0E9D081CAF EMF.008 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MET_ISSL', t0.MET_ISSL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.008'
, 'NAPR/ 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 t0.MET_ISSL is null
and t0.NAPR_V=3
--92E51F9F-445A-400F-A7E5-0D0E9D081CAF EMF.008 END

--B12B31B2-3C53-4728-9713-9814747E1D24 EMF.009 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MET_ISSL', t0.MET_ISSL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.009'
, 'NAPR/ 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 t0.MET_ISSL is not null
and t0.NAPR_V<>3
--B12B31B2-3C53-4728-9713-9814747E1D24 EMF.009 END

--A90E7001-E9D3-453A-B8FB-ACE1E31995F5 EMF.010 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_USL', t0.NAPR_USL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.010'
, 'NAPR/ 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 NAPR_USL is null
and MET_ISSL is not null
--A90E7001-E9D3-453A-B8FB-ACE1E31995F5 EMF.010 END

--DF174C9A-E755-47B0-BBFF-F0B14694AE92 EMF.011 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_USL', t0.NAPR_USL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.011'
, 'NAPR/ 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 NAPR_USL is not null
and MET_ISSL is null
--DF174C9A-E755-47B0-BBFF-F0B14694AE92 EMF.011 END

--96F2A39F-0B7C-41B6-B325-71693E3C1F46 EMF.012 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_DATE', t0.NAPR_DATE as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.012'
, 'NAPR/ NAPR_DATE обязательно к заполнению'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where NAPR_DATE is null
--96F2A39F-0B7C-41B6-B325-71693E3C1F46 EMF.012 END

--C375CEE6-031F-49FB-BCB6-E30080BA65AC EMF.014 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, 'CONS', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.014'
, '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 not in ('C','T')
AND EXISTS (
		SELECT TOP 1 1
		FROM tempdb..TEMP_CONS t3
		WHERE t3.SL_ID = t0.SL_ID
			AND t3.IDCASE = t0.IDCASE
		)
--C375CEE6-031F-49FB-BCB6-E30080BA65AC EMF.014 END

--8B0799D8-75C1-46C4-9427-9B99663EBBF0 EMF.015 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_USL', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.015'
, 'При заполеннии  DS_ONK = 1   ONK_USL  не заполняются'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.idcase = t1.idcase
where t0.DS_ONK = 1
and exists(SELECT TOP 1 1 FROM tempdb..TEMP_ONK_USL t2 with(nolock) where t0.SL_ID = t2.SL_ID and t0.IDCASE = t2.IDCASE)
--8B0799D8-75C1-46C4-9427-9B99663EBBF0 EMF.015 END

--39C571D7-7188-4E25-8B47-A5808B4B8348 EMF.016 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAZ', null as ZN_POL ,@NSCHET, 'NAZ', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.016'
, 'Для диспансеризации направления NAPR указываются в разделе назначений NAZ в соответствии с реглментом информ. взаим.'
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 EXISTS (
		SELECT TOP 1 1
		FROM tempdb..TEMP_NAPR t1
		WHERE t1.SL_ID = t0.SL_ID
			AND t1.IDCASE = t0.IDCASE
		)
--39C571D7-7188-4E25-8B47-A5808B4B8348 EMF.016 END

--01C11FFF-EFB0-45BD-89D2-DCBEC39A16F5 EMF.017 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', t0.C_ZAB as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.017'
, 'Поле 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')
--01C11FFF-EFB0-45BD-89D2-DCBEC39A16F5 EMF.017 END

--01C11FFF-EFB0-45BD-89D2-DCBEC39A16F5 EMF.017 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'C_ZAB', t0.C_ZAB as ZN_POL ,@NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.017'
, 'Поле 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
--01C11FFF-EFB0-45BD-89D2-DCBEC39A16F5 EMF.017 END

--979057CC-FA07-4608-A98A-15E8D989E1F3 EMF.018 END
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, 'CONS', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.018'
, 'CONS обязательно для счетов ЗНО'
from tempdb..TEMP_SLUCH t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where
@q019type in ('С')
and not exists (select top 1 1 from tempdb..TEMP_CONS t01 where t01.sl_id = t0.sl_id and t01.idcase = t0.idcase)
--979057CC-FA07-4608-A98A-15E8D989E1F3 EMF.018 BEGIN

--75E1B96D-EB78-4BB5-A1C2-78A5C0E401B7 EMF.019 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, 'CONS', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.019'
, '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 ('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
					)
				)
			))
--75E1B96D-EB78-4BB5-A1C2-78A5C0E401B7 EMF.019 END

--16F65171-71ED-4449-BA73-0B80F193E8F3 EMF.031 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_MO', t0.NPR_MO as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'EMF.031'
, 'Для данного случая необходимо заполнить код направившей МО в поле  NPR_MO'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where ((FOR_POM=3 and USL_OK in (1,2)) or (USL_OK=2 and USL_OK in (1)))
and NPR_MO is null
--16F65171-71ED-4449-BA73-0B80F193E8F3 EMF.031 END

--7480E1DD-5A82-4FBA-9D25-E05D6C3A1F71 EMF.032 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', t0.NPR_DATE as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'EMF.032'
, 'Для данного случая необходимо заполнить дату направления  NPR_DATE'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
where ((FOR_POM=3 and USL_OK in (1,2)) or (USL_OK=2 and USL_OK in (1)))
and NPR_DATE is null
--7480E1DD-5A82-4FBA-9D25-E05D6C3A1F71 EMF.032 END

--E1F99D57-5E45-4E86-943B-38F709F1BE7F EMF.034 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NPR_DATE', t1.NPR_DATE as ZN_POL ,@NSCHET, 'Z_SL', t0.N_ZAP, t0.ID_PAC, t0.idcase as IDCASE, null as SL_ID, null as IDSERV, 'EMF.032'
, 'Поле SL/WEI Масса тела (кг)  Обязательно для заполнения с 01.01.2022, если в DS1 указано значение заболевания U07.1 или U07.2 и REAB = 0 и CRIT <> STT5 и USL_OK = 1 и DS2 <> IN (O00-O99, Z34-Z35) и возраст пациента на дату начала лечения больше или равно 18 лет'
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 t1.usl_ok=1
and DATEDIFF(YEAR,t2.dr,t0.DATE_1)+(SIGN(DATEDIFF(DAY,t2.dr,DATEADD(YEAR,YEAR(t2.dr)-YEAR(t0.DATE_1),t0.DATE_1)))-1)/2 >= 18
--E1F99D57-5E45-4E86-943B-38F709F1BE7F EMF.034 END

--B813B519-E2DD-4558-95C5-784C8348D178 EMF.036 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATA_INJ', t0.DATA_INJ as ZN_POL, @NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.036'
,'LEK_PR/DATE_INJ Обязательно для заполнения'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
WHERE t0.DATA_INJ is null
--B813B519-E2DD-4558-95C5-784C8348D178 EMF.036 END

--268C71AE-F4FB-4924-BC68-D46F474A15FD EMF.037 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_SH', t0.CODE_SH as ZN_POL, @NSCHET, 'LEK_PR', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'EMF.037'
,'LEK_PR/CODE_SH Обязательно для заполнения'
from tempdb..TEMP_SLUCH_LEK_PR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
WHERE t0.CODE_SH is null
--268C71AE-F4FB-4924-BC68-D46F474A15FD EMF.037 END

--D0AA298C-AE80-411C-A88E-C13743046DF4 EMF.038 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'ONK_M', t0.ONK_M as ZN_POL, @NSCHET, 'ONK_SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t3.SL_ID as SL_ID, null as IDSERV, 'EMF.038'
, 'Обязательно к заполнению при первичном лечении (DS1_T=0) для пациентов, возраст которых на дату начала лечения более 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(36),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where t0.ONK_M is null
and t0.ds1_t = 0
and DATEDIFF(YEAR,t2.dr,t3.DATE_1)+(SIGN(DATEDIFF(DAY,t2.dr,DATEADD(YEAR,YEAR(t2.dr)-YEAR(t3.DATE_1),t3.DATE_1)))-1)/2 >= 18
--D0AA298C-AE80-411C-A88E-C13743046DF4 EMF.038 END

--686C9F2C-0538-4AA0-BC07-968A8B0E4EA3 EMF.038 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'TEST_VI', t2.TEST_VI as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t2.SL_ID as SL_ID, null as IDSERV, 'EMF.038'
, 'Отсутствует признак тестирования для МКБ ='+ cast(t1.DS1 as varchar(10)) +'и методе оплаты ='+ cast(t2.[METHOD] as varchar(10))
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where t0.USL_OK = 3
and t2.[METHOD] in ('1','1.1','2.1','2.2','10.2','10.3','10.4')
and ((t1.DS1 >= 'J00' and t1.DS1 < 'J23') or (t1.DS1 = 'B18.2' and @q019type NOT IN ('X','C','T')))
and isnull(t2.TEST_VI,-1) = -1
--686C9F2C-0538-4AA0-BC07-968A8B0E4EA3 EMF.038 END

--061DD9BB-6980-40ED-8EBF-64DDBBAC67F9 EMF.039 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KOL_TEST', t4.KOL_TEST as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, t4.IDSERV as IDSERV, 'EMF.039'
, 'Отсутствует количество проведённых тестов(KOL_TEST) для МКБ ='+ cast(t1.DS1 as varchar(10)) +'и методе оплаты ='+ cast(c.[METHOD] as varchar(10))
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
join tempdb..TEMP_USL t3 on t1.SL_ID = t3.SL_ID and t3.IDCASE = t1.IDCASE and t3.N_ZAP=t1.N_ZAP
JOIN tempdb..TEMP_COMENTSL c with(nolock) on c.idcase = t0.idcase and c.SL_ID = t1.SL_ID
left join tempdb..TEMP_COMENTU t4 on t4.IDSERV=t3.IDSERV and t4.SL_ID = t3.SL_ID and t3.IDCASE = t4.IDCASE and t3.N_ZAP=t4.N_ZAP
where t0.USL_OK = 3 and ISNULL(c.TEST_VI,0)<>0
and c.[METHOD] in ('1','1.1','2.1','2.2','10.2','10.3','10.4')
and ((t1.DS1 >= 'J00' and t1.DS1 < 'J23') or (t1.DS1 = 'B18.2' and @q019type NOT IN ('X','C','T')))
and isnull(t4.KOL_TEST,0) = 0
--061DD9BB-6980-40ED-8EBF-64DDBBAC67F9 EMF.039 END

----76B29A9C-F196-4216-81EE-F7083AD830AE EMF.040 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'DATE_NEXT', t2.DATE_NEXT as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t2.SL_ID as SL_ID, null as IDSERV, 'EMF.040'
--, 'Отсутствует количество проведённых тестов(KOL_TEST) для МКБ ='+ cast(t1.DS1 as varchar(10)) +'и методе оплаты ='+ cast(t2.[METHOD] as varchar(10))
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
--where @q019type IN ('H','X','C')
--and isnull(t2.DATE_NEXT,0) <> 0
----76B29A9C-F196-4216-81EE-F7083AD830AE EMF.040 END

--DAF7F620-ACE2-4E5C-9121-BFA25C416111 EMF.041 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DN', t1.DN as ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.041'
, 'Поле DN обязательно для заполнения,  если P_CEL=1.3, или METHOD=(1 ; 1.1) и DS1=С.'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where ((t0.USL_OK = '3' and t0.IDSP = '25') or @q019type IN ('T','C') or (@q019type NOT IN ('X') and t0.USL_OK = '3'))
and (t1.P_CEL = '1.3' or t2.METHOD in ('1','1.1')) and left(t1.DS1,1) = 'C'
and t1.DN is null
--DAF7F620-ACE2-4E5C-9121-BFA25C416111 EMF.041 END

--0E40B82F-93AE-4039-BAF0-776AFEA26860 EMF.042 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS', null as ZN_POL ,@NSCHET, 'USL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t0.SL_ID as SL_ID, t0.IDSERV as IDSERV, 'EMF.042'
,'Раздел SL/MR_USL_N Сведения о медицинских   работниках, выполнивших услугу   должны быть заполнены!'
from tempdb..TEMP_USL t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
where @q019type not in ('X')
and t0.DS is null
--0E40B82F-93AE-4039-BAF0-776AFEA26860 EMF.042 END

--B247450E-F01E-4A55-B754-FCD00A36FEF6 EMF.044 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'M_PROF', t2.[M_PROF] as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.044'
, 'Не заполнено M_PROF для метода оплаты (METHOD)=' + cast(t2.METHOD as varchar(10))
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where (left(t2.METHOD,3) in ('6.1','6.2','6.5','6.6') and @q019type in ('X')
or (t2.METHOD = '3.1' or left(t2.METHOD,2) in ('8.')) and @q019type in ('H','C'))
and isnull(t2.[M_PROF],-1)=-1 and t1.DATE_2>'20240301'
--B247450E-F01E-4A55-B754-FCD00A36FEF6 EMF.044 END

--3C9A9B66-DED9-4632-95AD-A1B69BD5EF0A EMF.045 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'PRVS', t2.PRVS as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'EMF.045'
--, 'Не заполнены поля PRVS(Специальность) или IDDOKT (код врача) для пакета D'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--where (isnull(t1.PRVS_D,0)=0 or isnull(t1.IDDOKT_D,'')='') and t1.DATE_2>'20240301' 
--3C9A9B66-DED9-4632-95AD-A1B69BD5EF0A EMF.045 END

--2147ACEB-CBCD-4667-A38E-A81B21598BB6 ESI.007 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'D_PROT', t0.D_PROT as ZN_POL, @NSCHET, 'B_PROT', t0.N_ZAP, t1.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ESI.007'
, 'D_PROT не может быть более даты окончания случая'
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 t0.D_PROT>t2.DATE_2
--2147ACEB-CBCD-4667-A38E-A81B21598BB6 ESI.007 END

--DC1C96DF-20DD-4ABF-83D9-4AB40EEB4B90 ESI.009 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS2', t0.DS2 as ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t1.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ESI.009'
, 'DS2 не должен быть равен  DS1'
from tempdb..TEMP_DS2 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 t0.DS2=t2.DS1
--DC1C96DF-20DD-4ABF-83D9-4AB40EEB4B90 ESI.009 END

--91499017-5878-4CE0-8FBC-3D4CB466CE9B ESI.012 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DATE_NEXT', t2.DATE_NEXT as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ESI.012'
, 'Дата следующего осмотра(DATE_NEXT) должно быть больше даты окончания DATE_2'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where t2.DATE_NEXT <= t1.DATE_2
--91499017-5878-4CE0-8FBC-3D4CB466CE9B ESI.012 END

--858B7C3A-18AC-4F93-87DA-79B6B598BA1D ESI.013 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 as IDCASE,  null as SL_ID, null as IDSERV, 'ESI.013'
, 'Некорректная версия файлов для пакетов  C, H. Должна быть 4.0'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_ZGLV t3 with(nolock) on 1 = 1
join tempdb..TEMP_SCHET s  with(nolock) on 1 = 1
where [VERSION] <> '4.0' 
and @q019type in ('H','C') and s.[year]>=2024 
--858B7C3A-18AC-4F93-87DA-79B6B598BA1D ESI.013 END

--0BFA0EFB-5298-4A31-86AC-637DAEB4317C ECR.527 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', a.METHOD as ZN_POL, @NSCHET, 'COMENTSL', null, a.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.527'
, 'Для метода оплаты= '+ a.METHOD +' количество услуг = '+cast(a.count_usl as varchar(10))+'  не соответствует допустимым значениям MIN_USL= '+cast(isnull(a.MIN_USL,0) as varchar(10))+'  MAX_USL='+cast(isnull(a.MAX_USL,0) as varchar(10))
from (
select s.ID_PAC, t2.METHOD, s.IDDOKT, q.count_usl as count_usl, isnull(met.MIN_USL,0)as MAX_USL, isnull(met.MIN_USL,0) as MIN_USL
from tempdb..TEMP_SLUCH s
join tempdb..TEMP_COMENTSL t2 with(nolock) on s.idcase = t2.idcase and s.SL_ID = t2.SL_ID
join ies.R_NSI_METHODS met on t2.METHOD=met.CODE and dateadd(yy, 2000, s.date_2) between met.[START_DATE] and met.FINAL_DATE
join (select u.ID_PAC, count(*) count_usl from tempdb..TEMP_USL u where u.code_usl not like 'B01.003.004%' group by u.ID_PAC) q on s.ID_PAC=q.ID_PAC
where met.TARIF_TYPE in (2,3) 
and (isnull(met.MAX_USL,0)<>0 and q.count_usl>isnull(met.MAX_USL,0)
or isnull(met.MIN_USL,0)<>0 and q.count_usl<isnull(met.MIN_USL,0))
)a
--0BFA0EFB-5298-4A31-86AC-637DAEB4317C ECR.527 END

--9C02FBF1-6158-43E3-B9B3-7CB56921C8CA ECR.528 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DS1', t3.DS1 as ZN_POL, @NSCHET, 'SL', t1.N_ZAP, t1.ID_PAC, t1.IDCASE as IDCASE, t3.SL_ID as SL_ID, null as IDSERV, 'ECR.528'
, 'Для метод оплаты 3.1 недопустим DS1(МКБ)=' + cast(t3.DS1 as varchar(10))
from tempdb..TEMP_COMENTSL 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),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
where left(t0.METHOD,3) in ('3.1') 
and DATEDIFF(YEAR,t2.dr,t3.DATE_1)+(SIGN(DATEDIFF(DAY,t2.dr,DATEADD(YEAR,YEAR(t2.dr)-YEAR(t3.DATE_1),t3.DATE_1)))-1)/2 >= 18
and (left(t3.DS1,1)='C' or (left(t3.DS1,3)>='D00' and left(t3.DS1,3)<='D07'))
--9C02FBF1-6158-43E3-B9B3-7CB56921C8CA ECR.528 END

--D0888842-A2FE-4681-AFD3-EDB54E0F2B1B ECR.530 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CODE_USL', a.CODE_USL as ZN_POL, @NSCHET, 'USL', null, a.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.530'
, 'Для метода оплаты 8.* более одной услуги тарификации с кодом CODE_USL' + cast(a.CODE_USL as varchar(10))
from (
select t1.ID_PAC, t0.METHOD, t3.IDDOKT, u.CODE_USL 
from tempdb..TEMP_COMENTSL 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
join tempdb..TEMP_USL u with(nolock) on t3.sl_id = u.sl_id and t3.idcase = u.idcase
where left(t0.METHOD,3) in ('8') 
and u.CODE_USL in (select f.CODE from ies.R_NSI_PRICE_N f)
group by t1.ID_PAC, t0.METHOD, t3.IDDOKT, u.CODE_USL 
having count(*)>1
)a
--D0888842-A2FE-4681-AFD3-EDB54E0F2B1B ECR.530 END

--7D083ECD-B323-42B7-841F-58519CCCDC9E ECR.531 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', a.METHOD as ZN_POL, @NSCHET, 'COMENTSL', null, a.ID_PAC, null as IDCASE, null as SL_ID, null as IDSERV, 'ECR.531'
, 'Метод оплаты должен содержать услугу «Осмотр врача»'
from (
select t1.ID_PAC, t0.METHOD, t3.IDDOKT, 'B04.047.001' as CodUsl, 
max(case when u.code_usl='B04.047.001' then 1 else 0 end) as pr_usl, 
count(u.code_usl) as cn_usl
from tempdb..TEMP_COMENTSL 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),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
join tempdb..TEMP_USL u with(nolock) on t3.sl_id = u.sl_id and t3.idcase = u.idcase
where t0.METHOD in ('8.1.8') and t3.DATE_2>='20240301'
group by t1.ID_PAC, t0.METHOD, t3.IDDOKT
having max(case when u.code_usl='B04.047.001' then 1 else 0 end)=0
union all
select t1.ID_PAC, t0.METHOD, t3.IDDOKT, 'B04.027.001' as CodUsl, 
max(case when u.code_usl='B04.027.001' then 1 else 0 end) as pr_usl, 
sum(case when left(t3.ds1,3)='C44' and t0.method='8.4.24' then 2 else 1 end) as cn_usl
from tempdb..TEMP_COMENTSL 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),t2.ID_PAC) = convert(varchar(36),t1.ID_PAC)
join tempdb..TEMP_SLUCH t3  with(nolock) on t3.sl_id = t0.sl_id and t3.idcase = t0.idcase
join tempdb..TEMP_USL u with(nolock) on t3.sl_id = u.sl_id and t3.idcase = u.idcase
where left(t0.METHOD,3)='8.4' and t3.DATE_2>='20240301' 
group by t1.ID_PAC, t0.METHOD, t3.IDDOKT
having max(case when u.code_usl='B04.027.001' then 1 else 0 end)=0
)a
--7D083ECD-B323-42B7-841F-58519CCCDC9E ECR.531 END
/*
--A5D82E6E-4E9A-4472-A19B-49F3FF03F15D ECR.532 BEGIN
 -- Закоментировал по причине того, что необходимо проверять t2.DEPART_OID по другому справочнику Цинцадзе А.П.
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DEPART_OID', t2.DEPART_OID as ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.532'
, 'Код DEPART_OID не соответствует МО в справочнике ТРМО'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
join tempdb..TEMP_COMENTSL t2 with(nolock) on t1.idcase = t2.idcase and t1.SL_ID = t2.SL_ID
--left join ies.T_F032_TRMO f032 on t2.DEPART_OID = f032.OID_MO and f032.MCOD = t0.LPU -- Требуется использовать региональный справочник ТРМО Цинцадзе.
--where t2.DEPART_OID is not null and f032.MCOD is null -- Требуется использовать региональный справочник ТРМО Цинцадзе.
left join ies.R_NSI_TRMO_GIS_OMS f032 on t2.DEPART_OID = f032.PODR_OID_FRMO and f032.Cod_MO = t0.LPU  -- R_NSI_TRMO_GIS_OMS - региональный справочник ТРМО из ГИС ОМС
where t2.DEPART_OID is not null and f032.Cod_MO is null

--A5D82E6E-4E9A-4472-A19B-49F3FF03F15D ECR.532 END
*/
--9431864C-DCAA-4AD2-AFF4-705F7350B18F ECR.533 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'DEPART_OID', t2.DEPART_OID as ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.533'
, 'Код DEPART_OID не заполнен'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
join tempdb..TEMP_COMENTSL t2 with(nolock) on t1.idcase = t2.idcase and t1.SL_ID = t2.SL_ID
where t1.DATE_2>='20240301' and t2.DEPART_OID is null
--9431864C-DCAA-4AD2-AFF4-705F7350B18F ECR.533 END

--6CE251CF-FFCF-46EA-BCA6-1CE1AC149869 ENN.019 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'NAPR_USL', t0.NAPR_USL as ZN_POL ,@NSCHET, 'NAPR', t1.N_ZAP, t1.ID_PAC, t1.idcase as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ENN.019'
, 'NAPR/ NAPR_USL :... не соответствует NAPR/ MET_ISSL :... по справочнику NAPR_V001'
from tempdb..TEMP_NAPR t0 with(nolock)
join tempdb..TEMP_Z_SLUCH t1 with(nolock) on t0.IDCASE = t1.IDCASE
left join ies.T_V001_NOMENCLATURE t2 on t2.Code=t0.NAPR_USL
where t0.NAPR_USL is not null
and t2.Code is null
--6CE251CF-FFCF-46EA-BCA6-1CE1AC149869 ENN.019 END

--CE473BE4-EEFD-4564-BC88-51688D7D6320 ECR.522 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SHRM_K', t2.SHRM_K as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.522'
, 'При заполненом профиле(PROFIL)=158 должно заполняться поле SHRM_K'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where isnull(t2.SHRM_K,-1)=-1
and t1.PROFIL = 158
--CE473BE4-EEFD-4564-BC88-51688D7D6320 ECR.522 END

--A87AFBAA-2E39-4993-B5CF-CA8631E8D21B ECR.515 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KSG_CODE', t2.KSG_CODE as ZN_POL, @NSCHET, 'KSG_STOMAT', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.515'
, 'КСГ  с признаком посещения(P_POS) =1 должно быть единственным в случае оказания медицинской помощи'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_KSG_STOMAT t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
join ies.R_NSI_PRICE_STOMAT s with(nolock) on s.KSG_CODE = t2.KSG_CODE and s.poseshenie = 1
where t1.KsgStomatCount>1
--A87AFBAA-2E39-4993-B5CF-CA8631E8D21B ECR.515 END

--9D74949A-25ED-433E-A912-4F8ACB85D850 ECR.520 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MR_USL_N', null as ZN_POL ,@NSCHET, 'MR_USL_N', null, a.ID_PAC, null as IDCASE, null as SL_ID,null as IDSERV, 'ECR.520'
,'Раздел SL/MR_USL_N Сведения о медицинских   работниках, выполнивших услугу   должны быть заполнены!'
from (
select ss.ID_PAC, un.PRVS , un.CODE_MD, u.DATE_IN
from tempdb..TEMP_USL u 
join tempdb..TEMP_MR_USL_N un on u.IDSERV = un.IDSERV
join tempdb..TEMP_SLUCH ss on ss.SL_ID = u.SL_ID and ss.IDCASE = u.IDCASE
join ies.R_NSI_SOOTV_STOMAT sp on u.code_usl=sp.MANIPULATION and un.prvs=sp.CODE_SPEC
where u.code_usl not like 'B01.003.004%'
group by ss.ID_PAC, un.PRVS , un.code_md, u.DATE_IN
having count(*)>1
)a
--9D74949A-25ED-433E-A912-4F8ACB85D850 ECR.520 END

--F38F8C91-B7C5-4A41-80B5-9DAA101FEFA2 ECR.521 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'VidPomOPMP', t0.VidPomOPMP as ZN_POL ,@NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t0.SL_ID as SL_ID, null as IDSERV, 'ECR.521'
,'Для случаев заместительной почечной терапии (вид помощи AT,CB) недопустимо указывать DS1=Z*'
from  tempdb..TEMP_SLUCH t0
where t0.DS1 like 'Z%' and t0.VidPomOPMP in ('AT', 'CB')
--F38F8C91-B7C5-4A41-80B5-9DAA101FEFA2 ECR.521 END

--7F539D5B-8642-4EBB-876D-FB01A276B04A ECR.526 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', t2.METHOD as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.526'
, 'Для метода оплаты услуги анестезии не предусмотрены'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
inner JOIN ies.R_NSI_METHODS s with(nolock) on s.CODE = t2.METHOD  and s.[START_DATE] <=t1.DATE_2 and (s.FINAL_DATE is null or s.FINAL_DATE>=t1.DATE_2)
where 
 exists(select * from tempdb..TEMP_USL u with(nolock) where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID and u.code_usl like 'B01.003.004%')
and 
s.TARIF_TYPE = 2
and s.P_ANEST <> 1


--7F539D5B-8642-4EBB-876D-FB01A276B04A ECR.526 END

--879C4B0A-907F-46CD-A404-69AE130C1237 ECR.501 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'MR_1S', t2.MR_1S as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.501'
, 'Не заполнен «Признак проведения ранней медицинской реабилитации» (MR_1S)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
CROSS APPLY(select top 1 s.CODE from ies.R_NSI_EXCEPTIONS_ERRORS s with(nolock) 
			join [IES].[R_NSI_EXCEPTIONS_ERRORS_MKB] a on a.Ref = s.DictionaryBaseID
			where a.DS = t1.DS1 
			and t1.[DATE_2] BETWEEN s.DATEBEG and s.DATEEND 
			and s.IS_ACTUAL = 1
			and s.DeletionMark = 0
			and (s.VD = t1.VD_5_6 or s.VD='')
			and s.OSN = 'МКБ_Для_ранней_МР'
			)a
where t1.PROFIL <> 158
and t0.ZSLType = 'Стационар'
and t2.MR_1S is null
--879C4B0A-907F-46CD-A404-69AE130C1237 ECR.501 END

--A728DB04-2798-418A-BFB5-F77B7F3F8D9C ECR.502 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SHRM_K', null as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.502'
, 'При использовании mr_it должен заполняться REAB = 0...6'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
join tempdb..TEMP_PERS p on p.ID_PAC = t1.ID_PAC
JOIN tempdb..TEMP_COMENTSL c with(nolock) on c.idcase = t0.idcase and c.SL_ID = t1.SL_ID
JOIN tempdb..TEMP_CRIT t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
CROSS APPLY(select top 1 s.CODE from ies.R_NSI_KSLP_G s with(nolock) where s.KSG = t2.CRIT and s.[START_DATE] <=t1.DATE_2 and (s.FINAL_DATE is null or s.FINAL_DATE>=t1.DATE_2))a
where c.SHRM_K is null 
and t0.USL_OK = 1 and t0.RSLT IN ('105','106')
and a.CODE = '2408'
and DATEDIFF(YEAR,p.dr,t1.DATE_1)+(SIGN(DATEDIFF(DAY,p.dr,DATEADD(YEAR,YEAR(p.dr)-YEAR(t1.DATE_1),t1.DATE_1)))-1)/2 >= 18
--A728DB04-2798-418A-BFB5-F77B7F3F8D9C ECR.502 END

--8DEB7101-1CC8-48B4-98AC-DE50DC05B232 ECR.503 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'SHRM', t2.SHRM as ZN_POL, @NSCHET, 'SL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.503'
, 'Поле SHRM должно быть от 0 до 6'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
CROSS APPLY(select top 1 s.CODE from ies.R_NSI_EXCEPTIONS_ERRORS s with(nolock) 
			join [IES].[R_NSI_EXCEPTIONS_ERRORS_MKB] a on a.Ref = s.DictionaryBaseID
			where a.DS = t1.DS1 
			and t1.[DATE_2] BETWEEN s.DATEBEG and s.DATEEND 
			and s.IS_ACTUAL = 1
			and s.DeletionMark = 0
			and (s.VD = t1.VD_5_6 or s.VD='')
			and s.USL_OK = t0.USL_OK
			and s.OSN = 'МКБ_приоритет_Для_ШРМ'
			)a
where t1.PROFIL <> 158
and (t0.ZSLType in ('Стационар','ДневнойСтационар')
or ((t0.ZSLType = 'Поликлиника'
	and t2.METHOD in ('1','1.1','1.2','3.1'))
	or (t1.Age18 = 1 and left(t2.METHOD,1) in ('6','8'))
	or (t1.Age18 = 0 and left(t2.METHOD,1) in ('5'))))
and t0.RSLT in (105,106,205,206,313)
and t2.SHRM is null
--8DEB7101-1CC8-48B4-98AC-DE50DC05B232 ECR.503 END

--07B864DD-80EE-41FE-ABD6-6B30E0061CD7 ECR.504 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'CRIT', t2.CRIT as ZN_POL, @NSCHET, 'KSG', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.504'
, 'При доп. критерии(CRIT)="'+cast(t2.CRIT as varchar(10))+'" длительность госпитализации < 5 дней'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_CRIT t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
CROSS APPLY(select top 1 s.CODE from ies.R_NSI_KSLP_G s with(nolock) where s.KSG = t2.CRIT and s.[START_DATE] <=t1.DATE_2 and (s.FINAL_DATE is null or s.FINAL_DATE>=t1.DATE_2))a
where t0.USL_OK = 1
and a.CODE = 2408
and (case when t0.USL_OK = 2 then DATEDIFF(DAY,t1.DATE_1,t1.DATE_2) + 1 else DATEDIFF(DAY,t1.DATE_1,t1.DATE_2) end) < 5
--07B864DD-80EE-41FE-ABD6-6B30E0061CD7 ECR.504 END

--46CE5327-075F-454C-A579-9D19D15300CD ECR.488 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'KOL_USL', u.KOL_USL as ZN_POL, @NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, u.IDSERV as IDSERV, 'ECR.488'
, 'При методе оплаты Обращение по заболеванию (1 или 10.4) должно быть оказано более одного/ой посещения/услуги'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
cross apply(select isnull(u.KOL_USL,0) as KOL_USL, u.IDSERV from tempdb..TEMP_USL u where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID)u
where t2.METHOD IN('1','10.4')
and (select count(*) as cnt from tempdb..TEMP_USL u where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID )<2
and u.KOL_USL<2
--46CE5327-075F-454C-A579-9D19D15300CD ECR.488 END

--изменено по задаче № 46826
--5A9A2AAB-7689-4A93-8821-429F084105BD ECR.489 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', t2.METHOD as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.489'
, 'При методе оплаты Обращение по заболеванию (1 или 10.4) отсутствует посещениe на Дату начала случая)'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where t2.METHOD IN('1','10.4')
and not exists(select top 1 1 from tempdb..TEMP_USL u where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID and u.DATE_IN = t1.DATE_2)
--5A9A2AAB-7689-4A93-8821-429F084105BD ECR.489 END

--изменено по задаче № 46818
--5785C385-C4C3-4CD7-8B9B-48D2C58441DA ECR.490 BEGIN
insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
select 'METHOD', t2.METHOD as ZN_POL, @NSCHET, 'COMENTSL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.490'
, 'При методе оплаты Обращение по заболеванию (1 или 10.4) отсутствует посещение на Дату окончания случая'
from tempdb..TEMP_Z_SLUCH t0 with(nolock)
join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
where t2.METHOD IN('1','10.4')
and not exists(select top 1 1 from tempdb..TEMP_USL u where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID and u.DATE_OUT = t1.DATE_2)
--5785C385-C4C3-4CD7-8B9B-48D2C58441DA ECR.490 END

--повторяет ECR.490 № 46818
----B42EE568-A63C-452B-9A62-014E975E279F ECR.491 BEGIN
--insert into #Errors (IM_POL, ZN_POL, NSCHET, BAS_EL, N_ZAP, ID_PAC, IDCASE, SL_ID, IDSERV, OSHIB, COMMENT)
--select 'METHOD', t2.METHOD as ZN_POL, @NSCHET, 'USL', t0.N_ZAP, t0.ID_PAC, t0.IDCASE as IDCASE, t1.SL_ID as SL_ID, null as IDSERV, 'ECR.491'
--, 'При методе оплаты Обращение по заболеванию (1 или 10.4) должно быть оказано более одного/ой посещения/услуги (Дата окончания)'
--from tempdb..TEMP_Z_SLUCH t0 with(nolock)
--join tempdb..TEMP_SLUCH t1 with(nolock) on t1.idcase = t0.idcase
--JOIN tempdb..TEMP_COMENTSL t2 with(nolock) on t2.idcase = t0.idcase and t2.SL_ID = t1.SL_ID
--where t2.METHOD IN('1','10.4')
--and not exists(select top 1 1 from tempdb..TEMP_USL u where u.IDCASE = t1.IDCASE and u.SL_ID = t1.SL_ID)
----B42EE568-A63C-452B-9A62-014E975E279F ECR.491 END

-----

IF EXISTS(SELECT TOP 1 1 FROM #Errors)
BEGIN
SELECT 
	OSHIB AS 'OSHIB',
	IM_POL AS 'IM_POL',
	ZN_POL AS 'ZN_POL',
	NSCHET AS 'NSCHET',
	BAS_EL AS 'BAS_EL',
	N_ZAP AS 'N_ZAP',
	IDCASE AS 'IDCASE',
	SL_ID AS 'SL_ID',	
	IDSERV AS 'IDSERV',
	COMMENT AS 'COMMENT',
	ID_PAC AS 'ID_PAC',
	FDATE as 'FDATE'

FROM #Errors
END

END
GO
