﻿ALTER TABLE BUD_CONTRACT_RENT_TERMS
ALTER COLUMN TERMS_ID VARCHAR(15) NULL

ALTER TABLE BUD_CONTRACT_RENT
ALTER COLUMN BUDC_RENT_CODE NVARCHAR(500) NULL

DECLARE @BUDC_RENT_NAME NVARCHAR(500), @BRANCH_RENT_CODE VARCHAR(10), @BUDC_RENT_CODE NVARCHAR(500),
        @SIGN_DT DATETIME2, @START_DT DATETIME2, @END_DT DATETIME2, @DEPOSIT_AMT DECIMAL, @TYPE_PERIOD INT,
        @ADJUST_PRICE_RATE NVARCHAR(500), @PERIOD_ADJUST NVARCHAR(500), @ADJUST_CONDITION VARCHAR(50),
        @TERMS_A NVARCHAR(500), @TERMS_B NVARCHAR(500), @TOTAL_AMT DECIMAL, @TOTAL_SER_AMT DECIMAL, @KEEP_PRICE_TIME DECIMAL, 
        @FREE_FIX_TIME DECIMAL, @CTSC_UNIT VARCHAR(10), @SENDMAIL_MONTH_PAY VARCHAR(10), @ID INT

DECLARE cur CURSOR FOR
	SELECT ID
          ,BUDC_RENT_NAME
          ,BRANCH_RENT_CODE
          ,BUDC_RENT_CODE
          ,SIGN_DT
          ,START_DT
          ,END_DT
          ,DEPOSIT_AMT
          ,TYPE_PERIOD
          ,ADJUST_PRICE_RATE
          ,PERIOD_ADJUST
          ,ADJUST_CONDITION
          ,TERMS_A
          ,TERMS_B
          ,TOTAL_AMT
          ,TOTAL_SER_AMT
          ,KEEP_PRICE_TIME
          ,FREE_FIX_TIME
          ,CTSC_UNIT
          ,SENDMAIL_MONTH_PAY
	FROM IMPORT_DT
OPEN cur

FETCH NEXT FROM cur INTO @ID, @BUDC_RENT_NAME, @BRANCH_RENT_CODE, @BUDC_RENT_CODE,
        @SIGN_DT, @START_DT, @END_DT, @DEPOSIT_AMT, @TYPE_PERIOD,
        @ADJUST_PRICE_RATE, @PERIOD_ADJUST, @ADJUST_CONDITION,
        @TERMS_A, @TERMS_B, @TOTAL_AMT, @TOTAL_SER_AMT, @KEEP_PRICE_TIME, 
        @FREE_FIX_TIME, @CTSC_UNIT, @SENDMAIL_MONTH_PAY

WHILE @@FETCH_STATUS = 0 BEGIN
    DECLARE @l_BUDC_RENT_ID VARCHAR(15)
	EXEC SYS_CodeMasters_Gen 'BUD_CONTRACT_RENT', @l_BUDC_RENT_ID out

	INSERT INTO BUD_CONTRACT_RENT (
                BUDC_RENT_ID, 
                BUDC_RENT_CODE, 
                BUDC_RENT_NAME, 
                BUDC_RENT_TYPE,
                TOTAL_AMT, 
                RANGE_CONTENT,
                DEPOSIT_AMT,
                TOTAL_SER_AMT,
                TOTAL_PAY_AMT,
                TOTAL_REMAIN_AMT,
                VAT, 
                PRICE_VAT, 
                SENDMAIL_MONTH_PAY, 
                TYPE_PERIOD, 
                DEPOSIT_NOTE, 
                PERIOD_ADJUST, 
                SIGN_DT, 
                START_DT, 
                END_DT, 
                NOTES, 
                RECORD_STATUS,
                MAKER_ID,
                CREATE_DT, 
                AUTH_STATUS,
                IS_SENDMAIL_PAY,
                ADJUST_CONDITION,
                FREE_FIX_TIME, 
                START_PAYMENT_DT, 
                KEEP_PRICE_TIME, 
                CTSC_UNIT,
                RENT_TIME,
                CUST_CODE,
                RATE_UNIT,
                ADJUST_PRICE_RANGE,
                VAT_MB, 
                VAT_TNCN,
                IS_AUTO_ADJUST,
                DELIVERY_DT, BRANCH_RENT_ID)
