IF OBJECT_ID(N'ky_NETAddTimeDayBook',N'P')IS NOT NULL
    DROP PROCEDURE [dbo].[ky_NETAddTimeDayBook]
GO

CREATE PROCEDURE
       [dbo].[ky_NETAddTimeDayBook]
      (@Matter                VARCHAR(20),
       @FeeEarner             VARCHAR(10),
       @Minutes               INT = 0,
       @StopwatchStatus       INT = 1,
       @RecordID              INT = 0,
       @UpdateByPass          BIT = 0,
       @DeleteByPass          BIT = 0,
       @AddNew                BIT = 0)
AS
/*******************************************************************************************
 *
 * exec [dbo].[ky_NETAddTimeDayBook] '','ADM',0,1,0,0,0,0
 * Used to add new Timedaybook entry
 *
 * Modification History
 * 2014-04-28 Praveen Yadav.P   Created
 * 2014-12-15 Pino        Matter Code can be up to 20 characters.
 * 2015-06-10 Magesh      Added two new parameter @period and @year to update
 *                        year and period value in timedaybook table while insert
 * 2015-06-23 Sridharen   KEYD - 2332 - Use of LTRIM(RTRIM(
 * 2015-08-27 Pino        Improve readability
 *                        Set default logging information
 * 2015-03-16 Magesh	  Included an condition to check whether the record in time or charge.
 * 2016-04-01 Suriya	  KEYD-3398-Allow users to have default value for chargeable/non-chargeable time
 * 2016-09-09 Arun        Have removed the condition to add blank timer 
 *******************************************************************************************/
