123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- import 'dart:typed_data';
- import 'package:pdf/pdf.dart';
- import 'package:pdf/widgets.dart' as pw;
- import '../../models/models.dart';
- import 'package:printing/printing.dart';
- import 'package:flutter/services.dart' show rootBundle;
- // Future<Uint8List> generateTicket(Pedido pedido) async {
- // final pdf = pw.Document();
- // final image = pw.MemoryImage(
- // (await rootBundle.load('assets/JoshiLogo-BN.png')).buffer.asUint8List(),
- // );
- // pdf.addPage(
- // pw.Page(
- // pageFormat: PdfPageFormat.roll57,
- // build: (pw.Context context) {
- // return pw.Column(
- // crossAxisAlignment: pw.CrossAxisAlignment.center,
- // children: [
- // pw.Center(child: pw.Image(image, width: 50, height: 50)),
- // pw.SizedBox(height: 10),
- // pw.Text('Joshi Papas Tu Sabor tu Estilo',
- // style:
- // pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
- // pw.SizedBox(height: 10),
- // pw.Text('RFC: XXXX0000', style: pw.TextStyle(fontSize: 9)),
- // pw.Text('Dirección: Calle Falsa 123',
- // style: pw.TextStyle(fontSize: 9)),
- // pw.Text('Ciudad: Ciudad Ejemplo', style: pw.TextStyle(fontSize: 9)),
- // pw.Text('Régimen: General de Ley',
- // style: pw.TextStyle(fontSize: 9)),
- // pw.SizedBox(height: 10),
- // pw.Text('Folio: ${pedido.folio}',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10)),
- // pw.SizedBox(height: 10),
- // ...pedido.productos.map(
- // (producto) => pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Expanded(
- // flex: 2,
- // child: pw.Text(
- // producto.producto?.nombre ?? "Producto no especificado",
- // style: pw.TextStyle(fontSize: 7)),
- // ),
- // pw.Expanded(
- // flex: 1,
- // child: pw.Text('x${producto.cantidad}',
- // style: pw.TextStyle(fontSize: 9)),
- // ),
- // pw.Expanded(
- // flex: 1,
- // child: pw.Text('\$${producto.producto?.precio}',
- // style: pw.TextStyle(fontSize: 9)),
- // ),
- // ],
- // ),
- // ),
- // pw.Divider(),
- // pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
- // children: [
- // pw.Text('Total:',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 9)),
- // pw.Padding(
- // padding: pw.EdgeInsets.only(right: 20),
- // child: pw.Text(
- // '\$${pedido.productos.fold<double>(0.0, (sum, p) => sum + (double.parse(p.producto?.precio ?? '0') * (p.cantidad ?? 1)))}',
- // style: pw.TextStyle(
- // fontWeight: pw.FontWeight.bold, fontSize: 9)),
- // ),
- // ],
- // ),
- // pw.SizedBox(height: 5),
- // pw.Text('¡GRACIAS POR SU COMPRA!',
- // style:
- // pw.TextStyle(fontSize: 8, fontWeight: pw.FontWeight.bold)),
- // pw.SizedBox(height: 20),
- // pw.Divider(),
- // pw.SizedBox(height: 10),
- // // Existing Ticket Content
- // pw.Text('Folio: ${pedido.folio}',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
- // pw.SizedBox(height: 10),
- // pw.Text('Cliente: ${pedido.nombreCliente}',
- // style:
- // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
- // pw.SizedBox(height: 10),
- // ...pedido.productos.map(
- // (producto) => pw.Row(
- // mainAxisAlignment: pw.MainAxisAlignment.start,
- // children: [
- // pw.Expanded(
- // flex: 3,
- // child: pw.Text(
- // producto.producto?.nombre ?? "Producto no especificado",
- // style: pw.TextStyle(fontSize: 9)),
- // ),
- // pw.Expanded(
- // flex: 1,
- // child: pw.Text('x${producto.cantidad}',
- // style: pw.TextStyle(fontSize: 9)),
- // ),
- // ],
- // ),
- // ),
- // pw.SizedBox(height: 80),
- // ],
- // );
- // },
- // ),
- // );
- // return Uint8List.fromList(await pdf.save());
- // }
- Future<Uint8List> primerTicket(Pedido pedido) async {
- final pdf = pw.Document();
- final image = pw.MemoryImage(
- (await rootBundle.load('assets/JoshiLogo-BN.png')).buffer.asUint8List(),
- );
- pdf.addPage(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.Text('Joshi Papas Tu Sabor tu Estilo',
- style: pw.TextStyle(
- fontSize: 12, fontWeight: pw.FontWeight.bold)),
- pw.SizedBox(height: 10),
- pw.Text('Fecha: ${pedido.peticion}',
- style: const pw.TextStyle(fontSize: 9)),
- pw.Text('RFC: XXXX0000',
- style: const pw.TextStyle(fontSize: 9)),
- pw.Text('Dirección: Calle Falsa 123',
- style: const pw.TextStyle(fontSize: 9)),
- pw.Text('Ciudad: Ciudad Ejemplo',
- style: const pw.TextStyle(fontSize: 9)),
- pw.Text('Régimen: General de Ley',
- style: const pw.TextStyle(fontSize: 9)),
- ])),
- pw.SizedBox(height: 10),
- pw.Text('Folio: ${pedido.folio}',
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 10)),
- pw.SizedBox(height: 10),
- ...pedido.productos.map(
- (producto) => 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: 9)),
- ),
- pw.Expanded(
- flex: 1,
- child: pw.Text('\$${producto.producto?.precio}',
- style: const pw.TextStyle(fontSize: 9)),
- ),
- ],
- ),
- ),
- pw.Divider(),
- 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: 20),
- child: pw.Text(
- '\$${pedido.productos.fold<double>(0.0, (sum, p) => sum + (double.parse(p.producto?.precio ?? '0') * (p.cantidad ?? 1)))}',
- 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.SizedBox(height: 20),
- pw.Divider(),
- ]);
- }));
- return Uint8List.fromList(await pdf.save());
- }
- Future<Uint8List> segundoTicket(Pedido pedido) async {
- final pdf = pw.Document();
- pdf.addPage(pw.Page(
- pageFormat: PdfPageFormat.roll57,
- build: (pw.Context context) {
- List<pw.Widget> content = [
- pw.SizedBox(height: 10),
- pw.Padding(
- padding: const pw.EdgeInsets.only(right: 15),
- child: pw.Text('Fecha: ${pedido.peticion}',
- style: pw.TextStyle(
- fontSize: 9, fontWeight: pw.FontWeight.bold))),
- pw.SizedBox(height: 5),
- pw.Text('Folio: ${pedido.folio}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
- pw.SizedBox(height: 10),
- pw.Text('Cliente: ${pedido.nombreCliente}',
- style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
- pw.SizedBox(height: 10),
- ];
- content.addAll(pedido.productos.map((producto) => pw.Row(
- mainAxisAlignment: pw.MainAxisAlignment.start,
- children: [
- pw.Expanded(
- flex: 3,
- child: pw.Text(
- producto.producto?.nombre ?? "Producto no especificado",
- style: const pw.TextStyle(fontSize: 9)),
- ),
- pw.Expanded(
- flex: 1,
- child: pw.Text('x${producto.cantidad}',
- style: const pw.TextStyle(fontSize: 9)),
- ),
- ],
- )));
- if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
- content.add(pw.SizedBox(height: 10));
- content.add(pw.Text('Comentarios:',
- style:
- pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)));
- content.add(pw.Padding(
- padding: const pw.EdgeInsets.only(right: 15),
- child: pw.Text(pedido.comentarios!,
- style: const pw.TextStyle(fontSize: 9)),
- ));
- }
- content.add(
- pw.SizedBox(height: 20)); // Some spacing before finalizing the page
- return pw.Column(
- crossAxisAlignment: pw.CrossAxisAlignment.center,
- children: content);
- }));
- return Uint8List.fromList(await pdf.save());
- }
- Future<void> printPdf(Uint8List pdfBytes) async {
- await Printing.layoutPdf(
- onLayout: (PdfPageFormat format) => pdfBytes,
- );
- }
- void printTickets(Pedido pedido) async {
- Uint8List firstTicket = await primerTicket(pedido);
- await printPdf(firstTicket);
- Uint8List secondTicket = await segundoTicket(pedido);
- await printPdf(secondTicket);
- }
|