DECLARE @CD_EMP INT
DECLARE @CD_FILIAL INT

DECLARE @I INT = 1

SET NOCOUNT ON

DECLARE INSERE_CONFIG_PROMO CURSOR FAST_FORWARD FOR 

SELECT 
    PRC_FILIAL.CD_EMP,
    PRC_FILIAL.CD_FILIAL
FROM 
    PRC_FILIAL


OPEN INSERE_CONFIG_PROMO
FETCH NEXT FROM INSERE_CONFIG_PROMO INTO @CD_EMP,@CD_FILIAL
WHILE @@FETCH_STATUS=0
	BEGIN
		
		IF (@I % 1) = 0 
		BEGIN
			PRINT 'PROCESSADO ' + CAST(@I AS VARCHAR)

----------------------------------------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'USA_LINX_PROMO' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL)
BEGIN	
	--PARAMETRO USA LINX PROMO DEVE ESTAR SETADO COMO ZERO CASO CLIENTE NÃO UTILIZE O PRODUTO PROMO, CASO SEJA DEFINIDO COMO 1 IRÁ OCASIONAR ERROS SE OS DEMAIS PARAMETROS NÃO FORAM CONFIGURADOS CORRETAMENTE
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@cd_emp,  @cd_filial, 'USA_LINX_PROMO', '0', 1, '2021-06-11 00:00:00.000');
	
END; 
----------------------------------------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_DEMONSTRA_PONTOS' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@cd_emp, @cd_filial, 'LINX_PROMO_DEMONSTRA_PONTOS', '1', 1, '2021-07-28 00:00:00.000');
END; 
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_EXIBE_SUGESTAO' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_EXIBE_SUGESTAO', '1', 1, '2021-06-16 00:00:00.000');
END;
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_EXIBE_SUGESTAO_PDV' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_EXIBE_SUGESTAO_PDV', '1', 1, '2021-05-21 00:00:00.000');
END;
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_GERA_CUPOM_NAO_FISCAL' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_GERA_CUPOM_NAO_FISCAL', '1', 1, '2021-05-26 00:00:00.000');
END; 
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_SUGERIR_FIDELIZACAO'  AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_SUGERIR_FIDELIZACAO', '0', 1, '2021-07-06 00:00:00.000');
END; 
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'URL_LINX_PROMO_REST_API'  AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'URL_LINX_PROMO_REST_API', 'http://seuambienteaqui:porta/', 1, '2021-08-23 00:00:00.000');
END;
----------------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_UTILIZA_FIDELIDADE' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_UTILIZA_FIDELIDADE', '0', 1, '2021-08-13 00:00:00.000');
END;
----------------------------------------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'LINX_PROMO_EXIBIR_TOTAL_DESCONTOS' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@CD_EMP, @CD_FILIAL, 'LINX_PROMO_EXIBIR_TOTAL_DESCONTOS', '1', 1, '2021-01-28 00:00:00.000');
END;
-------------------------------------------------------------------------------------------------------------------
 
IF NOT EXISTS (SELECT 1 FROM prc_filial_config WHERE cd_chave = 'URL_LINX_PROMO' AND CD_EMP = @CD_EMP AND CD_FILIAL =  @CD_FILIAL ) 
BEGIN
	INSERT INTO prc_filial_config (cd_emp, cd_filial, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@cd_emp, @CD_FILIAL, 'URL_LINX_PROMO', 'http://seuambienteaqui:porta/engine/evaluate', 1, '2021-08-24 00:00:00.000');
END;
----------------------------------------------------------------------------------------------------------------------------

--Parametros Nível Empresa

IF NOT EXISTS (SELECT 1 FROM prc_emp_config WHERE cd_chave = 'LINX_PROMO_VALOR_BASE' AND CD_EMP = @CD_EMP ) 
BEGIN
	INSERT INTO prc_emp_config (cd_emp, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@cd_emp, 'LINX_PROMO_VALOR_BASE', '0', 1,  '2021-08-21');
END;
----------------------------------------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT 1 FROM prc_emp_config WHERE cd_chave = 'LINX_PROMO_COMPANY' AND CD_EMP = @CD_EMP) 
BEGIN
	INSERT INTO prc_emp_config (cd_emp, cd_chave, valor, cd_usu, dt_ult_alt)
	VALUES(@cd_emp, 'LINX_PROMO_COMPANY', 'test', 1,'2021-08-23');
END;
----------------------------------------------------------------------------------------------------------------------------

END		  		  
	SET @I  = @I + 1
	FETCH NEXT FROM INSERE_CONFIG_PROMO INTO  @CD_EMP,@CD_FILIAL
	END

	PRINT 'PROCESSADO ' + CAST(@I AS VARCHAR)

--AJUSTA_CONCILIACAO_CARTAO_DIANDO CURSOS
CLOSE INSERE_CONFIG_PROMO;
--DESALOCANDO CURSOS DA MEMORIA
DEALLOCATE INSERE_CONFIG_PROMO;
--APAGANDO A TABELA TEMPORARIA