Untitled

 avatar
unknown
plain_text
2 years ago
24 kB
7
Indexable
import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:firewood/consts/FirewoodRes.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firewood/model/SellModel.dart';
import 'package:http/http.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

import '../Reports/SalePurchaseHeaderInvoice.dart';
import '../model/report/SalePurchaseInvoiceModel.dart';
import 'BaseInvoice.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:universal_html/html.dart';


class SaleAndSaleReturnInvoiceModel {
  String productName;
  String saleType;
  String unitPrice;
  String quantity;
  String total;

  SaleAndSaleReturnInvoiceModel(
      {this.productName,
      this.saleType,
      this.unitPrice,
      this.quantity,
      this.total
      });

  @override
  String toString() {
    return 'SaleAndSaleReturnInvoiceModel{productName: $productName, saleType: $saleType, unitPrice: $unitPrice, quantity: $quantity, total: $total}';
  }
}

class SaleAndSaleReturnInvoice {
  static Future<Uint8List> generatePdf(
      {PdfPageFormat format,
      SellType sellType,
      String userName,
      String userAddress,
      String userMobile,
      String sellID,
      String invoiceNo,
      String invoiceDate,
      String vatNo,
      String name,
      String mobile,
      String saleBy,
      String availableEmptyCylinderDue,
      String availableCommission,
      List<SaleAndSaleReturnInvoiceModel> saleAndSaleReturnInvoiceModel,
      String extraCharge,
      String discount,
      String totalBillable,
      String previousDue,
      String paid,
      String totalQuantity,
      String totalAmount,
      String availableDue,
      bool isShowPaidOverlay = true,
      bool isLPG,
      String note,
      bool secondaryInvoice,
        double payByCommission,

        BuildContext my_context,
        SalePurchaseInvoiceModel salePurchaseInvoiceModel


      }) async {

    // print("Preparing Preview");

    final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
    // final banglaFont = await PdfGoogleFonts.hindSiliguriLight();
    //final banglaFont = await PdfGoogleFonts.notoSansBengaliMedium();

  //  final banglaFont = await fontFromAssetBundle('assets/fonts/NotoSansBengali-Medium.ttf');


    Uint8List ff =  (await rootBundle.load("assets/fonts/NotoSansBengali-Medium.ttf")).buffer.asUint8List();
   // final banglaFont = MultipartFile.fromBytes(ff.buffer.asByteData().toString(), ff.toList());
    final banglaFont = pw.Font.ttf(ff.buffer.asByteData());

    // final font = await rootBundle.load("assets/fonts/HindSiliguri-Regular.ttf");
    // final banglaFont = pw.Font.ttf(font);

   // final Uint8List fontData = File('fonts/HindSiliguri-Regular.ttf');
    //final ttf = pw.Font.ttf(fontData.buffer.asByteData());

    //final banglaFont = await PdfGoogleFonts.hindSiliguriLight();
    /*final Uint8List fontData = File('fonts/HindSiliguri-Regular.ttf').readAsBytesSync();
    final ttf = pw.Font.ttf(fontData.buffer.asByteData());*/
    //final Uint8List fontData = File('fonts/HindSiliguri-Regular.ttf').readAsBytesSync();


    //final fontLocation =  await rootBundle.load("assets/fonts/HindSiliguri-Regular.ttf");
      Uint8List fontLocation =  (await rootBundle.load("assets/fonts/solaimanlipi.ttf")).buffer.asUint8List();

    final ttf = pw.Font.ttf(/*fontData.buffer.asByteData()*/fontLocation.buffer.asByteData());

  // final banglaFont = ttf;










    double dueBalance =double.parse(totalBillable) - double.parse(paid) - payByCommission;
    var data = isLPG ?secondaryInvoice?List.generate(saleAndSaleReturnInvoiceModel.length + 1,
            (index) => (index == saleAndSaleReturnInvoiceModel.length) ? [
          'Total',
          '',
          '',
          totalQuantity,
        ] :

            [
          index + 1,
          saleAndSaleReturnInvoiceModel
              .elementAt(index)
              .productName
              .length >
              30
              ? saleAndSaleReturnInvoiceModel
              .elementAt(index)
              .productName
              .substring(0, 30) +
              "..."
              : saleAndSaleReturnInvoiceModel
              .elementAt(index)
              .productName,
          saleAndSaleReturnInvoiceModel.elementAt(index).saleType,
          saleAndSaleReturnInvoiceModel.elementAt(index).quantity,
        ]).toList()

        : List.generate(saleAndSaleReturnInvoiceModel.length + 1,
            (index) => (index == saleAndSaleReturnInvoiceModel.length) ? [
                    '',
                    'Total',
                    '',
                    '',
                    totalQuantity,
              (double.parse(totalAmount)+double.parse(discount)).toStringAsFixed(2),
                  ] : [
                    index + 1,
                    saleAndSaleReturnInvoiceModel
                                .elementAt(index)
                                .productName
                                .length >
                            30
                        ? saleAndSaleReturnInvoiceModel
                                .elementAt(index)
                                .productName
                                .substring(0, 30) +
                            "..."
                        : saleAndSaleReturnInvoiceModel
                            .elementAt(index)
                            .productName,
                    saleAndSaleReturnInvoiceModel.elementAt(index).saleType,
              double.parse (saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice).toStringAsFixed(2),
                    saleAndSaleReturnInvoiceModel.elementAt(index).quantity,

              (double.parse(saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice)*
                  int.parse(saleAndSaleReturnInvoiceModel.elementAt(index).quantity.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).quantity)).toStringAsFixed(2)==0?"":(double.parse(saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice)*
                  int.parse(saleAndSaleReturnInvoiceModel.elementAt(index).quantity.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).quantity)).toStringAsFixed(2) ]).toList()
        : List.generate(
            saleAndSaleReturnInvoiceModel.length + 1,
            (index) => (index == saleAndSaleReturnInvoiceModel.length)
                ? [
                    '',
                    'Total ',
                    '',
                    totalQuantity,
              (double.parse(totalAmount)+double.parse(discount)).toStringAsFixed(2),
                  ]
                : [
                    index + 1,
                    saleAndSaleReturnInvoiceModel
                                .elementAt(index)
                                .productName
                                .length >
                            30
                        ? saleAndSaleReturnInvoiceModel
                                .elementAt(index)
                                .productName
                                .substring(0, 30) +
                            "..."
                        : saleAndSaleReturnInvoiceModel
                            .elementAt(index)
                            .productName,
              double.parse(saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice).toStringAsFixed(2),
                    saleAndSaleReturnInvoiceModel.elementAt(index).quantity,
              (double.parse(saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice)*
                  int.parse(saleAndSaleReturnInvoiceModel.elementAt(index).quantity.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).quantity)).toStringAsFixed(2)==0?"":(double.parse(saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).unitPrice)*
                  int.parse(saleAndSaleReturnInvoiceModel.elementAt(index).quantity.isEmpty?"0":saleAndSaleReturnInvoiceModel.elementAt(index).quantity)).toStringAsFixed(2)
                  ]).toList();

    int numberOfPage = (data.length / 15).ceil().toInt();
    final paidOverlayPath = (await rootBundle.load(FirewoodRes.paidOverlay)).buffer.asUint8List();

    var footer = pw.Column(
      crossAxisAlignment: pw.CrossAxisAlignment.start,
      mainAxisAlignment: pw.MainAxisAlignment.start,
      children: [
        pw.SizedBox(
          height: 0.5 * PdfPageFormat.cm,
        ),



        pw.Row(
            mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                  child: pw.Column(
                      crossAxisAlignment: pw.CrossAxisAlignment.start,
                      mainAxisAlignment: pw.MainAxisAlignment.start,
                      children: [
                    isLPG ? pw.Row(
                            mainAxisSize: pw.MainAxisSize.max,
                            mainAxisAlignment:
                                pw.MainAxisAlignment.spaceBetween,
                            children: [
                              pw.Text('Available Empty Cylinder Due: ',
                                  style: pw.TextStyle(
                                      fontWeight: pw.FontWeight.bold)),
                              pw.SizedBox(
                                width: 0.5 * PdfPageFormat.cm,
                              ),
                              pw.Text(availableEmptyCylinderDue),
                            ],
                          ) : pw.SizedBox.shrink(),


                    //notes
                        pw.Row(
                          mainAxisSize: pw.MainAxisSize.max,
                          mainAxisAlignment: pw.MainAxisAlignment.start,
                          crossAxisAlignment: pw.CrossAxisAlignment.start,
                          children: [
                            pw.Text(/*'Note: '*/ AppLocalizations.of(my_context).note,
                                style: pw.TextStyle(
                                    fontWeight: pw.FontWeight.bold,
                                    font: banglaFont

                                )),

                            pw.Text( note),
                          ],
                        ),



                    pw.SizedBox(
                      width: isShowPaidOverlay ? 0.5 * PdfPageFormat.cm : 0,
                      height: isShowPaidOverlay ? 0.5 * PdfPageFormat.cm : 0,
                    ),
                        dueBalance==0 ?isShowPaidOverlay ? pw.Image(pw.MemoryImage(paidOverlayPath),
                            height: 100,
                            width: 100,
                            alignment: pw.Alignment.center)
                        : pw.SizedBox.shrink():pw.SizedBox.shrink(),
                  ]


                  )),
              pw.SizedBox(
                width: 3 * PdfPageFormat.cm,
              ),
              pw.Expanded(
                child: pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    mainAxisAlignment: pw.MainAxisAlignment.start,
                    children: [

                      pw.Row(
                        mainAxisSize: pw.MainAxisSize.max,
                        mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                        children: [
                          pw.Text('Discount (-): ',
                              style:
                                  pw.TextStyle(fontWeight: pw.FontWeight.bold)),
                          pw.SizedBox(
                            width: 0.3 * PdfPageFormat.cm,
                          ),
                          pw.Text(discount),
                        ],
                      ),
                      pw.SizedBox(
                        width: 0.3 * PdfPageFormat.cm,
                      ),



                      sellType == SellType.SELL
                          ? pw.Row(
                          mainAxisAlignment: pw.MainAxisAlignment.end ,
                          children: [
                            pw.SizedBox(
                                width: 8.2 * PdfPageFormat.cm,
                                height: 1,
                                child:
                                pw.Container(color: PdfColors.black)),
                          ])
                          : pw.SizedBox.shrink(),



                      pw.SizedBox(
                        width: 0.3 * PdfPageFormat.cm,
                      ),


                      pw.Row(
                        mainAxisSize: pw.MainAxisSize.max,
                        mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                        children: [
                          pw.Text(
                              sellType == SellType.SELL
                                  ? 'Total Billable: '
                                  : 'Total Amount: ',
                              style:
                                  pw.TextStyle(fontWeight: pw.FontWeight.bold)),
                          pw.SizedBox(
                            width: 0.5 * PdfPageFormat.cm,
                          ),
                          pw.Text( double.parse(totalBillable).toStringAsFixed(2)),
                        ],
                      ),
                      sellType == SellType.SELL
                          ? pw.Row(
                              mainAxisSize: pw.MainAxisSize.max,
                              mainAxisAlignment:
                                  pw.MainAxisAlignment.spaceBetween,
                              children: [
                                pw.Text('Paid (-): ',
                                    style: pw.TextStyle(
                                        fontWeight: pw.FontWeight.bold)),
                                pw.SizedBox(
                                  width: 0.5 * PdfPageFormat.cm,
                                ),
                                pw.Text(paid),

                              ],
                            )
                          : pw.SizedBox.shrink(),

                      sellType == SellType.SELL
                          ? pw.Row(
                              mainAxisSize: pw.MainAxisSize.max,
                              mainAxisAlignment:
                                  pw.MainAxisAlignment.spaceBetween,
                              children: [
                                pw.Text('Paid By Commission (-): ',
                                    style: pw.TextStyle(
                                        fontWeight: pw.FontWeight.bold)),
                                pw.SizedBox(
                                  width: 0.3 * PdfPageFormat.cm,
                                ),
                                pw.Text(payByCommission.toString()),

                              ],
                            )
                          : pw.SizedBox.shrink(),




                      sellType == SellType.SELL
                          ? pw.Row(
                              mainAxisAlignment: pw.MainAxisAlignment.end,
                              children: [
                                  pw.SizedBox(
                                      width: 8.2 * PdfPageFormat.cm,
                                      height: 1,
                                      child:
                                          pw.Container(color: PdfColors.black)),
                                ])
                          : pw.SizedBox.shrink(),


                      sellType == SellType.SELL
                          ? pw.Row(
                              mainAxisSize: pw.MainAxisSize.max,
                              mainAxisAlignment:
                                  pw.MainAxisAlignment.spaceBetween,
                              children: [
                                pw.Text('Available Due: ',
                                    style: pw.TextStyle(
                                        fontWeight: pw.FontWeight.bold)),
                                pw.SizedBox(
                                  width: 0.5 * PdfPageFormat.cm,
                                ),
                                pw.Text(dueBalance.toStringAsFixed(2)),
                              ],
                            )
                          : pw.SizedBox.shrink(),
                    ]),
              )
            ]),
        pw.SizedBox(
          height: 1.5 * PdfPageFormat.cm,
        ),

        BaseInvoice.defaultFooter()
      ],
    );



    final companyIcon = (await rootBundle.load('assets/images/bkash_icon.png'))
        .buffer
        .asUint8List();

    pdf.addPage(pw.MultiPage(
      pageFormat: format,
      maxPages: 200,
      margin: pw.EdgeInsets.symmetric(
          horizontal: .8 * PdfPageFormat.cm, vertical: .5 * PdfPageFormat.cm),
      build: (context) => [
        pw.Column(children: [

          pw.SizedBox(height: 10),

          SalePurchaseInvoiceHeader.generateSalePurchaseInvoiceHeader(salePurchaseInvoice: salePurchaseInvoiceModel),

          pw.SizedBox(
            height: .5 * PdfPageFormat.cm,
          ),

          pw.Table.fromTextArray(
            headers: isLPG ? secondaryInvoice?
            [
              /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
              /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
              /*'Sale Type'*/'${AppLocalizations.of(my_context).saleType}',
              /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
            ]

                :[

              /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
              /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
              /*'Sale Type'*/'${AppLocalizations.of(my_context).saleType}',
              /*'Unit Price'*/'${AppLocalizations.of(my_context).unitPrice}',
              /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
              /*'Total'*/ '${AppLocalizations.of(my_context).total}',
            ]
                : [

              /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
              /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
              /*'Unit Price'*/'${AppLocalizations.of(my_context).unitPrice}',
              /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
              /*'Total'*/ '${AppLocalizations.of(my_context).total}',



            ],
            data: data.sublist(0, data.length > 15 ? 15 : data.length),
            border: pw.TableBorder.all(width: .5),
            headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold,font: banglaFont ),
            headerDecoration: pw.BoxDecoration(color: PdfColors.grey300),
            cellHeight: 30,
            cellDecoration: (x, y, z) {
              return pw.BoxDecoration(color: PdfColors.grey100);
            },
            cellAlignments: isLPG ? secondaryInvoice?{
              0: pw.Alignment.centerLeft,
              1: pw.Alignment.center,
              2: pw.Alignment.center,
              3: pw.Alignment.center,
            }: {
                    0: pw.Alignment.centerLeft,
                    1: pw.Alignment.center,
                    2: pw.Alignment.center,
                    3: pw.Alignment.center,
                    4: pw.Alignment.center,
                    5: pw.Alignment.centerRight,}
                :
            {
                    0: pw.Alignment.centerLeft,
                    1: pw.Alignment.center,
                    2: pw.Alignment.center,
                    3: pw.Alignment.center,
                    4: pw.Alignment.centerRight,
                  },


          ),
          numberOfPage <= 1 ? secondaryInvoice?pw.SizedBox(width: 1): footer : pw.SizedBox(width: 0, height: 0),
        ]),


      ],
    ));

    if (numberOfPage > 1) {
      pdf.addPage(
          pw.MultiPage(
            pageFormat: format,
            maxPages: 200,
            margin: pw.EdgeInsets.symmetric(
                horizontal: 2 * PdfPageFormat.cm,
                vertical: 1.5 * PdfPageFormat.cm),
            build: (context) => [
              pw.Table.fromTextArray(
                cellStyle: pw.TextStyle(font:  banglaFont),
                headers: isLPG ? secondaryInvoice?
               /* [

                  'SL',
                  'Product Name',
                  'Sale Type',
                  'Quantity',
                ]:
                [
                        'SL',
                        'Product Name',
                        'Sale Type',
                        'Unit Price',
                        'Quantity',
                        'Total'
                      ] :
                ['SL',
                  'Product Name',
                  'Unit Price',
                  'Quantity',
                  'Total'
                ],*/


                  [
              /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
          /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
          /*'Sale Type'*/'${AppLocalizations.of(my_context).saleType}',
          /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
          ]

          :[

          /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
    /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
    /*'Sale Type'*/'${AppLocalizations.of(my_context).saleType}',
    /*'Unit Price'*/'${AppLocalizations.of(my_context).unitPrice}',
    /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
    /*'Total'*/ '${AppLocalizations.of(my_context).total}',
    ]
        : [

    /*'SL'*/ '${AppLocalizations.of(my_context).sl}',
    /*'Product Name'*/ '${AppLocalizations.of(my_context).productName}',
    /*'Unit Price'*/'${AppLocalizations.of(my_context).unitPrice}',
    /*'Quantity'*/ '${AppLocalizations.of(my_context).quantity}',
    /*'Total'*/ '${AppLocalizations.of(my_context).total}',



    ],


                data: data.sublist((15 * 1), data.length),
                border: null,
                headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold,font: banglaFont),
                headerDecoration: pw.BoxDecoration(color: PdfColors.grey300),
                cellHeight: 15,
                cellDecoration: (x, y, z) {
                  return pw.BoxDecoration(color: PdfColors.grey100);
                },
                cellAlignments: isLPG ? secondaryInvoice ?{
                  0: pw.Alignment.centerLeft,
                  1: pw.Alignment.center,
                  2: pw.Alignment.center,
                  3: pw.Alignment.center,
                }: {
                        0: pw.Alignment.centerLeft,
                        1: pw.Alignment.center,
                        2: pw.Alignment.center,
                        3: pw.Alignment.center,
                        4: pw.Alignment.center,
                        5: pw.Alignment.centerRight,
                      }
                    : {
                        0: pw.Alignment.centerLeft,
                        1: pw.Alignment.center,
                        2: pw.Alignment.center,
                        3: pw.Alignment.center,
                        4: pw.Alignment.centerRight,
                      },
              ),
              secondaryInvoice ?footer:pw.SizedBox(height: 1 * PdfPageFormat.cm,
    ),
            ],
          ),
          index: 1);
    }




    await Printing.layoutPdf(
        onLayout: (PdfPageFormat format) async => pdf.save());


  }
}
Editor is loading...