123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- import 'dart:typed_data';
- import 'package:flutter/material.dart';
- import 'package:intl/intl.dart';
- import 'package:pdf/pdf.dart';
- import 'package:pdf/widgets.dart' as pw;
- import 'package:provider/provider.dart';
- import '../../models/models.dart';
- import '../../viewmodels/viewmodels.dart';
- import 'package:printing/printing.dart';
- import 'package:flutter/services.dart' show rootBundle;
- Future<void> imprimirTicketsJuntos(BuildContext context, Pedido pedido) async {
- bool ticketCocinaActivo =
- await Provider.of<VariableViewModel>(context, listen: false)
- .isVariableActive('ticket_cocina');
- bool ticketVentaActivo =
- await Provider.of<VariableViewModel>(context, listen: false)
- .isVariableActive('ticket_venta');
- final pdf = pw.Document();
- final image = pw.MemoryImage(
- (await rootBundle.load('assets/icono-BN.png')).buffer.asUint8List(),
- );
- if (ticketVentaActivo) {
- pdf.addPage(
- generarPaginaPrimerTicket(pedido, image),
- );
- }
- if (ticketCocinaActivo) {
- final paginaSegundoTicket =
- await generarPaginaSegundoTicket(context, pedido);
- pdf.addPage(paginaSegundoTicket);
- }
- await printPdf(Uint8List.fromList(await pdf.save()));
- }
- pw.Page generarPaginaPrimerTicket(Pedido pedido, pw.MemoryImage image) {
- final numberFormat = NumberFormat('#,##0.00', 'es_MX');
- double totalSinDescuento = 0;
- double totalConDescuento = 0;
- double descuento = pedido.descuento?.toDouble() ?? 0.0;
- double precioDescuento = 0;
- final productList = pedido.productos
- .map(
- (producto) {
- final productPrice = producto.producto?.precio ?? 0.0;
- final productTotal = productPrice * (producto.cantidad ?? 1);
- totalSinDescuento += productTotal;
- final toppingsList = producto.toppings.where((topping) {
- final toppingPrice = topping.topping?.precio ?? 0.0;
- return toppingPrice > 0;
- }).map((topping) {
- final toppingPrice = topping.topping?.precio ?? 0.0;
- totalSinDescuento += toppingPrice * (producto.cantidad ?? 1);
- return pw.Row(
- children: [
- pw.Text(
- '- ${topping.topping?.nombre ?? "Topping no especificado"}',
- style: const pw.TextStyle(fontSize: 7)),
- pw.Spacer(),
- pw.Text('\$${numberFormat.format(toppingPrice)}',
- style: const pw.TextStyle(fontSize: 7)),
- ],
- );
- }).toList();
- return [
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Expanded(
- flex: 2,
- child: pw.Text(
- producto.producto?.nombre ?? "Producto no especificado",
- style: const pw.TextStyle(fontSize: 7)),
- ),
- pw.Expanded(
- flex: 1,
- child: pw.Text('x${producto.cantidad}',
- style: const pw.TextStyle(fontSize: 7)),
- ),
- pw.Padding(
- padding: pw.EdgeInsets.only(right: 2),
- child: pw.Expanded(
- flex: 1,
- child: pw.Text('\$${numberFormat.format(productPrice)}',
- style: const pw.TextStyle(fontSize: 8)),
- ),
- )
- ],
- ),
- ...toppingsList,
- ];
- },
- )
- .expand((e) => e)
- .toList();
- precioDescuento = totalSinDescuento * (descuento / 100);
- totalConDescuento = totalSinDescuento - precioDescuento;
- return pw.Page(
- pageFormat: PdfPageFormat.roll57,
- build: (pw.Context context) {
- return pw.Column(
- crossAxisAlignment: pw.CrossAxisAlignment.center,
- children: [
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 20),
- child:
- pw.Center(child: pw.Image(image, width: 50, height: 50))),
- pw.SizedBox(height: 10),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 15),
- child: pw.Column(children: [
- pw.SizedBox(height: 5),
- pw.Text('Fecha: ${pedido.peticion}',
- style: const pw.TextStyle(fontSize: 9)),
- pw.Text('Conalep', style: const pw.TextStyle(fontSize: 9)),
- pw.Text('Hermosillo',
- style: const pw.TextStyle(fontSize: 9)),
- ])),
- pw.SizedBox(height: 10),
- pw.Text('Pedido: ${pedido.folio}',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 10)),
- pw.SizedBox(height: 10),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 20),
- child: pw.Column(children: productList)),
- pw.Divider(),
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Subtotal:',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 30),
- child: pw.Text(
- '\$${numberFormat.format(totalSinDescuento)}',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- ),
- ],
- ),
- if (descuento > 0) ...[
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Descuento:',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 30),
- child: pw.Text(
- '-\$${numberFormat.format(precioDescuento)}',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- ),
- ],
- ),
- ],
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Total:',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 30),
- child: pw.Text(
- '\$${numberFormat.format(totalConDescuento)}',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9)),
- ),
- ],
- ),
- pw.SizedBox(height: 5),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 15),
- child: pw.Text('¡GRACIAS POR SU COMPRA!',
- style: pw.TextStyle(
- fontSize: 8, fontWeight: pw.FontWeight.bold))),
- pw.Divider(),
- pw.SizedBox(height: 20),
- pw.Text('.', style: pw.TextStyle(fontSize: 1)),
- ]);
- });
- }
- //Ticket PC
- // Future<pw.Page> generarPaginaSegundoTicket(
- // BuildContext context, Pedido pedido) async {
- // final numberFormat = NumberFormat('#,##0.00', 'es_MX');
- // double subtotal = 0;
- // double totalConDescuento = 0;
- // double descuento = pedido.descuento?.toDouble() ?? 0.0;
- // double precioDescuento = 0;
- // final sucursalVariable =
- // await Provider.of<VariableViewModel>(context, listen: false)
- // .getVariableByClave('Sucursal');
- // final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
- // List<pw.Widget> content = [
- // pw.Padding(
- // padding: pw.EdgeInsets.only(right: 10),
- // child: pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
- // children: [
- // pw.Text('${pedido.folio}/ ',
- // style:
- // pw.TextStyle(fontSize: 10.5, fontWeight: pw.FontWeight.bold)),
- // if (sucursalDescripcion.isNotEmpty)
- // pw.Text('$sucursalDescripcion/ ',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
- // ],
- // ),
- // ),
- // pw.SizedBox(height: 2),
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
- // children: [
- // pw.Text('${_formatDateTime(pedido.peticion)}',
- // style:
- // pw.TextStyle(fontSize: 10.5, fontWeight: pw.FontWeight.bold)),
- // ],
- // ),
- // pw.SizedBox(height: 2),
- // if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
- // pw.Text('Cliente: ${pedido.nombreCliente}',
- // style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
- // pw.SizedBox(height: 2),
- // ];
- // // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
- // content.addAll(pedido.productos
- // .map((producto) {
- // final productPrice = producto.producto?.precio ?? 0.0;
- // final productTotal = productPrice * (producto.cantidad ?? 1);
- // subtotal += productTotal;
- // final toppingsList = producto.toppings.map((topping) {
- // final toppingPrice = topping.topping?.precio ?? 0.0;
- // final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
- // subtotal += toppingTotal;
- // return pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.start,
- // children: [
- // pw.Expanded(
- // flex: 3,
- // child: pw.Text(
- // '-${topping.topping?.nombre ?? "Topping no especificado"}',
- // style: const pw.TextStyle(fontSize: 8.5)),
- // ),
- // if (toppingPrice > 0)
- // pw.Expanded(
- // flex: 1,
- // child: pw.Text(
- // '\$${numberFormat.format(toppingTotal)}',
- // style: const pw.TextStyle(fontSize: 8.5),
- // textAlign: pw.TextAlign.right,
- // ),
- // ),
- // ],
- // );
- // }).toList();
- // return [
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Expanded(
- // flex: 1,
- // child: pw.Text('${producto.cantidad}',
- // style: const pw.TextStyle(fontSize: 10.5)),
- // ),
- // pw.Expanded(
- // flex: 6,
- // child: pw.Text(
- // producto.producto?.nombre ?? "Producto no especificado",
- // style: const pw.TextStyle(fontSize: 10.5)),
- // ),
- // pw.Expanded(
- // flex: 4,
- // child: pw.Align(
- // alignment: pw.Alignment.centerLeft,
- // child: pw.Text(
- // '\$${numberFormat.format(productTotal)}',
- // style: const pw.TextStyle(fontSize: 10.5),
- // ),
- // )),
- // ],
- // ),
- // ...toppingsList,
- // ];
- // })
- // .expand((e) => e)
- // .toList());
- // // Calcular el descuento y el total final
- // precioDescuento = subtotal * (descuento / 100);
- // totalConDescuento = subtotal - precioDescuento;
- // content.add(pw.Divider());
- // if (descuento > 0) {
- // content.addAll([
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Text('Subtotal:',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
- // pw.Padding(
- // padding: pw.EdgeInsets.only(right: 15),
- // child: pw.Text('\$${numberFormat.format(subtotal)}',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
- // ],
- // ),
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Text('Descuento:',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
- // pw.Padding(
- // padding: pw.EdgeInsets.only(right: 15),
- // child: pw.Text('-\$${numberFormat.format(precioDescuento)}',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
- // ],
- // ),
- // ]);
- // }
- // content.add(
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Text('Total:',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
- // pw.Padding(
- // padding: pw.EdgeInsets.only(right: 20),
- // child: pw.Text(
- // '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
- // ],
- // ),
- // );
- // if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
- // content.add(pw.SizedBox(height: 1));
- // content.add(pw.Text('Comentarios:',
- // style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)));
- // content.add(pw.Padding(
- // padding: const pw.EdgeInsets.only(right: 15),
- // child: pw.Text(pedido.comentarios!,
- // style: const pw.TextStyle(fontSize: 10.5)),
- // ));
- // }
- // content.add(pw.SizedBox(height: 15));
- // content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
- // return pw.Page(
- // pageFormat: PdfPageFormat.roll57,
- // margin: pw.EdgeInsets.all(5),
- // build: (pw.Context context) {
- // return pw.Column(
- // crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
- // },
- // );
- // }
- //Ticket Tablet
- Future<pw.Page> generarPaginaSegundoTicket(
- BuildContext context, Pedido pedido) async {
- final numberFormat = NumberFormat('#,##0.00', 'es_MX');
- double subtotal = 0;
- double totalConDescuento = 0;
- double descuento = pedido.descuento?.toDouble() ?? 0.0;
- double precioDescuento = 0;
- final sucursalVariable =
- await Provider.of<VariableViewModel>(context, listen: false)
- .getVariableByClave('Sucursal');
- final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
- List<pw.Widget> content = [
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
- children: [
- pw.Text('${pedido.folio}/ ',
- style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
- if (sucursalDescripcion.isNotEmpty)
- pw.Text('$sucursalDescripcion/ ',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- ],
- ),
- pw.SizedBox(height: 2),
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
- children: [
- pw.Text('${_formatDateTime(pedido.peticion)}',
- style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
- ],
- ),
- pw.SizedBox(height: 2),
- if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
- pw.Text('Cliente: ${pedido.nombreCliente}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- pw.SizedBox(height: 2),
- ];
- // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
- content.addAll(pedido.productos
- .map((producto) {
- final productPrice = producto.producto?.precio ?? 0.0;
- final productTotal = productPrice * (producto.cantidad ?? 1);
- subtotal += productTotal;
- final toppingsList = producto.toppings.map((topping) {
- final toppingPrice = topping.topping?.precio ?? 0.0;
- final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
- subtotal += toppingTotal;
- return pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.start,
- children: [
- pw.Expanded(
- flex: 3,
- child: pw.Text(
- '-${topping.topping?.nombre ?? "Topping no especificado"}',
- style: const pw.TextStyle(fontSize: 6)),
- ),
- if (toppingPrice > 0)
- pw.Expanded(
- flex: 1,
- child: pw.Text(
- '\$${numberFormat.format(toppingTotal)}',
- style: const pw.TextStyle(fontSize: 6),
- textAlign: pw.TextAlign.right,
- ),
- ),
- ],
- );
- }).toList();
- return [
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Expanded(
- flex: 1,
- child: pw.Text('${producto.cantidad}',
- style: const pw.TextStyle(fontSize: 7)),
- ),
- pw.Expanded(
- flex: 5,
- child: pw.Text(
- producto.producto?.nombre ?? "Producto no especificado",
- style: const pw.TextStyle(fontSize: 7)),
- ),
- pw.Expanded(
- flex: 2,
- child: pw.Align(
- alignment: pw.Alignment.centerRight,
- child: pw.Text(
- '\$${numberFormat.format(productTotal)}',
- style: const pw.TextStyle(fontSize: 7),
- ),
- )),
- ],
- ),
- ...toppingsList,
- ];
- })
- .expand((e) => e)
- .toList());
- // Calcular el descuento y el total final
- precioDescuento = subtotal * (descuento / 100);
- totalConDescuento = subtotal - precioDescuento;
- content.add(pw.Divider());
- if (descuento > 0) {
- content.addAll([
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Subtotal:',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- pw.Text('\$${numberFormat.format(subtotal)}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- ],
- ),
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Descuento:',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- pw.Text('-\$${numberFormat.format(precioDescuento)}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- ],
- ),
- ]);
- }
- content.add(
- pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- children: [
- pw.Text('Total:',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- pw.Text(
- '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
- ],
- ),
- );
- if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
- content.add(pw.SizedBox(height: 1));
- content.add(pw.Text('Comentarios:',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)));
- content.add(pw.Padding(
- padding: const pw.EdgeInsets.only(right: 15),
- child:
- pw.Text(pedido.comentarios!, style: const pw.TextStyle(fontSize: 7)),
- ));
- }
- content.add(pw.SizedBox(height: 1));
- return pw.Page(
- pageFormat: PdfPageFormat.roll57,
- margin: pw.EdgeInsets.all(5),
- build: (pw.Context context) {
- return pw.Column(
- crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
- },
- );
- }
- Future<void> printPdf(Uint8List pdfBytes) async {
- await Printing.layoutPdf(
- onLayout: (PdfPageFormat format) => pdfBytes,
- );
- }
- String _formatDateTime(String? dateTimeString) {
- if (dateTimeString == null) return "Sin fecha";
- DateTime parsedDate = DateTime.parse(dateTimeString);
- var formatter = DateFormat('dd-MM-yyyy HH:mm:ss');
- return formatter.format(parsedDate.toLocal());
- }
|