IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_LOANBILLDS') 
 CREATE TABLE T_ER_LOANBILLDS (
FID BIGINT DEFAULT 0 NOT NULL,
FBILLNO VARCHAR(80) DEFAULT ' ' NOT NULL,
FBILLSTATUS VARCHAR(1) DEFAULT ' ' NOT NULL,
FCREATORID BIGINT DEFAULT 1 NOT NULL,
FCREATETIME DATETIME,
FMODIFIERID BIGINT DEFAULT 1 NOT NULL,
FMODIFYTIME DATETIME,
FAUDITORID BIGINT DEFAULT 0 NOT NULL,
FAUDITDATE DATETIME,
FCOMPANYID BIGINT DEFAULT 0 NOT NULL,
FBIZDATE DATETIME,
FISCURRENCY CHAR(1) DEFAULT '0' NOT NULL,
FDESCRIPTION NVARCHAR(1000) DEFAULT ' ' NOT NULL,
FCOSTCOMPANYID BIGINT DEFAULT 0 NOT NULL,
FCOSTDEPTID BIGINT DEFAULT 0 NOT NULL,
FAPPLIERID BIGINT DEFAULT 0 NOT NULL,
FORGID BIGINT DEFAULT 0 NOT NULL,
FCURRENCYID BIGINT DEFAULT 0 NOT NULL,
FFORMID VARCHAR(30) DEFAULT ' ' NOT NULL,
FHASVOUCHER CHAR(1) DEFAULT '0' NOT NULL,
FHEAD_PAYDATE DATETIME,
FISOVERBUDGET CHAR(1) DEFAULT '0' NOT NULL,
FREPAYMENTDATE DATETIME,
FAPPROVEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FUSEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FNOTPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FLOANAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FAPPLIEDREIMBURSEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FIMAGENUMBER VARCHAR(80) DEFAULT ' ' NOT NULL,
FISIMPORT CHAR(1) DEFAULT '0' NOT NULL,
FRETURNEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FDETAILTYPE VARCHAR(50) DEFAULT ' ' NOT NULL,
FSTD_COSTCENTER BIGINT DEFAULT 0 NOT NULL,
FBILLPAYERTYPE VARCHAR(30) DEFAULT ' ' NOT NULL,
FISURGENT CHAR(1) DEFAULT '0' NOT NULL,
FBILLPAYERID BIGINT DEFAULT 0 NOT NULL,
FUNAUDITMSG NVARCHAR(1000) DEFAULT ' ' NOT NULL,
FBOOKEDDATE DATETIME,
FTRIPTYPEID BIGINT DEFAULT 0 NOT NULL,
FNEXTAUDITOR NVARCHAR(100) DEFAULT ' ' NOT NULL,
FISADVANCE CHAR(1) DEFAULT '0' NOT NULL,
FPREPAYTYPE VARCHAR(50) DEFAULT ' ' NOT NULL
);

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

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_LOANBILLDSENTRY') 
 CREATE TABLE T_ER_LOANBILLDSENTRY (
FID BIGINT DEFAULT 0 NOT NULL,
FENTRYID BIGINT DEFAULT 0 NOT NULL,
FSEQ INT DEFAULT 0 NOT NULL,
FEXPENSEITEMID BIGINT DEFAULT 0 NOT NULL,
FHAPPENDATE DATETIME,
FEXCHANGERATE DECIMAL(23,10) DEFAULT 0 NOT NULL,
FENTRYCURRENCY BIGINT DEFAULT 0 NOT NULL,
FLOANAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FCURRLOANAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEAPPROVEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEAPPROVECURRAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FENTRYCOSTCOMPANYID BIGINT DEFAULT 0 NOT NULL,
FENTRYCOSTDEPTID BIGINT DEFAULT 0 NOT NULL,
FPROJECTID BIGINT DEFAULT 0 NOT NULL,
FREMARK NVARCHAR(255) DEFAULT ' ' NOT NULL,
FORGIEXPEBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPORIUSEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPUSEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEORIREPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEREPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEORIHASREIMAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FEXPEHASREIMAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FSOURCEBILLID BIGINT DEFAULT 0 NOT NULL,
FSOURCEENTRYID BIGINT DEFAULT 0 NOT NULL,
FENTRYPROJECTNO VARCHAR(30) DEFAULT ' ' NOT NULL,
FENTRYCONTRACTNO VARCHAR(30) DEFAULT ' ' NOT NULL,
FENTRYCONTRACTNAME VARCHAR(200) DEFAULT ' ',
FSTD_ENTRYCOSTCENTER BIGINT DEFAULT 0 NOT NULL,
FSOURCEBILLTYPE VARCHAR(30) DEFAULT ' ' NOT NULL,
FQUOTETYPE CHAR(1) DEFAULT '0' NOT NULL,
FSTARTDATE DATETIME,
FENDDATE DATETIME ,
FFROMPLACEID BIGINT DEFAULT 0 NOT NULL,
FTOPLACEID BIGINT DEFAULT 0 NOT NULL,
FVEHICLE CHAR(5) DEFAULT ' ' NOT NULL,
FTRIPDAY INT DEFAULT 0 NOT NULL
);

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_LOANBILLDSRECENTRY') 
 CREATE TABLE T_ER_LOANBILLDSRECENTRY (
FID BIGINT DEFAULT 0 NOT NULL,
FENTRYID BIGINT DEFAULT 0 NOT NULL,
FSEQ INT DEFAULT 0 NOT NULL,
FPAYMODEID BIGINT DEFAULT 0 NOT NULL,
FPAYERNAME NVARCHAR(100) DEFAULT ' ' NOT NULL,
FPAYERID BIGINT DEFAULT 0 NOT NULL,
FPAYERBANKID BIGINT DEFAULT 0 NOT NULL,
FPAYERACCOUNT02 VARCHAR(100) DEFAULT ' ' NOT NULL,
FPAYERACCOUNT VARCHAR(100) DEFAULT ' ' NOT NULL,
FACCOUNTCURRENCY BIGINT DEFAULT 0 NOT NULL,
FEXCHANGERATE DECIMAL(23,10) DEFAULT 0 NOT NULL,
FORIAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FPAYERACCOUNT01 VARCHAR(100) DEFAULT ' ' NOT NULL,
FBANKLOGO VARCHAR(50) DEFAULT ' ' NOT NULL,
FENTRYREPAYDATE DATETIME,
FACCBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FORIACCBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FORIACCNOTPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FACCNOTPAYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FBUILDEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FORIACCPAYEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FACCPAYEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FPAYERDEPTID BIGINT DEFAULT 0 NOT NULL,
FPAYERCOMPID BIGINT DEFAULT 0 NOT NULL,
FSUPPLIER BIGINT DEFAULT 0 NOT NULL,
FCUSTOMER BIGINT DEFAULT 0 NOT NULL,
FENTRYPAYERTYPE VARCHAR(30) DEFAULT ' ' NOT NULL,
FPAYERACCOUNTNAME VARCHAR(100) DEFAULT ' ' NOT NULL,
FCASORG BIGINT DEFAULT 0 NOT NULL,
FQUOTETYPE CHAR(1) DEFAULT '0' NOT NULL
);

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_LOADBILLDSOWER') 
 CREATE TABLE T_ER_LOADBILLDSOWER (
FPKID BIGINT DEFAULT 0 NOT NULL,
FID BIGINT DEFAULT 0 NOT NULL,
FBASEDATAID BIGINT DEFAULT 0 NOT NULL
);


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_LOANBILLDSPARTNER') 
 CREATE TABLE T_ER_LOANBILLDSPARTNER (
FPKID BIGINT DEFAULT 0 NOT NULL,
FID BIGINT DEFAULT 0 NOT NULL,
FBASEDATAID BIGINT DEFAULT 0 NOT NULL,
FENTRYID BIGINT DEFAULT 0 NOT NULL
);

 EXEC P_ALTERPK 'PK_T_ER_LOANBILLDS', 'T_ER_LOANBILLDS', 'FID', '1';
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDS_FAPPLYERID') CREATE INDEX IDX_ER_LOANBILLDS_FAPPLYERID ON T_ER_LOANBILLDS ( FAPPLIERID );
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDS_FCOMPANYID') CREATE INDEX IDX_ER_LOANBILLDS_FCOMPANYID ON T_ER_LOANBILLDS ( FCOMPANYID );
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDS_FSTATUS') CREATE INDEX IDX_ER_LOANBILLDS_FSTATUS ON T_ER_LOANBILLDS ( FBILLSTATUS );
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDS_FDATE_NO') CREATE INDEX IDX_ER_LOANBILLDS_FDATE_NO ON T_ER_LOANBILLDS ( FBILLNO ,FBIZDATE );


 EXEC P_ALTERPK 'PK_T_ER_LOANBILLDSENTRY', 'T_ER_LOANBILLDSENTRY', 'FENTRYID', '1';
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDSENTRY_FSEQ') CREATE INDEX IDX_ER_LOANBILLDSENTRY_FSEQ ON T_ER_LOANBILLDSENTRY ( FID ,FSEQ );


 EXEC P_ALTERPK 'PK_T_ER_LOANBILLDSRECENTRY', 'T_ER_LOANBILLDSRECENTRY', 'FENTRYID', '1';
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOABBILLDSRECENTRY_FSEQ') CREATE INDEX IDX_ER_LOABBILLDSRECENTRY_FSEQ ON T_ER_LOANBILLDSRECENTRY ( FID ,FSEQ );


 EXEC P_ALTERPK 'PK_T_ER_LOADBILLDSOWER', 'T_ER_LOADBILLDSOWER', 'FPKID', '1';
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDSID') CREATE INDEX IDX_ER_LOANBILLDSID ON T_ER_LOADBILLDSOWER ( FID );
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDSUSERID') CREATE INDEX IDX_ER_LOANBILLDSUSERID ON T_ER_LOADBILLDSOWER ( FBASEDATAID );


 EXEC P_ALTERPK 'PK_T_ER_LOANBILLDSPARTNER', 'T_ER_LOANBILLDSPARTNER', 'FPKID', '1';
 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_LOANBILLDS_FENTRYID') CREATE INDEX IDX_ER_LOANBILLDS_FENTRYID ON T_ER_LOANBILLDSPARTNER ( FENTRYID );

