﻿INSERT INTO BUD_CONTRACT_RENT (
BUDC_RENT_ID, 
BUDC_RENT_CODE, 
BUDC_RENT_NAME, 
BUDC_RENT_TYPE,
TOTAL_AMT, 
ADJUST_PRICE_RATE,
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,
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)
SELECT 
    'BCR' + right('000000000000' + convert(varchar(20),IC.ID), 15 - len('BCR')),
    IC.BUDC_RENT_CODE, IC.BUDC_RENT_NAME, 'CT', IC.TOTAL_AMT, IC.ADJUST_PRICE_RATE, IC.DEPOSIT_AMT, 0,
    IC.TOTAL_AMT-((IC.TOTAL_AMT)*(10/(100+10))) AS TOTAL_PAY_AMT, IC.TOTAL_AMT, 5, (IC.TOTAL_AMT)*(10/(100+10)), IC.SENDMAIL_MONTH_PAY,
    IC.TYPE_PERIOD,N'',IC.PERIOD_ADJUST, IC.SIGN_DT, IC.START_DT, IC.END_DT,IC.NOTES,
    '1',GETDATE(),'U','Y','GG',IC.FREE_FIX_TIME, DATEADD(MONTH, IC.FREE_FIX_TIME, IC.START_DT), IC.KEEP_PRICE_TIME,
    IC.CTSC_UNIT,DATEDIFF(MONTH, IC.START_DT, IC.END_DT),IC.CUST_CODE,
    'RATE',CASE WHEN IC.ADJUST_PRICE_RATE > 0 THEN 'UP' ELSE 'DOWN' END,0,5,'N',IC.START_DT
FROM IMPORT_CT IC

UPDATE SYS_CODEMASTERS SET 	CurValue=(SELECT dbo.FN_GetNumberOfPrefixID(MAX(BCR.BUDC_RENT_ID)) FROM BUD_CONTRACT_RENT BCR) + 1
				WHERE Prefix='BCR'