 IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICETYPE' AND KSQL_COL_NAME ='FISPROFORMA')
 ALTER TABLE T_ER_INVOICETYPE ADD (FISPROFORMA CHAR(1) DEFAULT '0' NOT NULL );
 IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICETYPE' AND KSQL_COL_NAME ='FUNIQUECONDITION')
  ALTER TABLE T_ER_INVOICETYPE ADD (FUNIQUECONDITION VARCHAR(100) DEFAULT ' ' NOT NULL );


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_BILLINGTYPE_REL')
 CREATE TABLE T_ER_BILLINGTYPE_REL (
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_BILLINGPOOL')
 CREATE TABLE T_ER_BILLINGPOOL (
FID BIGINT DEFAULT 0 NOT NULL,
FBILLNO VARCHAR(80) DEFAULT ' ' NOT NULL,
FBILLSTATUS VARCHAR(30) DEFAULT ' ' NOT NULL,
FORGID BIGINT DEFAULT 0 NOT NULL,
FGATHERBUYER VARCHAR(200),
FAIRCONSTFEE DECIMAL(23,10) DEFAULT 0 NOT NULL,
FBILLTYPE BIGINT DEFAULT 0 NOT NULL,
FSERIALNO VARCHAR(80) DEFAULT ' ' NOT NULL,
FCOMPANYID BIGINT DEFAULT 0 NOT NULL,
FUSAGE NVARCHAR(2000),
FBUYERADDRESSPHONE NVARCHAR(200),
FISGENVOUCHER CHAR(1) DEFAULT '0' NOT NULL,
FORICURRENCY BIGINT DEFAULT 0 NOT NULL,
FRECEIPTDATE DATETIME,
FCHECKRESULT NVARCHAR(2000),
FBASEALLTAXRATE BIGINT DEFAULT 0 NOT NULL,
FVEHICLENUM VARCHAR(50) DEFAULT ' ' NOT NULL,
FBTAXPAYERID VARCHAR(50) DEFAULT ' ' NOT NULL,
FTEL VARCHAR(20) DEFAULT ' ' NOT NULL,
FGATHERTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FISTARTPROVINCE NVARCHAR(20) DEFAULT ' ' NOT NULL,
FBRAND VARCHAR(20) DEFAULT ' ' NOT NULL,
FSELLEROPENBANK NVARCHAR(200),
FCOSTDEPTID BIGINT DEFAULT 0 NOT NULL,
FISSEQUENCE CHAR(1) DEFAULT '0' NOT NULL,
FCUSTOMERIDNUMBER VARCHAR(30) DEFAULT ' ' NOT NULL,
FISMUTILREIMBURSE CHAR(1) DEFAULT '0' NOT NULL,
FEXPENSEITEM BIGINT DEFAULT 0 NOT NULL,
FAPPLIERID BIGINT DEFAULT 0 NOT NULL,
FTOTALAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FIDESTPROVINCE NVARCHAR(20) DEFAULT ' ' NOT NULL,
FREGNUM VARCHAR(50) DEFAULT ' ' NOT NULL,
FORIUSEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FINVOICECODE VARCHAR(129) DEFAULT ' ' NOT NULL,
FINVOICESTATUS VARCHAR(20) DEFAULT ' ' NOT NULL,
FINVOICETYPE BIGINT DEFAULT 0 NOT NULL,
FINVOICEFUELSURCHARGE DECIMAL(23,10) DEFAULT 0 NOT NULL,
FSEATGRADE NVARCHAR(20) DEFAULT ' ' NOT NULL,
FINVOICEDATE DATETIME,
FGATHERTOTALAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FCURRENCY BIGINT DEFAULT 0 NOT NULL,
FTRIPTO NVARCHAR(30) DEFAULT ' ' NOT NULL,
FOUTAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FVEHPLATE VARCHAR(20) DEFAULT ' ' NOT NULL,
FGATHERSELLER VARCHAR(200),
FAPPLIERPOSITIONSTR NVARCHAR(10) DEFAULT ' ' NOT NULL,
FSELLERADDRESSPHONE NVARCHAR(100) DEFAULT ' ' NOT NULL,
FNOTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FBILLPOOLTYPE BIGINT DEFAULT 0 NOT NULL,
FOFFSET CHAR(1) DEFAULT '0' NOT NULL,
FVALIDATEORG BIGINT DEFAULT 0 NOT NULL,
FSTAXPAYERID VARCHAR(50) DEFAULT ' ' NOT NULL,
FINSURANCEPREMIUM DECIMAL(23,10) DEFAULT 0 NOT NULL,
FSELLER BIGINT DEFAULT 0 NOT NULL,
FCOUNTRY BIGINT DEFAULT 0 NOT NULL,
FTRIPFROM NVARCHAR(30) DEFAULT ' ' NOT NULL,
FPROXYMARK CHAR(1) DEFAULT '0' NOT NULL,
FTIMEGETON DATETIME,
FCHECKSTATUS CHAR(1) DEFAULT ' ' NOT NULL,
FBUYEROPENBANK VARCHAR(100) DEFAULT ' ' NOT NULL,
FOFFSETAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FOTHERTOTALTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FCOSTCOMPANYID BIGINT DEFAULT 0 NOT NULL,
FBUYERBANKACCOUNT VARCHAR(100) DEFAULT ' ' NOT NULL,
FIMAGENO VARCHAR(50) DEFAULT ' ' NOT NULL,
FOUTREASON NVARCHAR(2000),
FGATHER BIGINT DEFAULT 0 NOT NULL,
FGATHERDATE DATETIME,
FORIBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FPASSENGERNAME VARCHAR(200),
FNAME NVARCHAR(2000) DEFAULT ' ' NOT NULL,
FBUYERORG BIGINT DEFAULT 0 NOT NULL,
FREGION NVARCHAR(100) DEFAULT ' ' NOT NULL,
FCITY NVARCHAR(100) DEFAULT ' ' NOT NULL,
FINVOICENO VARCHAR(129) DEFAULT ' ' NOT NULL,
FOFFSETEXPENSE VARCHAR(8) DEFAULT ' ' NOT NULL,
FEXPIRYPAYDATE DATETIME,
FSOURCE VARCHAR(16) DEFAULT ' ' NOT NULL,
FTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FRELBILLTYPE NVARCHAR(200) DEFAULT ' ' NOT NULL
);


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_BILLINGPOOL_E')
 CREATE TABLE T_ER_BILLINGPOOL_E (
FID BIGINT DEFAULT 0 NOT NULL,
FISTARTCITY VARCHAR(20) DEFAULT 0 NOT NULL,
FVEHICLETYPE VARCHAR(50) DEFAULT ' ' NOT NULL,
FSHOWVEHICLE CHAR(1) DEFAULT '0' NOT NULL,
FENGINENUM VARCHAR(20) DEFAULT ' ' NOT NULL,
FINVOICEENDDATE DATETIME,
FVOUCHERNO VARCHAR(200),
FBLOCKCHAIN CHAR(1) DEFAULT ' ' NOT NULL,
FATTACHMENTCOUNT INT DEFAULT 0 NOT NULL,
FVINCODE VARCHAR(50) DEFAULT ' ' NOT NULL,
FPROJECTTYPE BIGINT DEFAULT 0 NOT NULL,
FISRED VARCHAR(8) DEFAULT ' ' NOT NULL,
FCREATORID BIGINT DEFAULT 0 NOT NULL,
FMODIFIERID BIGINT DEFAULT 0 NOT NULL,
FAUDITORID BIGINT DEFAULT 0 NOT NULL,
FIDESTCITY VARCHAR(20) DEFAULT 0 NOT NULL,
FRELBILL VARCHAR(200),
FFORMID VARCHAR(20) DEFAULT ' ' NOT NULL,
FENDORSEMENT VARCHAR(255),
FSHOWDETAIL CHAR(1) DEFAULT '0' NOT NULL,
FINVOICEAIRTIME VARCHAR(16) DEFAULT ' ' NOT NULL,
FINVOICESTARTDATE DATETIME,
FTIMEGETOFF DATETIME,
FCREATETIME DATETIME,
FMODIFYTIME DATETIME,
FAUDITDATE DATETIME,
FREMARK NVARCHAR(2000),
FUNAUDITMSG NVARCHAR(2000),
FINVADDR VARCHAR(1000),
FTAXAUTHORITY NVARCHAR(30) DEFAULT ' ' NOT NULL,
FSHOWTRIP CHAR(1) DEFAULT '0' NOT NULL,
FVEHICLEREMARK NVARCHAR(2000) DEFAULT ' ' NOT NULL,
FISXBRL CHAR(1) DEFAULT '0' NOT NULL,
FALLTAXRATE VARCHAR(20) DEFAULT ' ' NOT NULL,
FMODIFIED CHAR(1) DEFAULT '0' NOT NULL,
FISEXPENSESYNC CHAR(1) DEFAULT '0' NOT NULL
);


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_BILLINGPOOL_DETAIL')
 CREATE TABLE T_ER_BILLINGPOOL_DETAIL (
FID BIGINT DEFAULT 0 NOT NULL,
FENTRYID BIGINT DEFAULT 0 NOT NULL,
FSEQ INT DEFAULT 0 NOT NULL,
FENTRYORIUSEDAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FENTRYREMARK NVARCHAR(2000),
FENTRYORIBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FENTRYTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FTAXCLASSCODE VARCHAR(50) DEFAULT ' ' NOT NULL,
FSPECMODEL VARCHAR(50) DEFAULT ' ' NOT NULL,
FNUM BIGINT DEFAULT 0 NOT NULL,
FPRICE DECIMAL(23,10) DEFAULT 0 NOT NULL,
FENTRYNOTAXAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL,
FUNIT BIGINT DEFAULT 0 NOT NULL,
FENTRYTAXRATE VARCHAR(10) DEFAULT 0 NOT NULL,
FTAXRATE BIGINT DEFAULT 0 NOT NULL,
FGOODSNAME NVARCHAR(50) DEFAULT ' ' NOT NULL,
FENTRYAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL
);


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_BILLINGPOOL_VOUCHER')
 CREATE TABLE T_ER_BILLINGPOOL_VOUCHER (
FID BIGINT DEFAULT 0 NOT NULL,
FENTRYID BIGINT DEFAULT 0 NOT NULL,
FSEQ INT DEFAULT 0 NOT NULL,
FVOUCHERTYPE VARCHAR(8) DEFAULT ' ' NOT NULL,
FPERIOD DATETIME,
FVOUCHERID BIGINT DEFAULT 0 NOT NULL,
FENTITYID VARCHAR(50) DEFAULT ' ' NOT NULL,
FRELBILLID BIGINT DEFAULT 0 NOT NULL,
FVOUCHERCREATOR VARCHAR(200),
FVOUCHERNUM VARCHAR(200),
FRELBILLS VARCHAR(200),
FRELENTRYID BIGINT DEFAULT 0 NOT NULL
);



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


 EXEC P_ALTERPK 'PK_T_ER_BILLINGTYPE_REL', 'T_ER_BILLINGTYPE_REL', 'FPKID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_BILLING_REL') CREATE INDEX IDX_ER_BILLING_REL ON T_ER_BILLINGTYPE_REL ( FBASEDATAID );



 EXEC P_ALTERPK 'PK_T_ER_BILLINGPOOL', 'T_ER_BILLINGPOOL', 'FID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_BILLINGPOOL_INVOICE') CREATE INDEX IDX_ER_BILLINGPOOL_INVOICE ON T_ER_BILLINGPOOL ( FINVOICECODE ,FINVOICENO );


 EXEC P_ALTERPK 'PK_T_ER_BILLINGPOOL_E', 'T_ER_BILLINGPOOL_E', 'FID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_BILLINGPOOL_AUDITER') CREATE INDEX IDX_ER_BILLINGPOOL_AUDITER ON T_ER_BILLINGPOOL_E ( FAUDITORID );


 EXEC P_ALTERPK 'PK_T_ER_BILLINGPOOL_DETAIL', 'T_ER_BILLINGPOOL_DETAIL', 'FENTRYID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_BILLINGPOOL_BALANCEA') CREATE INDEX IDX_ER_BILLINGPOOL_BALANCEA ON T_ER_BILLINGPOOL_DETAIL ( FENTRYORIBALANCEAMOUNT );


 EXEC P_ALTERPK 'PK_T_ER_BILLINGPOOL_VOUCHER', 'T_ER_BILLINGPOOL_VOUCHER', 'FENTRYID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_BILLINGPOOL_VOUCHER_BILLNO') CREATE INDEX IDX_BILLINGPOOL_VOUCHER_BILLNO ON T_ER_BILLINGPOOL_VOUCHER ( FRELBILLS );


 EXEC P_ALTERPK 'PK_T_ER_BILLINGPOOL_L', 'T_ER_BILLINGPOOL_L', 'FPKID', '1';

 IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_BILLINGPOOL_L_FID') CREATE INDEX IDX_BILLINGPOOL_L_FID ON T_ER_BILLINGPOOL_L ( FID );
