Add v6 database schema DDL (sections 2-3)

Two schema files covering all v6 tables:
- 001-tables.sql: Organizations (denormalized OOD), Users, Roles/RBAC,
  FinancialAccounts (with OwnerUserId, native currency, bitcoin fields),
  FinancialTransactions (with OrganizationSequenceId, BlockchainHash),
  FinancialTransactionRows (with foreign currency, bitcoin derivation),
  Dependencies, Tags/TagSets/TagTypes, Budgets
- 002-tables-apar.sql: ExpenseClaims (with VatCents, groups),
  InboundInvoices (with native currency, bitcoin satoshis),
  OutboundInvoices + Items (with credit links, VAT),
  Payouts, PaymentGroups, Payments, Refunds, Documents,
  FinancialAccountDocuments, Currencies, ExchangeRateSnapshots +
  Datapoints (micro-BTC scaling), CryptoChains, HotWalletAddresses,
  HotWalletUtxos, MailQueue, MailTemplates, EventQueue, SystemSettings

Key v5→v6 differences:
- ObjectOptionalData EAV denormalized to first-class columns
- FinancialTransactionRowsNativeCurrency merged into Rows table
- People → Users (simplified, no geography/membership)
- utf8mb4 throughout, no legacy latin1
- Amount (double) columns removed; AmountCents (bigint) only
- Budget tables unified (annual + monthly in one table)
- v5 FlatData → SystemSettings

Also updates data-migration spec for mysqldump-based workflow.

