--Update Script for CRParameter Value mismatch

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [ParameterType] = 'N')
BEGIN
	UPDATE KaaS_CRParameter
	   SET ParameterType = 'Number'
	 WHERE ParameterType = 'N'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [ParameterType] = 'D')
BEGIN
	UPDATE KaaS_CRParameter
	   SET ParameterType = 'Date'
	 WHERE ParameterType = 'D'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [ParameterType] = 'T')
BEGIN
	UPDATE KaaS_CRParameter
	   SET ParameterType = 'Text'
	 WHERE ParameterType = 'T'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [requiredfield] = '1')
BEGIN
	UPDATE KaaS_CRParameter
	   SET requiredfield = 'Yes'
	 WHERE requiredfield = '1'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [userrequest] = '0')
BEGIN
	UPDATE KaaS_CRParameter
	   SET userrequest = 'No'
	 WHERE userrequest = '0'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [controltype] = 'List')
BEGIN
	UPDATE KaaS_CRParameter
	   SET listoption = REPLACE(CAST(listoption as nVarchar(4000)), '|', ';')
     WHERE controltype = 'List'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [infile] = 'Fee Earner')
BEGIN
	UPDATE KaaS_CRParameter
	   SET infile = 'Fee Earners'
	 WHERE infile = 'Fee Earner'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [infile] = 'Branch')
BEGIN
	UPDATE KaaS_CRParameter
	   SET infile = 'Branches'
	 WHERE infile = 'Branch'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM KaaS_CRParameter  WHERE [defaultvalue] = '0' and infile !='')
BEGIN
	UPDATE Kaas_CRParameter
	   SET defaultvalue = ''
	 WHERE defaultvalue = '0'
	   AND infile !=''
END
GO

IF EXISTS
  (SELECT TOP 1 1
     FROM SYS.[objects] SO
    WHERE SO.[name] = N'KaaS_CRParameter'
      AND SO.[type] = 'U') and 
   EXISTS
  (SELECT TOP 1 1 
     FROM KaaS_CRParameter  
	WHERE [defaultvalue] = 'GLO:Branch' 
	  AND [parametername] ='Branch')
BEGIN
	UPDATE Kaas_CRParameter 
	   SET defaultvalue = '' 
	 WHERE [defaultvalue] = 'GLO:Branch' 
	   AND [parametername] ='Branch'
END
GO

--Update infile value from Supplier to Suppliers
IF EXISTS
  (SELECT TOP 1 1
     FROM SYS.[objects] SO
    WHERE SO.[name] = N'KaaS_CRParameter'
      AND SO.[type] = 'U') and 
   EXISTS
  (SELECT TOP 1 1 
     FROM KaaS_CRParameter  
	WHERE [Infile] = 'Supplier')
BEGIN
	UPDATE Kaas_CRParameter 
	   SET [Infile] = 'Suppliers' 
	 WHERE [Infile] = 'Supplier'
END
GO

--UPDATE Sequ nO to 1 when it is 0
IF EXISTS(SELECT TOP 1 1 FROM [dbo].[KaaS_CRParameter] WHERE [Seqno] = 0 )
BEGIN
	UPDATE [dbo].[KaaS_CRParameter]
	   SET [Seqno] = 1
	 WHERE [Seqno] = 0
END
GO

--UPDATE name from Cheque to payment	
IF EXISTS(SELECT TOP 1 1 FROM [dbo].[Kaas_CrReport] WHERE [Name] = 'Cheque Requisition' )
BEGIN
	UPDATE [dbo].[Kaas_CrReport] 
       SET   [Name] = 'Payment Request' 
	   WHERE [Name] = 'Cheque Requisition'
END
GO

IF EXISTS(SELECT TOP 1 1 FROM [dbo].[Kaas_CrReport] WHERE [Name] = 'Cheque Requisitions List' )
BEGIN
	UPDATE [dbo].[Kaas_CrReport] 
       SET   [Name] = 'Payment Request List' 
	   WHERE [Name] = 'Cheque Requisitions List'
END
GO


IF EXISTS(SELECT TOP 1 1 FROM [dbo].[Kaas_CrParameter] WHERE [UserPrompt] = 'Enter the Cheque Requisition Number' )
BEGIN
	UPDATE [dbo].[Kaas_CrParameter] 
       SET   [UserPrompt] = 'Enter the Payment Request Number' 
	   WHERE [UserPrompt] = 'Enter the Cheque Requisition Number'
END
GO

