orders_tab.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:ristoris/database/database.dart';
  6. import 'package:ristoris/providers/database_provider.dart';
  7. import 'package:ristoris/core/constants.dart';
  8. import 'package:ristoris/ui/widgets/order_actions.dart';
  9. String _statoLabel(String? stato) {
  10. switch (stato) {
  11. case AppConstants.kStatusNew:
  12. return 'Nuovo';
  13. case AppConstants.kStatusTransferred:
  14. return 'Trasmesso';
  15. case AppConstants.kStatoHold:
  16. return 'Sospeso';
  17. default:
  18. return stato ?? '';
  19. }
  20. }
  21. Color _statoColor(String? stato) {
  22. switch (stato) {
  23. case AppConstants.kStatusNew:
  24. return Colors.orange;
  25. case AppConstants.kStatusTransferred:
  26. return Colors.green;
  27. case AppConstants.kStatoHold:
  28. return Colors.blue;
  29. default:
  30. return Colors.grey;
  31. }
  32. }
  33. class _StatoPill extends StatelessWidget {
  34. final String? stato;
  35. const _StatoPill({this.stato});
  36. @override
  37. Widget build(BuildContext context) {
  38. final label = _statoLabel(stato);
  39. final color = _statoColor(stato);
  40. return Container(
  41. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
  42. decoration: BoxDecoration(
  43. color: color.withAlpha(25),
  44. borderRadius: BorderRadius.circular(4),
  45. ),
  46. child: Text(
  47. label,
  48. textAlign: TextAlign.center,
  49. style: TextStyle(
  50. fontSize: 12,
  51. height: 16 / 12,
  52. fontWeight: FontWeight.w500,
  53. color: color,
  54. ),
  55. ),
  56. );
  57. }
  58. }
  59. class OrdersTab extends ConsumerWidget {
  60. final Cliente cliente;
  61. final ThemeData theme;
  62. const OrdersTab({required this.cliente, required this.theme, super.key});
  63. @override
  64. Widget build(BuildContext context, WidgetRef ref) {
  65. final ordiniDao = ref.read(ordiniDaoProvider);
  66. final isPhone = MediaQuery.of(context).size.width < 600;
  67. Future<void> deleteOrder(VOrdiniData o) async {
  68. final dao = ref.read(ordiniDaoProvider);
  69. await dao.zapOrderLines(o.idOrdine);
  70. await dao.deleteOrder(o.idOrdine);
  71. if (context.mounted) context.go('/dashboard/customers');
  72. }
  73. final blocco = cliente.codBlocco;
  74. final isBloccato = blocco == '001';
  75. final hasWarning =
  76. blocco != null && blocco.isNotEmpty && blocco != '001';
  77. return Scaffold(
  78. body: SafeArea(
  79. child: FutureBuilder(
  80. future: ordiniDao.customerOrdersWidthData(cliente.idCliente, false),
  81. builder: (_, snap) {
  82. if (!snap.hasData) {
  83. return const Center(child: CircularProgressIndicator());
  84. }
  85. final orders = snap.data!;
  86. if (orders.isEmpty) {
  87. return const Center(child: Text('Nessun ordine'));
  88. }
  89. if (isPhone) {
  90. return ListView.builder(
  91. padding: const EdgeInsets.all(16),
  92. itemCount: orders.length,
  93. itemBuilder: (_, i) => _OrderCard(
  94. order: orders[i],
  95. onEdit: () =>
  96. context.push('/dashboard/orders/${orders[i].idOrdine}'),
  97. onDelete: () => deleteOrder(orders[i]),
  98. ),
  99. );
  100. }
  101. return _OrderTable(
  102. orders: orders,
  103. onEdit: (o) => context.push('/dashboard/orders/${o.idOrdine}'),
  104. onDelete: deleteOrder,
  105. );
  106. },
  107. ),
  108. ),
  109. floatingActionButton: isBloccato
  110. ? null
  111. : FloatingActionButton(
  112. heroTag: 'addOrder',
  113. backgroundColor: hasWarning ? Colors.amber : null,
  114. onPressed: () {
  115. if (hasWarning) {
  116. ScaffoldMessenger.of(context).showSnackBar(
  117. SnackBar(
  118. content: Text('Attenzione: cliente con blocco $blocco'),
  119. backgroundColor: Colors.amber.shade800,
  120. ),
  121. );
  122. }
  123. context.push('/dashboard/orders/new?cliente=${cliente.idCliente}');
  124. },
  125. child: hasWarning
  126. ? const Icon(Icons.warning_amber_rounded)
  127. : const Icon(Icons.add),
  128. ),
  129. );
  130. }
  131. }
  132. class _OrderCard extends StatelessWidget {
  133. final VOrdiniData order;
  134. final VoidCallback onEdit;
  135. final Future<void> Function() onDelete;
  136. const _OrderCard({
  137. required this.order,
  138. required this.onEdit,
  139. required this.onDelete,
  140. });
  141. @override
  142. Widget build(BuildContext context) {
  143. final theme = Theme.of(context);
  144. final cs = theme.colorScheme;
  145. final dateFormat = DateFormat('dd/MM/yyyy');
  146. final currencyFormat = NumberFormat.currency(
  147. symbol: '€ ',
  148. decimalDigits: 2,
  149. );
  150. return Card(
  151. margin: const EdgeInsets.only(bottom: 12),
  152. elevation: 0,
  153. shape: RoundedRectangleBorder(
  154. borderRadius: BorderRadius.circular(12),
  155. side: BorderSide(color: cs.outlineVariant),
  156. ),
  157. child: InkWell(
  158. onTap: onEdit,
  159. borderRadius: BorderRadius.circular(12),
  160. child: Padding(
  161. padding: const EdgeInsets.all(16),
  162. child: Column(
  163. crossAxisAlignment: CrossAxisAlignment.start,
  164. children: [
  165. Row(
  166. children: [
  167. Expanded(
  168. child: Text(
  169. '#${order.numeroOrdine ?? ''}',
  170. style: theme.textTheme.titleMedium?.copyWith(
  171. fontWeight: FontWeight.bold,
  172. ),
  173. ),
  174. ),
  175. Text(
  176. order.dataOrdine != null ? dateFormat.format(DateTime.parse(order.dataOrdine!)) : '',
  177. style: theme.textTheme.bodyMedium?.copyWith(
  178. color: cs.onSurfaceVariant,
  179. ),
  180. ),
  181. const SizedBox(width: 8),
  182. _StatoPill(stato: order.stato),
  183. ],
  184. ),
  185. const SizedBox(height: 8),
  186. Text(
  187. '${order.filiale ?? ''}${order.indirizzo ?? ''} ${order.citta ?? ''}${order.provincia != null ? ' (${order.provincia})' : ''}',
  188. style: theme.textTheme.bodySmall?.copyWith(
  189. color: cs.onSurfaceVariant,
  190. ),
  191. maxLines: 2,
  192. overflow: TextOverflow.ellipsis,
  193. ),
  194. const SizedBox(height: 12),
  195. Center(
  196. child: Text(
  197. currencyFormat.format(order.totale ?? 0),
  198. style: theme.textTheme.titleLarge?.copyWith(
  199. fontWeight: FontWeight.bold,
  200. ),
  201. ),
  202. ),
  203. const SizedBox(height: 8),
  204. Align(
  205. alignment: Alignment.centerRight,
  206. child: OrderActions(
  207. order: order,
  208. onEdit: onEdit,
  209. onDelete: onDelete,
  210. ),
  211. ),
  212. ],
  213. ),
  214. ),
  215. ),
  216. );
  217. }
  218. }
  219. class _OrderTable extends StatelessWidget {
  220. final List<VOrdiniData> orders;
  221. final void Function(VOrdiniData) onEdit;
  222. final Future<void> Function(VOrdiniData) onDelete;
  223. const _OrderTable({
  224. required this.orders,
  225. required this.onEdit,
  226. required this.onDelete,
  227. });
  228. @override
  229. Widget build(BuildContext context) {
  230. final theme = Theme.of(context);
  231. final cs = theme.colorScheme;
  232. final dateFormat = DateFormat('dd/MM/yyyy');
  233. final currencyFormat = NumberFormat.currency(
  234. symbol: '€ ',
  235. decimalDigits: 2,
  236. );
  237. return LayoutBuilder(
  238. builder: (context, constraints) {
  239. final isCompact = constraints.maxWidth < 1024;
  240. return SingleChildScrollView(
  241. scrollDirection: Axis.horizontal,
  242. child: ConstrainedBox(
  243. constraints: BoxConstraints(
  244. minWidth: constraints.maxWidth,
  245. maxWidth: constraints.maxWidth,
  246. ),
  247. child: Column(
  248. children: [
  249. Container(
  250. padding: const EdgeInsets.symmetric(
  251. horizontal: 24,
  252. vertical: 14,
  253. ),
  254. decoration: BoxDecoration(
  255. color: cs.surfaceContainerHighest.withAlpha(50),
  256. border: Border(
  257. bottom: BorderSide(color: cs.outlineVariant),
  258. ),
  259. ),
  260. child: Row(
  261. children: isCompact
  262. ? [
  263. _headerCell(context, 'Ordine', flex: 3),
  264. _headerCell(context, 'Destinazione', flex: 5),
  265. _headerCell(context, 'Totale', flex: 2),
  266. _headerCell(context, 'Stato', flex: 2),
  267. _headerCell(context, 'Azioni', flex: 2),
  268. ]
  269. : [
  270. _headerCell(context, 'N. Ordine', flex: 1),
  271. _headerCell(context, 'Data', flex: 2),
  272. _headerCell(context, 'Destinazione', flex: 5),
  273. _headerCell(context, 'Totale', flex: 2),
  274. _headerCell(context, 'Stato', flex: 1),
  275. _headerCell(context, 'Azioni', flex: 2),
  276. ],
  277. ),
  278. ),
  279. Expanded(
  280. child: SingleChildScrollView(
  281. scrollDirection: Axis.vertical,
  282. child: Column(
  283. crossAxisAlignment: CrossAxisAlignment.stretch,
  284. children: orders.map((o) {
  285. return Container(
  286. padding: const EdgeInsets.symmetric(
  287. horizontal: 24,
  288. vertical: 16,
  289. ),
  290. decoration: BoxDecoration(
  291. border: Border(
  292. bottom: BorderSide(
  293. color: cs.outlineVariant.withAlpha(80),
  294. ),
  295. ),
  296. ),
  297. child: InkWell(
  298. onTap: () => onEdit(o),
  299. child: Row(
  300. children: isCompact
  301. ? [
  302. _cell(
  303. Column(
  304. mainAxisAlignment: MainAxisAlignment.center,
  305. crossAxisAlignment: CrossAxisAlignment.start,
  306. children: [
  307. Text(
  308. '${o.numeroOrdine ?? ''}',
  309. style: theme.textTheme.bodyMedium?.copyWith(
  310. fontWeight: FontWeight.bold,
  311. fontFamily: 'monospace',
  312. ),
  313. ),
  314. const SizedBox(height: 4),
  315. Text(
  316. o.dataOrdine != null
  317. ? dateFormat.format(
  318. DateTime.parse(
  319. o.dataOrdine!,
  320. ),
  321. )
  322. : '',
  323. style: theme.textTheme.bodySmall?.copyWith(
  324. color: cs.onSurfaceVariant,
  325. ),
  326. ),
  327. ],
  328. ),
  329. flex: 3,
  330. ),
  331. _cell(
  332. Text(
  333. '${o.filiale ?? ''} ${o.indirizzo ?? ''} ${o.citta ?? ''}${o.provincia != null ? ' (${o.provincia})' : ''}',
  334. style: theme.textTheme.bodyMedium?.copyWith(
  335. color: cs.onSurfaceVariant,
  336. ),
  337. maxLines: 2,
  338. overflow: TextOverflow.ellipsis,
  339. ),
  340. flex: 5,
  341. ),
  342. _cell(
  343. Padding(
  344. padding: const EdgeInsets.only(
  345. right: 16,
  346. ),
  347. child: Text(
  348. currencyFormat.format(
  349. o.totale ?? 0,
  350. ),
  351. style: theme.textTheme.bodyMedium?.copyWith(
  352. fontWeight: FontWeight.bold,
  353. ),
  354. textAlign: TextAlign.right,
  355. ),
  356. ),
  357. flex: 2,
  358. ),
  359. _cell(
  360. _StatoPill(stato: o.stato),
  361. flex: 2,
  362. ),
  363. _cell(
  364. OrderActions(
  365. order: o,
  366. onEdit: () => onEdit(o),
  367. onDelete: () => onDelete(o),
  368. ),
  369. flex: 2,
  370. ),
  371. ]
  372. : [
  373. _cell(
  374. Text(
  375. '${o.numeroOrdine ?? ''}',
  376. style: theme.textTheme.bodyMedium?.copyWith(
  377. fontWeight: FontWeight.bold,
  378. fontFamily: 'monospace',
  379. ),
  380. ),
  381. flex: 1,
  382. ),
  383. _cell(
  384. Text(
  385. o.dataOrdine != null
  386. ? dateFormat.format(
  387. DateTime.parse(
  388. o.dataOrdine!,
  389. ),
  390. )
  391. : '',
  392. style: theme.textTheme.bodyMedium?.copyWith(
  393. color: cs.onSurfaceVariant,
  394. ),
  395. ),
  396. flex: 2,
  397. ),
  398. _cell(
  399. Text(
  400. '${o.filiale ?? ''} ${o.indirizzo ?? ''} ${o.citta ?? ''}${o.provincia != null ? ' (${o.provincia})' : ''}',
  401. style: theme.textTheme.bodyMedium?.copyWith(
  402. color: cs.onSurfaceVariant,
  403. ),
  404. maxLines: 2,
  405. overflow: TextOverflow.ellipsis,
  406. ),
  407. flex: 5,
  408. ),
  409. _cell(
  410. Padding(
  411. padding: const EdgeInsets.only(
  412. right: 16,
  413. ),
  414. child: Text(
  415. currencyFormat.format(
  416. o.totale ?? 0,
  417. ),
  418. style: theme.textTheme.bodyMedium?.copyWith(
  419. fontWeight: FontWeight.bold,
  420. ),
  421. textAlign: TextAlign.right,
  422. ),
  423. ),
  424. flex: 2,
  425. ),
  426. _cell(
  427. _StatoPill(stato: o.stato),
  428. flex: 1,
  429. ),
  430. _cell(
  431. OrderActions(
  432. order: o,
  433. onEdit: () => onEdit(o),
  434. onDelete: () => onDelete(o),
  435. ),
  436. flex: 2,
  437. ),
  438. ],
  439. ),
  440. ),
  441. );
  442. }).toList(),
  443. ),
  444. ),
  445. ),
  446. ],
  447. ),
  448. ),
  449. );
  450. },
  451. );
  452. }
  453. Widget _headerCell(BuildContext context, String label, {int flex = 1}) {
  454. return Expanded(
  455. flex: flex,
  456. child: Text(
  457. label,
  458. style: TextStyle(
  459. fontSize: 12,
  460. fontWeight: FontWeight.w600,
  461. color: Theme.of(context).colorScheme.onSurfaceVariant,
  462. ),
  463. ),
  464. );
  465. }
  466. Widget _cell(Widget child, {int flex = 1}) {
  467. return Expanded(flex: flex, child: child);
  468. }
  469. }