---- =============================================
---- Author:  Ramil M. Enaliev
---- Create date: 2010-06-30
---- Description: Удаление расчета движения больных на дату
---- =============================================
--CREATE or Replace Function sp_ClearMovenmentsLpuSteps("MovementsLpuID" UUID, "Param" Int)
--RETURNS void 
--As 
--$$
--Declare
--	_PostingDate TimeStamp;
--	_LpuID UUID;
--Begin
--	IF "Param" = 1
--	then    
--		--declare @tt table (id uniqueidentifier)
--		Drop Table If Exists tt;
--		Create Temp Table tt(ID UUID);
		
--		select into
--			_PostingDate, _LpuID
--			ml.PostingDate, ml.LpuMain
--		from akuz.T_MOVEMENTS_LPU ml
--		where ml.MovementsLpuID = "MovementsLpuID";
   
--		insert into tt(Id)
--		select eb.EventID 
--		from akuz.T_EVENT_BASE eb
--		inner 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
--		inner 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 
--			inner join 
--				akuz.T_MOVEMENT m 
--				on mc.MedicalcardRegistration = m.MovementID 
--			where m.EmergencyEvent = eb.EventID
--		);
		
--		delete from akuz.T_LOCATION_INTERIOR using akuz.T_MOVEMENTS_CARD, akuz.T_MOVEMENT, akuz.T_EVENT_BASE, akuz.T_AMBULANCE_CARD
--		where akuz.T_LOCATION_INTERIOR.InteriorIncoming = akuz.T_MOVEMENTS_CARD.MovementsCardID
--		and akuz.T_MOVEMENT.MovementID = akuz.T_MOVEMENTS_CARD.MedicalcardRegistration
--		and akuz.T_EVENT_BASE.EventID = akuz.T_MOVEMENT.EmergencyEvent
--		and akuz.T_AMBULANCE_CARD.AmbulanceCardID = akuz.T_EVENT_BASE.AmbulanceCard
--		and akuz.T_EVENT_BASE.EventID in (select id from tt);

--		delete from akuz.T_LOCATION_PATIENT using akuz.T_MOVEMENT, akuz.T_EVENT_BASE, akuz.T_AMBULANCE_CARD
--		where akuz.T_MOVEMENT.MovementID = akuz.T_LOCATION_PATIENT.Patient
--		and akuz.T_EVENT_BASE.EventID = akuz.T_MOVEMENT.EmergencyEvent
--		and akuz.T_AMBULANCE_CARD.AmbulanceCardID = akuz.T_EVENT_BASE.AmbulanceCard
--		and akuz.T_EVENT_BASE.EventID in (select id from tt);
		
--		delete from akuz.T_MOVEMENTS_CARD using akuz.T_MOVEMENTS_DIVISION, akuz.T_DIVISION
--		where akuz.T_MOVEMENTS_DIVISION.MovementsDivisionID = akuz.T_MOVEMENTS_CARD.MovementsDivision
--		and akuz.T_DIVISION.DivisionID = akuz.T_MOVEMENTS_DIVISION.Division
--		and akuz.T_MOVEMENTS_DIVISION.MovementsLpu = "MovementsLpuID"
--		and 
--		(
--			akuz.T_MOVEMENTS_CARD.MovementType = 4 
--			or 
--			akuz.T_MOVEMENTS_CARD.MovementType = 5 
--			or 
--			akuz.T_MOVEMENTS_CARD.MovementType = 15 
--			or 
--			akuz.T_MOVEMENTS_CARD.MovementType = 99 
--			or 
--			akuz.T_DIVISION.IsRequestonDivision = '1'
--		);
		
--		update akuz.T_MOVEMENTS_CARD mc
--		set	StepsNumber = null
--		,	BeginCureDate = null
--		,	BeginCureTime = null
--		from akuz.T_MOVEMENTS_DIVISION md 			
--		where md.MovementsDivisionID = mc.MovementsDivision
--		and md.MovementsLpu = "MovementsLpuID";

--		update akuz.T_MOVEMENTS_LPU 
--		set	CameChildren = 0
--		,	CameFromDayHospital = 0
--		,	CameFromHospitalWard = 0
--		,	CameFromTheDivision = 0
--		,	CameGranny = 0
--		,	CameTatar = 0
--		,	WithAccompanying = 0
--		,	CameVillager = 0
--		,	DayBegin = 0
--		,	DayEnd = 0
--		,	DayEndMan = 0
--		,	DayEndWoman = 0
--		,	DeadOutcome = 0
--		,	Outcome = 0
--		,	OutcomeToDayHospital = 0
--		,	OutcomeToDivision = 0
--		,	OutcomeToOtherLpu = 0
--		,	RepairBeds = 0
--		,	SoprCount = 0
--		,	BedsCount = 0
--		where MovementsLpuID = "MovementsLpuID";

--		update akuz.T_MOVEMENTS_SEVEN 
--		set	CameChildren = 0
--		,	CameFromDayHospital = 0
--		,	CameFromHospitalWard = 0
--		,	CameFromTheDivision = 0
--		,	CameGranny = 0
--		,	CameTatar = 0
--		,	WithAccompanying = 0
--		,	CameVillager = 0
--		,	DayBegin = 0
--		,	DayEnd = 0
--		,	DayEndMan = 0
--		,	DayEndWoman = 0
--		,	DeadOutcome = 0
--		,	Outcome = 0
--		,	OutcomeToDayHospital = 0
--		,	OutcomeToDivision = 0
--		,	OutcomeToOtherLpu = 0
--		,	RepairBeds = 0
--		,	SoprCount = 0
--		,	BedsCount = 0
--		where MovementsLpu = "MovementsLpuID";

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

--		update akuz.T_MOVEMENTS_LPU 
--		set CalculateDateTime = null 
--		where MovementsLpuID = "MovementsLpuID";

--		update akuz.T_EVENT_BASE 
--		set CloseDate = null
--		where CloseDate is not null 
--		and EventID in (select id from tt);
		
--	END if;
--End;
--$$ Language plpgsql;