﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mfr__updt__companies]
	@tblname as nvarchar(50)
	, @referencetable nvarchar(50)
	, @tgtid bigint
AS
BEGIN
	SET NOCOUNT ON;

    DECLARE @now datetime = GETUTCDATE();
    DECLARE @tmp [dbo].[mfr__tt__companies];

	DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
	INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;

	IF @referencetable = 'mfr__companies'
		BEGIN
			DELETE p
				OUTput deleted.*, @now INTO [dbo].[mfr__d_*PartnerSet]
				FROM [dbo].[mfr__*PartnerSet] as p 
				WHERE p.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = p.[EntityId]);
			DELETE cv
				OUTput deleted.*, @now INTO [dbo].[mfr__d_#customvalues]
				FROM [dbo].[mfr__#customvalues] as cv 
				WHERE cv.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = cv.[EntityId]);
			DELETE lc
				OUTPUT deleted.*, @now INTO [dbo].[mfr__d_#locations]
				FROM [dbo].[mfr__#locations] as lc 
				WHERE lc.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = lc.[EntityId]);
			DELETE cm
				OUTPUT deleted.*, @now INTO [dbo].[mfr__d_comments]
				FROM [dbo].[mfr__comments] as cm
				WHERE EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = cm.[CompanyId] AND ISNULL(cm.[ServiceRequestId],0) = 0);
		END


	MERGE [dbo].[mfr__companies] as TARGET
		USING @tmp as SOURCE ON TARGET.[Id] = SOURCE.[Id]
	WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version] OR TARGET.[DateModified] <= SOURCE.[DateModified]) THEN
		UPDATE
			SET  [Id] = SOURCE.[Id]
				,[Location#ID] = SOURCE.[Location#ID]
				,[Version] = SOURCE.[Version]
				,[IsPhysicalPerson] = SOURCE.[IsPhysicalPerson]
				,[IsOwner] = SOURCE.[IsOwner]
				,[IsEmailInvoicingActive] = SOURCE.[IsEmailInvoicingActive]
				,[IsSupplier] = SOURCE.[IsSupplier]
				,[MappingId] = SOURCE.[MappingId]
				,[ExternalId] = SOURCE.[ExternalId]
				,[Name] = SOURCE.[Name]
				,[Note] = SOURCE.[Note]
				,[SupportTelephone] = SOURCE.[SupportTelephone]
				,[SupportFax] = SOURCE.[SupportFax]
				,[SupportMail] = SOURCE.[SupportMail]
				,[QuickSearch] = SOURCE.[QuickSearch]
				,[DateModified] = SOURCE.[DateModified]
				,[CustomValues] = SOURCE.[CustomValues]
				,[MainContactId] = SOURCE.[MainContactId]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT
			   ( [Id]
				,[Location#ID]
				,[Version]
				,[IsPhysicalPerson]
				,[IsOwner]
				,[IsEmailInvoicingActive]
				,[IsSupplier]
				,[MappingId]
				,[ExternalId]
				,[Name]
				,[Note]
				,[SupportTelephone]
				,[SupportFax]
				,[SupportMail]
				,[QuickSearch]
				,[DateModified]
				,[CustomValues]
				,[MainContactId])
		 VALUES
			    (SOURCE.[Id]
				,SOURCE.[Location#ID]
				,SOURCE.[Version]
				,SOURCE.[IsPhysicalPerson]
				,SOURCE.[IsOwner]
				,SOURCE.[IsEmailInvoicingActive]
				,SOURCE.[IsSupplier]
				,SOURCE.[MappingId]
				,SOURCE.[ExternalId]
				,SOURCE.[Name]
				,SOURCE.[Note]
				,SOURCE.[SupportTelephone]
				,SOURCE.[SupportFax]
				,SOURCE.[SupportMail]
				,SOURCE.[QuickSearch]
				,SOURCE.[DateModified]
				,SOURCE.[CustomValues]
				,SOURCE.[MainContactId]);
END