Metadata-Version: 2.4
Name: actual-to-firefly
Version: 0.1.0
Summary: Idempotent, balance-verified migration CLI from an Actual Budget SQLite database to Firefly III.
Author: Jake Zhang
License-Expression: MIT
Project-URL: Homepage, https://github.com/JacobYZ/actual-to-firefly
Project-URL: Repository, https://github.com/JacobYZ/actual-to-firefly
Project-URL: Issues, https://github.com/JacobYZ/actual-to-firefly/issues
Keywords: actual-budget,firefly-iii,personal-finance,migration,sqlite,self-hosted
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial :: Accounting
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# actual-to-firefly

Zero-dependency Python CLI for migrating an unencrypted [Actual Budget](https://actualbudget.org/) SQLite database (`db.sqlite`) into [Firefly III](https://www.firefly-iii.org/) through the public Firefly III REST API.

Built for clean, repeatable cutovers: **dry-run by default**, **idempotent on re-run**, and **post-import balance verified** down to the cent.

## Why This Exists

Most migration guides go from Firefly III to Actual Budget via per-account CSV exports. Going the other way (from Actual's local SQLite ledger into Firefly III's double-entry model) breaks quickly with CSV exports:

- **Transfers** get duplicated or decoupled across accounts.
- **Split transactions** lose their parent grouping.
- **Same-account transfers** fail in Firefly III (`source_id == destination_id` is rejected).
- **Refunds from merchants** collide with expense/revenue counterparty naming unless disambiguated.
- **Interrupted imports** are painful to resume safely without deterministic `external_id` tracking.

`actual-to-firefly` reads `db.sqlite` in **read-only SQLite mode**, plans the exact Firefly payloads, writes through `/api/v1/*`, and reconciles every account balance and `external_id` before returning exit code `0`.

## What It Migrates

- **Asset accounts** (preserving opening balance, opening date, active/closed state, and off-budget → `include_net_worth` mapping)
- **Categories** (flattened from `Category Group / Category`)
- **Budgets & non-zero monthly budget limits** (from Actual's `zero_budgets` table)
- **Regular withdrawals and deposits**
- **Transfers** (paired rows merged into one Firefly `transfer`; same-account transfers converted into balanced withdrawal + deposit entries)
- **Split transactions** (parent + children mapped to Firefly multi-split transaction groups)

### Intentionally Out of Scope

- Actual rules, schedules, attachments, and bank-sync automation (reported as skipped counts in `migration_report.json`)
- Encrypted Actual backups (export an unencrypted copy of `db.sqlite` from Actual first)
- Mixed-sign split transactions (Firefly III requires all splits in a group to share the same direction)

## Quick Start

Requires **Python 3.11+**. No third-party runtime dependencies.

### 1. Dry-Run First (Read-Only, No Firefly Token Needed)

```bash
uvx actual-to-firefly \
  --actual-db /path/to/db.sqlite \
  --currency USD \
  --report migration_report.json
```

Or install with `pipx` / `pip`:

```bash
pip install actual-to-firefly
actual-to-firefly --actual-db /path/to/db.sqlite --currency EUR
```

Inspect `migration_report.json` to verify account counts, planned transaction groups, detected dual-role payees, and any warnings (for example, orphan transfer rows).

### 2. Execute Against Firefly III

Generate a Personal Access Token in Firefly III (**Options → Profile → OAuth → Personal Access Tokens**) and export it in your environment (tokens are never accepted on the CLI so they stay out of shell history):

```bash
export FIREFLY_TOKEN="your-personal-access-token"

actual-to-firefly \
  --actual-db /path/to/db.sqlite \
  --firefly-url https://firefly.example.com \
  --currency USD \
  --execute
```

> [!TIP]
> Test against a fresh disposable Firefly III instance or database snapshot first. If an import is interrupted midway, re-running the exact same `--execute` command skips already-imported accounts, categories, budgets, monthly limits, and transactions by `external_id`.

## Optional Payee Cleanup

### Automatic Dual-Role Payee Disambiguation (On by Default)

If a merchant appears in both withdrawals and deposits (for example, a store purchase plus a later refund), `actual-to-firefly` automatically appends ` (refund)` to the revenue counterparty so expense and revenue accounts stay unambiguous in Firefly III. Customize with `--refund-suffix` or disable with `--no-disambiguate-dual-payees`.

### Custom Payee Aliases & Counterparty Overrides

Normalize noisy bank descriptions or override specific payees with JSON files (see [`examples/`](examples/)):

```bash
actual-to-firefly \
  --actual-db /path/to/db.sqlite \
  --firefly-url https://firefly.example.com \
  --currency USD \
  --payee-aliases examples/payee-aliases.example.json \
  --counterparty-map examples/counterparty-map.example.json \
  --execute
```

## Verification & Exit Codes

After `--execute`, `actual-to-firefly` re-fetches all Firefly accounts and transactions and writes a full reconciliation block into `migration_report.json`:

- **`balance_validation`**: exact `Decimal` comparison of each Actual account balance against Firefly's `current_balance`.
- **`external_id_validation`**: confirms every planned `actual:<id>` and `actual-transfer:<id1>:<id2>` exists in Firefly.

Exit codes:
- `0`: dry-run succeeded, or `--execute` finished with `0.00` balance difference on every account and zero missing `external_id`s.
- `1`: API error, invalid split data, or post-import balance/ID mismatch.
- `2`: invalid CLI usage or missing `FIREFLY_TOKEN`.

## CLI Reference

| Flag | Default | Description |
| --- | --- | --- |
| `--actual-db` | `$ACTUAL_DB` | Path to unencrypted Actual `db.sqlite` |
| `--firefly-url` | `$FIREFLY_URL` / `http://localhost:8080` | Base URL of Firefly III |
| `--currency` | `$FIREFLY_CURRENCY` / `USD` | ISO 4217 currency code |
| `--token-env` | `FIREFLY_TOKEN` | Env var name holding the Firefly PAT |
| `--execute` | `false` | Write to Firefly III (omit for dry-run) |
| `--set-primary-currency` | `false` | Update Firefly default user group primary currency to `--currency` |
| `--payee-aliases` | none | JSON file normalizing raw payee variants |
| `--counterparty-map` | none | JSON file overriding expense/revenue counterparty names |
| `--no-disambiguate-dual-payees` | `false` | Disable automatic dual-role refund suffixing |
| `--refund-suffix` | ` (refund)` | Suffix for dual-role revenue counterparties |
| `--report` | `migration_report.json` | Output path for structured JSON report |
| `--sleep` | `0.0` | Optional delay (seconds) between API requests |

## Development

```bash
python3 -m unittest discover -s tests -v
uvx ruff check src tests
```

## License

[MIT](LICENSE)
