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

--SET ANSI_NULLS ON
--GO

--SET QUOTED_IDENTIFIER ON
--GO

---- =============================================
---- Author:  Ramil M. Enaliev
---- Create date: 2010-06-30
---- Description: Удаление расчета движения больных на дату
---- =============================================
--CREATE PROCEDURE [dbo].[sp_ClearMovenmentsLpuSteps] 
--    @MovementsLpuID uniqueidentifier,
--	@Param int
--AS
--BEGIN
--	SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; --Хранимка выполняется внутри блокировки по ЛПУ со стороны сервера приложения, поэтому дополнительные локи на чтение не нужны.
--    SET NOCOUNT ON;
--	IF(@Param = 1)
--	BEGIN
    
--		declare @PostingDate datetime,
--				@LpuID uniqueidentifier
--		--declare @tt table (id uniqueidentifier)

--		select @PostingDate = PostingDate,
--			   @LpuID = LpuMain
--		from [AKUZ].T_MOVEMENTS_LPU --with(xlock,rowlock)
--		where MovementsLpuID = @MovementsLpuID
   
--		--insert @tt
--		--	select eb.EventID from [AKUZ].T_EVENT_BASE eb
--		--	join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		--	where eb.type_ = 584 and eb.CloseDate >= @PostingDate and ac.CardLpu = @LpuID
--		--	and exists
--		--	(
--		--		select *
--		--		from [AKUZ].T_MOVEMENTS_CARD mc
--		--		join [AKUZ].T_MOVEMENT m on m.MovementID = mc.MedicalcardRegistration
--		--		where m.EmergencyEvent = eb.EventID
--		--	)
--		--	union 
--		--	select eb.EventID from [AKUZ].T_EVENT_BASE eb
--		--	join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		--	where eb.CloseDate is null and ac.CardLpu = @LpuID
--		--	and exists (select * from [AKUZ].T_MOVEMENTS_CARD mc join [AKUZ].T_MOVEMENT m on mc.MedicalcardRegistration = m.MovementID where m.EmergencyEvent = eb.EventID)
		
--		select tmp.EventID into #tmp
--		from
--		(
--			select eb.EventID
--			from [AKUZ].T_EVENT_BASE eb
--			join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--			where eb.type_ = 584 
--			and eb.CloseDate >= @PostingDate 
--			and ac.CardLpu = @LpuID
--			union all
--			select eb.EventID 
--			from [AKUZ].T_EVENT_BASE eb
--			join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--			where eb.CloseDate is null 
--			and ac.CardLpu = @LpuID
--		) tmp
--		where exists 
--		(
--			select * from [AKUZ].T_MOVEMENTS_CARD mc 
--			join [AKUZ].T_MOVEMENT m on mc.MedicalcardRegistration = m.MovementID 
--			where m.EmergencyEvent = tmp.EventID
--		)
		
--		delete li
--		from [AKUZ].T_LOCATION_INTERIOR li
--		join [AKUZ].T_MOVEMENTS_CARD mc on mc.MovementsCardID = li.InteriorIncoming
--		join [AKUZ].T_MOVEMENT mm on mm.MovementID = mc.MedicalcardRegistration
--		join [AKUZ].T_EVENT_BASE eb on eb.EventID = mm.EmergencyEvent
--		join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		join #tmp tmp on tmp.EventID = eb.EventID
--		--where eb.EventID in (select id from @tt)

--		delete lp
--		from [AKUZ].T_LOCATION_PATIENT lp
--		join [AKUZ].T_MOVEMENT mm on mm.MovementID = lp.Patient
--		join [AKUZ].T_EVENT_BASE eb on eb.EventID = mm.EmergencyEvent
--		join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		join #tmp tmp on tmp.EventID = eb.EventID
--		--where eb.EventID in (select id from @tt)
		
	
--		delete mc
--		from [AKUZ].T_MOVEMENTS_CARD mc
--		join [AKUZ].T_MOVEMENTS_DIVISION md on md.MovementsDivisionID = mc.MovementsDivision
--		join [AKUZ].T_DIVISION d on d.DivisionID = md.Division
--		where md.MovementsLpu = @MovementsLpuID and (mc.MovementType = 4 or mc.MovementType = 5 or mc.MovementType = 15 or mc.MovementType = 99 or d.IsRequestonDivision = 1)

