Files
kivvi/docs/SYSTEM_DESIGN.md
CatoandClaude 085e6188ab docs: correct drifted paths and retired host in docs (#136)
Webhook endpoint is evig.orangecat.ch (revampit.orangecat.ch retired);
email paths point at apps/web; cross-repo metrics path names bitbaum/evig;
planned spec modules say they are not yet built. The 2026-07 audit
report moves to docs/archive.


Claude-Session: https://claude.ai/code/session_01PJkbo18cRNXtgPWZN2S9cD

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-19 13:58:09 +02:00

25 KiB
Raw Permalink Blame History

Kivvi — Exemplary System Design: Architecture, Integration & Accounting

created_date: 2026-07-02 last_modified_date: 2026-09-04 last_modified_summary: Docs truth-sweep: pnpm db:push has no root script — the working command is pnpm --filter @kivvi/database db:push. Status: Design reference (living doc) Audience: Kivvi engineers + revamp-it ops + the Verein's Treuhänder (for the flagged VAT/NPO items) Scope: How Kivvi supports revamp-it as a modern ERP companion replacing kivitendo: accounting, documents, payments, banking, VAT, finance-relevant contacts, and ERP inventory records. revamp-it remains its own application, database, and workflow system.

See also: docs/REVAMPIT_ERP_COMPANION_ROADMAP.md for the strict "like kivitendo / better than kivitendo / never absorb from revamp-it" boundary.


1. Principles (the ground truths this design serves)

  1. A transaction either happened or it didn't → every money event is recorded completely and atomically.
  2. State defines behaviour → one source of truth per fact.
  3. Automate the mechanical; reserve humans for judgment (grade, repair, price, tax classification).
  4. Swiss law governs → VAT/NPO logic is validated by a Treuhänder before it is coded. Correctness beats speed.
  5. Functional is the floor; helpful is the goal — Kivvi should guide, not just record.

2. Target architecture — ERP companion, not revamp-it backend

┌──────────────────────────── revamp-it ────────────────────────────┐
│ Own app/database: marketplace, checkout, P2P, workshops,          │
│ appointments, IT-Hilfe, community/customer workflows, CMS          │
└───────────────┬───────────────────────────────┬───────────────────┘
                │ finance / ERP events          │ signed ERP feedback
                ▼                               ▼
┌───────────────────────────── Kivvi ───────────────────────────────┐
│ ERP system of record: contacts for accounting, invoices, payments, │
│ banking, VAT, GL, dunning, finance-relevant item records, AI help  │
└───────────────────────────────────────────────────────────────────┘
  • revamp-it owns its operational product model, public marketplace, P2P relationships, cart/checkout, workshops, appointments, IT-Hilfe, community profiles, CMS, and local database.
  • Kivvi owns ERP outcomes: accounting/GL, invoices, payments, banking, VAT, dunning, financial counterparties, and finance-relevant inventory/accounting records.
  • The rule that prevents drift: each business event has a stable ERP projection in Kivvi, with idempotent writes, signed feedback, reconciliation, and diagnostics. Kivvi should behave like a much smarter kivitendo replacement, not like revamp-it's application backend.

Ownership split (SSOT per field):

Fact Authority
Marketplace listings, product copy, images, SEO, favourites, Q&A, cart revamp-it
Workshops, appointments, IT-Hilfe, community/customer workflows revamp-it
Operational inventory workflow and revamp-it local item records revamp-it
ERP inventory mirror / finance-relevant item record Kivvi
Accounting / GL / VAT / invoices Kivvi
Financial counterparty/contact record Kivvi for accounting; revamp-it for community profile
P2P seller relationship (isRevampit=false) revamp-it (Kivvi only sees the agency economics — §4)

3. Integration contract (revamp-it ↔ Kivvi) — verified, and how to make it live

3.1 Contract (verified to match Kivvi main and revamp-it PR #206)

Forward (revamp-it → Kivvi) — finance-relevant item created/edited on revamp-it:

  • POST /api/v1/inventory-items on intake (Erfassung); PATCH /api/v1/inventory-items/{id} on edit.
  • Bearer kv_… token; send an Idempotency-Key (revamp-it item id) so retries can't duplicate.

Reverse (Kivvi → revamp-it) — ERP item/status changed in Kivvi:

  • Kivvi emits signed webhooks from the domain layer, so API-origin changes fire too.
  • Headers: X-Kivvi-Event, X-Kivvi-Signature = HMAC-SHA256(rawBody, secret) (hex).
  • Body: { event, timestamp, companyId, data: { id, itemNumber, description, condition, status, warehouseId, askingPrice } } — same shape for created, updated, and status_changed (SSOT: buildInventoryItemWebhookPayload in inventory-items.ts).
  • Combined PATCH /inventory-items/{id} (status + fields) emits one webhook (status_changed wins). sellInventoryItem (invoice line with inventoryItemId) also emits status_changed.
  • Receiver verifies the HMAC over the raw body, joins data.id → inventory_items.kivviInventoryItemId, maps askingPrice→sellingPriceChf, condition→conditionOverride, and delists the listing on a terminal status (sold/returned/recycled/donated).
  • Loop-safe: receiver writes are internal and never push back; only human UI edits push forward.

Field ownership on the wire: revamp-it pushes the facts Kivvi needs for ERP/accounting projection; Kivvi pushes ERP lifecycle/status feedback. Minimal overlap, deterministic. This is integration between two systems, not a migration of revamp-it's backend.

3.2 Go-live checklist (the ops wiring — makes the connection actually work)

  1. Deploy current Kivvi main (includes per-company modules, durable sync, repair-labor billing); run pnpm --filter @kivvi/database db:push or pnpm db:migrate so api_idempotency_keys exists.
  2. Deploy revamp-it #206 → the receiver goes live at https://evig.orangecat.ch/api/webhooks/kivvi.
  3. In Kivvi (revamp-it's tenant): Settings → Webhooks → Add endpoint
    • URL https://evig.orangecat.ch/api/webhooks/kivvi
    • Events inventory_item.updated, inventory_item.status_changed
    • Generate a shared secret.
  4. Set revamp-it env: KIVVI_WEBHOOK_SECRET (= step 3 secret), KIVVI_API_URL, KIVVI_API_TOKEN, KIVVI_DEFAULT_WAREHOUSE_ID.
  5. One-time backfill: push owned items created before the integration (kivviSyncStatus IS NULL/'error').
  6. Add a reconciliation cron on revamp-it that retries kivviSyncStatus='error' rows (turns fire-and-forget into self-healing).

3.3 Known gap to close for "same product, right status"

An item lands in Kivvi at intake and does not auto-advance to listed when published for sale. Add: on publish/list, PATCH Kivvi status → listed, so Kivvi mirrors reality between intake and sale.

3.4 P2P marketplace sync contract (syncP2POrderToKivvi)

When Payrexx confirms payment, revamp-it must branch on who owns the listing — never on seller email domain.

payment confirmed (Payrexx webhook)
        │
        ├─ listing.is_revampit === true  →  syncOrderToKivvi()     (existing)
        │                                 invoice + payment + mark Kivvi item sold
        │
        └─ listing.is_revampit === false →  syncP2POrderToKivvi() (new)
                                          agency journal only — NO invoice, NO full revenue
                                          on Payrexx RESERVED/PAID
                                          syncP2PPayoutToKivvi() on Payrexx CONFIRMED
                                          (clears 2140 seller payable — §3.4 payout)

Owned stock (is_revampit=true) — unchanged

Keep the current path in payment-webhook.ts:syncOrderToKivvi:

  1. POST /api/v1/documents (invoice) → PATCH …/status → sent
  2. POST /api/v1/documents/{id}/payments
  3. PATCH /api/v1/inventory-items/{id} → status: sold (when backed by a Kivvi item)

Idempotency-Key on document create: marketplace-order:{orderId}:invoice

P2P facilitated sale (is_revampit=false) — agency model

Do not create an invoice or book full-price revenue. Call:

POST /api/v1/marketplace/agency-sales
Authorization: Bearer kv_…
Idempotency-Key: marketplace-order:{orderId}:paid
Content-Type: application/json

Request body (all amounts decimal strings, never floats):

Field Source Notes
orderReference MO-{order.id} or marketplace order number Stable, human-readable
date payment date YYYY-MM-DD ISO 8601
grossAmount order.amountChf What the buyer paid (incl. shipping if in total)
commissionAmount net commission Platform fee excl. VAT
commissionVatAmount computed VAT on the fee only (8.1% of commissionAmount when taxable)
sellerPayout order.sellerPayoutChf Optional but recommended — when sent, must balance gross
sourceId order.id UUID for journal sourceId
description optional e.g. P2P sale MO-… Payrexx {txnId}

Invariant (enforced by Kivvi Zod schema when sellerPayout is provided):

grossAmount = commissionAmount + commissionVatAmount + sellerPayout  (±0.01)

At 0% commission (COMMISSION_RATE=0 today):

{
  "orderReference": "MO-550e8400-e29b-41d4-a716-446655440099",
  "date": "2026-07-08",
  "grossAmount": "350.00",
  "commissionAmount": "0",
  "commissionVatAmount": "0",
  "sourceId": "550e8400-e29b-41d4-a716-446655440099"
}

→ Kivvi posts DR 1020 / CR 2140 for CHF 350.00 (pure pass-through).

With commission (future COMMISSION_RATE > 0):

{
  "orderReference": "MO-…",
  "date": "2026-07-08",
  "grossAmount": "110.81",
  "commissionAmount": "10.00",
  "commissionVatAmount": "0.81",
  "sourceId": "…"
}

→ DR 1020 110.81 / CR 2140 100.00 / CR 3200 10.00 / CR 2200 0.81

Response (success):

{
  "success": true,
  "data": {
    "journalEntryId": "…",
    "reference": "MO-…",
    "sourceType": "marketplace_agency_sale"
  }
}

Retries with the same Idempotency-Key return the stored response (Idempotent-Replayed: true).

Seller payout (when funds released)

Separate event — do not combine with the paid webhook:

POST /api/v1/marketplace/payouts
Idempotency-Key: marketplace-order:{orderId}:payout
{
  "amount": "100.00",
  "date": "2026-07-15",
  "reference": "PAYOUT-MO-…",
  "description": "Seller payout MO-…"
}

→ DR 2140 / CR 1020

Reference implementation sketch (revamp-it)

async function syncP2POrderToKivvi(
  order: MarketplaceOrder,
  payrexxTxnId: string | null,
) {
  const commissionNet = order.commissionChf; // must be NET excl. VAT
  const commissionVat = computeCommissionVat(commissionNet); // 0 when rate is 0
  const sellerPayout = new Decimal(order.sellerPayoutChf);
  const gross = new Decimal(order.amountChf);
  // Assert: gross = commissionNet + commissionVat + sellerPayout (±0.01 rounding)

  await kivviFetch("/api/v1/marketplace/agency-sales", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${KIVVI_API_TOKEN}`,
      "Idempotency-Key": `marketplace-order:${order.id}:paid`,
    },
    body: JSON.stringify({
      orderReference: `MO-${order.id}`,
      date: new Date().toISOString().split("T")[0],
      grossAmount: gross.toFixed(2),
      commissionAmount: commissionNet,
      commissionVatAmount: commissionVat,
      sourceId: order.id,
      description: payrexxTxnId ? `P2P Payrexx ${payrexxTxnId}` : undefined,
    }),
  });
  // No invoice. No inventory mark-sold (P2P items are not Kivvi owned stock).
}

Inventory import dry-run (10-item sample)

Fixture: docs/fixtures/inventory-import-sample-10.csv — upload at Intake → Items → Import (/intake/items/import). The file deliberately mixes ready rows, missing locations, duplicates, and incomplete info so the review UI can be exercised before any Shopware bulk import.


4. Accounting model — exact double-entry per transaction type

Kivvi uses expense-as-incurred (periodic inventory): purchases are expensed immediately (4000 Warenaufwand), inventory is not capitalized, and there is no COGS on sale. This is deliberate and correct — do not add COGS/inventory-asset/disposal-write-off entries; they would double-count.

Accounts per Swiss KMU Kontenrahmen. ✅ = works today · 🟡 = partial · 🔨 = to build · ⚖️ = needs Treuhänder sign-off before coding.

Transaction Double entry State
Owned refurb sale invoice: DR 1100 / CR 3000 + CR 2200 (VAT 8.1%); payment: DR 1020 / CR 1100 ✅
Purchase (parts/goods/services) DR 4000 + DR 1170 (Vorsteuer) / CR 2000; payment: DR 2000 / CR 1020 ✅
Consignment sale sale as above + consignor share DR 4200 / CR 2140; payout DR 2140 / CR 1020 ✅ (merged)
P2P secure sale (agency) Kivvi books only revamp-it's economics: DR 1020 gross; CR 2140 pass-through owed to seller; CR 3200 commission; CR 2200 VAT on the fee only; payout DR 2140 / CR 1020. No full-price revenue. 0% commission collapses safely to DR 1020 / CR 2140. ✅ Kivvi domain + tests; ✅ revamp-it syncP2POrderToKivvi + recordKivviPayout on CONFIRMED
Customer repair parts already expensed at purchase (no COGS); labor → CR 3200 Dienstleistungserlöse as an invoice line ✅ tracked hours can now create a draft labor invoice
Pure service (consulting/Linux install) DR 1100 / CR 3200 + CR 2200 ✅ service invoice lines now route to 3200
Donated goods in none (expense-as-incurred → nothing capitalized). Optionally record fair-value donation income for transparency. ✅ (no entry is correct); income = policy choice
Monetary donation (Spende) DR 1020 / CR 3xxx Spenden — no input-VAT reduction (Art. 33 Abs. 1) 🔨 ⚖️
Grant / Subvention DR 1020 / CR 3xxx Subventionen + proportional input-VAT reduction (Art. 33 Abs. 2) 🔨 ⚖️ (highest error-risk rule)
Purchased used goods full-price output VAT on resale + fiktiver Vorsteuerabzug (Art. 28a) keyed to the per-item purchase price 🔨 ⚖️ (~0 for donated stock)
Foreign services (SaaS/ads) Bezugsteuer / reverse charge (Art. 45) self-declared above CHF 10k/yr 🔨 ⚖️
Advance payment / deposit DR 1020 / CR 2030 Erhaltene Anzahlungen (liability, VAT deferred); settle on final invoice 🔨 (currently all to AR 1100)
Disposal / recycle / scrap none (nothing was capitalized). If input VAT was deducted on a purchased item → possible Eigenverbrauch/Vorsteuerkorrektur (Art. 31) ✅ (no entry is correct) ⚖️ (Art. 31 edge case)
Fixed assets / depreciation manual journal entries for now ⚪ low (Kivitendo lacks it too — parity, not a regression)

Do NOT build: German-style margin taxation (Differenzbesteuerung) — abolished for used goods in CH (2010); only art/collectibles (Art. 24a). Refurb IT is fully taxable at 8.1% regardless of donated origin.


5. Helpful, not just functional — the AI-first layer

Recording correctly is the floor. Kivvi should actively reduce the human's cognitive load and catch the errors above before they hit the books. The same domain functions power the UI and the AI, so guidance is consistent.

Proactive guidance (the highest-value "helpful"):

  • Money-in classifier: on an incoming payment with no matching invoice, Kivvi suggests Spende vs. Subvention vs. sale and, if Subvention, warns: "This triggers a proportional input-VAT reduction (Art. 33) — confirm the funded activity." Turns the single most error-prone rule into a guided step.
  • Repair labor prompt: "You logged 3.5 h on this repair — add a labor line at CHF X/h?" — closes the revenue-leakage gap.
  • Foreign-VAT watchdog: on a foreign invoice without Swiss VAT, "Bezugsteuer may apply — you're at CHF Y of the CHF 10k/yr threshold."
  • Status nudge: "This item is listed for sale on revamp-it but shows intake in Kivvi — advance to listed?" (or automate via §3.3).
  • Reconciliation: "3 items failed to sync to the shop in the last hour — retry now."
  • Fiktive-Vorsteuer helper: for a purchased (not donated) used item, prompt to capture the purchase price so Art. 28a can be computed.

Command bar (Cmd+K, natural language): "50 laptops donated by UBS", "invoice the repair for K-00123", "how much VAT do I owe this quarter" — each maps to the same audited domain functions.

Smart defaults & progressive disclosure: per-company module toggles hide what a business doesn't use; a repair-café sees repairs, a vintage shop doesn't see data-erasure. Complexity appears only when needed.

Guardrails as help: API idempotency keys make retries safe; the P2P revenue guard stops phantom income; validation blocks unbalanced journals. Helpful = the system won't let you post something wrong.

Smart inventory import (/intake/items/import): a bulk import of secondhand items is never a blind insert — an external export (Shopware, a spreadsheet) often lists items we may no longer physically have, that lack key info, or whose location is unknown. Before anything is written, Kivvi classifies every row and asks the three questions a human would (analyzeInventoryImportRows, pure/testable):

  1. Do we actually have it? Presence is unconfirmed by default; a row can't be imported until a human ticks "present" (bulk-confirm available). This directly handles "most of which don't even exist".
  2. Do we have the right info? A per-row completeness score + missing-field list; missing description blocks, missing price/condition/serial warn.
  3. Where exactly is it? Each row must resolve to a warehouse (shop vs storage vs which storage) — the free-text hint is matched to known warehouses (exact → unique-substring → ambiguous/unresolved is flagged, never guessed); shelf/bin is captured as a recommended detail.

The importer de-duplicates on serial number (against existing items and within the file), caps batch size to protect production, and reuses createInventoryItem so imported items inherit the same numbering + webhook behaviour as manual intake. Recommended flow: dry-run with ~10 rows, review the worklist, then scale up.


6. Roadmap (prioritized)

Now — makes the connection actually work + closes revenue leakage (no tax-law dependency):

  1. Deploy + wire the sync (§3.2); add reconciliation + status-on-publish (§3.3).
  2. Repair-labor billing (hours × rate → invoice line) + route service revenue to 3200. ✅
  3. Cost centers (Kostenstellen) — per-activity P&L + grant reporting.

After Treuhänder sign-off (⚖️ items) — Swiss NPO/VAT correctness: 4. Subvention/Spende classifier + Art. 33 input-VAT reduction. 5. Fiktiver Vorsteuerabzug (Art. 28a) for purchased used goods. 6. Bezugsteuer (Art. 45) tracking for foreign services. 7. Monetary donation/grant income + (if FER 21 applies) restricted-fund accounting.

Later — structural: 8. ✅ P2P agency accounting (commission + pass-through liability + payout) — Kivvi domain + revamp-it caller (syncP2POrderToKivvi on PAID, recordKivviPayout on CONFIRMED). 9. Advance-payment liability routing. 10. Reconciliation/read-model hardening so revamp-it can compare its local operational state against Kivvi's ERP projection without making Kivvi the revamp-it backend. 11. Fixed assets/depreciation; Postgres RLS (defense-in-depth before scaling tenants).


7. Open items requiring Treuhänder / cantonal ruling (do not hard-code from inference)

  • Whether FER 21, the CHF 250k vs 100k VAT threshold, or Art. 21 exemptions apply to this Verein (depends on charitable status + activities).
  • Exact mechanics of Art. 28a fiktive Vorsteuer and Art. 33 Vorsteuerkürzung, and the Art. 31 disposal edge case.
  • Confirm CHF-0 fiktive Vorsteuer for donated goods against ESTV MWST-Info 09.

Statutory references here came from secondary sources; verify against Fedlex before implementing any VAT logic.