|
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
|
|
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
+import 'package:turquessa_app/views/venta/venta_csv.dart';
|
|
import '../../widgets/widgets_components.dart';
|
|
import '../../widgets/widgets_components.dart';
|
|
import '../../models/models.dart';
|
|
import '../../models/models.dart';
|
|
import '../../viewmodels/viewmodels.dart';
|
|
import '../../viewmodels/viewmodels.dart';
|
|
@@ -52,6 +53,15 @@ class _VentaScreenState extends State<VentaScreen> {
|
|
"Resumen de Pedidos por Periodo",
|
|
"Resumen de Pedidos por Periodo",
|
|
style: TextStyle(color: AppTheme.secondary),
|
|
style: TextStyle(color: AppTheme.secondary),
|
|
),
|
|
),
|
|
|
|
+ actions: <Widget>[
|
|
|
|
+ IconButton(
|
|
|
|
+ icon: const Icon(Icons.save_alt),
|
|
|
|
+ onPressed: () {
|
|
|
|
+ exportCSV(fechaInicialSeleccionada, fechaFinalSeleccionada);
|
|
|
|
+ },
|
|
|
|
+ tooltip: 'Exportar a CSV',
|
|
|
|
+ ),
|
|
|
|
+ ],
|
|
iconTheme: IconThemeData(color: AppTheme.secondary)),
|
|
iconTheme: IconThemeData(color: AppTheme.secondary)),
|
|
body: Padding(
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
padding: const EdgeInsets.all(16.0),
|
|
@@ -188,7 +198,7 @@ class _VentaScreenState extends State<VentaScreen> {
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
),
|
|
),
|
|
Text(
|
|
Text(
|
|
- "Propina: \$${_propinaPedido(pedido.id)}",
|
|
|
|
|
|
+ "Propina: \$${formatCurrency(_propinaPedido(pedido.id) ?? 0)}",
|
|
style: TextStyle(
|
|
style: TextStyle(
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
),
|
|
),
|
|
@@ -212,6 +222,16 @@ class _VentaScreenState extends State<VentaScreen> {
|
|
color: Colors.red),
|
|
color: Colors.red),
|
|
),
|
|
),
|
|
Text(
|
|
Text(
|
|
|
|
+ "Fecha/Hora: ${_formatDateTime(pedido.peticion)}",
|
|
|
|
+ style: TextStyle(
|
|
|
|
+ fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
+ ),
|
|
|
|
+ Text(
|
|
|
|
+ "Propina: \$${formatCurrency(_propinaPedido(pedido.id) ?? 0)}",
|
|
|
|
+ style: TextStyle(
|
|
|
|
+ fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
+ ),
|
|
|
|
+ Text(
|
|
"Total: \$${formatCurrency(pedido.totalPedido ?? 0)}",
|
|
"Total: \$${formatCurrency(pedido.totalPedido ?? 0)}",
|
|
style: TextStyle(
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontSize: 20,
|
|
@@ -405,6 +425,49 @@ class _VentaScreenState extends State<VentaScreen> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ void exportCSV(DateTime? fechaInicial, DateTime? fechaFinal) async {
|
|
|
|
+ final pedidosViewModel =
|
|
|
|
+ Provider.of<PedidoViewModel>(context, listen: false);
|
|
|
|
+ List<Pedido> pedidosConProductos = [];
|
|
|
|
+ List<Propinas> propinas = [];
|
|
|
|
+ List<Propinas> altPropinas = [];
|
|
|
|
+
|
|
|
|
+ for (Pedido pedido in pedidosViewModel.pedidos) {
|
|
|
|
+ Pedido? pedidoConProductos =
|
|
|
|
+ await pedidosViewModel.fetchPedidoConProductos(pedido.id);
|
|
|
|
+ if (pedidoConProductos != null) {
|
|
|
|
+ pedidosConProductos.add(pedidoConProductos);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (pedidosConProductos.isNotEmpty) {
|
|
|
|
+ String fileName = 'Ventas_Turquessa_POS';
|
|
|
|
+ if (fechaInicial != null && fechaFinal != null) {
|
|
|
|
+ String startDateStr = DateFormat('dd-MM-yyyy').format(fechaInicial!);
|
|
|
|
+ String endDateStr = DateFormat('dd-MM-yyyy').format(fechaFinal!);
|
|
|
|
+ fileName += '_${startDateStr}_al_${endDateStr}';
|
|
|
|
+ }
|
|
|
|
+ fileName += '.csv';
|
|
|
|
+
|
|
|
|
+ for (var pedido in pedidosConProductos) {
|
|
|
|
+ altPropinas =
|
|
|
|
+ await Provider.of<PropinaViewModel>(context, listen: false)
|
|
|
|
+ .obtenerPropinasPorPedido(pedido.id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (var alt in altPropinas) {
|
|
|
|
+ propinas.add(alt);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ await exportarVentasACSV(pedidosConProductos, propinas, fileName);
|
|
|
|
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
+ content: Text('Archivo CSV descargado! Archivo: $fileName')));
|
|
|
|
+ } else {
|
|
|
|
+ ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
+ SnackBar(content: Text('No hay pedidos para exportar.')));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
Future<void> imprimirResumenPedidos(List<Pedido> pedidos) async {
|
|
Future<void> imprimirResumenPedidos(List<Pedido> pedidos) async {
|
|
if (fechaInicialSeleccionada == null) {
|
|
if (fechaInicialSeleccionada == null) {
|
|
print("No se ha seleccionado una fecha inicial.");
|
|
print("No se ha seleccionado una fecha inicial.");
|