-- 租赁合同新增计息状态
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_LEASE_CONTRACT_NEW' AND KSQL_COL_NAME IN ('FRENTSETTLESTATUS'))
ALTER TABLE T_FA_LEASE_CONTRACT_NEW ADD (FRENTSETTLESTATUS CHAR(1) DEFAULT 'A' NOT NULL );

-- 租赁合同初始化
IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_FA_LEASE_INIT')
CREATE TABLE T_FA_LEASE_INIT (
                                 FID BIGINT DEFAULT 0 NOT NULL,
                                 FORGID BIGINT DEFAULT 0 NOT NULL,
                                 FNUMBER NVARCHAR(50) DEFAULT ' ' NOT NULL,
                                 FBASECURRENCYID BIGINT DEFAULT 0 NOT NULL,
                                 FEXCHANGETABLEID BIGINT DEFAULT 0 NOT NULL,
                                 FPERIODTYPEID BIGINT DEFAULT 0 NOT NULL,
                                 FSTATUS CHAR(1) DEFAULT ' ' NOT NULL,
                                 FENABLE CHAR(1) DEFAULT '0' NOT NULL,
                                 FENABLEPERIODID BIGINT DEFAULT 0 NOT NULL,
                                 FCURRENTPERIODID BIGINT DEFAULT 0 NOT NULL,
                                 FCREATORID BIGINT DEFAULT 0 NOT NULL,
                                 FMODIFIERID BIGINT DEFAULT 0 NOT NULL,
                                 FCREATETIME DATETIME,
                                 FMODIFYTIME DATETIME,
                                 FMASTERID BIGINT DEFAULT 0 NOT NULL,
                                 FDEPRESYSTEMID BIGINT DEFAULT 0 NOT NULL,
                                 FNAME NVARCHAR(100) DEFAULT ' ',
                                 FSYSTEMSWITCHDAY DATETIME
);

EXEC P_ALTERPK 'PK_FA_LEASE_INIT', 'T_FA_LEASE_INIT', 'FID', '1';

IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_T_FA_LEASE_INIT_ORG') CREATE INDEX IDX_T_FA_LEASE_INIT_ORG ON T_FA_LEASE_INIT ( FORGID );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_FA_LEASE_INIT_L')
CREATE TABLE T_FA_LEASE_INIT_L (
                                   FPKID VARCHAR(36) DEFAULT ' ' NOT NULL,
                                   FID BIGINT DEFAULT 0 NOT NULL,
                                   FLOCALEID VARCHAR(10) DEFAULT ' ' NOT NULL,
                                   FNAME NVARCHAR(100) DEFAULT ' ' NOT NULL
);

-- 摊销与计息增加摊销期间
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_LEASE_RENT_SETTLE' AND KSQL_COL_NAME IN ('FAMORTIZATIONPERIODID'))
ALTER TABLE T_FA_LEASE_RENT_SETTLE ADD (FAMORTIZATIONPERIODID BIGINT DEFAULT 0 NOT NULL );

-- 租赁变更单增加摊销日期
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_LEASE_CHANGE_BILL' AND KSQL_COL_NAME IN ('FAMORTIZATIONDATE'))
ALTER TABLE T_FA_LEASE_CHANGE_BILL ADD (FAMORTIZATIONDATE DATETIME );

-- 计息明细分录增加摊销期间、合同版本、最新数据
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_INTEREST_DETAIL_E' AND KSQL_COL_NAME IN ('FAMORTIZATIONPERIODID'))
ALTER TABLE T_FA_INTEREST_DETAIL_E ADD (FAMORTIZATIONPERIODID BIGINT DEFAULT 0 NOT NULL );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_INTEREST_DETAIL_E' AND KSQL_COL_NAME IN ('FCONTRACTVERSION'))
ALTER TABLE T_FA_INTEREST_DETAIL_E ADD (FCONTRACTVERSION NVARCHAR(50) DEFAULT '0' NOT NULL );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_FA_INTEREST_DETAIL_E' AND KSQL_COL_NAME IN ('FLATESTDATA'))
ALTER TABLE T_FA_INTEREST_DETAIL_E ADD (FLATESTDATA CHAR(1) DEFAULT '1' NOT NULL );