feat(plantare): reproduction-commitment data layer (schema v9)

The Plantare — a promise to reproduce seed and return some (data-model §2.7,
the seed-domain Pledge with return_kind=similar). NOT a sale. Local-first
v1: your honest ledger of commitments (from/to a person by name); the
bilateral signed cross-party form is a later social-layer phase.

- New Plantares table (SyncColumns): varietyId, direction (iReturn/owedToMe),
  counterparty, owedDescription, madeOn, dueBy, status (open/returned/
  forgiven), settledOn, note. schemaVersion 8 -> 9 with a guarded createTable
  migration; schema exported + migration-test helper regenerated.
- VarietyRepository: createPlantare / watchPlantares / watchPlantaresForVariety
  / setPlantareStatus / deletePlantare (soft-delete, CRDT-stamped).
- Included in backups + sync: InventorySnapshot + JSON codec (round-trips
  isDeleted) + exportInventory/exportForSync/importInventory, so commitments
  survive restore and replicate LWW like every other row.

Tests: v1..v8 -> v9 migration + fresh v9; repo create/list/settle/reopen/
delete; and a backup round-trip preserving a commitment. 13 green.

UI (detail section + Plantares screen) follows.
This commit is contained in:
vjrj 2026-07-11 02:01:25 +02:00
parent 0e41293de5
commit 81094f25a8
12 changed files with 5418 additions and 60 deletions

View file

@ -1707,6 +1707,80 @@ class VarietyRepository {
return id;
}
// --- Plantares: reproduction commitments (data-model §2.7) ----------------
/// Records a Plantare a promise to reproduce seed and return some (or a
/// promise made TO you). Returns the new commitment id.
Future<String> createPlantare({
required PlantareDirection direction,
String? varietyId,
String? counterparty,
String? owedDescription,
int? dueBy,
String? note,
}) async {
final (created, updated) = await _stamp();
final id = idGen.newId();
await _db.into(_db.plantares).insert(
PlantaresCompanion.insert(
id: id,
createdAt: created,
updatedAt: updated,
lastAuthor: nodeId,
direction: direction,
varietyId: Value(varietyId),
counterparty: Value(counterparty),
owedDescription: Value(owedDescription),
madeOn: created,
dueBy: Value(dueBy),
note: Value(note),
),
);
return id;
}
/// All live commitments, newest first (for the Plantares screen).
Stream<List<Plantare>> watchPlantares() => (_db.select(_db.plantares)
..where((p) => p.isDeleted.equals(false))
..orderBy([(p) => OrderingTerm.desc(p.madeOn)]))
.watch();
/// The commitments recorded against one variety.
Stream<List<Plantare>> watchPlantaresForVariety(String varietyId) =>
(_db.select(_db.plantares)
..where(
(p) => p.varietyId.equals(varietyId) & p.isDeleted.equals(false),
)
..orderBy([(p) => OrderingTerm.desc(p.madeOn)]))
.watch();
/// Moves a commitment to [status]; stamps the settle time for returned/
/// forgiven, clears it when reopened.
Future<void> setPlantareStatus(String id, PlantareStatus status) async {
final (now, updated) = await _stamp();
await (_db.update(_db.plantares)..where((p) => p.id.equals(id))).write(
PlantaresCompanion(
status: Value(status),
settledOn:
Value(status == PlantareStatus.open ? null : now),
updatedAt: Value(updated),
lastAuthor: Value(nodeId),
),
);
}
/// Soft-deletes a commitment (tombstone, so it syncs as a removal).
Future<void> deletePlantare(String id) async {
final (_, updated) = await _stamp();
await (_db.update(_db.plantares)..where((p) => p.id.equals(id))).write(
PlantaresCompanion(
isDeleted: const Value(true),
updatedAt: Value(updated),
lastAuthor: Value(nodeId),
),
);
}
/// Snapshots the live inventory (tombstones excluded) for the interchange
/// export data-model §7. Includes photo bytes; the JSON codec embeds them
/// as base64 and the CSV codec ignores them.
@ -1742,6 +1816,9 @@ class VarietyRepository {
attachments: await (_db.select(
_db.attachments,
)..where((a) => a.isDeleted.equals(false))).get(),
plantares: await (_db.select(
_db.plantares,
)..where((p) => p.isDeleted.equals(false))).get(),
);
}
@ -1764,6 +1841,7 @@ class VarietyRepository {
conditionChecks: await _db.select(_db.conditionChecks).get(),
movements: await _db.select(_db.movements).get(),
parties: await _db.select(_db.parties).get(),
plantares: await _db.select(_db.plantares).get(),
// attachments intentionally omitted photos don't ride the sync wire.
);
}
@ -1856,6 +1934,14 @@ class VarietyRepository {
updatedAtOf: (r) => r.updatedAt,
reconciler: reconciler,
);
summary += await _importMutableRows(
table: _db.plantares,
idColumn: _db.plantares.id,
rows: snapshot.plantares,
idOf: (r) => r.id,
updatedAtOf: (r) => r.updatedAt,
reconciler: reconciler,
);
summary += await _importMovements(snapshot.movements, reconciler);
});
@ -1868,6 +1954,7 @@ class VarietyRepository {
for (final c in snapshot.conditionChecks) c.updatedAt,
for (final p in snapshot.parties) p.updatedAt,
for (final a in snapshot.attachments) a.updatedAt,
for (final pl in snapshot.plantares) pl.updatedAt,
]);
if (maxIncoming != null) {
_clock = _clock.receiveEvent(maxIncoming, _now());