// Report (the web's /work/report): a range — this / last week, month, year, or any days — and // optionally one customer; the totals, the tree with hours / billable / money per node, and every // entry (tap = edit). "Copy CSV" puts the entries on the clipboard in the Pi's CSV columns. import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../app/sync_service.dart'; import '../app/theme.dart'; import 'entry_sheet.dart'; import 'work_model.dart'; class ReportPage extends StatefulWidget { const ReportPage({super.key, required this.sync, this.s = const WorkSettings()}); final SyncService sync; final WorkSettings s; @override State createState() => _ReportPageState(); } class _ReportPageState extends State { String preset = 'month'; late (DateTime, DateTime) range = presetRange('month')!; String? client; String _amounts(Agg a) => (a.amounts.entries.toList()..sort((x, y) => x.key.compareTo(y.key))).map((e) => money(e.value, e.key)).join(' · '); Future _custom() async { final p = await showDateRangePicker( context: context, firstDate: DateTime(2000), lastDate: DateTime(2100), initialDateRange: DateTimeRange(start: range.$1, end: range.$2), ); if (p != null) { setState(() { preset = 'custom'; range = (dayOf(p.start), dayOf(p.end)); }); } } @override Widget build(BuildContext context) => ListenableBuilder( listenable: widget.sync, builder: (context, _) { final store = widget.sync.store; final t = Tree(store, widget.s); final r = report(store, t, range.$1, range.$2, client: client); final clients = [ for (final (n, d) in t.walk(archived: true)) if (n.kind == 'client') (n, d), ]; return Scaffold( appBar: AppBar( title: const Text('Report'), actions: [ IconButton( tooltip: 'Copy as CSV', icon: const Icon(Icons.copy_all), onPressed: r.items.isEmpty ? null : () async { await Clipboard.setData(ClipboardData(text: reportCsv(r, t))); if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('${r.items.length} entries copied as CSV'))); } }, ), ], ), body: ListView( padding: const EdgeInsets.fromLTRB(12, 6, 12, 24), children: [ Wrap( spacing: 6, runSpacing: 4, children: [ for (final (k, label) in presets) ChoiceChip( label: Text(label), selected: preset == k, onSelected: (_) => setState(() { preset = k; range = presetRange(k)!; }), ), ChoiceChip( label: Text(preset == 'custom' ? '${dayLabel(range.$1)} – ${dayLabel(range.$2)}' : 'Days…'), selected: preset == 'custom', onSelected: (_) => _custom(), ), ], ), const SizedBox(height: 8), DropdownButtonFormField( initialValue: client, isExpanded: true, decoration: const InputDecoration(labelText: 'Customer', border: OutlineInputBorder(), isDense: true), items: [ const DropdownMenuItem(value: null, child: Text('Everyone')), for (final (n, d) in clients) DropdownMenuItem(value: n.id, child: Text('${' ' * d}${n.name}')), ], onChanged: (v) => setState(() => client = v), ), const SizedBox(height: 10), Card( child: Padding( padding: const EdgeInsets.all(14), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('${dayLabel(range.$1)} – ${dayLabel(range.$2)}', style: const TextStyle(color: muted)), const SizedBox(height: 4), Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text(hm(r.total.m), style: const TextStyle(fontSize: 26, fontWeight: FontWeight.bold)), const SizedBox(width: 8), Text('${(r.total.m / 60).toStringAsFixed(2)} h', style: const TextStyle(color: muted)), const Spacer(), Text(_amounts(r.total), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600)), ], ), Text.rich( TextSpan( children: [ TextSpan( text: '${hm(r.total.bm)} billable', style: const TextStyle(color: good), ), TextSpan( text: ' · ${hm(r.total.m - r.total.bm)} not billable', style: const TextStyle(color: muted), ), ], ), ), ], ), ), ), if (r.rows.isEmpty) const Padding( padding: EdgeInsets.all(24), child: Center( child: Text('Nothing booked in this range', style: TextStyle(color: muted)), ), ), if (r.rows.isNotEmpty) ...[ const SizedBox(height: 10), Card( child: Column( children: [ for (final row in r.rows) ListTile( dense: true, contentPadding: EdgeInsets.only(left: 12.0 + row.depth * 18, right: 12), leading: Icon(Icons.circle, size: 11, color: hexColor(t.colour(row.node.id))), minLeadingWidth: 10, title: Text(row.node.name, style: TextStyle(fontWeight: row.depth == 0 ? FontWeight.bold : null)), subtitle: Text( '${hm(row.agg.bm)} billable${row.agg.m > row.agg.bm ? ' · ${hm(row.agg.m - row.agg.bm)} not' : ''}', style: const TextStyle(color: muted, fontSize: 12), ), trailing: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text(hm(row.agg.m), style: const TextStyle(fontWeight: FontWeight.w600)), if (row.agg.amounts.isNotEmpty) Text(_amounts(row.agg), style: const TextStyle(fontSize: 12, color: muted)), ], ), onTap: row.node.kind == 'client' ? () => setState(() => client = row.node.id) : null, ), ], ), ), const SizedBox(height: 14), Text( 'Entries (${r.items.length})', style: const TextStyle(color: muted, fontWeight: FontWeight.w600), ), const SizedBox(height: 4), ..._entries(t, r), ], ], ), ); }, ); List _entries(Tree t, Report r) { final out = []; String? lastDay; for (final i in r.items) { final e = i.e, d = e.start.substring(0, 10); if (d != lastDay) { out.add( Padding( padding: const EdgeInsets.fromLTRB(4, 10, 4, 2), child: Text( dayLabel(dayOf(e.s)), style: const TextStyle(color: accent, fontWeight: FontWeight.w600), ), ), ); lastDay = d; } out.add( Card( margin: const EdgeInsets.only(bottom: 4), child: ListTile( dense: true, leading: Container(width: 4, height: 36, color: hexColor(t.colour(e.node))), minLeadingWidth: 4, title: Text(t.path(e.node)), subtitle: Text( ['${e.start.substring(11)}–${e.end.substring(11)}', if (e.note.isNotEmpty) e.note.split('\n').first].join(' · '), maxLines: 2, overflow: TextOverflow.ellipsis, style: const TextStyle(color: muted), ), trailing: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text(hm(e.minutes), style: const TextStyle(fontWeight: FontWeight.w600)), Text(e.billable ? money(i.amount, i.currency) : 'not billable', style: const TextStyle(fontSize: 12, color: muted)), ], ), onTap: () => openEntry(context, widget.sync, entry: e, s: widget.s), ), ), ); } return out; } }