DO $$
BEGIN
	IF EXISTS (
		SELECT * 
		FROM pg_proc p 
		WHERE p.proname = 'sp_dropschet'
	) THEN 
		DROP FUNCTION PUBLIC.sp_dropschet (uuid, integer, integer);
	END IF;
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION public.sp_dropschet(
	"SchetID" uuid,
	"Type" integer,
	"IsFull" integer
) RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
	"SchetAct" uuid;
	"IDCurs" uuid;
	_schetID uuid;
	_ID uuid;
	v_schetid uuid;
	crs_my2 CURSOR FOR
		SELECT s.SchetID
		FROM IES.T_SCHET s
		WHERE SchetMain = "SchetID";
BEGIN
	
	RAISE NOTICE 'Отвязать SCHET_ZAP от FRZL_MESSAGE %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	UPDATE ies.T_FRZL_MESSAGE fr
	SET SchetZap = NULL
	FROM ies.T_SCHET_ZAP sz 
	WHERE sz.Schet = "SchetID" AND fr.SchetZap = sz.SchetZapID;
	
	RAISE NOTICE 'Удаление нарушений мэк добавленных вручную %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	DELETE FROM IES.T_ITEM_ADD_REASON_BY_HAND ia
	USING ies.T_SCHET_SLUCH ss
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	JOIN ies.T_SCHET_ACT sa ON sa.schet = s.schetid
	JOIN ies.T_SCHET_ACT_ITEM sai ON sai.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	WHERE 
		s.schetid = "SchetID"
		-- AND s.type_ IN (555, 538)
		AND ss.isdelete = 0
		AND ia.schetactitem = sai.schetactitemid;

	RAISE NOTICE 'Удаление всех сущностей связанных с экспертизой МЭЭ и ЭКМП %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	DELETE FROM ies.T_MEDICAL_EXPERTISE_ERROR mee
	USING ies.T_SCHET_SLUCH ss
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	JOIN ies.T_MEDICAL_EXPERTISE_SCHET mes ON mes.Schet = s.SchetID
	JOIN ies.T_MEDICAL_EXPERTISE_ITEM mei ON mei.MedicalExpertiseSchet = mes.MedicalExpertiseSchetID
	WHERE 
		SchetID = "SchetID" AND mei.MedicalExpertiseItemID = mee.MedicalExpertiseItem;

	DELETE FROM ies.T_MEDICAL_ERROR me
	USING ies.T_SCHET_SLUCH ss 
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	WHERE 
		SchetID = "SchetID" AND ss.SchetSluchID = me.SchetSluch;

	DELETE FROM IES.T_MEDICAL_REQUEST_ITEM mri
	USING ies.T_SCHET_SLUCH ss 
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	WHERE 
		SchetID = "SchetID" AND ssa.SchetSluchAccomplishedID = mri.SchetSluchAccomplished;

	DELETE FROM ies.T_MEDICAL_EXPERTISE_ITEM mei
	USING ies.T_SCHET_SLUCH ss 
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	JOIN ies.T_MEDICAL_EXPERTISE_SCHET mes ON mes.Schet = s.SchetID
	WHERE 
		SchetID = "SchetID" AND mei.MedicalExpertiseSchet = mes.MedicalExpertiseSchetID;

	DELETE FROM ies.T_MEDICAL_REQUEST_SCHET mrs
	USING ies.T_SCHET s 
	JOIN ies.T_MEDICAL_EXPERTISE_SCHET mes ON mes.Schet = s.SchetID
	WHERE 
		SchetID = "SchetID" AND mrs.MedicalExpertiseSchet = mes.MedicalExpertiseSchetID;

	DELETE FROM ies.T_MEDICAL_EXPERTISE_GROUP meg 
	USING IES.T_MEDICAL_EXPERTISE_SCHET mes
	JOIN ies.T_SCHET s ON s.SchetID = mes.Schet
	WHERE 
		SchetID = "SchetID" AND meg.MedicalExpertiseReestr = mes.MedicalExpertiseSchetID;

	DELETE FROM ies.T_MEDICAL_EXPERTISE_SCHET mes
	USING ies.T_SCHET_SLUCH ss 
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	WHERE 
		SchetID = "SchetID" AND mes.Schet = s.SchetID;

	DELETE FROM IES.T_MEDICAL_EXPERTISE_ERROR mee
	USING ies.T_SCHET t1
	JOIN ies.T_MEDICAL_EXPERTISE_SCHET mes ON t1.SchetID = mes.Schet
	JOIN IES.T_MEDICAL_EXPERTISE_ITEM mei ON mei.MedicalExpertiseSchet = mes.MedicalExpertiseSchetID
	WHERE t1.SchetID = "SchetID" AND mee.MedicalExpertiseItem = mei.MedicalExpertiseItemID;

	DELETE FROM IES.T_MEDICAL_REQEST_SUBITEM mri
	USING ies.T_SCHET_SLUCH ss 
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	JOIN ies.T_SCHET s ON s.SchetID = sz.Schet
	JOIN ies.T_SCHET_SLUCH_ACCOMPLISHED ssa ON ss.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID
	WHERE 
		SchetID = "SchetID" AND mri.SchetSluch = ss.SchetSluchID;

	DELETE FROM IES.T_MEDICAL_EXPERTISE_ITEM mei
	USING ies.T_SCHET t1
	JOIN ies.T_MEDICAL_EXPERTISE_SCHET mes ON t1.SchetID = mes.Schet
	WHERE t1.SchetID = "SchetID" AND mei.MedicalExpertiseSchet = mes.MedicalExpertiseSchetID;

	DELETE FROM IES.T_MEDICAL_EXPERTISE_SCHET t2
	USING ies.T_SCHET t1
	WHERE t1.SchetID = "SchetID" AND t1.SchetID = t2.Schet;

	"SchetAct" := (
		SELECT SchetActID
		FROM IES.T_SCHET_ACT
		WHERE Schet = "SchetID"
		LIMIT 1
	);

	RAISE NOTICE 'Удаление T_INSURANCE_AFFILATIION %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');

	DELETE FROM IES.T_INSURANCE_AFFILATIION t3
	USING IES.T_SCHET_ZAP t1
	JOIN IES.T_INSURANCE_AFFILIATION_HISTORY t2 ON t1.SchetZapID = t2.SchetZap
	WHERE t1.Schet = "SchetID" AND t2.InsuranceAffiliationHistoryID = t3.InsuranceAffiliationHistory;

	DELETE FROM IES.T_INSURANCE_AFFILIATION_HISTORY t2
	USING IES.T_SCHET_ZAP t1
	WHERE t1.Schet = "SchetID" AND t1.SchetZapID = t2.SchetZap;

	RAISE NOTICE 'Удаление T_SCHET_SLUCH_SANK %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');

	DELETE FROM ies.T_SCHET_SLUCH_SANK sank
	USING ies.T_SCHET_SLUCH_ACCOMPLISHED ssa
	JOIN ies.T_SCHET_ZAP sz ON ssa.SchetZap = sz.SchetZapID
	WHERE sz.Schet = "SchetID" AND sank.SchetSluchAccomplished = ssa.SchetSluchAccomplishedID;

	RAISE NOTICE 'Удаление T_MR_USL_N %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	DELETE FROM ies.T_MR_USL_N mun	
	USING ies.T_SCHET_USL su
	JOIN ies.T_SCHET_SLUCH ss ON ss.SchetSluchID = su.SchetSluch
	JOIN ies.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
	WHERE sz.Schet = "SchetID" AND su.SchetUslID = mun.SchetUsl;

	RAISE NOTICE 'Удаление T_SCHET_USL_NAPR %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	DELETE FROM ies.T_SCHET_USL_NAPR napr
	USING ies.T_SCHET_SLUCH ss
	JOIN ies.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
	WHERE sz.schet = "SchetID" AND napr.SchetSluch = ss.SchetSluchID;

	RAISE NOTICE 'Удаление всех онкологических сущностей %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
	DELETE FROM ies.T_SCHET_USL_ONK_LEK_PR_DATE lekdate
	USING ies.T_SCHET_USL_ONK_LEK_PR lek
	JOIN ies.T_SCHET_USL_ONK suo ON lek.SchetUslOnk = suo.SchetUslOnkID
	JOIN ies.T_SCHET_SLUCH_ONK sso ON suo.SchetSluchOnk = sso.SchetSluchOnkID
	JOIN ies.T_SCHET_SLUCH ss ON sso.SchetSluch = ss.SchetSluchID
	JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
	WHERE sz.schet = "SchetID" AND lekdate.SchetUslOnkLekPr = lek.SchetUslOnkLekPrID;

	DELETE FROM ies.T_SCHET_USL_ONK_LEK_PR suol
	USING ies.T_SCHET_USL_ONK suo
	JOIN ies.T_SCHET_SLUCH_ONK sso ON sso.SchetSluchOnkID = suo.SchetSluchOnk
	JOIN ies.T_SCHET_SLUCH ss ON ss.SchetSluchID = sso.SchetSluch
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	WHERE sz.Schet = "SchetID" AND suo.SchetUslOnkID = suol.SchetUslOnk;

	DELETE FROM ies.T_SCHET_SLUCH_LEK_PR lek
	USING ies.T_SCHET_SLUCH ss
	JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
	WHERE sz.schet = "SchetID" AND lek.SchetSluch = ss.SchetSluchID;

	DELETE FROM ies.T_SCHET_USL_ONK suo
	USING ies.T_SCHET_SLUCH_ONK sso
	JOIN ies.T_SCHET_SLUCH ss ON ss.SchetSluchID = sso.SchetSluch
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	WHERE sz.Schet = "SchetID" AND sso.SchetSluchOnkID = suo.SchetSluchOnk;
	
	DELETE FROM ies.t_schet_sluch_onk_diag t0
	USING ies.T_SCHET_SLUCH_ONK sso
	JOIN ies.T_SCHET_SLUCH ss ON ss.SchetSluchID = sso.SchetSluch
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	WHERE sz.Schet = "SchetID" AND sso.SchetSluchOnkID = t0.SchetSluchOnk;
	
	DELETE FROM ies.t_schet_sluch_onk_prot t0
	USING ies.T_SCHET_SLUCH_ONK sso
	JOIN ies.T_SCHET_SLUCH ss ON ss.SchetSluchID = sso.SchetSluch
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	WHERE sz.Schet = "SchetID" AND sso.SchetSluchOnkID = t0.SchetSluchOnk;

	   
	DELETE FROM ies.T_SCHET_SLUCH_ONK sso
	USING ies.T_SCHET_SLUCH ss
	JOIN ies.T_SCHET_ZAP sz ON sz.SchetZapID = ss.SchetZap
	WHERE sz.Schet = "SchetID" AND ss.SchetSluchID = sso.SchetSluch;

	DELETE FROM IES.T_SCHET_ACT
	WHERE SchetActID = "SchetAct";

	DELETE FROM ies.T_KSLP kslp
	USING IES.T_SCHET_SLUCH ss
	JOIN ies.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
	WHERE sz.Schet = "SchetID" AND ss.SchetSluchID = kslp.OmsSchetSluch;									  

	IF ("Type" = 554) THEN
		DROP TABLE IF EXISTS SCHET_OUT;
		
		-- Запоминаем реестры в которые попали записи из этого счета
		CREATE TEMP TABLE SCHET_OUT AS
		
		SELECT t3.Schet
		FROM IES.T_SCHET_ZAP t3
		JOIN IES.T_SCHET_ZAP t4 ON t4.SchetZapID = t3.OmsSchetZap
		WHERE t4.Schet IS NULL
		GROUP BY t3.Schet;

		DELETE FROM ies.T_SCHET_USL_ONK_LEK_PR_DATE dt 
		USING ies.T_SCHET_USL_ONK_LEK_PR pr
		JOIN ies.T_SCHET_USL_ONK suo ON pr.SchetUslOnk = suo.SchetUslOnkID
		JOIN ies.T_SCHET_SLUCH_ONK sso ON suo.SchetSluchOnk = sso.SchetSluchOnkID
		JOIN ies.T_SCHET_SLUCH ss ON sso.SchetSluch = ss.SchetSluchID
		JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
		JOIN IES.T_SCHET_ZAP t5 ON t5.SchetZapID = sz.OmsSchetZap
		WHERE t5.schet = "SchetID" AND pr.SchetUslOnkLekPrID = dt.SchetUslOnkLekPr;

		DELETE FROM ies.T_SCHET_USL_ONK_LEK_PR pr
		USING ies.T_SCHET_USL_ONK suo
		JOIN ies.T_SCHET_SLUCH_ONK sso ON suo.SchetSluchOnk = sso.SchetSluchOnkID
		JOIN ies.T_SCHET_SLUCH ss ON sso.SchetSluch = ss.SchetSluchID
		JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
		JOIN IES.T_SCHET_ZAP t5 ON t5.SchetZapID = sz.OmsSchetZap
		WHERE t5.schet = "SchetID" AND pr.SchetUslOnk = suo.SchetUslOnkID;

		DELETE FROM ies.T_SCHET_USL_ONK suo
		USING ies.T_SCHET_SLUCH_ONK sso
		JOIN ies.T_SCHET_SLUCH ss ON sso.SchetSluch = ss.SchetSluchID
		JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
		JOIN IES.T_SCHET_ZAP t5 ON t5.SchetZapID = sz.OmsSchetZap
		WHERE t5.schet = "SchetID" AND suo.SchetSluchOnk = sso.SchetSluchOnkID;

		DELETE FROM ies.T_SCHET_USL_NAPR napr
		USING ies.T_SCHET_SLUCH ss
		JOIN IES.T_SCHET_ZAP sz ON ss.SchetZap = sz.SchetZapID
		JOIN IES.T_SCHET_ZAP t5 ON t5.SchetZapID = sz.OmsSchetZap
		WHERE t5.schet = "SchetID" AND napr.SchetSluch = ss.SchetSluchID;
		 
		-- DELETE t4
		-- FROM ies.T_SCHET_ZAP t1
		-- JOIN ies.T_SCHET_SLUCH t2 ON (t1.SchetZapID = t2.SchetZap)
		-- JOIN ies.T_SCHET_USL t3 ON (t2.SchetSluchID = t3.SchetSluch)
		-- JOIN ies.T_SCHET_USL t4 ON (t3.SchetUslID = t4.OmsSchetUsl)
		-- WHERE t1.Schet = @SchetID

		DELETE FROM ies.T_SCHET_SLUCH t3
		USING ies.T_SCHET_ZAP t1
		JOIN ies.T_SCHET_SLUCH t2 ON t1.SchetZapID = t2.SchetZap
		WHERE t1.Schet = "SchetID" AND t2.SchetSluchID = t3.OmsSchetSluch;
   
		DELETE FROM ies.T_SCHET_ZAP t2
		USING ies.T_SCHET_ZAP t1
		WHERE t1.Schet = "SchetID" AND t1.SchetZapID = t2.OmsSchetZap;

		DELETE FROM IES.T_MTR_SCHET_OUT_LINK t2
		USING ies.T_SCHET t1
		WHERE t1.SchetID = "SchetID" AND t1.SchetID = t2.MtrSchetLpu;

		/*
		--этот код закоментирован, т.к. не найден в Чите, которая является единственным боевым сервером на ПГ
		DELETE FROM IES.T_SCHET_ZAP t4
		USING IES.T_SCHET_ZAP t5
		WHERE t5.Schet = "SchetID" AND t5.SchetZapID = t4.OmsSchetZap;

		FOR _ID IN
			SELECT m.ID
			FROM SCHET_OUT m
			ORDER BY m.ID 
		LOOP
			PERFORM sp_SchetRecalc (_ID);
		END LOOP;

		OPEN crs_my2;
		LOOP
			FETCH crs_my2 INTO _schetID;
			IF NOT FOUND THEN EXIT; END IF;
			PERFORM sp_DropSchet (_schetID, 1::bool, 693::int);
			EXIT WHEN _schetID IS NULL;
		END LOOP;
		CLOSE crs_my2;
		--*/
	END IF;--end 554

	IF "Type" = 538::int THEN
		DELETE FROM IES.T_SCHET_RESULTS
		WHERE MtrSchetIn = "SchetID";
	END IF;--end 538

	IF "Type" = 539::int THEN
		UPDATE IES.T_OUTER_ACT
		SET SchetCorrect = NULL
		WHERE SchetCorrect = "SchetID";

		DELETE FROM IES.T_SCHET_RESULTS
		WHERE MtrSchetIn = "SchetID";
	END IF;--end 539

	IF "Type" = 555::int THEN
		UPDATE IES.T_SCHET_RESULTS t1
		SET t1.SchetSluchAccepted = NULL
		FROM IES.T_SCHET_SLUCH t2
		JOIN IES.T_SCHET_ZAP t3 ON t3.SchetZapID = t2.SchetZap
		WHERE t3.Schet = "SchetID" AND t2.SchetSluchID = t1.SchetSluchAccepted;
	END IF;--end 555

	IF "Type" = (SELECT e.entityid FROM vclib.t_entities e where e.entname = 'FMO_SCHET' LIMIT 1) THEN
		RAISE NOTICE 'УДАЛЕНИЕ СЧЕТОВ FMO %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		
		v_schetid := "SchetID";
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_usl_med_dev %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_usl_med_dev t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		JOIN ies.t_schet_usl su ON ss.schetsluchid = su.schetsluch
		WHERE sz.schet = v_schetid			
		AND su.schetuslid = t0.schetusl;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_usl %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_usl t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		WHERE sz.schet = v_schetid
		AND ss.schetsluchid = t0.schetsluch;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_sluch_lek_pr %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_sluch_lek_pr t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		WHERE sz.schet = v_schetid			
		AND ss.schetsluchid = t0.schetsluch;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_ksg %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_ksg t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		WHERE sz.schet = v_schetid			
		AND ss.schetsluchid = t0.schetsluch;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_sluch_cons %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_sluch_cons t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		WHERE sz.schet = v_schetid			
		AND ss.schetsluchid = t0.schetsluch;
	
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_sluch_ds %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_sluch_ds t0
		USING ies.t_schet_zap sz
		JOIN ies.t_schet_sluch ss ON ss.schetzap = sz.schetzapid
		WHERE sz.schet = v_schetid			
		AND ss.schetsluchid = t0.schetsluch;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_sluch %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_sluch t0
		USING ies.t_schet_zap sz
		WHERE sz.schet = v_schetid			
		AND sz.schetzapid = t0.schetzap;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_schet_sluch_accomplished %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_sluch_accomplished t0
		USING ies.t_schet_zap sz
		WHERE sz.schet = v_schetid			
		AND sz.schetzapid = t0.schetzap;
	   
		RAISE NOTICE 'УДАЛЕНИЕ t_fmo_ds %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_fmo_ds t0
		USING ies.t_schet_zap sz
		JOIN ies.t_fmo_npr n ON sz.schetzapid = n.schetzap
		WHERE sz.schet = v_schetid
		AND n.fmonprid = t0.fmonpr;	
	
		RAISE NOTICE 'УДАЛЕНИЕ t_fmo_napr %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_fmo_npr t0
		USING ies.t_schet_zap sz
		WHERE sz.schet = v_schetid
		AND sz.schetzapid = t0.schetzap;

		RAISE NOTICE 'УДАЛЕНИЕ t_schet_zap %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
		DELETE FROM ies.t_schet_zap sz			
		WHERE sz.schet = v_schetid;
	END IF;
	
	IF "IsFull" = 1::int THEN
		RAISE NOTICE 'ПОЛНОЕ УДАЛЕНИЕ СЧЁТА (IsFull = 1) %', to_char(clock_timestamp(), 'DD.MM.YYYY HH24:MI:SS');
	
		DELETE FROM IES.T_SIGNER s
		USING IES.T_SIGNED_MESSAGE sm
		JOIN IES.T_SIGNED_ACT_FILE saf ON sm.SignedMessageID = saf.SignedMessage
		WHERE sm.SchetAct = "SchetAct" AND saf.FileID = s.SignedFile;

		DELETE FROM IES.T_SIGNED_ACT_FILE saf
		USING IES.T_SIGNED_MESSAGE sm
		WHERE sm.SchetAct = "SchetAct" AND sm.SignedMessageID = saf.SignedMessage;
		
		DELETE FROM VCLib.T_FILE f
		USING IES.T_SIGNED_ACT_FILE saf
		JOIN ies.t_signed_message sm ON saf.signedmessage = sm.signedmessageid
		WHERE type_ = 759::int
		AND sm.schetact = "SchetAct";			

		DELETE FROM IES.T_SIGNED_MESSAGE sm
		WHERE sm.SchetAct = "SchetAct";

		DELETE
		FROM ies.t_schet_zap sz
		WHERE sz.schet = "SchetID";

		DELETE
		FROM IES.T_SCHET_FILE tsf
		WHERE tsf.schet = "SchetID";

		DELETE
		FROM IES.T_SCHET
		WHERE SchetID = "SchetID";

		DELETE
		FROM VCLib.T_FILE f
		USING IES.T_SCHET_FILE sf
		WHERE type_ = 545::INT
			AND sf.FILEID IS NULL
			AND f.FileID = sf.FileID;

	END IF;
END;
$BODY$;