DROP FUNCTION if exists sp_GetSetDivisionXml(text, xml, boolean, uuid);
--CREATE OR REPLACE FUNCTION sp_GetSetDivisionXml
--(
--	"LpuCode" text DEFAULT NULL::text
--,	"Xml" xml DEFAULT NULL::xml
--,	"IsNew" integer = 1::boolean
--,	"ID" uuid DEFAULT NULL::uuid
--)
--RETURNS void AS
--$BODY$
--Declare	_Result text;
--	_ContentEncoding text;
--	_Type text;
--	_OperationType text;
--	_OwnerID uuid;
--Begin
--	IF "Xml" is null
--	then
--		--выясняем тип операции
--		if "IsNew" = '1'
--		then
--			_OperationType := 'Insert';
--		elsif "IsNew" = '0' and exists(select * from RDS.T_DIVISION d where d.DivisionID = "ID")
--		then
--			_OperationType := 'Update';
--		elsif "IsNew" = '0' and not exists(select * from RDS.T_DIVISION d where d.DivisionID = "ID")
--		then
--			_OperationType := 'Delete';
--			_Result := XMLELEMENT
--				  (
--					name "DIVISION"
--				  ,	XMLELEMENT
--					(
--						name header
--					,	XMLFOREST
--						(
--							"LpuCode" 	as "LPU"
--						,	_OperationType	as "TYPE"
--						,	"ID"		as "ID"
--						)
--					)
--				  ); 
--		END if;

--		_ContentEncoding := 'utf-8';
--		_Type := 'DIVISION';
--		_OwnerID := "ID";
	
--		if _OperationType <> 'Delete'
--		then
--			select 	
--			XMLELEMENT
--			(
--				name "DIVISION"
--			,	XMLELEMENT
--				(
--					name header
--				,	XMLFOREST
--					(
--						"LpuCode"	as "LPU"
--					,	_OperationType	as "TYPE"
--					,	"ID"		as "ID"
--					)
--				)				
--			,	XMLELEMENT
--				(
--					name body
--				,	XMLFOREST
--					(
--						d.Code		as "DV_CODE"
--					,	d.Name		as "NAME"
--					,	d.FullName	as "FULLNAME"
--					,	d.Level		as "LEVEL"
--					,	d.CodeMz	as "CODE_MZ"
--					,	d.Nofilet	as "PHONE"
--					,	d.Adress	as "ADDRESS"
--					,	d.Branch	as "BRANCH_ID"
--					,	d.DivisionType	as "DIVISION_TYPE"
--					,	d.RevenueType	as "REVENUE_TYPE"
--					,	d.IDNumberCard	as "ID_NUMBER_CARD"
--					,	d.IsActive	as "ACTIVE"
--					)
--				)
--			)
--			from RDS.T_DIVISION d		
--			into _Result
--			where d.DivisionID = "ID";
--		END if;