--		update mc set mc.StepsNumber = null, mc.BeginCureDate = null, mc.BeginCureTime = null
--		from [AKUZ].T_MOVEMENTS_CARD mc
--		join [AKUZ].T_MOVEMENTS_DIVISION md on md.MovementsDivisionID = mc.MovementsDivision
--		where md.MovementsLpu = @MovementsLpuID

--		update t set
--			t.CameChildren = 0,
--			t.CameFromDayHospital = 0,
--			t.CameFromHospitalWard = 0,
--			t.CameFromTheDivision = 0,
--			t.CameGranny = 0,
--			t.CameTatar = 0,
--			t.WithAccompanying = 0,
--			t.CameVillager = 0,
--			t.DayBegin = 0,
--			t.DayEnd = 0,
--			t.DayEndMan = 0,
--			t.DayEndWoman = 0,
--			t.DeadOutcome = 0,
--			t.Outcome = 0,
--			t.OutcomeToDayHospital = 0,
--			t.OutcomeToDivision = 0,
--			t.OutcomeToOtherLpu = 0,
--			t.RepairBeds = 0,
--			t.SoprCount = 0,
--			t.BedsCount = 0
--		from [AKUZ].T_MOVEMENTS_LPU t
--		where t.MovementsLpuID = @MovementsLpuID

--		update t set
--			t.CameChildren = 0,
--			t.CameFromDayHospital = 0,
--			t.CameFromHospitalWard = 0,
--			t.CameFromTheDivision = 0,
--			t.CameGranny = 0,
--			t.CameTatar = 0,
--			t.WithAccompanying = 0,
--			t.CameVillager = 0,
--			t.DayBegin = 0,
--			t.DayEnd = 0,
--			t.DayEndMan = 0,
--			t.DayEndWoman = 0,
--			t.DeadOutcome = 0,
--			t.Outcome = 0,
--			t.OutcomeToDayHospital = 0,
--			t.OutcomeToDivision = 0,
--			t.OutcomeToOtherLpu = 0,
--			t.RepairBeds = 0,
--			t.SoprCount = 0,
--			t.BedsCount = 0
--		from [AKUZ].T_MOVEMENTS_SEVEN t
--		where t.MovementsLpu = @MovementsLpuID
	
--		update t set
--			t.CameChildren = 0,
--			t.CameFromDayHospital = 0,
--			t.CameFromHospitalWard = 0,
--			t.CameFromTheDivision = 0,
--			t.CameGranny = 0,
--			t.CameTatar = 0,
--			t.WithAccompanying = 0,
--			t.CameVillager = 0,
--			t.DayBegin = 0,
--			t.DayEnd = 0,
--			t.DayEndMan = 0,
--			t.DayEndWoman = 0,
--			t.DeadOutcome = 0,
--			t.Outcome = 0,
--			t.OutcomeToDayHospital = 0,
--			t.OutcomeToDivision = 0,
--			t.OutcomeToOtherLpu = 0,
--			t.RepairBeds = 0,
--			t.SoprCount = 0,
--			t.BedsCount = 0,
--			t.CameFromHospitalWardMan = 0,
--			t.CameFromHospitalWardWoman = 0,
--			t.CameFromHospitalWardChildren = 0,
--			t.ChilrenOccupiedBeds = 0,
--			t.ChildrenFreeBeds = 0,
--			t.OutcomeMan = 0,
--			t.OutcomeChildren = 0,
--			t.OutcomeWoman = 0,
--			t.PlannedBedsCount = 0,
--            t.TransfersCountToReanimation = 0,
--            t.DeliveryCountFromReanimation = 0,
--            t.ReanimationDayBegin = 0,
--            t.ReanimationDayEnd = 0
--		from [AKUZ].T_MOVEMENTS_SEVEN_PROFILE t
--		join [AKUZ].T_MOVEMENTS_SEVEN ms on ms.MovementsSevenID = t.MovementsSeven
--		where ms.MovementsLpu = @MovementsLpuID

--		update [AKUZ].T_MOVEMENTS_LPU set CalculateDateTime = null where MovementsLpuID = @MovementsLpuID
--		--exec [dbo].[sp_HospitalCloseDay] @MovementsLpuID

--		update eb 
--		set eb.CloseDate = null
--		from [AKUZ].T_EVENT_BASE eb
--		join #tmp tmp on tmp.EventID = eb.EventID
--		where eb.CloseDate is not null 
--		--and eb.EventID in (select id from @tt)
--	END
--END
--GO


