feat(sales): local sales/purchase ledger (any currency)
A sale is a distinct model from a gift or a Plantare (reproduction
commitment): a recorded seed sale or purchase with an optional price in
ANY currency — €, Ğ1, time, or none yet. Mirrors the Plantare feature.
- schema v10: Sales table (SyncColumns), guarded createTable migration,
schema dump + generated schema_v10 for the migration round-trip test
- enum SaleDirection { iSold, iBought }
- VarietyRepository: create/watch/watchForVariety/delete + backup
export/exportForSync/import (LWW-by-HLC, tombstones)
- InventorySnapshot.sales + JSON codec encode/decode (round-trips amount,
currency, counterparty)
- UI: SalesScreen (/sales), sale sheet, drawer entry, variety-detail
action beside 'add Plantare'; money hides a trailing .0
- i18n sale block + menu.sales in en/es/pt/ast
- tests: sales repo test (5) incl. Ğ1/price-less/backup round-trip;
Sales screen added to the small-screen overflow guard
Also harden the overflow guard: swallow the headless engine's image
resource service PNG-decode errors (unrelated to layout) while still
failing on real RenderFlex overflows, so home/about stop reporting
false failures.
This commit is contained in:
parent
de6938d5d7
commit
6de039d518
27 changed files with 6156 additions and 23 deletions
|
|
@ -44,6 +44,15 @@ class AppDrawer extends StatelessWidget {
|
|||
context.push('/plantares');
|
||||
},
|
||||
),
|
||||
// Local sales ledger — also offline.
|
||||
_DrawerItem(
|
||||
icon: const Icon(Icons.sell_outlined),
|
||||
label: t.menu.sales,
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
context.push('/sales');
|
||||
},
|
||||
),
|
||||
_DrawerItem(
|
||||
icon: const Icon(Symbols.storefront),
|
||||
label: t.menu.market,
|
||||
|
|
|
|||
165
apps/app_seeds/lib/ui/sale_sheet.dart
Normal file
165
apps/app_seeds/lib/ui/sale_sheet.dart
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../data/variety_repository.dart';
|
||||
import '../db/enums.dart';
|
||||
import '../i18n/strings.g.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
/// Opens the "record a sale" sheet. Optionally pre-attached to [varietyId]
|
||||
/// (when opened from a variety's detail).
|
||||
Future<void> showSaleSheet(
|
||||
BuildContext context, {
|
||||
required VarietyRepository repository,
|
||||
String? varietyId,
|
||||
}) {
|
||||
return showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => _SaleSheet(repository: repository, varietyId: varietyId),
|
||||
);
|
||||
}
|
||||
|
||||
class _SaleSheet extends StatefulWidget {
|
||||
const _SaleSheet({required this.repository, this.varietyId});
|
||||
|
||||
final VarietyRepository repository;
|
||||
final String? varietyId;
|
||||
|
||||
@override
|
||||
State<_SaleSheet> createState() => _SaleSheetState();
|
||||
}
|
||||
|
||||
class _SaleSheetState extends State<_SaleSheet> {
|
||||
SaleDirection _direction = SaleDirection.iSold;
|
||||
final _counterparty = TextEditingController();
|
||||
final _amount = TextEditingController();
|
||||
final _currency = TextEditingController();
|
||||
final _note = TextEditingController();
|
||||
bool _saving = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_counterparty.dispose();
|
||||
_amount.dispose();
|
||||
_currency.dispose();
|
||||
_note.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String? _nullIfBlank(String s) => s.trim().isEmpty ? null : s.trim();
|
||||
|
||||
Future<void> _save() async {
|
||||
setState(() => _saving = true);
|
||||
await widget.repository.createSale(
|
||||
direction: _direction,
|
||||
varietyId: widget.varietyId,
|
||||
counterparty: _nullIfBlank(_counterparty.text),
|
||||
amount: double.tryParse(_amount.text.trim().replaceAll(',', '.')),
|
||||
currency: _nullIfBlank(_currency.text),
|
||||
note: _nullIfBlank(_note.text),
|
||||
);
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final t = context.t;
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: 16,
|
||||
right: 16,
|
||||
top: 16,
|
||||
bottom: MediaQuery.of(context).viewInsets.bottom + 16,
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(t.sale.add, style: Theme.of(context).textTheme.titleLarge),
|
||||
const SizedBox(height: 4),
|
||||
Text(t.sale.help,
|
||||
style: const TextStyle(color: seedMuted, fontSize: 13)),
|
||||
const SizedBox(height: 16),
|
||||
Text(t.sale.direction,
|
||||
style: Theme.of(context).textTheme.labelLarge),
|
||||
const SizedBox(height: 6),
|
||||
for (final d in SaleDirection.values)
|
||||
ListTile(
|
||||
key: Key('sale.direction.${d.name}'),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(
|
||||
_direction == d
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: _direction == d ? seedGreen : seedMuted,
|
||||
),
|
||||
title: Text(
|
||||
d == SaleDirection.iSold ? t.sale.iSold : t.sale.iBought),
|
||||
onTap: () => setState(() => _direction = d),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
key: const Key('sale.counterparty'),
|
||||
controller: _counterparty,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: InputDecoration(
|
||||
labelText: t.sale.counterparty,
|
||||
helperText: t.sale.counterpartyHint,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
key: const Key('sale.amount'),
|
||||
controller: _amount,
|
||||
keyboardType: const TextInputType.numberWithOptions(
|
||||
decimal: true),
|
||||
decoration: InputDecoration(
|
||||
labelText: t.sale.amount,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
key: const Key('sale.currency'),
|
||||
controller: _currency,
|
||||
decoration: InputDecoration(
|
||||
labelText: t.sale.currency,
|
||||
helperText: t.sale.currencyHint,
|
||||
helperMaxLines: 2,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
key: const Key('sale.note'),
|
||||
controller: _note,
|
||||
minLines: 1,
|
||||
maxLines: 3,
|
||||
decoration: InputDecoration(
|
||||
labelText: t.sale.note,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
FilledButton(
|
||||
key: const Key('sale.save'),
|
||||
onPressed: _saving ? null : _save,
|
||||
child: Text(t.sale.save),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
111
apps/app_seeds/lib/ui/sales_screen.dart
Normal file
111
apps/app_seeds/lib/ui/sales_screen.dart
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../data/variety_repository.dart';
|
||||
import '../db/database.dart';
|
||||
import '../db/enums.dart';
|
||||
import '../i18n/strings.g.dart';
|
||||
import 'sale_sheet.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
/// The Sales screen — a local ledger of seed sold or bought, in any currency.
|
||||
/// A model separate from a gift or a Plantare. Reads the encrypted inventory DB.
|
||||
class SalesScreen extends StatelessWidget {
|
||||
const SalesScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final t = context.t;
|
||||
final repo = context.read<VarietyRepository>();
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(t.sale.title)),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
key: const Key('sales.add'),
|
||||
onPressed: () => showSaleSheet(context, repository: repo),
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(t.sale.add),
|
||||
),
|
||||
body: StreamBuilder<List<Sale>>(
|
||||
stream: repo.watchSales(),
|
||||
builder: (context, snapshot) {
|
||||
final sales = snapshot.data;
|
||||
if (sales == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (sales.isEmpty) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Text(
|
||||
t.sale.empty,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: seedMuted, fontSize: 15),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.only(bottom: 88),
|
||||
itemCount: sales.length,
|
||||
separatorBuilder: (_, _) => const Divider(height: 1),
|
||||
itemBuilder: (context, i) => _SaleTile(sales[i], repo),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SaleTile extends StatelessWidget {
|
||||
const _SaleTile(this.s, this.repo);
|
||||
|
||||
final Sale s;
|
||||
final VarietyRepository repo;
|
||||
|
||||
String _money() {
|
||||
final amount = s.amount;
|
||||
if (amount == null) return s.currency?.trim() ?? '';
|
||||
// Drop a trailing .0 so "5.0 €" reads "5 €".
|
||||
final n = amount == amount.roundToDouble()
|
||||
? amount.toInt().toString()
|
||||
: amount.toString();
|
||||
return [n, s.currency?.trim()].where((e) => e != null && e.isNotEmpty).join(' ');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final t = context.t;
|
||||
final iSold = s.direction == SaleDirection.iSold;
|
||||
final money = _money();
|
||||
final date = MaterialLocalizations.of(context)
|
||||
.formatShortDate(DateTime.fromMillisecondsSinceEpoch(s.soldOn));
|
||||
final subtitle = [
|
||||
iSold ? t.sale.iSold : t.sale.iBought,
|
||||
if (s.counterparty?.trim().isNotEmpty ?? false) s.counterparty!.trim(),
|
||||
date,
|
||||
].join(' · ');
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
iSold ? Icons.sell_outlined : Icons.shopping_bag_outlined,
|
||||
color: seedGreen,
|
||||
),
|
||||
title: Text(
|
||||
money.isEmpty
|
||||
? (s.note?.trim().isNotEmpty ?? false
|
||||
? s.note!.trim()
|
||||
: (iSold ? t.sale.iSold : t.sale.iBought))
|
||||
: money,
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
subtitle: Text(subtitle, maxLines: 2, overflow: TextOverflow.ellipsis),
|
||||
trailing: PopupMenuButton<String>(
|
||||
key: Key('sale.menu.${s.id}'),
|
||||
onSelected: (v) {
|
||||
if (v == 'delete') repo.deleteSale(s.id);
|
||||
},
|
||||
itemBuilder: (context) =>
|
||||
[PopupMenuItem(value: 'delete', child: Text(t.sale.delete))],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ import 'harvest_date_picker.dart';
|
|||
import 'photo_pick.dart';
|
||||
import 'plantare_sheet.dart';
|
||||
import 'quantity_kind_l10n.dart';
|
||||
import 'sale_sheet.dart';
|
||||
import 'quantity_picker.dart';
|
||||
import 'seed_glyph.dart';
|
||||
import 'theme.dart';
|
||||
|
|
@ -110,19 +111,31 @@ class _DetailView extends StatelessWidget {
|
|||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_SectionTitle(t.plantare.title),
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: OutlinedButton.icon(
|
||||
key: const Key('detail.addPlantare'),
|
||||
icon: const Icon(Icons.volunteer_activism_outlined, size: 18),
|
||||
onPressed: () => showPlantareSheet(
|
||||
context,
|
||||
repository: context.read<VarietyRepository>(),
|
||||
varietyId: detail.id,
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
key: const Key('detail.addPlantare'),
|
||||
icon: const Icon(Icons.volunteer_activism_outlined, size: 18),
|
||||
onPressed: () => showPlantareSheet(
|
||||
context,
|
||||
repository: context.read<VarietyRepository>(),
|
||||
varietyId: detail.id,
|
||||
),
|
||||
label: Text(t.plantare.add),
|
||||
),
|
||||
label: Text(t.plantare.add),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
key: const Key('detail.addSale'),
|
||||
icon: const Icon(Icons.sell_outlined, size: 18),
|
||||
onPressed: () => showSaleSheet(
|
||||
context,
|
||||
repository: context.read<VarietyRepository>(),
|
||||
varietyId: detail.id,
|
||||
),
|
||||
label: Text(t.sale.add),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (detail.notes != null && detail.notes!.trim().isNotEmpty) ...[
|
||||
const SizedBox(height: 16),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue