USE [prince]
GO
/****** Object:  StoredProcedure [Common].[CPN_UpdateMobileNumber]    Script Date: 12/15/2025 11:35:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		zareie
-- Create date: 2025-12-36 | 1404.09.24
-- Description:	
-- =============================================
ALTER PROCEDURE [Common].[CPN_UpdateTroubleInfo]
  @CPN_ID				[Common].[_INT_],
  @IsChangeNationalCode [Common].[_BOOLEAN_],
  @CPN_NationalCode     [Common].[_TINY_], 
  @IsChangeEconomicCode [Common].[_BOOLEAN_],
  @CPN_EconomicCode     [Common].[_TINY_],
  @IsChangeMobileNumber [Common].[_BOOLEAN_],
  @CPN_MobileNumber		[Common].[_PHONE_]
AS
BEGIN

    SET NOCOUNT ON;
    
    DECLARE @LKP_ID   [Common].[_INT_]
    SET @LKP_ID = 0; 
	SELECT @LKP_ID = [LKP_ID] FROM [Common].[Lookup] WHERE [LKP_Type] = 8 AND [LKP_EngCaption] = 'Contact_Mobile'
	   
    IF @IsChangeNationalCode =  1
    BEGIN   
		UPDATE [Common].[CompanyPerson]  
		SET
			[CPN_NationalCode] = @CPN_NationalCode    
		WHERE 
			[CPN_ID] = @CPN_ID
    END 
    
    IF @IsChangeEconomicCode = 1
    BEGIN   
		UPDATE [Common].[CompanyPerson]  
		SET
			[CPN_EconomicCode] = @CPN_EconomicCode    
		WHERE 
			[CPN_ID] = @CPN_ID
    END 
    
    IF @IsChangeMobileNumber = 1  
    BEGIN   
		UPDATE [Common].[CompanyPerson]  
		SET
			[CPN_MobileNumber] = @CPN_MobileNumber    
		WHERE 
			[CPN_ID] = @CPN_ID
		
		
		DELETE [Common].[ContactInformation]  
 		 WHERE [CIN_LKP_Type] = @LKP_ID AND 
			   [CIN_CPN_ID] = @CPN_ID AND 
			   [CIN_IsDefault] = 1
			
        IF EXISTS (SELECT 1 
					FROM [Common].[ContactInformation] 
					WHERE [CIN_LKP_Type] = @LKP_ID AND 
							[CIN_CPN_ID] = @CPN_ID AND 
							 [CIN_Value] = @CPN_MobileNumber)
			BEGIN
				UPDATE [Common].[ContactInformation]  
				SET [CIN_IsDefault] = 0
				WHERE [CIN_LKP_Type] = @LKP_ID AND 
						[CIN_CPN_ID] = @CPN_ID
				  
				UPDATE [Common].[ContactInformation]  
				SET [CIN_IsDefault] = 1
				WHERE [CIN_LKP_Type] = @LKP_ID AND 
						[CIN_CPN_ID] = @CPN_ID AND 
						[CIN_Value] = @CPN_MobileNumber		      
			END
			ELSE
			BEGIN
				UPDATE [Common].[ContactInformation]  
				SET [CIN_IsDefault] = 0
				WHERE [CIN_LKP_Type] = @LKP_ID AND 
						[CIN_CPN_ID] = @CPN_ID

				EXEC [Common].[CIN_InsertNew] @CPN_MobileNumber, 1, @CPN_ID, 1, NULL
			END 	
			
    END 
END