DECLARE @reportId INT; 
SELECT @reportId =[ReportId] 
  FROM KaaS_CRReport 
 WHERE [name] = 'Fees Rec By Matter By Date' 
   AND [report_type] = 'A' 

IF EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CrParameter] 
	 WHERE InFile = 'Branches' 
	   AND [ReportId] = @reportId
	   AND [RequiredField] = 'Yes')
BEGIN
    UPDATE [dbo].[Kaas_CrParameter] 
       SET [RequiredField] = 'No' 
	   WHERE InFile = 'Branches' 
	     AND [ReportId] = @reportId
END
GO

DECLARE @reportId INT; 
SELECT @reportId =[ReportId] 
  FROM KaaS_CRReport 
 WHERE [name] = 'Fees Rec By Matter By Period' 
   AND [report_type] = 'A' 

IF EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CrParameter] 
     WHERE InFile = 'Branches'
	   AND [ReportId] = @reportId
	   AND [RequiredField] = 'Yes')
BEGIN
    UPDATE [dbo].[Kaas_CrParameter] 
       SET [RequiredField] = 'No' 
	 WHERE InFile = 'Branches' 
       AND [ReportId] = @reportId
END
GO

 
IF NOT EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CRReport] 
     WHERE [ReportNameKey] = 'MATTERLEDGER'
	   AND [Report_Type] = 'A'
	   AND [Name] = 'Matter Ledger')
BEGIN
    UPDATE [dbo].[Kaas_CRReport] 
       SET [ReportNameKey] = 'MATTERLEDGER' 
	 WHERE [Report_Type] = 'A'
	   AND [Name] = 'Matter Ledger'
END
GO

DECLARE @reportId INT; 
SELECT @reportId =[ReportId]
  FROM KaaS_CRReport 
 WHERE [name] = 'Client Statements' 
   AND [report_type] = 'A' 
IF EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CrParameter] 
     WHERE [ReportId] = @reportId
	   AND [parametername] = 'ToClient'
	   AND [RequiredField] = 'Yes')
BEGIN
    UPDATE [dbo].[Kaas_CrParameter] 
       SET [RequiredField] = 'No' 
	 WHERE [ReportId] = @reportId
	   AND [parametername] = 'ToClient'
	   AND [RequiredField] = 'Yes'
END
GO

IF NOT EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CRReport] 
     WHERE [ReportNameKey] = 'BILLLAYOUT'
	   AND [Report_Type] = 'B'
	   AND [Name] = 'Bill Layout')
BEGIN
    UPDATE [dbo].[Kaas_CRReport] 
       SET [ReportNameKey] = 'BILLLAYOUT' 
	 WHERE [Report_Type] = 'B'
	   AND [Name] = 'Bill Layout'
END
GO

IF NOT EXISTS
   (SELECT TOP 1 1
      FROM [dbo].[Kaas_CRReport] 
     WHERE [ReportNameKey] = 'CLIENTBALANCEMATTER'
	   AND [Report_Type] = 'A'
	   AND [Name] = 'Client Balances By Matter Report')
BEGIN
    UPDATE [dbo].[Kaas_CRReport] 
       SET [ReportNameKey] = 'CLIENTBALANCEMATTER' 
     WHERE [Report_Type] = 'A'
	   AND [Name] = 'Client Balances By Matter Report'
END
GO

IF NOT EXISTS
   (
    SELECT TOP 1 1 FROM 
	       [dbo].[Kaas_CRReport] 
		   WHERE 
		   [ReportNameKey] = 'CLIENTBALANCES'
		   AND
		   [Report_Type] = 'A'
		   AND
		   [Name] = 'Client Balances Report'
   )
BEGIN
    UPDATE [dbo].[Kaas_CRReport] 
       SET [ReportNameKey] = 'CLIENTBALANCES' 
	   WHERE 
		   [Report_Type] = 'A'
		   AND
		   [Name] = 'Client Balances Report'
END
GO

--For Case Management Module 'Time ledger' - Billing Guide Report
IF NOT EXISTS
   (SELECT TOP 1 1
	  FROM [dbo].[Kaas_CRReport] 
     WHERE [ReportNameKey] = 'MATTERBILLGUIDE'
	   AND [Report_Type] = 'A'
	   AND [Name] = 'Billing Guide')
BEGIN
    UPDATE [dbo].[Kaas_CRReport] 
       SET [ReportNameKey] = 'MATTERBILLGUIDE' 
	 WHERE [Report_Type] = 'A'
	   AND [Name] = 'Billing Guide'
END
GO