--		INSERT INTO RDS.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--		select gen_random_uuid(), s.ServerName, _Result, null, null, _ContentEncoding, _Type, null, 0, Current_TimeStamp::TIMESTAMP, _OwnerID
--		from RDS.T_SYNC_SERVERS s
--		where s.ServerType <> 1 
--		and not exists 
--		(
--			select * from RDS.T_SYNC_SERVERS s1
--			inner join 
--				RDS.T_SERVERS_LINK sl 
--				on sl.SyncServers = s1.SyncServersID
--			inner join 
--				RDS.T_SERVERS_LINK_LPU ll 
--				on ll.ServersLinkID = sl.ServersLinkID
--			inner join 
--				RDS.T_LPU_MAIN lp 
--				on lp.MCOD = ll.LpuMain
--			where lp.MCOD = "LpuCode" 
--			and s1.SyncServersID = s.SyncServersID
--		);
--	elsif "Xml" is not null
--	then	
--		-- Generate table #DIVISION
--		Drop Table If Exists TmpDIVISION;
--		Create Temp Table TmpDIVISION
--		As
--		select  (xpath('/DIVISION/header[1]/TYPE[1]/text()', x))[1]::text		as OperationType
--		,	(xpath('/DIVISION/header[1]/LPU[1]/text()', x))[1]::text		as LpuMain
--		,	(xpath('/DIVISION/header[1]/ID[1]/text()', x))[1]::text::uuid		as ID
--		,	(xpath('/DIVISION/body[1]/DV_CODE[1]/text()', x))[1]::text		as Code
--		,	(xpath('/DIVISION/body[1]/NAME[1]/text()', x))[1]::text			as Name
--		,	(xpath('/DIVISION/body[1]/FULLNAME[1]/text()', x))[1]::text		as FullName
--		,	(xpath('/DIVISION/body[1]/LEVEL[1]/text()', x))[1]::text::int		as Level
--		,	(xpath('/DIVISION/body[1]/CODE_MZ[1]/text()', x))[1]::text		as CodeMz
--		,	(xpath('/DIVISION/body[1]/PHONE[1]/text()', x))[1]::text		as Nofilet
--		,	(xpath('/DIVISION/body[1]/ADDRESS[1]/text()', x))[1]::text		as Adress
--		,	(xpath('/DIVISION/body[1]/BRANCH_ID[1]/text()', x))[1]::text::uuid	as Branch
--		,	(xpath('/DIVISION/body[1]/DIVISION_TYPE[1]/text()', x))[1]::text::int	as DivisionType
--		,	(xpath('/DIVISION/body[1]/REVENUE_TYPE[1]/text()', x))[1]::text::int	as RevenueType
--		,	(xpath('/DIVISION/body[1]/ID_NUMBER_CARD[1]/text()', x))[1]::text::int	as IDNumberCard
--		,	(xpath('/DIVISION/body[1]/ACTIVE[1]/text()', x))[1]::text::boolean	as Active
--		from
--		(
--			select "Xml" as x
--		) t;
		
--		select tmp.OperationType 
--		from TmpDIVISION tmp
--		into _OperationType
--		Limit 1;

--		if _OperationType <> 'Delete'
--		then
--			-- update RDS.T_DIVISION
--			update RDS.T_DIVISION d
--			set	Code = div.Code 
--			,	Name = div.Name
--			,	FullName = div.FullName
--			,	Level = div.Level
--			,	CodeMz = div.CodeMz
--			,	Nofilet = div.Nofilet
--			,	Adress = div.Adress
--			,	DivisionType = div.DivisionType
--			,	RevenueType = CASE d.RevenueType WHEN 0 THEN 12900 WHEN 1 THEN 12901 WHEN 2 THEN 12902 ELSE div.RevenueType END
--			,	IsActive = div.Active
--			,	d.ID = div.ID
--			,	d.Branch = (select b.BranchId from RDS.T_BRANCH b where b.BranchId = div.Branch or b.ID = div.Branch limit 1)
--			from TmpDIVISION div 
--			where div.ID = d.ID or div.ID = d.DivisionID;

--			-- INSERT RDS.T_DIVISION
--			insert into RDS.T_DIVISION(DivisionID, Code, Name, FullName, Level, CodeMz, Nofilet, Adress, Branch, DivisionType, RevenueType, IsActive, ID, IDNumberCard)
--			select gen_random_uuid(), d.Code, d.Name, d.FullName, d.Level, d.CodeMz, d.Nofilet, d.Adress
--			, (select b.BranchID from RDS.T_BRANCH b where b.ID = d.Branch limit 1)
--			, d.DivisionType, CASE d.RevenueType WHEN 0 THEN 12900 WHEN 1 THEN 12901 WHEN 2 THEN 12902 ELSE d.RevenueType END, d.Active, d.ID, d.IDNumberCard
--			from TmpDIVISION d
--			where not exists 
--			(
--				select * from RDS.T_DIVISION d1 where d.ID = d1.ID or d.ID = d1.DivisionID
--			);
--		else
--			delete from RDS.T_DIVISION d
--			where exists
--			(
--				select* from TmpDIVISION dd where dd.ID = d.ID or d.DivisionID = dd.ID
--			);
--		end if;
		
--		-- Drop table
--		drop table if exists TmpDIVISION;
--	end if;
--End;
--$BODY$
--  LANGUAGE plpgsql VOLATILE
--  COST 100;
--ALTER FUNCTION sp_GetSetDivisionXml(text, xml, boolean, uuid)
--  OWNER TO postgres;