Made-with: Cursor
This commit is contained in:
2026-04-08 16:25:58 +02:00
parent 8aafe681b1
commit 0514b9bd18
3 changed files with 635 additions and 17 deletions
+17 -17
View File
@@ -10,26 +10,26 @@
## 2. Database Schema — Core Tables
- [ ] 2.1 Design and write DDL for Organizations table (denormalized from ObjectOptionalData: Currency, FirstFiscalYear, ClosedThroughYear, VatEnabled, DefaultVatRatePercent, VatReportFrequencyMonths, EconomyEnabled, GovtRegistrationId, TaxAuthority, TaxAccount, TaxOcr, FinancialMailName, FinancialMailAddress, LogoLandscape, LogoSquare)
- [ ] 2.2 Design and write DDL for Users table (Name, Email, PasswordHash, Active, PreferredCulture, BankName, BankClearing, BankAccount, BitcoinPayoutAddress, NeverMail, ResetPasswordTicket)
- [ ] 2.3 Design and write DDL for Roles, UserRoles, RolePermissions tables
- [ ] 2.4 Design and write DDL for FinancialAccounts table (including OwnerUserId, NativeCurrencyId, BitcoinPublicAddress, BitcoinPrivateRoot, BitcoinPublicRoot, BankTransactionTag, EnabledForExpensing, EnabledForInvoicing, AutomationProfileId, OpenedYear, ClosedYear)
- [ ] 2.5 Design and write DDL for FinancialTransactions table (including OrganizationSequenceId, BlockchainHash, ImportSha256)
- [ ] 2.6 Design and write DDL for FinancialTransactionRows table (including ForeignCurrencyCode, ForeignCurrencyAmountCents, BitcoinDerivationPath)
- [ ] 2.7 Design and write DDL for FinancialTransactionDependencies table (for continuation chains and business object links)
- [ ] 2.8 Design and write DDL for FinancialTransactionTags, TagSets, TagTypes tables
- [ ] 2.9 Design and write DDL for FinancialAccountBudgets table (monthly/yearly)
- [x] 2.1 Design and write DDL for Organizations table (denormalized from ObjectOptionalData: Currency, FirstFiscalYear, ClosedThroughYear, VatEnabled, DefaultVatRatePercent, VatReportFrequencyMonths, EconomyEnabled, GovtRegistrationId, TaxAuthority, TaxAccount, TaxOcr, FinancialMailName, FinancialMailAddress, LogoLandscape, LogoSquare)
- [x] 2.2 Design and write DDL for Users table (Name, Email, PasswordHash, Active, PreferredCulture, BankName, BankClearing, BankAccount, BitcoinPayoutAddress, NeverMail, ResetPasswordTicket)
- [x] 2.3 Design and write DDL for Roles, UserRoles, RolePermissions tables
- [x] 2.4 Design and write DDL for FinancialAccounts table (including OwnerUserId, NativeCurrencyId, BitcoinPublicAddress, BitcoinPrivateRoot, BitcoinPublicRoot, BankTransactionTag, EnabledForExpensing, EnabledForInvoicing, AutomationProfileId, OpenedYear, ClosedYear)
- [x] 2.5 Design and write DDL for FinancialTransactions table (including OrganizationSequenceId, BlockchainHash, ImportSha256)
- [x] 2.6 Design and write DDL for FinancialTransactionRows table (including ForeignCurrencyCode, ForeignCurrencyAmountCents, BitcoinDerivationPath)
- [x] 2.7 Design and write DDL for FinancialTransactionDependencies table (for continuation chains and business object links)
- [x] 2.8 Design and write DDL for FinancialTransactionTags, TagSets, TagTypes tables
- [x] 2.9 Design and write DDL for FinancialAccountBudgets table (monthly/yearly)
## 3. Database Schema — AP/AR & Support Tables
- [ ] 3.1 Design and write DDL for ExpenseClaims table (including VatCents, status lifecycle columns)
- [ ] 3.2 Design and write DDL for InboundInvoices table (including Description, VatCents, ExactBitcoinAmountSatoshis, NativeCurrencyCode, NativeCurrencyAmountCents)
- [ ] 3.3 Design and write DDL for OutboundInvoices and OutboundInvoiceItems tables (including VatCents, CreditedWithInvoice, CreditsInvoice, ToUserId)
- [ ] 3.4 Design and write DDL for Payouts, PayoutItems, Payments, PaymentGroups, Refunds tables
- [ ] 3.5 Design and write DDL for Documents and FinancialAccountDocuments tables
- [ ] 3.6 Design and write DDL for Currencies, ExchangeRateSnapshots, ExchangeRateDatapoints tables
- [ ] 3.7 Design and write DDL for CryptoChains, HotWalletAddresses, HotWalletUtxos tables
- [ ] 3.8 Design and write DDL for MailQueue, MailTemplates, EventQueue, SystemSettings tables
- [x] 3.1 Design and write DDL for ExpenseClaims table (including VatCents, status lifecycle columns)
- [x] 3.2 Design and write DDL for InboundInvoices table (including Description, VatCents, ExactBitcoinAmountSatoshis, NativeCurrencyCode, NativeCurrencyAmountCents)
- [x] 3.3 Design and write DDL for OutboundInvoices and OutboundInvoiceItems tables (including VatCents, CreditedWithInvoice, CreditsInvoice, ToUserId)
- [x] 3.4 Design and write DDL for Payouts, PayoutItems, Payments, PaymentGroups, Refunds tables
- [x] 3.5 Design and write DDL for Documents and FinancialAccountDocuments tables
- [x] 3.6 Design and write DDL for Currencies, ExchangeRateSnapshots, ExchangeRateDatapoints tables
- [x] 3.7 Design and write DDL for CryptoChains, HotWalletAddresses, HotWalletUtxos tables
- [x] 3.8 Design and write DDL for MailQueue, MailTemplates, EventQueue, SystemSettings tables
## 4. Database Schema — Stored Procedures & Security
@@ -0,0 +1,253 @@
-- UsingCash v6 Schema: Core Tables
-- Designed from v5 Swarmops schema with denormalized ObjectOptionalData,
-- removed governance/geography/membership, and clean naming.
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;
-- ============================================================
-- ORGANIZATIONS
-- ============================================================
CREATE TABLE `Organizations` (
`OrganizationId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(128) NOT NULL,
`NameShort` VARCHAR(64) NOT NULL DEFAULT '',
`NameInternational` VARCHAR(128) NOT NULL DEFAULT '',
`Domain` VARCHAR(128) NOT NULL DEFAULT '',
`MailPrefix` VARCHAR(64) NOT NULL DEFAULT '',
`Active` TINYINT(1) NOT NULL DEFAULT 1,
-- Denormalized from v5 ObjectOptionalData
`CurrencyId` INT UNSIGNED NOT NULL DEFAULT 0,
`DefaultVatRatePercent` INT NOT NULL DEFAULT 0,
`FirstFiscalYear` INT NOT NULL DEFAULT 0,
`ClosedThroughYear` INT NOT NULL DEFAULT 0,
`VatEnabled` TINYINT(1) NOT NULL DEFAULT 0,
`VatReportFrequencyMonths` INT NOT NULL DEFAULT 3,
`EconomyEnabled` TINYINT(1) NOT NULL DEFAULT 0,
`GovtRegistrationId` VARCHAR(64) NOT NULL DEFAULT '',
`TaxAuthority` VARCHAR(128) NOT NULL DEFAULT '',
`TaxAccount` VARCHAR(64) NOT NULL DEFAULT '',
`TaxOcr` VARCHAR(64) NOT NULL DEFAULT '',
`FinancialMailName` VARCHAR(128) NOT NULL DEFAULT '',
`FinancialMailAddress` VARCHAR(128) NOT NULL DEFAULT '',
`LogoDocumentId` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`OrganizationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- USERS (replaces v5 People + ObjectOptionalData)
-- ============================================================
CREATE TABLE `Users` (
`UserId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(128) NOT NULL,
`Email` VARCHAR(128) NOT NULL,
`PasswordHash` VARCHAR(256) NOT NULL DEFAULT '',
`Active` TINYINT(1) NOT NULL DEFAULT 1,
`RequirePasswordReset` TINYINT(1) NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- Denormalized from v5 ObjectOptionalData
`PreferredCulture` VARCHAR(16) NOT NULL DEFAULT 'en',
`BankName` VARCHAR(128) NOT NULL DEFAULT '',
`BankClearing` VARCHAR(32) NOT NULL DEFAULT '',
`BankAccount` VARCHAR(64) NOT NULL DEFAULT '',
`BitcoinPayoutAddress` VARCHAR(128) NOT NULL DEFAULT '',
`NeverMail` TINYINT(1) NOT NULL DEFAULT 0,
`ResetPasswordTicket` VARCHAR(256) NOT NULL DEFAULT '',
`LastLoginOrganizationId` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`UserId`),
UNIQUE KEY `UX_Email` (`Email`),
KEY `IX_Active` (`Active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- RBAC
-- ============================================================
CREATE TABLE `Roles` (
`RoleId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(64) NOT NULL,
`Description` VARCHAR(256) NOT NULL DEFAULT '',
`IsSystemRole` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`RoleId`),
UNIQUE KEY `UX_Name` (`Name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `UserRoles` (
`UserRoleId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`UserId` INT UNSIGNED NOT NULL,
`RoleId` INT UNSIGNED NOT NULL,
`OrganizationId` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '0 = instance-level',
`GrantedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`UserRoleId`),
UNIQUE KEY `UX_UserRoleOrg` (`UserId`, `RoleId`, `OrganizationId`),
KEY `IX_UserId` (`UserId`),
KEY `IX_RoleId` (`RoleId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `RolePermissions` (
`RolePermissionId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`RoleId` INT UNSIGNED NOT NULL,
`PermissionId` INT NOT NULL COMMENT 'Maps to UsingCash.Common.Enums.Permission',
PRIMARY KEY (`RolePermissionId`),
UNIQUE KEY `UX_RolePerm` (`RoleId`, `PermissionId`),
KEY `IX_RoleId` (`RoleId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- FINANCIAL ACCOUNTS
-- ============================================================
CREATE TABLE `FinancialAccounts` (
`FinancialAccountId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`Name` VARCHAR(128) NOT NULL,
`AccountType` INT NOT NULL COMMENT 'UsingCash.Common.Enums.FinancialAccountType',
`ParentFinancialAccountId` INT UNSIGNED NOT NULL DEFAULT 0,
`OwnerUserId` INT UNSIGNED NOT NULL DEFAULT 0,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`OpenedYear` INT NOT NULL DEFAULT 1,
`ClosedYear` INT NOT NULL DEFAULT 9999,
`Expensable` TINYINT(1) NOT NULL DEFAULT 1,
`Administrative` TINYINT(1) NOT NULL DEFAULT 0,
-- Denormalized from v5 ObjectOptionalData
`NativeCurrencyId` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '0 = org default currency',
`BitcoinPublicAddress` VARCHAR(128) NOT NULL DEFAULT '',
`BitcoinPrivateRoot` VARCHAR(256) NOT NULL DEFAULT '',
`BitcoinPublicRoot` VARCHAR(256) NOT NULL DEFAULT '',
`BankTransactionTag` VARCHAR(128) NOT NULL DEFAULT '',
`EnabledForExpensing` TINYINT(1) NOT NULL DEFAULT 0,
`EnabledForInvoicing` TINYINT(1) NOT NULL DEFAULT 0,
`AutomationProfileId` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`FinancialAccountId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_Parent` (`ParentFinancialAccountId`),
KEY `IX_Owner` (`OwnerUserId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- FINANCIAL TRANSACTIONS
-- ============================================================
CREATE TABLE `FinancialTransactions` (
`FinancialTransactionId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`OrganizationSequenceId` INT NOT NULL DEFAULT 0,
`DateTime` DATETIME NOT NULL,
`Comment` TEXT NOT NULL,
`ImportHash` VARCHAR(32) NOT NULL DEFAULT '',
`ImportSha256` VARCHAR(80) NOT NULL DEFAULT '',
`HasDocumentation` TINYINT(1) NOT NULL DEFAULT 0,
-- Denormalized from v5 ObjectOptionalData
`BlockchainHash` VARCHAR(128) NOT NULL DEFAULT '',
PRIMARY KEY (`FinancialTransactionId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_DateTime` (`DateTime`),
KEY `IX_ImportHash` (`ImportHash`),
KEY `IX_OrgSequenceId` (`OrganizationId`, `OrganizationSequenceId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- FINANCIAL TRANSACTION ROWS
-- ============================================================
CREATE TABLE `FinancialTransactionRows` (
`FinancialTransactionRowId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FinancialTransactionId` INT UNSIGNED NOT NULL,
`FinancialAccountId` INT UNSIGNED NOT NULL,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL DEFAULT 0,
`Deleted` TINYINT(1) NOT NULL DEFAULT 0,
`DeletedDateTime` DATETIME NOT NULL DEFAULT '1990-01-01 00:00:00',
`DeletedByUserId` INT UNSIGNED NOT NULL DEFAULT 0,
-- Foreign currency (replaces v5 FinancialTransactionRowsNativeCurrency table)
`ForeignCurrencyCode` VARCHAR(8) NOT NULL DEFAULT '',
`ForeignCurrencyAmountCents` BIGINT NOT NULL DEFAULT 0,
-- Denormalized from v5 ObjectOptionalData
`BitcoinDerivationPath` VARCHAR(256) NOT NULL DEFAULT '',
PRIMARY KEY (`FinancialTransactionRowId`),
KEY `IX_TransactionId` (`FinancialTransactionId`),
KEY `IX_AccountId` (`FinancialAccountId`),
KEY `IX_Deleted` (`Deleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- TRANSACTION DEPENDENCIES (continuation chains + business object links)
-- ============================================================
CREATE TABLE `FinancialDependencyTypes` (
`FinancialDependencyTypeId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(64) NOT NULL,
PRIMARY KEY (`FinancialDependencyTypeId`),
UNIQUE KEY `UX_Name` (`Name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `FinancialTransactionDependencies` (
`FinancialTransactionId` INT UNSIGNED NOT NULL,
`FinancialDependencyTypeId` INT UNSIGNED NOT NULL,
`ForeignId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`FinancialTransactionId`),
KEY `IX_ForeignKey` (`FinancialDependencyTypeId`, `ForeignId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- TRANSACTION TAGS
-- ============================================================
CREATE TABLE `FinancialTransactionTagSets` (
`FinancialTransactionTagSetId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`Name` VARCHAR(128) NOT NULL DEFAULT '',
`DisplayOrder` INT NOT NULL DEFAULT 0,
`AllowUntagged` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`FinancialTransactionTagSetId`),
KEY `IX_OrganizationId` (`OrganizationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `FinancialTransactionTagTypes` (
`FinancialTransactionTagTypeId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FinancialTransactionTagSetId` INT UNSIGNED NOT NULL,
`ParentFinancialTransactionTagTypeId` INT UNSIGNED NOT NULL DEFAULT 0,
`Name` VARCHAR(128) NOT NULL,
`Active` TINYINT(1) NOT NULL DEFAULT 1,
`OpenedYear` INT NOT NULL DEFAULT 1,
`ClosedYear` INT NOT NULL DEFAULT 9999,
PRIMARY KEY (`FinancialTransactionTagTypeId`),
KEY `IX_SetId` (`FinancialTransactionTagSetId`),
KEY `IX_Parent` (`ParentFinancialTransactionTagTypeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `FinancialTransactionTags` (
`FinancialTransactionTagId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FinancialTransactionId` INT UNSIGNED NOT NULL,
`FinancialTransactionTagTypeId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`FinancialTransactionTagId`),
KEY `IX_TransactionId` (`FinancialTransactionId`),
KEY `IX_TagTypeId` (`FinancialTransactionTagTypeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- BUDGETS
-- ============================================================
CREATE TABLE `FinancialAccountBudgets` (
`FinancialAccountBudgetId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FinancialAccountId` INT UNSIGNED NOT NULL,
`Year` INT NOT NULL,
`Month` INT NOT NULL DEFAULT 0 COMMENT '0 = annual, 1-12 = monthly',
`AmountCents` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`FinancialAccountBudgetId`),
UNIQUE KEY `UX_AccountYearMonth` (`FinancialAccountId`, `Year`, `Month`),
KEY `IX_AccountId` (`FinancialAccountId`),
KEY `IX_Year` (`Year`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -0,0 +1,365 @@
-- UsingCash v6 Schema: AP/AR and Support Tables
-- ============================================================
-- EXPENSE CLAIMS
-- ============================================================
CREATE TABLE `ExpenseClaims` (
`ExpenseClaimId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`OrganizationSequenceId` INT NOT NULL DEFAULT 0,
`ClaimingUserId` INT UNSIGNED NOT NULL,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ExpenseDate` DATETIME NOT NULL,
`Description` VARCHAR(256) NOT NULL,
`BudgetId` INT UNSIGNED NOT NULL DEFAULT 0,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`VatCents` BIGINT NOT NULL DEFAULT 0,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`Attested` TINYINT(1) NOT NULL DEFAULT 0,
`Validated` TINYINT(1) NOT NULL DEFAULT 0,
`Claimed` TINYINT(1) NOT NULL DEFAULT 0,
`Repaid` TINYINT(1) NOT NULL DEFAULT 0,
`KeepSeparate` TINYINT(1) NOT NULL DEFAULT 0,
`ClosedDateTime` DATETIME NOT NULL DEFAULT '9999-12-31 00:00:00',
`ExpenseClaimGroupId` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`ExpenseClaimId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_OrgSequenceId` (`OrganizationId`, `OrganizationSequenceId`),
KEY `IX_ClaimingUserId` (`ClaimingUserId`),
KEY `IX_Open` (`Open`),
KEY `IX_Claimed` (`Claimed`),
KEY `IX_CreatedDateTime` (`CreatedDateTime`),
KEY `IX_ClosedDateTime` (`ClosedDateTime`),
KEY `IX_GroupId` (`ExpenseClaimGroupId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `ExpenseClaimGroups` (
`ExpenseClaimGroupId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`GroupType` VARCHAR(64) NOT NULL DEFAULT '',
`GroupData` TEXT NOT NULL,
PRIMARY KEY (`ExpenseClaimGroupId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_Open` (`Open`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- INBOUND INVOICES (supplier invoices / accounts payable)
-- ============================================================
CREATE TABLE `InboundInvoices` (
`InboundInvoiceId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`OrganizationSequenceId` INT NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL,
`DueDate` DATETIME NOT NULL,
`BudgetId` INT UNSIGNED NOT NULL DEFAULT 0,
`Supplier` VARCHAR(128) NOT NULL DEFAULT '',
`PayToAccount` VARCHAR(128) NOT NULL DEFAULT '',
`Ocr` VARCHAR(64) NOT NULL DEFAULT '',
`InvoiceReference` VARCHAR(64) NOT NULL DEFAULT '',
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`VatCents` BIGINT NOT NULL DEFAULT 0,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`Attested` TINYINT(1) NOT NULL DEFAULT 0,
`ClosedDateTime` DATETIME NOT NULL DEFAULT '9999-12-31 00:00:00',
`ClosedByUserId` INT UNSIGNED NOT NULL DEFAULT 0,
-- Denormalized from v5 ObjectOptionalData
`Description` TEXT NOT NULL,
`ExactBitcoinAmountSatoshis` BIGINT NOT NULL DEFAULT 0,
`NativeCurrencyCode` VARCHAR(8) NOT NULL DEFAULT '',
`NativeCurrencyAmountCents` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`InboundInvoiceId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_OrgSequenceId` (`OrganizationId`, `OrganizationSequenceId`),
KEY `IX_Open` (`Open`),
KEY `IX_BudgetId` (`BudgetId`),
KEY `IX_CreatedDateTime` (`CreatedDateTime`),
KEY `IX_ClosedDateTime` (`ClosedDateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- OUTBOUND INVOICES (customer invoices / accounts receivable)
-- ============================================================
CREATE TABLE `OutboundInvoices` (
`OutboundInvoiceId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`OrganizationSequenceId` INT NOT NULL DEFAULT 0,
`CustomerName` VARCHAR(128) NOT NULL,
`InvoiceAddressPaper` VARCHAR(256) NOT NULL DEFAULT '',
`InvoiceAddressMail` VARCHAR(128) NOT NULL DEFAULT '',
`CurrencyId` INT UNSIGNED NOT NULL,
`BudgetId` INT UNSIGNED NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL,
`DueDate` DATETIME NOT NULL,
`Reference` VARCHAR(64) NOT NULL DEFAULT '',
`TheirReference` VARCHAR(64) NOT NULL DEFAULT '',
`SecurityCode` VARCHAR(16) NOT NULL DEFAULT '',
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`Sent` TINYINT(1) NOT NULL DEFAULT 0,
`ReminderCount` INT UNSIGNED NOT NULL DEFAULT 0,
`ClosedDateTime` DATETIME NOT NULL DEFAULT '9999-12-31 00:00:00',
-- Denormalized from v5 ObjectOptionalData
`CreditedWithInvoiceId` INT UNSIGNED NOT NULL DEFAULT 0,
`CreditsInvoiceId` INT UNSIGNED NOT NULL DEFAULT 0,
`ToUserId` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`OutboundInvoiceId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_OrgSequenceId` (`OrganizationId`, `OrganizationSequenceId`),
KEY `IX_Open` (`Open`),
KEY `IX_DueDate` (`DueDate`),
KEY `IX_CustomerName` (`CustomerName`),
KEY `IX_CreatedDateTime` (`CreatedDateTime`),
KEY `IX_ClosedDateTime` (`ClosedDateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `OutboundInvoiceItems` (
`OutboundInvoiceItemId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OutboundInvoiceId` INT UNSIGNED NOT NULL,
`SortOrder` INT UNSIGNED NOT NULL DEFAULT 0,
`Description` VARCHAR(256) NOT NULL,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`VatCents` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`OutboundInvoiceItemId`),
KEY `IX_OutboundInvoiceId` (`OutboundInvoiceId`),
KEY `IX_SortOrder` (`SortOrder`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- PAYOUTS & PAYMENTS
-- ============================================================
CREATE TABLE `Payouts` (
`PayoutId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`Bank` VARCHAR(128) NOT NULL DEFAULT '',
`Account` VARCHAR(128) NOT NULL DEFAULT '',
`Reference` VARCHAR(256) NOT NULL DEFAULT '',
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`ExpectedTransactionDate` DATETIME NOT NULL,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`PayoutId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_Open` (`Open`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `PaymentGroups` (
`PaymentGroupId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`CurrencyId` INT UNSIGNED NOT NULL,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`DateTime` DATETIME NOT NULL,
`Tag` VARCHAR(64) NOT NULL DEFAULT '',
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CreatedByUserId` INT UNSIGNED NOT NULL,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`PaymentGroupId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_Tag` (`Tag`),
KEY `IX_Open` (`Open`),
KEY `IX_DateTime` (`DateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Payments` (
`PaymentId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`PaymentGroupId` INT UNSIGNED NOT NULL,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`Reference` VARCHAR(64) NOT NULL DEFAULT '',
`FromAccount` VARCHAR(64) NOT NULL DEFAULT '',
`PaymentKey` VARCHAR(64) NOT NULL DEFAULT '',
`HasImage` TINYINT(1) NOT NULL DEFAULT 0,
`OutboundInvoiceId` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`PaymentId`),
KEY `IX_PaymentGroupId` (`PaymentGroupId`),
KEY `IX_PaymentKey` (`PaymentKey`),
KEY `IX_OutboundInvoiceId` (`OutboundInvoiceId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Refunds` (
`RefundId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`PaymentId` INT UNSIGNED NOT NULL,
`Open` TINYINT(1) NOT NULL DEFAULT 1,
`AmountCents` BIGINT NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ClosedDateTime` DATETIME NOT NULL DEFAULT '9999-12-31 00:00:00',
`CreatedByUserId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`RefundId`),
KEY `IX_PaymentId` (`PaymentId`),
KEY `IX_Open` (`Open`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- DOCUMENTS
-- ============================================================
CREATE TABLE `Documents` (
`DocumentId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ServerFileName` VARCHAR(256) NOT NULL,
`ClientFileName` VARCHAR(256) NOT NULL DEFAULT '',
`Description` VARCHAR(256) NOT NULL DEFAULT '',
`DocumentTypeId` INT NOT NULL COMMENT 'UsingCash.Common.Enums.DocumentType',
`ForeignId` INT UNSIGNED NOT NULL DEFAULT 0,
`FileSize` BIGINT UNSIGNED NOT NULL DEFAULT 0,
`UploadedByUserId` INT UNSIGNED NOT NULL,
`UploadedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`DocumentId`),
KEY `IX_ForeignId` (`DocumentTypeId`, `ForeignId`),
KEY `IX_Uploader` (`UploadedByUserId`),
KEY `IX_DateTime` (`UploadedDateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `FinancialAccountDocuments` (
`FinancialAccountDocumentId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FinancialAccountId` INT UNSIGNED NOT NULL,
`DocumentTypeId` INT NOT NULL,
`UploadedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UploadedByUserId` INT UNSIGNED NOT NULL,
`ConcernsPeriodStart` DATETIME NOT NULL,
`ConcernsPeriodEnd` DATETIME NOT NULL,
`RawDocumentText` TEXT NOT NULL,
PRIMARY KEY (`FinancialAccountDocumentId`),
KEY `IX_AccountId` (`FinancialAccountId`),
KEY `IX_DocType` (`DocumentTypeId`),
KEY `IX_Period` (`ConcernsPeriodStart`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- CURRENCIES & EXCHANGE RATES
-- ============================================================
CREATE TABLE `Currencies` (
`CurrencyId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Code` VARCHAR(8) NOT NULL,
`Name` VARCHAR(64) NOT NULL,
`Sign` VARCHAR(8) NOT NULL DEFAULT '',
`IsCrypto` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`CurrencyId`),
UNIQUE KEY `UX_Code` (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `ExchangeRateSnapshots` (
`ExchangeRateSnapshotId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`DateTime` DATETIME NOT NULL,
PRIMARY KEY (`ExchangeRateSnapshotId`),
KEY `IX_DateTime` (`DateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `ExchangeRateDatapoints` (
`ExchangeRateSnapshotId` INT UNSIGNED NOT NULL,
`CurrencyId` INT UNSIGNED NOT NULL,
`RatePerBtcMicro` DOUBLE NOT NULL COMMENT 'Rate per BTC / 1000000 (micro-BTC scaling); BCH stored at face value',
PRIMARY KEY (`ExchangeRateSnapshotId`, `CurrencyId`),
KEY `IX_CurrencyId` (`CurrencyId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- CRYPTO TREASURY
-- ============================================================
CREATE TABLE `CryptoChains` (
`CryptoChainId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(64) NOT NULL,
`Code` VARCHAR(8) NOT NULL,
`CurrencyId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`CryptoChainId`),
UNIQUE KEY `UX_Code` (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `HotWalletAddresses` (
`HotWalletAddressId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`CryptoChainId` INT UNSIGNED NOT NULL,
`DerivationPath` VARCHAR(256) NOT NULL,
`AddressString` VARCHAR(128) NOT NULL,
`BalanceSatoshis` BIGINT NOT NULL DEFAULT 0,
`ThroughputSatoshis` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`HotWalletAddressId`),
KEY `IX_OrganizationId` (`OrganizationId`),
KEY `IX_Address` (`AddressString`),
KEY `IX_Balance` (`BalanceSatoshis`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `HotWalletUtxos` (
`HotWalletUtxoId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`HotWalletAddressId` INT UNSIGNED NOT NULL,
`AmountSatoshis` BIGINT NOT NULL,
`TransactionHash` VARCHAR(128) NOT NULL,
`TransactionOutputIndex` INT NOT NULL,
`ConfirmationCount` INT NOT NULL DEFAULT 0,
`Spent` TINYINT(1) NOT NULL DEFAULT 0,
`SpentInTransactionHash` VARCHAR(128) NOT NULL DEFAULT '',
PRIMARY KEY (`HotWalletUtxoId`),
KEY `IX_AddressId` (`HotWalletAddressId`),
KEY `IX_TxHash` (`TransactionHash`),
KEY `IX_Spent` (`Spent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- COMMS & EVENT BUS
-- ============================================================
CREATE TABLE `MailQueue` (
`MailQueueId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL DEFAULT 0,
`RecipientEmail` VARCHAR(256) NOT NULL,
`RecipientName` VARCHAR(128) NOT NULL DEFAULT '',
`Subject` VARCHAR(256) NOT NULL,
`Body` LONGTEXT NOT NULL,
`Status` VARCHAR(16) NOT NULL DEFAULT 'pending' COMMENT 'pending, sending, sent, failed',
`RetryCount` INT NOT NULL DEFAULT 0,
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`SentDateTime` DATETIME NULL,
`ErrorMessage` TEXT NOT NULL,
PRIMARY KEY (`MailQueueId`),
KEY `IX_Status` (`Status`),
KEY `IX_CreatedDateTime` (`CreatedDateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `MailTemplates` (
`MailTemplateId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`OrganizationId` INT UNSIGNED NOT NULL,
`TemplateName` VARCHAR(64) NOT NULL,
`Subject` VARCHAR(256) NOT NULL DEFAULT '',
`Body` TEXT NOT NULL,
PRIMARY KEY (`MailTemplateId`),
UNIQUE KEY `UX_OrgTemplate` (`OrganizationId`, `TemplateName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `EventQueue` (
`EventQueueId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`EventType` VARCHAR(64) NOT NULL,
`Payload` TEXT NOT NULL,
`Status` VARCHAR(16) NOT NULL DEFAULT 'pending' COMMENT 'pending, processing, completed, failed',
`CreatedDateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ProcessedDateTime` DATETIME NULL,
PRIMARY KEY (`EventQueueId`),
KEY `IX_Status` (`Status`),
KEY `IX_CreatedDateTime` (`CreatedDateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ============================================================
-- SYSTEM SETTINGS (replaces v5 FlatData)
-- ============================================================
CREATE TABLE `SystemSettings` (
`SettingKey` VARCHAR(128) NOT NULL,
`SettingValue` LONGTEXT NOT NULL,
PRIMARY KEY (`SettingKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;