CREATE TABLE kmmAccounts (
id varchar(32) NOT NULL,
institutionId varchar(32),
parentId varchar(32),
lastReconciled datetime,
lastModified datetime,
openingDate date,
accountNumber mediumtext,
accountType varchar(16) NOT NULL,
accountTypeString mediumtext,
isStockAccount char(1),
accountName mediumtext,
description mediumtext,
currencyId varchar(32),
balance mediumtext,
balanceFormatted mediumtext,
transactionCount bigint unsigned,
PRIMARY KEY (id)
);

CREATE TABLE kmmAccountsPayeeIdentifier (
accountId varchar(32) NOT NULL,
userOrder smallint unsigned NOT NULL,
identifierId varchar(32) NOT NULL,
PRIMARY KEY (accountId,userOrder)
);

CREATE TABLE kmmBudgetConfig (
id varchar(32) NOT NULL,
name text NOT NULL,
start date NOT NULL,
XML longtext,
PRIMARY KEY (id)
);

CREATE TABLE kmmCostCenter (
id varchar(32) NOT NULL,
name text NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE kmmCurrencies (
ISOcode char(3) NOT NULL,
name text NOT NULL,
type smallint unsigned,
typeString mediumtext,
symbol1 smallint unsigned,
symbol2 smallint unsigned,
symbol3 smallint unsigned,
symbolString varchar(255),
smallestCashFraction varchar(24),
smallestAccountFraction varchar(24),
pricePrecision smallint unsigned NOT NULL DEFAULT 4,
PRIMARY KEY (ISOcode)
);

CREATE TABLE kmmFileInfo (
version varchar(16),
created date,
lastModified date,
baseCurrency char(3),
institutions bigint unsigned,
accounts bigint unsigned,
payees bigint unsigned,
tags bigint unsigned,
transactions bigint unsigned,
splits bigint unsigned,
securities bigint unsigned,
prices bigint unsigned,
currencies bigint unsigned,
schedules bigint unsigned,
reports bigint unsigned,
kvps bigint unsigned,
dateRangeStart date,
dateRangeEnd date,
hiInstitutionId bigint unsigned,
hiPayeeId bigint unsigned,
hiTagId bigint unsigned,
hiAccountId bigint unsigned,
hiTransactionId bigint unsigned,
hiScheduleId bigint unsigned,
hiSecurityId bigint unsigned,
hiReportId bigint unsigned,
encryptData varchar(255),
updateInProgress char(1),
budgets bigint unsigned,
hiBudgetId bigint unsigned,
hiOnlineJobId bigint unsigned,
hiPayeeIdentifierId bigint unsigned,
logonUser varchar(255),
logonAt datetime,
fixLevel int unsigned
);

CREATE TABLE kmmInstitutions (
id varchar(32) NOT NULL,
name text NOT NULL,
manager mediumtext,
routingCode mediumtext,
addressStreet mediumtext,
addressCity mediumtext,
addressZipcode mediumtext,
telephone mediumtext,
PRIMARY KEY (id)
);

CREATE TABLE kmmKeyValuePairs (
kvpType varchar(16) NOT NULL,
kvpId varchar(32),
kvpKey varchar(255) NOT NULL,
kvpData mediumtext
);
CREATE INDEX kmmKeyValuePairs_type_id_idx ON kmmKeyValuePairs (kvpType,kvpId);

CREATE TABLE kmmOnlineJobs (
id varchar(32) NOT NULL,
type varchar(255) NOT NULL,
jobSend datetime,
bankAnswerDate datetime,
state varchar(15) NOT NULL,
locked char(1) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE kmmPayeeIdentifier (
id varchar(32) NOT NULL,
type varchar(255),
PRIMARY KEY (id)
);

CREATE TABLE kmmPayees (
id varchar(32) NOT NULL,
name mediumtext,
reference mediumtext,
email mediumtext,
addressStreet mediumtext,
addressCity mediumtext,
addressZipcode mediumtext,
addressState mediumtext,
telephone mediumtext,
notes longtext,
defaultAccountId varchar(32),
matchData tinyint unsigned,
matchIgnoreCase char(1),
matchKeys mediumtext,
idPattern mediumtext,
urlTemplate mediumtext,
PRIMARY KEY (id)
);

CREATE TABLE kmmPayeesPayeeIdentifier (
payeeId varchar(32) NOT NULL,
userOrder smallint unsigned NOT NULL,
identifierId varchar(32) NOT NULL,
PRIMARY KEY (payeeId,userOrder)
);

CREATE TABLE kmmPluginInfo (
iid varchar(255) NOT NULL,
versionMajor tinyint unsigned NOT NULL,
versionMinor tinyint unsigned,
uninstallQuery longtext,
PRIMARY KEY (iid)
);

CREATE TABLE kmmPrices (
fromId varchar(32) NOT NULL,
toId varchar(32) NOT NULL,
priceDate date NOT NULL,
price text NOT NULL,
priceFormatted mediumtext,
priceSource mediumtext,
PRIMARY KEY (fromId,toId,priceDate)
);

CREATE TABLE kmmReportConfig (name varchar(255) NOT NULL,
XML longtext,
id varchar(32) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE kmmSchedulePaymentHistory (schedId varchar(32) NOT NULL,
payDate date NOT NULL,
PRIMARY KEY (schedId,payDate)
);

CREATE TABLE kmmSchedules (id varchar(32) NOT NULL,
name text NOT NULL,
type tinyint unsigned NOT NULL,
typeString mediumtext,
occurence smallint unsigned NOT NULL,
occurenceMultiplier smallint unsigned NOT NULL,
occurenceString mediumtext,
paymentType tinyint unsigned,
paymentTypeString longtext,
startDate date NOT NULL,
endDate date,
fixed char(1) NOT NULL,
lastDayInMonth char(1) NOT NULL DEFAULT 'N',
autoEnter char(1) NOT NULL,
lastPayment date,
nextPaymentDue date,
weekendOption tinyint unsigned NOT NULL,
weekendOptionString mediumtext,
PRIMARY KEY (id)
);

CREATE TABLE kmmSecurities (id varchar(32) NOT NULL,
name text NOT NULL,
symbol mediumtext,
type smallint unsigned NOT NULL,
typeString mediumtext,
smallestAccountFraction varchar(24),
pricePrecision smallint unsigned NOT NULL DEFAULT 4,
tradingMarket mediumtext,
tradingCurrency char(3),
roundingMethod smallint unsigned NOT NULL DEFAULT 7,
PRIMARY KEY (id)
);

CREATE TABLE kmmSplits (
transactionId varchar(32) NOT NULL,
txType char(1),
splitId smallint unsigned NOT NULL,
payeeId varchar(32),
reconcileDate datetime,
action varchar(16),
reconcileFlag char(1),
value text NOT NULL,
valueFormatted text,
shares text NOT NULL,
sharesFormatted mediumtext,
price text,
priceFormatted mediumtext,
memo mediumtext,
accountId varchar(32) NOT NULL,
costCenterId varchar(32),
checkNumber varchar(32),
postDate datetime,
bankId mediumtext,
PRIMARY KEY (transactionId, splitId)
);
CREATE INDEX kmmSplits_kmmSplitsaccount_type_idx ON kmmSplits (accountId,txType);

CREATE TABLE kmmTagSplits (transactionId varchar(32) NOT NULL,
tagId varchar(32) NOT NULL,
splitId smallint unsigned NOT NULL,
PRIMARY KEY (transactionId,tagId,splitId)
);

CREATE TABLE kmmTags (id varchar(32) NOT NULL,
name mediumtext,
closed char(1),
notes longtext,
tagColor mediumtext,
PRIMARY KEY (id)
);

CREATE TABLE kmmTransactions (id varchar(32) NOT NULL,
txType char(1),
postDate datetime,
memo mediumtext,
entryDate datetime,
currencyId char(3),
bankId mediumtext,
PRIMARY KEY (id)
);
