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

CREATE PROCEDURE
       [dbo].[Ky_NetUpdateTimeEntry]
      (@RecordID       INT,
       @Date           DATETIME,
       @Time           INT,
       @Rate           DECIMAL(10,2),
       @Task           VARCHAR(6),
       @IsChargeable   VARCHAR(3),
       @Comment        VARCHAR(1000))
AS
/*************************************************************************************************************
 *
 * [dbo].[Ky_NetUpdateTimeEntry]
 * modifications: 2015-02-12    Pino    Change NVARCHAR to VARCHAR
 *                                      rate is 10, 2
 *                                      comment can be up to 1000 chars.
 *                2015-09-17    John G. Round up units
 *
 *************************************************************************************************************/
BEGIN


INSERT 
  INTO [dbo].[TimeEntryLogNew]
      ([MATTER],
       [FEEEARN],
       [DATE],
       [TIME],
       [RATE],
       [CHARGE],
       [TASK],
       [TIMEORCHARGE],
       [COMMENT],
       [BILLED],
       [NCCODE],
       [DOCKET],
       [TEAM],
       [TRACKREF],
       [REC_IRR],
       [SHOULDHAVE],
       [INVOICENO],
       [PERIOD],
       [YEAR],
       [BILLINGPERIOD],
       [BILLINGYEAR],
       [OPENINV],
       [RECORDID],
       [ActionId],
       [StageCode],
       [Leave],
       [BilledAmount],
       [IncludeInBill],
       [BILLINGDATE],
       [AxleNumber],
       [WriteOffDT],
       [WriteOffID],
       [LogFor])
SELECT TIE.[MATTER],
       TIE.[FEEEARN],
       TIE.[DATE],
       TIE.[TIME],
       TIE.[RATE],
       TIE.[CHARGE],
       TIE.[TASK],
       TIE.[TIMEORCHARGE],
       TIE.[COMMENT],
       TIE.[BILLED],
       TIE.[NCCODE],
       TIE.[DOCKET],
       TIE.[TEAM],
       TIE.[TRACKREF],
       TIE.[REC_IRR],
       TIE.[SHOULDHAVE],
       TIE.[INVOICENO],
       TIE.[PERIOD],
       TIE.[YEAR],
       TIE.[BILLINGPERIOD],
       TIE.[BILLINGYEAR],
       TIE.[OPENINV],
       TIE.[RECORDID],
       TIE.[ActionId],
       TIE.[StageCode],
       TIE.[Leave],
       TIE.[BilledAmount],
       TIE.[IncludeInBill],
       TIE.[BILLINGDATE],
       TIE.[AxleNumber],
       TIE.[WriteOffDT],
       TIE.[WriteOffID],
       'C'
  FROM [dbo].[TimeEntry] TIE
 WHERE TIE.[RecordID] = @RecordID

DECLARE @MatterCode VARCHAR(20)
DECLARE @TYPE VARCHAR(1)

SELECT @TYPE = TIE.[TimeOrCharge],
       @MatterCode = TIE.[Matter]
  FROM [dbo].[TimeEntry] TIE
 WHERE RECORDID = @RecordID
 
DECLARE @CHARGE DECIMAL(10, 2)

/************************************
 * John - Recalculating units Start *
 ************************************/
DECLARE @ISSAM4 BIT
DECLARE @UNITS DECIMAL(11, 2)
DECLARE @ROUNDUPUNITS BIT
DECLARE @TUTNUMVALUE DECIMAL(17, 2)

SET @ISSAM4 = [dbo].[ISSAM4]()
SET @ROUNDUPUNITS = 0

SELECT @ROUNDUPUNITS = [SET].[KeyValue]
  FROM [dbo].[Settings] [SET]
 WHERE [SET].[KeyName] = 'RoundupUnits'

IF (@ROUNDUPUNITS = 0)
BEGIN
  SET @TUTNUMVALUE = 1
END
ELSE
BEGIN
  IF @ISSAM4 = 1
    BEGIN
      SELECT @TUTNUMVALUE = CTN.[NumValue]
        FROM [dbo].[CtrlNum] CTN
       WHERE CTN.[Code] = 'TimeUnit'
    END

  SET @TUTNUMVALUE = CASE WHEN ISNULL(@TUTNUMVALUE, 0) = 0 THEN 6 ELSE @TUTNUMVALUE END
END

IF @Time >= 0
  BEGIN
    SET @UNITS = Ceiling(CONVERT(FLOAT, @Time) / CONVERT(FLOAT, @TUTNUMVALUE))
  END
ELSE
  BEGIN
    SET @UNITS = -Ceiling(CONVERT(FLOAT, -@Time) / CONVERT(FLOAT, @TUTNUMVALUE))
  END

SET @Time = @UNITS * @TUTNUMVALUE

/**********************************
 * John - Recalculating units END *
 **********************************/

SET @CHARGE = CASE @TYPE
                   WHEN 'C' THEN @Rate
              ELSE @Rate * (@Time / 60.0) END

UPDATE TIE
   SET TIE.[DATE] = @Date,
       TIE.[TIME] = @Time,
       TIE.[Task] = @Task,
       TIE.[RATE] = @Rate,
       TIE.[CHARGE] = @CHARGE,
       TIE.[Units] = @UNITS,
       TIE.[COMMENT] = @Comment,
       TIE.[REC_IRR] = @IsChargeable
  FROM [dbo].[TimeEntry] TIE
 WHERE TIE.[RecordID] = @RecordID

EXEC [dbo].[ky_WDUpdateMatterTimeBalance] @MatterCode

END

GO
