IF OBJECT_ID(N'KY_NETRevenueCheckPayment', N'P') IS NOT NULL
DROP PROCEDURE KY_NETRevenueCheckPayment
GO
CREATE PROCEDURE KY_NETRevenueCheckPayment
(
	@POSTINGREF INT
)
AS
	/*
		KY_NETRevenueCheckPayment
		
		This procedure is used to check whether the payment is sent to revenue or not
	*/
BEGIN
	SELECT
		COUNT(BreakdownId)
	FROM
		DebtAllocations 
	WHERE
		PaymentId =@POSTINGREF 
			AND
		[Sent]  <>0
			AND
		[SendDate]IS NOT NULL
			AND
		[Sequence] <>0
END
GO

IF OBJECT_ID(N'KY_NETRevenueInsertDebtAllocation',N'P')IS NOT NULL
	DROP PROCEDURE KY_NETRevenueInsertDebtAllocation
GO
CREATE PROCEDURE KY_NETRevenueInsertDebtAllocation

	@PAYMENTID AS INT,
	@BREAKDOWNID AS INT,
	@TAXAMOUNT AS DECIMAL (18,2),
	@INTERESTAMOUNT AS DECIMAL(18,2),
	--@ALLOCDATE AS DATETIME,
	--@PROCESSDATE AS DATETIME,
	@TYPE AS VARCHAR(100),
	@MATTERCODE AS VARCHAR(20)
AS	
	/*
		KY_NETRevenueInsertDebtAllocation
		
		This procedure is used insert details to debt allocation table
	*/
