venta_csv.dart 3.2 KB

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