pedido_csv.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'dart:convert';
  2. import 'package:csv/csv.dart';
  3. import 'package:intl/intl.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'dart:io';
  6. import 'package:path/path.dart' as p;
  7. import '../../models/models.dart';
  8. Future<void> exportarPedidosACSV(List<Pedido> pedidos, String fileName) async {
  9. List<List<dynamic>> rows = [
  10. [
  11. "Folio",
  12. "Cliente",
  13. "Producto",
  14. "Cantidad",
  15. "Precio Unitario",
  16. "Toppings",
  17. "Descuento (%)",
  18. "Estado",
  19. "Fecha",
  20. "Total con Descuento",
  21. "Tipo de Pago",
  22. "Cantidad Efectivo",
  23. "Cantidad Tarjeta",
  24. "Cantidad Transferencia"
  25. ]
  26. ];
  27. for (var pedido in pedidos) {
  28. for (var producto in pedido.productos) {
  29. // Convertir toppings a una cadena de texto y calcular el total adicional de los toppings
  30. double totalToppingsPrecio = 0.0;
  31. String toppingsText = producto.toppings.isNotEmpty
  32. ? producto.toppings.map((t) {
  33. String toppingNombre =
  34. t.topping?.nombre ?? 'Topping no especificado';
  35. double toppingPrecio =
  36. double.tryParse(t.topping?.precio ?? '0') ?? 0.0;
  37. if (toppingPrecio > 0) {
  38. toppingNombre += "(+\$${formatCurrency(toppingPrecio)})";
  39. totalToppingsPrecio += toppingPrecio;
  40. }
  41. return toppingNombre;
  42. }).join(', ')
  43. : 'Sin toppings';
  44. // Calcular el total con descuento para este producto
  45. double precioUnitario =
  46. double.tryParse(producto.producto?.precio ?? '0') ?? 0.0;
  47. double subtotal =
  48. (precioUnitario + totalToppingsPrecio) * (producto.cantidad ?? 1);
  49. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  50. double precioDescuento = subtotal * (descuento / 100);
  51. double totalConDescuento = subtotal - precioDescuento;
  52. List<dynamic> row = [
  53. pedido.folio,
  54. pedido.nombreCliente,
  55. producto.producto?.nombre ?? 'No especificado',
  56. producto.cantidad,
  57. formatCurrency(precioUnitario),
  58. toppingsText,
  59. descuento,
  60. pedido.estatus,
  61. pedido.peticion ?? '',
  62. formatCurrency(totalConDescuento),
  63. pedido.tipoPago ?? 'No especificado',
  64. formatCurrency(pedido.cantEfectivo ?? 0.0),
  65. formatCurrency(pedido.cantTarjeta ?? 0.0),
  66. formatCurrency(pedido.cantTransferencia ?? 0.0),
  67. ];
  68. rows.add(row);
  69. }
  70. }
  71. String csv = const ListToCsvConverter().convert(rows);
  72. final directory = await getApplicationDocumentsDirectory();
  73. final path = p.join(directory.path, fileName);
  74. final file = File(path);
  75. final utf8Csv = utf8.encode('\uFEFF' + csv);
  76. await file.writeAsBytes(utf8Csv, flush: true);
  77. print('Archivo CSV guardado en $path');
  78. }
  79. String formatCurrency(double amount) {
  80. final format = NumberFormat("#,##0.00", "es_MX");
  81. return format.format(amount);
  82. }