BEGIN
	--IF NOT EXISTS (SELECT TOP 1 1 FROM DebtAllocations WHERE BreakdownId = @BREAKDOWNID )
	--BEGIN
		INSERT INTO 
			DebtAllocations 
				(BreakdownId ,
				PaymentId ,
				TaxAmount ,
				InterestAmount ,
				--AllocDate ,
				[Type] )
		VALUES
				(@BREAKDOWNID ,
				@PAYMENTID ,
				@TAXAMOUNT ,
				@INTERESTAMOUNT ,
				--@ALLOCDATE ,
				@TYPE )
		UPDATE 
			debtbreakdown 
		SET 
			debtbreakdown.ostaxamount = debtbreakdown.TaxAmount - ISNULL((select SUM(TaxAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0), 
			debtbreakdown.osinterestamt = debtbreakdown.Interest - ISNULL((select SUM(InterestAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0) 
		WHERE 
			MatterCode = @MATTERCODE 
			
	--END
END
GO


IF OBJECT_ID(N'KY_NETRevenueUnAllocate',N'P')IS NOT NULL
	DROP PROCEDURE KY_NETRevenueUnAllocate
GO
	/*
		KY_NETRevenueUnAllocate
		
		This procedure is used to delete the records from debtallocation table based on the paymentID
	*/
CREATE PROCEDURE KY_NETRevenueUnAllocate
	@MATTERCODE VARCHAR(20),
	@POSTREF	INT
AS
BEGIN

UPDATE 
	debtbreakdown 
SET 
	debtbreakdown.ostaxamount = debtbreakdown.TaxAmount + ISNULL((select SUM(TaxAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0), 
	debtbreakdown.osinterestamt = debtbreakdown.Interest + ISNULL((select SUM(InterestAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0) 
WHERE 
	MatterCode = @MATTERCODE 

DELETE 
	DebtAllocations 
WHERE 
	PaymentId = @POSTREF	

END
GO

IF EXISTS
  (SELECT TOP 1 1
     FROM SYS.[objects] SO
    WHERE SO.[name] = N'ky_NETGetAllocateDebt'
      AND SO.[type] = 'P')
  BEGIN
    DROP PROCEDURE [dbo].[ky_NETGetAllocateDebt]
  END
GO

CREATE PROCEDURE
       [dbo].[ky_NETGetAllocateDebt]
      (@MATTERCODE VARCHAR(20),
       @POSTREF int)
AS
/********************************************************************************************
 * ky_NETGetAllocateDebt
 *
 * This Procedure is used to get debt allocation from the debtallocation table
 *
 * Modification History
 * 2016-01-17 Magesh              Created
 * 2016-01-19 Pino Carafa         Reverted to original after making SAM3 and SAM4 identical
 *                                Inserted header comment.... Standards! Used 3-letter
 *                                acronyms for tables.
 *
 ********************************************************************************************/
BEGIN

SELECT DAL.[BREAKDOWNID],
        DAL.[PAYMENTID],
        DAL.[TAXAMOUNT],
        DAL.[INTERESTAMOUNT],
        DAL.[RECORDID],
        DAL.[PROCESSDATE],
        DBD.[MATTERCODE],
        DBD.[STARTPERIOD],
        DBD.[ENDPERIOD],
        DBD.[TAXHEAD],
        DBD.[TAXHEADREGISTRATION],
        DBD.[CHECKCHARACTER],
        DBD.[CHARGETYPE],
        DBD.[SEQUENCE],
        DBD.[RECORDID],
        DBD.[OUTFILENO],
        DBD.[OSINTERESTAMT] AS OSINTERESTAMT ,
		DBD.[OSTAXAMOUNT] AS OSTAXAMOUNT
   FROM [dbo].[DebtAllocations] DAL
        LEFT OUTER JOIN [dbo].[DebtBreakDown] DBD
                     ON DAL.[BREAKDOWNID] = DBD.[RECORDID]
  WHERE DBD.[MatterCode] = @MATTERCODE
    AND DAL.[PaymentId] = @POSTREF
END

GO
IF OBJECT_ID(N'Ky_NETDebtLedgerUpdateDeletePayment', N'P') IS NOT NULL
DROP PROCEDURE Ky_NETDebtLedgerUpdateDeletePayment
GO
/*    
Created By : Magesh Kumar    
Date	   : 19-MAR-2014    
Description: Created to Update Payment Type details FROM debtcostingledger table    
     
Modification History    
-----------------------------------------------------    
Date  | Modified By   | Description    
-----------------------------------------------------    
28JAN2016	Magesh	Added revenue details 
              
*/
CREATE PROCEDURE [dbo].[Ky_NETDebtLedgerUpdateDeletePayment] @postref INT
    ,@Mattercode			VARCHAR(15)
    ,@date					DATETIME
    ,@reference				VARCHAR(20)
    ,@type					VARCHAR(20)
    ,@Method				VARCHAR(40)
    ,@value					DECIMAL(18, 2)
    ,@principalPaid			DECIMAL(18, 2)
    ,@costpaid				DECIMAL(18, 2)
    ,@interestpaid			DECIMAL(18, 2)
    ,@pstatus				CHAR(1)
    ,@Cstatus				CHAR(1)
    ,@Istatus				CHAR(1)
    ,@description			VARCHAR(200)
    ,@enteredby				VARCHAR(20)
    ,@feeearner				VARCHAR(10)
    ,@time					INT
    ,@rate					DECIMAL(18, 2)
    ,@chargevalue			DECIMAL(18, 2)
    ,@timeorcharge			CHAR(1)
    ,@bounce				VARCHAR(1)
    ,@remittanceno			INT
    ,@DATEREMITTED			DATETIME
    ,@HELDDATE				DATETIME
    ,@isRevenue				BIT
    
AS
/*
Last Updated: 12 FEB 2015 Arun- Change NVARCHAR to VARCHAR
			  04JAN2016  Magesh - added query to add hold days and task to the revenue payment
*/
BEGIN
    --Update into debtcosting table              
    DECLARE @amt			DECIMAL(18, 2)
    DECLARE @Bounceamt		DECIMAL(18, 2)
    DECLARE @mattercharge	DECIMAL(18, 2)
	DECLARE @CARRYFORWARD	DECIMAL(18,2)
    DECLARE @TASK			VARCHAR(100)
    DECLARE @HOLDDAYS		INT
    DECLARE @COMISSIONAMT	DECIMAL(18,2)
    
    SET @COMISSIONAMT =(SELECT (@principalPaid  + @interestpaid) )
    
    SET @HOLDDAYS =(SELECT ISNULL(KEYVALUE,0) FROM Settings WHERE UPPER(KeyName) ='REV_HOLDDATE')
    
    SET @mattercharge = @chargevalue
        
    SET @amt =(SELECT ISNULL(VALUE,0) FROM DebtCostingLedger WHERE postingref = @postref)

	IF @isRevenue =1
		BEGIN
			IF @HOLDDAYS =0
			BEGIN
				SELECT @HELDDATE =DATEADD (DAY,21,GETDATE ())
			END
			ELSE
			BEGIN
				SELECT @HELDDATE=CASE WHEN DATEPART(DW,DATEADD (DAY,@HOLDDAYS,GETDATE ()))=1 THEN DATEADD (DAY,@HOLDDAYS+1,GETDATE ())
								 WHEN DATEPART(DW,DATEADD (DAY,@HOLDDAYS,GETDATE ()))=7 THEN DATEADD (DAY,@HOLDDAYS+2,GETDATE ())
								 ELSE DATEADD (DAY,@HOLDDAYS,GETDATE ()) END
			END
			SELECT @TASK =ISNULL(RTRIM(LTRIM(ChargeArrangement)),'')  FROM matters WHERE Code =@Mattercode 
			
			SET @description= ('Commission on ' + CONVERT(VARCHAR(20),@COMISSIONAMT))
		END
	ELSE
		BEGIN
			SET @TASK=''
		END

    --IF (@bounce = 'Y') BEGIN
    --SET @amt = 0.00
    ----SET @mattercharge=0.00           
    --END ELSE BEGIN
    --SET @amt = @value
    --END

	  IF @value >@amt 
		BEGIN
			SET @CARRYFORWARD =@value -@amt 
			SET @value =@amt +@CARRYFORWARD 
		END
	  ELSE
		BEGIN
			SET @CARRYFORWARD  =@amt -@value 
			SET @value =@amt -@CARRYFORWARD 
		END
	IF @bounce='Y'
		BEGIN
			SET @Bounceamt =0
		END
	ELSE
		BEGIN
			SET @Bounceamt =@value 
		END
		


    UPDATE 
        DebtCostingLedger
    SET    
        [DATE] = @date,
        [reference] = @reference,
        [description] = @description,
        [type] = @type,
        --value = @amt,
        Value=@Bounceamt,
        bounced = @value,
        premiumpaid = @principalpaid,
        premiumstatus = @pstatus,
        interestpaid = @interestpaid,
        intereststatus = @istatus,
        costspaid = @costpaid,
        coststatus = @Cstatus,
        paymentmethod = @method,
        HELDDATE = @HELDDATE,
        DATEREMITTED = @DATEREMITTED,
        REMITTANCENO = @remittanceno
    WHERE 
        postingref = @postref

	--Update debtallocation
	
		UPDATE 
			DebtAllocations 
		SET
			[Type] = @type,
			ProcessDate =@HELDDATE ,
			[Sent] =0,
			Sequence =0
			--[SendDate] =GETDATE()  
		WHERE
			PaymentId =@postref 


    --update matter table              
    BEGIN
    DECLARE @collectvalue DECIMAL(18, 2)

    SET 
        @collectvalue = (SELECT
								ISNULL(CollectingBalance,0)-@amt 
							FROM 
								matters
							WHERE 
								code = @mattercode)
        
   SET @collectvalue=@collectvalue +@value 

    --DECLARE @debtvalue DECIMAL(18, 2)

    --SET @debtvalue = (@value - @collectvalue)
    

    IF (@bounce = 'Y') BEGIN
        --IF (@collectvalue = 0) BEGIN
        --    SET @debtvalue = 0
        --SET @mattercharge = 0
        --END ELSE BEGIN
        --    SET @debtvalue = @value
        --END

        UPDATE 
            matters
        SET    
            DebtCollected = (ISNULL(DebtCollected, 0) - @amt),  
            CollectingBalance = (ISNULL(CollectingBalance, 0) - @amt),  
            ChargeBal = ISNULL(ChargeBal, 0) - @mattercharge 
        WHERE 
            code = @mattercode

        DELETE FROM 
            TimeEntry
        WHERE 
            TRACKREF = @postref
    END 
    ELSE BEGIN
        UPDATE matters
        SET    DebtCollected = 0,
            CollectingBalance = 0,
            ChargeBal = ISNULL(ChargeBal, 0) + @mattercharge
        WHERE code = @mattercode
        
			UPDATE  M
			SET CollectingBalance=ISNULL(CollectingBalance,0)+ISNULL(@collectvalue,0),
				DebtCollected =ISNULL(DebtCollected,0)+ISNULL(@collectvalue,0)
			FROM 
			Matters M
			WHERE 
			M.Code =@Mattercode 

    BEGIN
        ---Update into timeentry      
        IF @mattercharge <> 0 
        BEGIN
            IF EXISTS (SELECT TOP 1 1  FROM TimeEntry WHERE TRACKREF = @postref) 
            BEGIN
                UPDATE 
                    TimeEntry
                SET    
                    [DATE] = @date,
                    COMMENT = @description,
                    CHARGE = @mattercharge
                WHERE 
                    TRACKREF = @postref
            END
            ELSE 
            BEGIN
                INSERT INTO 
                    TimeEntry (MATTER
                    , FEEEARN
                    , [DATE]
                    , COMMENT
                    , [TIME]
                    , RATE
                    , CHARGE
                    , TIMEORCHARGE
                    , TRACKREF
                    , DOCKET
                    , REC_IRR
                    ,Task)
                VALUES 
                    (@mattercode, @feeearner, @date, 'Commission on ' + CONVERT(VARCHAR(20),@COMISSIONAMT), @time, @rate, @chargevalue, 'C', @postref, @postref, 'C',@TASK)
            END
        END
    END
    END
    END
END
GO
IF EXISTS
  (SELECT TOP 1 1
     FROM SYS.[objects] SO
    WHERE SO.[name] = N'ky_NETInsertDebtDetailLines'
      AND SO.[type] = 'P')
  BEGIN
    DROP PROCEDURE [dbo].[ky_NETInsertDebtDetailLines]
  END
GO

CREATE PROCEDURE
       [dbo].[ky_NETInsertDebtDetailLines]
      (@MatterCode                VARCHAR(20),
       @StartPeriod               DATETIME,
       @EndPeriod                 DATETIME,
       @TaxHead                   VARCHAR(2),
       @TaxHeadRegistration       VARCHAR(7),
       @CheckCharacter            VARCHAR(1),
       @TaxAmount                 DECIMAL(9),
       @Interest                  DECIMAL(9),
       @ChargeType                VARCHAR(2),
       @Sequence                  DECIMAL(5),
       @OSTaxAmount               DECIMAL(9),
       @OSInterestAmt             DECIMAL(9),
       @RecordID                  DECIMAL(9),
       @OriginalTax               DECIMAL(9),
       @OriginalInterest          DECIMAL(9),
       @OUTFileNo                 INT,
       @CustCheckCharacter        VARCHAR(1),
       @AddCustCheckCharacter     VARCHAR(1),
       @AddCheckCharacter         VARCHAR(1))
AS
/*****************************************************************************
 *
 * Stored Procedure Name: [ky_NETInsertDebtDetailLines]
 * Description:  This procedure is used Insert & update the DebtDetailLines
 *
 * Modification History
 * 2015-11-26 Sridharan             Created
 * 2016-01-19 Pino Carafa           Reverted to original after ensuring
 *                                  that SAM3 and SAM4 versions of the
 *                                  DebtBreakDown table are identical
 *                                  Question: why is AmendmentReason not
 *                                  included in this procedure?
 *
 *****************************************************************************/
BEGIN
 SET NOCOUNT ON
    IF(@RecordID = 0)
    BEGIN
    		IF @Sequence =0
			BEGIN
				SET @Sequence =(SELECT MAX(ISNULL([Sequence],0))+1  FROM DebtBreakDown WHERE MatterCode =@MatterCode)
			END
        INSERT INTO DebtBreakDown
           ([MatterCode]
           ,[StartPeriod]
           ,[EndPeriod]
           ,[TaxHead]
           ,[TaxHeadRegistration]
           ,[CheckCharacter]
           ,[TaxAmount]
           ,[Interest]
           ,[ChargeType]
           ,[Sequence]
           ,[OSTaxAmount]
           ,[OSInterestAmt]
           ,[OriginalTax]
           ,[OriginalInterest]
           ,[OUTFileNo]
           ,[CustCheckCharacter]
           ,[AddCustCheckCharacter]
           ,[AddCheckCharacter])
     VALUES
           (@MatterCode
           ,@StartPeriod
           ,@EndPeriod
           ,@TaxHead
           ,@TaxHeadRegistration
           ,@CheckCharacter
           ,@TaxAmount 
           ,@Interest 
           ,@ChargeType
           ,@Sequence
           ,@OSTaxAmount
           ,@OSInterestAmt
           ,@OriginalTax
           ,@OriginalInterest
           ,@OUTFileNo
           ,@CustCheckCharacter
           ,@AddCustCheckCharacter
           ,@AddCheckCharacter)

    END
    ELSE
    BEGIN
        UPDATE
            DebtBreakDown
        SET
            [MatterCode]                = @MatterCode
            ,[StartPeriod]                = @StartPeriod
            ,[EndPeriod]                = @EndPeriod
            ,[TaxHead]                    = @TaxHead
            ,[TaxHeadRegistration]        = @TaxHeadRegistration
            ,[CheckCharacter]            = @CheckCharacter
            ,[TaxAmount]                = @TaxAmount
            ,[Interest]                    = @Interest
            ,[ChargeType]                = @ChargeType
            ,[Sequence]                    = @Sequence
            ,[OSTaxAmount]                = @OSTaxAmount
            ,[OSInterestAmt]            = @OSInterestAmt
            ,[OriginalTax]                = @OriginalTax
            ,[OriginalInterest]            = @OriginalInterest
            ,[OUTFileNo]                = @OUTFileNo
            ,[CustCheckCharacter]        = @CustCheckCharacter
            ,[AddCustCheckCharacter]    = @AddCustCheckCharacter
            ,[AddCheckCharacter]        = @AddCheckCharacter
        WHERE
            RecordID                    = @RecordID
    END

            UPDATE
                CaseMaster
            SET
                CsOrgDebt = ISNULL((select SUM(TaxAmount)+SUM(Interest) from DebtBreakDown where MatterCode = CaseMaster.CsCode),0)
            WHERE
                CsCode = @MatterCode


            UPDATE
                matters
            SET
                OriginalDebt =ISNULL((select SUM(TaxAmount)+SUM(Interest) from DebtBreakDown where MatterCode = matters.Code),0)
            WHERE
                Code =@MatterCode

 SET NOCOUNT OFF
END

GO
IF OBJECT_ID(N'[Ky_NETDebtLedgerDeleteCostingLedger]',N'P')IS NOT NULL
	DROP PROCEDURE [Ky_NETDebtLedgerDeleteCostingLedger]
GO 
-- =============================================
-- Author:		Magesh Kumar
-- Create date: 19-MAR-2014
-- Description:	Created to Delete Costing Ledger table based on the type
-- Last Modification :
--				23JUN2015	Sridharen	KEYD - 2332 - Use of LTRIM(RTRIM(
--				28JAN2016	Magesh		Added debtallocation and debtbreakdown while deleting payment
-- =============================================
CREATE PROCEDURE [Ky_NETDebtLedgerDeleteCostingLedger] 
	@postref INT
AS
BEGIN
	DECLARE @type VARCHAR(20)
	SET @type=(SELECT Upper(RTRIM(ISNULL([type],''))) FROM DebtCostingLedger WHERE POSTINGREF=@postref)
	
	IF @type='INTEREST'
	BEGIN
		UPDATE 
			matters 
		SET 
			DebtInterest=ISNULL(DebtInterest,0)-(SELECT ISNULL(interest,0) FROM DebtCostingLedger WHERE POSTINGREF=@postref) 
		WHERE 
			Code=(SELECT RTRIM(MATTERCODE) FROM DebtCostingLedger WHERE POSTINGREF=@postref)
	END
	ELSE IF @type='COSTS'
		BEGIN
			UPDATE 
				matters 
			SET 
				RecoverableCosts=ISNULL(RecoverableCosts,0)-(SELECT ISNULL(COSTS,0) FROM DebtCostingLedger WHERE POSTINGREF=@postref) 
			WHERE 
				Code=(SELECT RTRIM(MATTERCODE) FROM DebtCostingLedger WHERE POSTINGREF=@postref)
		END
	ELSE
		BEGIN
		--update Matter Table
			UPDATE 
				matters 
			SET 
				DebtCollected=ISNULL(DebtCollected,0)-(SELECT ISNULL(Value,0) FROM DebtCostingLedger WHERE POSTINGREF=@postref),
				CollectingBalance=ISNULL(CollectingBalance,0)-(SELECT ISNULL(Value,0) FROM DebtCostingLedger WHERE POSTINGREF=@postref) 
			WHERE 
				Code=(SELECT RTRIM(MATTERCODE) FROM DebtCostingLedger WHERE POSTINGREF=@postref);
		--Delete Data FROM Time Entry
			DELETE FROM 
				TimeEntry 
			WHERE 
				DOCKET=@postref;	
				
		--Revenue update 			
			UPDATE 
				debtbreakdown 
			SET 
				debtbreakdown.ostaxamount = debtbreakdown.ostaxamount + ISNULL((select SUM(TaxAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0), 
				debtbreakdown.osinterestamt = debtbreakdown.osinterestamt + ISNULL((select SUM(InterestAmount) from DebtAllocations where BreakdownId = debtbreakdown.RecordId),0) 
			WHERE 
				MatterCode = (SELECT RTRIM(MATTERCODE) FROM DebtCostingLedger WHERE POSTINGREF=@postref)  				  
				
			DELETE 
				DebtAllocations 
			WHERE 
				PaymentId = @POSTREF
			
		END

	DELETE FROM 
		debtcostingledger 
	WHERE 
		postingref=@postref
END
GO

IF OBJECT_ID(N'KY_NETRevenueInsertintoDiary',N'P')IS NOT NULL
	DROP PROCEDURE KY_NETRevenueInsertintoDiary
GO
CREATE PROCEDURE KY_NETRevenueInsertintoDiary

	(@CASECODE          VARCHAR(20),
    @FNCODE             VARCHAR(10),
    @TEAMCODE           VARCHAR(10),
    @value				VARCHAR(100),
    @principal			VARCHAR(100),
    @intrest			VARCHAR(100),
    @From				VARCHAR(100)='')
 AS
 
	/*
		KY_NETRevenueInsertintoDiary
		
		This Procedure is used to insert dairy action when ever changes made in debt details or while allocate payment in debt ledger.
	*/
BEGIN

	DECLARE @ACTIONID AS BIGINT
	DECLARE @Subject  AS VARCHAR(500)
	DECLARE @ACTIONCODE  AS VARCHAR(15)
	
	EXEC @ACTIONID = [dbo].[ky_NETGetNextActionID]
	
	DECLARE @TIMECONVERT INT
    SET @TIMECONVERT = dbo.ky_ConvertTimeToClarion(GETDATE())
    SET @principal =(SELECT CAST(@principal AS NUMERIC(18,2)))
    SET @intrest =(SELECT CAST(@intrest AS NUMERIC(18,2)))
    DECLARE @TEXT1 AS VARCHAR(5000)
    
    IF @From <>''
		BEGIN

			SET @TEXT1 = 'Manual Amendment.'+CHAR(13)+'Sequence No: '+@value + CHAR(13)+'New Tax Amount: ' +CONVERT(VARCHAR(100),@principal) +CHAR(13)+'New Interest Amount: '+CONVERT(VARCHAR(100),@intrest )
			SET @ACTIONCODE ='R004'
			SET @Subject ='Manual Debt Breakdown Amendment'
		END
	ELSE
		BEGIN
			SET @TEXT1 = 'Payment Received From Tax Payer'+CHAR(13)+'Amount:'+@value + CHAR(13)+'Tax:' +CONVERT(VARCHAR(100),@principal) +CHAR(13)+'Interest:'+CONVERT(VARCHAR(100),@intrest ) 
			SET @ACTIONCODE ='O01'
			SET @Subject ='Payment received'
		END
    
	INSERT
      INTO [dbo].[diary]
          ([CASECODE],
           [DATE],
           [STATUS],
           [ACTIONCODE],
           [ACTIONSTATUS],
           [ACTIONTYPE],
           [PROCESSTYPE],
           [FNCODE],
           [TEAMCODE],
           [TEXT1],
           [TEXT2],
           [DELEGATEDFNR],
           [DELEGATEDDATE],
           [DELEGATEDBACKDATE],
           [DEFERRED],
           [DUEDATE],
           [PUBLISH],
           [DYSTARTTIME],
           [DYENDTIME],
           [DURATION],
           [ACTIONID],
           [ORGINALACTIONID],
           [PRIORITY],
           [HIGHLIGHTED],
           [MILESTEONETYPE],
           [ATTACHMENTS],
           [PROCESSSTATUS],
           [WORKPROCESS],
           [BILLABLE],
           [BILLDESCRIPTION],
           [EMAILADDRESS],
           [ADDRESSTO],
           [CCTo],
           [BCCTo],
           [EMAIL],
           [SUBJECT],
           [DELEGATIONSTATUS],
           [DRAFTBILLNO],
           [CHEQUEREQNO],
           [TxmSent],
           [Location],
           [HearingType],
           [ForCopy],
           [TxmDate],
           [TxmSeqNo],
           [DisplayText],
           [Flag])
    VALUES(@CASECODE,
           GETDATE(),
           1,
           @ACTIONCODE,
           null,
           null,
           null,
           @FNCODE,
           @TEAMCODE,
           @TEXT1 ,
           null,
           null,
           null,
           null,
           null,
           null,
           null,
           @TIMECONVERT,
           @TIMECONVERT,
           0,
           @ACTIONID,
           0,
           null,
           null,
           null,
           null,
           0,
           0,
           0,
           null,
           null,
           null,
           null,
           null,
           null,
           @Subject,
           0,
           0,
           0,
           0,
           null,
           'Not Applicable',
           null,
           null,
           0,
          null,
           0)
    INSERT
              INTO [dbo].[DiaryDelegations]
                  ([ACTIONID],
                   [HANDLER],
                   [TEAM],
                   [DATE],
                   [TIME],
                   [DATER],
                   [TIMER],
                   [DUEDATE],
                   [DUETIME],
                   [REVIEW],
                   [STATUS],
                   [OWNER],
                   [DELEGATE],
                   [DELEGATESTATUS],
                   [ActionType],
                   [FromHandler],
                   [ReturnedBy],
                   [DelType])
            VALUES(@ACTIONID,
                   @FNCODE,
                   @TEAMCODE,
                   GETDATE(),
                   @TIMECONVERT,
                   null,
           @TIMECONVERT,
                   GETDATE(),
                   @TIMECONVERT,
                   null,
                   1,
                   'Y',
                   @FNCODE,
                   0,
       null,
                   @FNCODE,
                   null,
                   'Created')
END
GO