BEGIN

  IF @UpdateByPass = 0
    BEGIN
      UPDATE TDB
         SET TDB.[StopwatchStatus] = 1
        FROM [dbo].[TimeDayBook] TDB
       WHERE TDB.[FeeEarn] = @FeeEarner
    END

  IF @DeleteByPass = 0
    BEGIN --IF @DeleteByPass = 0
      IF(@Minutes=0)
        BEGIN
          DELETE TDB
            FROM [dbo].[TimeDayBook] TDB
           WHERE TDB.[FeeEarn] = @FeeEarner
             AND ISNULL(TDB.[Time], 0) = 0
             AND TDB.[Matter] <> ''
             AND TDB.[TimeOrCharge] = 'T'
        END
      ELSE
        BEGIN
          DELETE TDB
            FROM [dbo].[TimeDayBook] TDB
           WHERE TDB.[FeeEarn] = @FeeEarner
             AND ISNULL(TDB.[Time], 0) = 0
             AND TDB.[Matter] NOT IN ('' , @Matter)
             AND TDB.[TimeOrCharge] = 'T'
            END
    END  --IF @DeleteByPass = 0

  IF @RecordID <> 0
    BEGIN -- IF @RecordID <> 0
      UPDATE TDB
         SET TDB.[StopwatchStatus] = @StopwatchStatus,
             TDB.[Time] = @Minutes,
             TDB.[Charge] = (ISNULL(TDB.[Rate], 0) * (@Minutes / 60.0)),
             TDB.[Post] = 'Y'
        FROM [dbo].[TimeDayBook] TDB
       WHERE TDB.[RecordID] = @RecordID

      SELECT TOP 1
             TDB.[RecordID],
             ISNULL(TDB.[Time], 0) AS [Minutes]
        FROM [dbo].[TimeDayBook] TDB
       WHERE TDB.[RecordID] = @RecordID
    END --IF @RecordID <> 0
  ELSE -- ELSE CONDITION: IF @RecordID <> 0
    BEGIN -- ELSE CONDITION: IF @RecordID <> 0
      IF (   NOT EXISTS(SELECT TOP 1 1
                          FROM [dbo].[TimeDayBook] TDB
                         WHERE TDB.[FeeEarn] = @FeeEarner
                           AND RTRIM(TDB.[Matter]) = @Matter
                           AND TDB.[Post] = 'Y'
                           AND TDB.[TIMEORCHARGE]='T')
          OR @Matter=''
          OR @AddNew=1) --Insert conditions
        BEGIN --Insert conditions
          DECLARE @PERIOD AS INT
          DECLARE @YEAR AS INT

          DECLARE @IsSAM4 bit
          SET @IsSAM4 = [dbo].[ISSAM4]()

          IF @IsSAM4 = 0
            BEGIN
              SELECT @PERIOD=ISNULL(CURPER,0),
                     @YEAR=ISNULL(CL.[YEAR],0)
                FROM [dbo].[control] CL
            END
          ELSE
            BEGIN
              SET @PERIOD=(SELECT ISNULL(numvalue,0) FROM CtrlNum WHERE code='CurPeriod')
              SET @YEAR=(SELECT ISNULL(numvalue,0) FROM CtrlNum WHERE code='CurYear')
            END
		DECLARE @IsChargeable VARCHAR(1)
		SET @IsChargeable = (SELECT ISNULL(ISChargeable,'C') from Handlers WHERE CODE = @FeeEarner)
         
         
         
           DECLARE @MatterBillingMethod CHAR(1)
		    DECLARE @ClientBillingMethod CHAR(1)
		    DECLARE @DefaultBillingMethod CHAR(1)
		    
		    
		    SELECT  
				@MatterBillingMethod = RTRIM(ISNULL(M.BillingMethod,'')), 
				@ClientBillingMethod = RTRIM(ISNULL(C.BillingMethod,''))
			FROM 
				Matters M
			LEFT JOIN Contacts C 
				ON C.Code = M.ClientCode 
			WHERE M.Code = @Matter
			
			IF(@MatterBillingMethod <> '')
			BEGIN
				SET @DefaultBillingMethod = @MatterBillingMethod
			END
			ELSE IF(@ClientBillingMethod <> '')
			BEGIN
				SET @DefaultBillingMethod = @ClientBillingMethod
			END
			ELSE
			BEGIN
				SET @DefaultBillingMethod = 'T'
			END
         
         
         
         
         
         
         
          INSERT
            INTO [dbo].[TimeDayBook]
                ([Matter],
                 [FeeEarn],
                 [Date],
                 [Time],
                 [Task],
                 [TimeOrCharge],
                 [Rec_Irr],
                 [Post],
                 [StopwatchStatus],
                 [Period],
                 [Year],
                 [MatterLevel])
          SELECT NW.[MATTER],
                 NW.[FeeEarn],
                 NW.[Date],
                 NW.[Time],
                 NW.[Task],
                 NW.[TimeOrCharge],
                 @IsChargeable,
                 NW.[Post],
                 NW.[StopwatchStatus],
                 NW.[Period],
                 NW.[Year],
                 MAT.[ChargeLevel]
            FROM (SELECT @Matter AS [MATTER],
                         @FeeEarner AS [FeeEarn],
                         CONVERT(VARCHAR(12), GETDATE(), 112) AS [Date],
                         @Minutes AS [Time],
                         '' AS [Task],
                         @DefaultBillingMethod AS [TimeOrCharge],
                         'Y' AS [Post],
                         @StopwatchStatus AS [StopwatchStatus],
                         @PERIOD AS [Period],
                         @YEAR As [Year]) NW
                 LEFT OUTER JOIN [dbo].[matters] MAT
                              ON MAT.[Code] = NW.[MATTER]

          SELECT SCOPE_IDENTITY() AS [RecordID],
                 0 AS [Minutes]
        END --Insert conditions
      ELSE --(ELSE CONDITION) Insert conditions
        BEGIN --(ELSE CONDITION) Insert conditions
          SET @RecordID = NULL

            SELECT TOP 1
                   @RecordID = TDB2.[RecordID]
              FROM [dbo].[TimeDayBook] TDB2
             WHERE TDB2.[FEEEARN] = @FeeEarner
               AND TDB2.[MATTER] = @Matter
               AND TDB2.[TIMEORCHARGE] = 'T'
          ORDER BY TDB2.[RecordID] DESC

          IF @RecordID IS NOT NULL
            BEGIN
              UPDATE TDB
                 SET TDB.[StopwatchStatus] = @StopwatchStatus,
                     TDB.[Post] = 'Y'
                FROM [dbo].[TimeDayBook] TDB
               WHERE TDB.[RecordID] = @RecordID
              
              SELECT TDB.[RecordID],
                     ISNULL(TDB.[Time], 0) AS [Minutes]
                FROM [dbo].[TimeDayBook] TDB
               WHERE TDB.[RecordID] = @RecordID
            END
        END --(ELSE CONDITION) Insert conditions

    END -- ELSE CONDITION: IF @RecordID <> 0

END
GO