123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- import 'package:flutter/material.dart';
- import 'package:intl/intl.dart';
- import 'package:omni_datetime_picker/omni_datetime_picker.dart';
- import 'package:provider/provider.dart';
- import '../../widgets/widgets_components.dart';
- import '../../models/models.dart';
- import '../../viewmodels/viewmodels.dart';
- import 'venta_ticket.dart';
- import '../../widgets/widgets.dart';
- import '../../themes/themes.dart';
- class VentaScreen extends StatefulWidget {
- @override
- _VentaScreenState createState() => _VentaScreenState();
- }
- class _VentaScreenState extends State<VentaScreen> {
- DateTime? fechaSeleccionada;
- List<Pedido> pedidosNoCancelados = [];
- List<Pedido> pedidosCancelados = [];
- final _busqueda = TextEditingController(text: '');
- double totalDelDia = 0.0;
- double totalCancelados = 0.0;
- double totalEfectivoDelDia = 0.0;
- double totalTarjetaDelDia = 0.0;
- double totalTransferenciaDelDia = 0.0;
- String formatCurrency(double amount) {
- final format = NumberFormat("#,##0.00", "es_MX");
- return format.format(amount);
- }
- void clearSearchAndReset() {
- setState(() {
- _busqueda.clear();
- fechaSeleccionada = null;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(
- "Resumen de Pedidos por Día",
- style: TextStyle(color: AppTheme.secondary),
- ),
- iconTheme: IconThemeData(color: AppTheme.secondary)),
- body: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- children: [
- Row(
- children: [
- Expanded(
- flex: 3,
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 0.0),
- child: tarjeta(
- ListTile(
- title: Text(
- "Fecha",
- style: TextStyle(
- color: AppTheme.quaternary,
- fontWeight: FontWeight.bold),
- ),
- subtitle: Text(
- fechaSeleccionada == null
- ? ""
- : DateFormat("dd/MM/yyyy")
- .format(fechaSeleccionada!),
- style: TextStyle(
- color: AppTheme.quaternary,
- fontWeight: FontWeight.bold),
- ),
- trailing: Icon(Icons.calendar_month,
- color: AppTheme.quaternary),
- onTap: () async {
- DateTime? d = await showDatetimePicker(
- context, fechaSeleccionada,
- inicia: fechaSeleccionada,
- tipo: OmniDateTimePickerType.date,
- solofecha: true);
- if (d == null) return;
- setState(() {
- fechaSeleccionada = d;
- });
- cargarPedidos(fechaSeleccionada!);
- }),
- color: AppTheme.tertiary,
- ),
- )),
- const SizedBox(
- width: 500,
- ),
- Expanded(
- flex: 2,
- child: Padding(
- padding: const EdgeInsets.only(top: 0),
- child: ElevatedButton(
- onPressed: clearSearchAndReset,
- style: ElevatedButton.styleFrom(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(20.0),
- ),
- primary: AppTheme.tertiary,
- padding: const EdgeInsets.symmetric(vertical: 25),
- ),
- child: Text('Limpiar',
- style: TextStyle(
- color: AppTheme.quaternary, fontSize: 18)),
- ),
- ),
- ),
- ],
- ),
- Expanded(
- child: tarjeta(
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: ListView.builder(
- itemCount:
- pedidosNoCancelados.length + pedidosCancelados.length,
- itemBuilder: (context, index) {
- if (index < pedidosNoCancelados.length) {
- final pedido = pedidosNoCancelados[index];
- return Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- "Folio: ${pedido.folio}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.w500),
- ),
- Text(
- "Total: \$${formatCurrency(pedido.totalPedido ?? 0)}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.w500)),
- ],
- );
- } else {
- final pedido =
- pedidosCancelados[index - pedidosNoCancelados.length];
- return Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- "Folio: ${pedido.folio} (Cancelado)",
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.w500,
- color: Colors.red),
- ),
- Text(
- "Total: \$${formatCurrency(pedido.totalPedido ?? 0)}",
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.w500,
- color: Colors.red)),
- ],
- );
- }
- },
- ),
- ),
- )),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- ElevatedButton.icon(
- icon: Icon(
- Icons.receipt_long_outlined,
- color: AppTheme.quaternary,
- size: 30,
- ),
- onPressed: pedidosNoCancelados.isNotEmpty ||
- pedidosCancelados.isNotEmpty
- ? () => imprimirResumenPedidos(
- pedidosNoCancelados + pedidosCancelados)
- : null,
- label: Text(
- "Imprimir Resumen",
- style: TextStyle(color: AppTheme.quaternary, fontSize: 18),
- ),
- style: ButtonStyle(
- backgroundColor:
- MaterialStatePropertyAll(AppTheme.tertiary),
- padding: MaterialStatePropertyAll(
- EdgeInsets.fromLTRB(30, 20, 30, 20))),
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Text(
- "Total del día: \$${formatCurrency(totalDelDia)}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.bold),
- ),
- ),
- if (totalEfectivoDelDia > 0)
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Text(
- "Total en Efectivo: \$${formatCurrency(totalEfectivoDelDia)}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.bold),
- ),
- ),
- if (totalTarjetaDelDia > 0)
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Text(
- "Total en Tarjeta: \$${formatCurrency(totalTarjetaDelDia)}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.bold),
- ),
- ),
- if (totalTransferenciaDelDia > 0)
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Text(
- "Total en Transferencia: \$${formatCurrency(totalTransferenciaDelDia)}",
- style: TextStyle(
- fontSize: 20, fontWeight: FontWeight.bold),
- ),
- ),
- if (totalCancelados > 0)
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Text(
- "Total cancelados: \$${formatCurrency(totalCancelados)}",
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.bold,
- color: Colors.red),
- ),
- ),
- ],
- ),
- ],
- )
- ],
- ),
- ),
- );
- }
- void cargarPedidos(DateTime fecha) async {
- final inicioDelDia = DateTime(fecha.year, fecha.month, fecha.day);
- final finDelDia = DateTime(fecha.year, fecha.month, fecha.day, 23, 59, 59);
- final pedidos = await Provider.of<PedidoViewModel>(context, listen: false)
- .buscarPorFecha(inicioDelDia, finDelDia);
- final pedidosNoCancelados =
- pedidos.where((p) => p.estatus != "CANCELADO").toList();
- final pedidosCancelados =
- pedidos.where((p) => p.estatus == "CANCELADO").toList();
- totalDelDia = 0.0;
- totalCancelados = 0.0;
- totalEfectivoDelDia = 0.0;
- totalTarjetaDelDia = 0.0;
- totalTransferenciaDelDia = 0.0;
- for (var pedido in pedidosNoCancelados) {
- totalDelDia += pedido.totalPedido ?? 0.0;
- totalEfectivoDelDia += pedido.cantEfectivo ?? 0.0;
- totalTarjetaDelDia += pedido.cantTarjeta ?? 0.0;
- totalTransferenciaDelDia += pedido.cantTransferencia ?? 0.0;
- }
- totalCancelados = pedidosCancelados.fold(
- 0.0, (sum, current) => sum + (current.totalPedido ?? 0.0));
- setState(() {
- this.pedidosNoCancelados = pedidosNoCancelados;
- this.pedidosCancelados = pedidosCancelados;
- });
- }
- Future<void> imprimirResumenPedidos(List<Pedido> pedidos) async {
- if (fechaSeleccionada == null) {
- print("No se ha seleccionado una fecha.");
- return;
- }
- await VentaTicket.imprimirResumenPedidos(pedidos, fechaSeleccionada!);
- }
- }
|