# villanuevayanmar/pig-ledger architecture

> Architecture diagram of the GitHub repository villanuevayanmar/pig-ledger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/villanuevayanmar/pig-ledger
- Repository: https://github.com/villanuevayanmar/pig-ledger

## Overview

Pig Ledger is a mobile-first piggery accounting and farm-management application. Its principal workflow is recording farm activity and financial transactions, then reviewing summaries and reports; offline storage and sync support working across connectivity changes. The tree also shows batch, feeding, health, customer, and supplier management capabilities. Evidence is limited: the README establishes the product and technology, while sampled source confirms selected storage, calculations, and sync behavior; many page-to-component links were not shown, so they are omitted.

Main entry points: [`PigLedger/lib/local-store.ts`](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/local-store.ts) (Offline ledger), [`PigLedger/app/api/sync/route.ts`](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/api/sync/route.ts) (Sync endpoint), [`PigLedger/lib/data.ts`](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/data.ts) (Farm data access), [`PigLedger/lib/totals.ts`](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/totals.ts) (Financial totals)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_farm["Farm Operations"]
  node_batches["Batch management<br/>[BatchClient.tsx]"]
  node_fattening["Fattening"]
  node_operations["Farm operations"]
  node_monitoring["Herd monitoring"]
  node_business["Business records<br/>[BusinessClient.tsx]"]
end

subgraph group_ledger["Accounting &amp; Reports"]
  node_expenses["Expense tracking<br/>[ExpensesClient.tsx]"]
  node_sales["Sales tracking<br/>[SalesClient.tsx]"]
  node_transactions["Transactions view<br/>[page.tsx]"]
  node_reports["Reports<br/>[page.tsx]"]
  node_dashboard["Farm dashboard<br/>[HomeClient.tsx]"]
  node_totals["Financial totals<br/>[totals.ts]"]
  node_ledgermath["Ledger calculations<br/>[ledger-math.mjs]"]
  node_format["Formatting utilities<br/>[format.ts]"]
end

subgraph group_runtime["Offline &amp; Sync"]
  node_synchandler["Sync endpoint<br/>[route.ts]"]
  node_localstore[("Offline ledger<br/>[local-store.ts]")]
  node_pending["Pending record merge"]
  node_migration["Legacy migration"]
  node_idempotency["Stable identifiers<br/>[idempotency.mjs]"]
  node_supabaseclient["Browser Supabase client<br/>[client.ts]"]
  node_dataaccess["Farm data access<br/>[data.ts]"]
end

subgraph group_platform["Application Platform"]
  node_settings["Farm settings<br/>[SettingsClient.tsx]"]
  node_authentication["Authentication<br/>[AuthForm.tsx]"]
  node_serveractions["Server actions<br/>[actions.ts]"]
end

node_farmer(("Farm user"))
node_supabase[("Supabase Auth &amp; PostgreSQL")]

node_localstore -->|"checks migration"| node_migration
node_localstore -->|"merges pending"| node_pending
node_localstore -->|"stabilizes IDs"| node_idempotency
node_localstore -->|"gets session"| node_supabaseclient
node_synchandler -->|"ensures farm"| node_dataaccess
node_synchandler -->|"stabilizes IDs"| node_idempotency
node_synchandler -->|"authenticates and writes"| node_supabase
node_dataaccess -->|"computes totals"| node_totals
node_totals -->|"delegates calculations"| node_ledgermath
node_dataaccess -->|"uses numeric helpers"| node_format

