Cotton code
605 lines · 14.0 KB · JSONRaw
{  "format_version": 1,  "id": "statement",  "name": "Statement",  "plural": "Statements",  "picture": null,  "builtin": true,  "engine": "money",  "record_route": "/money/statements/[id]",  "columns": [    {      "key": "document_id",      "media": true,      "name": "Document",      "type": "link",      "link_to": "document",      "readonly": true    },    {      "key": "account_id",      "name": "Account",      "type": "link",      "readonly": true,      "link_to": "money_account"    },    {      "key": "kind",      "type": "choice",      "choices": [        "bank",        "credit_card"      ],      "readonly": true,      "labels": {        "bank": "Bank statement",        "credit_card": "Card statement"      }    },    {      "key": "period_start",      "name": "Period",      "type": "date",      "readonly": true,      "until": "period_end"    },    {      "key": "period_end",      "name": "To",      "type": "date",      "readonly": true    },    {      "key": "opening_balance_minor",      "name": "Opening balance",      "type": "amount",      "readonly": true,      "currency": "currency"    },    {      "key": "closing_balance_minor",      "name": "Closing balance",      "type": "amount",      "readonly": true,      "currency": "currency"    },    {      "key": "currency",      "type": "text",      "readonly": true    },    {      "key": "read_status",      "name": "Reading",      "type": "choice",      "choices": [        "pending",        "read",        "mismatch",        "failed"      ],      "readonly": true,      "labels": {        "pending": "Not read yet",        "read": "Read",        "mismatch": "Read, totals differ",        "failed": "Could not be read"      }    },    {      "key": "read_error",      "name": "Read problem",      "type": "text",      "readonly": true    },    {      "key": "read_at",      "name": "Read",      "type": "date",      "readonly": true    },    {      "key": "line_count",      "name": "Lines",      "type": "number",      "readonly": true    },    {      "key": "sum_in_minor",      "name": "Money in",      "type": "amount",      "readonly": true,      "currency": "currency"    },    {      "key": "sum_out_minor",      "name": "Money out",      "type": "amount",      "readonly": true,      "currency": "currency"    },    {      "key": "balance_check",      "name": "Balance check",      "type": "choice",      "choices": [        "ok",        "mismatch",        "unknown"      ],      "readonly": true,      "labels": {        "ok": "Adds up",        "mismatch": "Does not add up",        "unknown": "Not checked"      }    },    {      "key": "period",      "name": "Period",      "type": "text",      "readonly": true    },    {      "key": "heading",      "name": "Statement",      "type": "text",      "readonly": true    },    {      "key": "balances",      "name": "Balance",      "type": "text",      "readonly": true    },    {      "key": "check",      "name": "Check",      "type": "text",      "readonly": true    },    {      "key": "off_by",      "name": "Check",      "type": "text",      "readonly": true    },    {      "key": "recorded_count",      "name": "Recorded lines",      "type": "number",      "readonly": true    },    {      "key": "line_progress",      "name": "Lines",      "type": "text",      "readonly": true    }  ],  "title": "period",  "heads": "heading",  "date": "period_end",  "project": null,  "order": null,  "slip": {    "title": "period",    "subtitle": "account_id",    "trailing": "read_status",    "photo": null,    "toggle": null,    "caption": {      "columns": [        "balances",        "check",        "off_by",        "line_progress"      ]    }  },  "tile": {    "face": "slip-list"  },  "page": [    {      "heading": null,      "columns": [        "period"      ]    },    {      "heading": null,      "columns": [        "balances",        "check",        "read_error"      ],      "role": "facts"    },    {      "heading": null,      "columns": [        "off_by"      ],      "when": {        "column": "off_by",        "present": true      },      "ink": "red",      "role": "facts"    },    {      "heading": "Lines",      "columns": [],      "list": {        "table": "statement_line",        "link": "statement_id",        "empty": {          "title": "Kept as a summary",          "message": "This statement was kept as its totals only. Its lines were not kept."        }      },      "when": {        "column": "read_status",        "values": [          "read",          "mismatch"        ]      },      "role": "main"    },    {      "heading": null,      "columns": [        "kind",        "currency",        "read_status",        "account_id",        "opening_balance_minor",        "closing_balance_minor"      ],      "role": "side"    },    {      "heading": "Totals",      "columns": [        "line_count",        "sum_in_minor",        "sum_out_minor",        "read_at",        "document_id"      ],      "role": "side"    }  ],  "instructions": "A statement is a bank or card statement read from a filed document, covering one period of one account. Read status is its only state: pending until read, read when the totals agreed, mismatch when they did not, failed when it could not be read. Nothing is recorded from a statement whose totals did not agree; amounts are integer minor units.",  "derived": true,  "delete": false,  "commands": [  {    "id": "book",    "label": "Book entries",    "kind": "update",    "verb": "book_statement",    "bindings": {      "id": "$id"    },    "when": {      "read_status": "read",      "balance_check": "ok"    },    "receipt": "Statement entries recorded",    "does": "completes"  },  {    "id": "open-account-id",    "label": "Open account",    "kind": "open",    "opens": "account_id"  },  {    "id": "open-document-id",    "label": "Open document",    "kind": "open",    "opens": "document_id"  }],  "parts": {  "shape": "sheet",  "head": {    "facts": []  },  "links": [],  "lists": [    {      "table": "statement_line",      "relation": "statement_id",      "heading": "Lines"    }  ],  "acts": {    "main": "book",    "more": [      "open-account-id",      "open-document-id"    ]  },  "cards": [    {      "column": "balance_check"    },    {      "column": "read_status"    },    {      "column": "read_error"    },    {      "column": "opening_balance_minor"    },    {      "column": "closing_balance_minor"    },    {      "column": "sum_in_minor"    },    {      "column": "sum_out_minor"    },    {      "column": "line_progress"    }  ],  "strip": []},  "tables": [    {      "name": "cotton_money_statements",      "columns": [        {          "name": "id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": true,          "declaration": "\n    id TEXT PRIMARY KEY",          "position": 0        },        {          "name": "workspace_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    workspace_id TEXT NOT NULL",          "position": 1        },        {          "name": "document_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    document_id TEXT NOT NULL",          "position": 2        },        {          "name": "account_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    account_id TEXT",          "position": 3        },        {          "name": "kind",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    kind TEXT NOT NULL",          "position": 4        },        {          "name": "period_start",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    period_start TEXT",          "position": 5        },        {          "name": "period_end",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    period_end TEXT",          "position": 6        },        {          "name": "opening_balance_minor",          "type": "INTEGER",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    opening_balance_minor INTEGER",          "position": 7        },        {          "name": "closing_balance_minor",          "type": "INTEGER",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    closing_balance_minor INTEGER",          "position": 8        },        {          "name": "currency",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    currency TEXT NOT NULL",          "position": 9        },        {          "name": "read_status",          "type": "TEXT",          "default": "'pending'",          "nullable": false,          "primary_key": false,          "declaration": "\n    read_status TEXT NOT NULL DEFAULT 'pending'",          "position": 10        },        {          "name": "read_error",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    read_error TEXT",          "position": 11        },        {          "name": "read_by",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    read_by TEXT",          "position": 12        },        {          "name": "read_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    read_at TEXT",          "position": 13        },        {          "name": "line_count",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    line_count INTEGER NOT NULL DEFAULT 0",          "position": 14        },        {          "name": "sum_in_minor",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    sum_in_minor INTEGER NOT NULL DEFAULT 0",          "position": 15        },        {          "name": "sum_out_minor",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    sum_out_minor INTEGER NOT NULL DEFAULT 0",          "position": 16        },        {          "name": "balance_check",          "type": "TEXT",          "default": "'unknown'",          "nullable": false,          "primary_key": false,          "declaration": "\n    balance_check TEXT NOT NULL DEFAULT 'unknown'",          "position": 17        },        {          "name": "created_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    created_at TEXT NOT NULL",          "position": 18        },        {          "name": "updated_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    updated_at TEXT NOT NULL",          "position": 19        },        {          "name": "_hlc",          "type": "TEXT",          "default": "''",          "nullable": false,          "primary_key": false,          "declaration": "\n    _hlc TEXT NOT NULL DEFAULT ''",          "position": 20        },        {          "name": "_born",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    _born TEXT",          "position": 21        }      ],      "constraints": [        {          "declaration": "\n    UNIQUE (document_id)",          "position": 22        },        {          "declaration": "\n    CHECK (balance_check IN ('ok', 'mismatch', 'unknown'))",          "position": 23        },        {          "declaration": "\n    CHECK (kind IN ('bank', 'credit_card'))",          "position": 24        },        {          "declaration": "\n    CHECK (line_count >= 0)",          "position": 25        },        {          "declaration": "\n    CHECK ((period_start IS NULL) OR (period_end IS NULL) OR (period_end >= period_start))",          "position": 26        },        {          "declaration": "\n    CHECK (read_status IN ('pending', 'read', 'mismatch', 'failed'))",          "position": 27        },        {          "declaration": "\n    FOREIGN KEY (document_id) REFERENCES cotton_documents(id)",          "position": 28        },        {          "declaration": "\n    FOREIGN KEY (workspace_id) REFERENCES cotton_workspaces(id) ON DELETE CASCADE\n  ",          "position": 29        }      ],      "prefix": "CREATE TABLE cotton_money_statements (",      "suffix": ")",      "indexes": [        "CREATE INDEX idx_cotton_money_statements_account ON cotton_money_statements (account_id, period_start) WHERE account_id IS NOT NULL",        "CREATE INDEX idx_cotton_money_statements_pending ON cotton_money_statements (created_at) WHERE read_status = 'pending'",        "CREATE INDEX idx_cotton_money_statements_workspace ON cotton_money_statements (workspace_id, period_start DESC)",        "CREATE INDEX IF NOT EXISTS idx_cotton_money_statements_author ON cotton_money_statements (substr(_hlc, 20), _hlc)"      ],      "full_text": [],      "triggers": []    }  ]}