VALUES (
    @l_BUDC_RENT_ID,
    @BUDC_RENT_CODE, @BUDC_RENT_NAME, 'DT', @TOTAL_AMT+@TOTAL_SER_AMT, @ADJUST_PRICE_RATE, @DEPOSIT_AMT, @TOTAL_SER_AMT,
    @TOTAL_AMT-(@TOTAL_AMT*(10.00/(100.00+10.00))), @TOTAL_AMT, 5, (@TOTAL_AMT)*(10.00/(100.00+10.00)), 9,
    @TYPE_PERIOD,N'',0, @SIGN_DT, @START_DT, @END_DT,@PERIOD_ADJUST,
    '1','thaohtt1',GETDATE(),'E','Y','GG',@KEEP_PRICE_TIME, DATEADD(MONTH, @KEEP_PRICE_TIME, @START_DT), @FREE_FIX_TIME,
    @CTSC_UNIT,DATEDIFF(MONTH, @START_DT, @END_DT),NULL,
    'RATE','UP',0,5,'N',@START_DT,
    (SELECT CB.BRANCH_ID FROM CM_BRANCH CB WHERE CB.BRANCH_CODE = @BRANCH_RENT_CODE)
    )


DECLARE @l_BUDC_TERMS_ID VARCHAR(15)

			EXEC SYS_CodeMasters_Gen 'BUD_CONTRACT_RENT_TERMS', @l_BUDC_TERMS_ID OUT
			INSERT INTO dbo.BUD_CONTRACT_RENT_TERMS (BUDC_TERMS_ID, BUDC_RENT_ID, SIDE, TERMS_CONTENT, 
                                    NOTES, RECORD_STATUS, MAKER_ID, CREATE_DT, AUTH_STATUS, CHECKER_ID, APPROVE_DT, STATUS)
            VALUES (@l_BUDC_TERMS_ID, @l_BUDC_RENT_ID, 'A', @TERMS_A, N'', 
                        '1', 'admin', CONVERT(DATETIME, GETDATE(), 103), 'U', '', NULL, '1');

            EXEC SYS_CodeMasters_Gen 'BUD_CONTRACT_RENT_TERMS', @l_BUDC_TERMS_ID OUT
			INSERT INTO dbo.BUD_CONTRACT_RENT_TERMS (BUDC_TERMS_ID, BUDC_RENT_ID, SIDE, TERMS_CONTENT, 
                                    NOTES, RECORD_STATUS, MAKER_ID, CREATE_DT, AUTH_STATUS, CHECKER_ID, APPROVE_DT, STATUS)
            VALUES (@l_BUDC_TERMS_ID, @l_BUDC_RENT_ID, 'B', @TERMS_B, N'', 
                        '1', 'admin', CONVERT(DATETIME, GETDATE(), 103), 'U', '', NULL, '1');


	FETCH NEXT FROM cur INTO @ID, @BUDC_RENT_NAME, @BRANCH_RENT_CODE, @BUDC_RENT_CODE,
        @SIGN_DT, @START_DT, @END_DT, @DEPOSIT_AMT, @TYPE_PERIOD,
        @ADJUST_PRICE_RATE, @PERIOD_ADJUST, @ADJUST_CONDITION,
        @TERMS_A, @TERMS_B, @TOTAL_AMT, @TOTAL_SER_AMT, @KEEP_PRICE_TIME, 
        @FREE_FIX_TIME, @CTSC_UNIT, @SENDMAIL_MONTH_PAY

END

CLOSE cur
DEALLOCATE cur