click node_batches "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/BatchClient.tsx"
click node_fattening "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/FatteningClient.tsx"
click node_operations "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/OperationsClient.tsx"
click node_monitoring "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/MonitoringClient.tsx"
click node_business "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/BusinessClient.tsx"
click node_expenses "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/ExpensesClient.tsx"
click node_sales "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/SalesClient.tsx"
click node_transactions "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/transactions/page.tsx"
click node_reports "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/reports/page.tsx"
click node_dashboard "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/HomeClient.tsx"
click node_settings "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/SettingsClient.tsx"
click node_authentication "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/AuthForm.tsx"
click node_serveractions "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/actions.ts"
click node_synchandler "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/api/sync/route.ts"
click node_localstore "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/local-store.ts"
click node_pending "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/pending-records.mjs"
click node_migration "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/legacy-migration.mjs"
click node_idempotency "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/idempotency.mjs"
click node_supabaseclient "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/supabase/client.ts"
click node_dataaccess "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/data.ts"
click node_totals "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/totals.ts"
click node_ledgermath "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/ledger-math.mjs"
click node_format "https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/format.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_batches,node_fattening,node_operations,node_monitoring,node_business,node_farmer toneBlue
class node_expenses,node_sales,node_transactions,node_reports,node_dashboard,node_totals,node_ledgermath,node_format,node_supabase toneAmber
class node_synchandler,node_localstore,node_pending,node_migration,node_idempotency,node_supabaseclient,node_dataaccess toneMint
class node_settings,node_authentication,node_serveractions toneRose
```

## Components

### Farm Operations

- [Batch management](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/BatchClient.tsx): `PigLedger/components/BatchClient.tsx`
- [Fattening](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/FatteningClient.tsx): `PigLedger/components/FatteningClient.tsx`
- [Farm operations](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/OperationsClient.tsx): `PigLedger/components/OperationsClient.tsx`
- [Herd monitoring](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/MonitoringClient.tsx): `PigLedger/components/MonitoringClient.tsx`
- [Business records](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/BusinessClient.tsx): `PigLedger/components/BusinessClient.tsx`

### Accounting & Reports

- [Expense tracking](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/ExpensesClient.tsx): `PigLedger/components/ExpensesClient.tsx`
- [Sales tracking](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/SalesClient.tsx): `PigLedger/components/SalesClient.tsx`
- [Transactions view](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/transactions/page.tsx): `PigLedger/app/transactions/page.tsx`
- [Reports](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/reports/page.tsx): `PigLedger/app/reports/page.tsx`
- [Farm dashboard](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/HomeClient.tsx): `PigLedger/components/HomeClient.tsx`
- [Financial totals](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/totals.ts): `PigLedger/lib/totals.ts`
- [Ledger calculations](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/ledger-math.mjs): `PigLedger/lib/ledger-math.mjs`
- [Formatting utilities](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/format.ts): `PigLedger/lib/format.ts`

### Offline & Sync

- [Sync endpoint](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/api/sync/route.ts): `PigLedger/app/api/sync/route.ts`
- [Offline ledger](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/local-store.ts): `PigLedger/lib/local-store.ts`
- [Pending record merge](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/pending-records.mjs): `PigLedger/lib/pending-records.mjs`
- [Legacy migration](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/legacy-migration.mjs): `PigLedger/lib/legacy-migration.mjs`
- [Stable identifiers](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/idempotency.mjs): `PigLedger/lib/idempotency.mjs`
- [Browser Supabase client](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/supabase/client.ts): `PigLedger/lib/supabase/client.ts`
- [Farm data access](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/lib/data.ts): `PigLedger/lib/data.ts`

### Application Platform

- [Farm settings](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/SettingsClient.tsx): `PigLedger/components/SettingsClient.tsx`
- [Authentication](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/components/AuthForm.tsx): `PigLedger/components/AuthForm.tsx`
- [Server actions](https://github.com/villanuevayanmar/pig-ledger/blob/main/PigLedger/app/actions.ts): `PigLedger/app/actions.ts`

### Other components

- **Farm user**
- **Supabase Auth & PostgreSQL**

## Connections

- Offline ledger → Legacy migration: checks migration (evidence: `PigLedger/lib/local-store.ts`)
- Offline ledger → Pending record merge: merges pending (evidence: `PigLedger/lib/local-store.ts`)
- Offline ledger → Stable identifiers: stabilizes IDs (evidence: `PigLedger/lib/local-store.ts`)
- Offline ledger → Browser Supabase client: gets session (evidence: `PigLedger/lib/local-store.ts`)
- Sync endpoint → Farm data access: ensures farm (evidence: `PigLedger/app/api/sync/route.ts`)
- Sync endpoint → Stable identifiers: stabilizes IDs (evidence: `PigLedger/app/api/sync/route.ts`)
- Sync endpoint → Supabase Auth & PostgreSQL: authenticates and writes (evidence: `PigLedger/app/api/sync/route.ts`)
- Farm data access → Financial totals: computes totals (evidence: `PigLedger/lib/data.ts`)
- Financial totals → Ledger calculations: delegates calculations (evidence: `PigLedger/lib/totals.ts`)
- Farm data access → Formatting utilities: uses numeric helpers (evidence: `PigLedger/lib/data.ts`)
