Flutter财政打印机集成插件yjy_fiscal_printer的使用

Flutter财政打印机集成插件yjy_fiscal_printer的使用

为了方便与不同品牌的财政打印机进行交互,并使用它们的功能,该模块定义了一个统一的交互数据结构和接口。通过实现这些接口来与特定品牌的财政打印机进行交互。

交互数据结构

Fiscal.Receipt

名称 描述
operator 识别操作员
sales 销售项目或取消销售
lottery 国家彩票唯一客户代码
personalTaxCode 个人税码
refunds 退款项目或取消退款
subtotals 小计
payments 支付
barCode 条形码
qrCode 二维码
graphicCoupon 图形优惠券
openDrawer 打开抽屉

Fiscal.Report

名称 描述
type 报告类型:每日财务报告或每日财政结账或两者兼有
operator 操作员
timeout 超时
openDrawer 打开抽屉

Fiscal.Cancel

名称 描述
type 空操作或冗余
zRepNum 每日财政结账报告编号
docNum 文档编号
date 日期
fiscalNum 打印机的财政序列号
operator 操作员

Fiscal.Command

名称 描述
code 命令类型
data 命令所需的数据

Commands

命令名称 描述 支持情况
OPEN_DRAWER 值:0,打开抽屉 Epson / 自定义
QUERY_PRINTER_STATUS 值 1,查询打印机状态 Epson / 自定义
RESET_PRINTER 值 2,重置打印机 Epson / 自定义
GET_NATIVE_CODE_FUNCTION 值 3,使用打印机原生命令 Epson / 自定义
GET_INFO 值 4,获取打印机设备信息 仅自定义

接口

Epson 自定义
printFiscalReceipt(receipt: Fiscal.Receipt) printFiscalReceipt(receipt: FPrinterCustom.Receipt)
printFiscalReport(report: Fiscal.Report) printFiscalReport(report: FPrinterCustom.Report)
printCancel(cancel: Fiscal.Cancel) printCancel(cancel: FPrinterCustom.Cancel)
executeCommand(...commands: Fiscal.Command[]) executeCommand(...commands: FPrinterCustom.Command[])

使用

Epson Fiscal ePOS-Print XML 示例

// 创建客户端
final fprinter = new EpsonXmlHttpClient(epson.Config(
    host: '192.168.1.1',
    deviceId: 'local_printer',
    timeout: 10000
));

// 财政收据
await fprinter.printFiscalReceipt(epson.Receipt(
    sales: [
        epson.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'A',
            quantity: 1,
            unitPrice: 5
        ),
        epson.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'B',
            quantity: 2,
            unitPrice: 2.5
        ),
        epson.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'C',
            quantity: 3,
            unitPrice: 3
        ),
    ],
    payments: [
      epson.Payment(
            description: 'Payment in cash',
            payment: 19
      )
    ]
));

// 财政报告
await fprinter.printFiscalReport(epson.Report(
    type: Fiscal.ReportType.DAILY_FISCAL_CLOUSE,
));

自定义协议示例

注意:单位数量乘以1000,单位价格乘以1000,包括折扣和支付

// 创建客户端
const fprinter = new CustomXmlHttpClient(custom.Config(
    host: '192.168.1.1',
    fiscalId: 'STMTE500432', // 11位数字
));

// 财政收据
await fprinter.printFiscalReceipt(custom.Receipt(
    sales: [
        custom.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'A',
            quantity: 1 * 1000,
            unitPrice: 5 * 100
        ),
        custom.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'B',
            quantity: 2 * 1000,
            unitPrice: 2.5 * 100
        ),
        custom.Sale(
            type: Fiscal.ItemType.HOLD,
            description: 'C',
            quantity: 3 * 1000,
            unitPrice: 3 * 100
        ),
    ],
    payments: [
        custom.Payment(
            description: 'Payment in cash',
            payment: 19 * 100,
            paymentType: 1
        )
    ]
));

// 财政退款
// 步骤1:请求确认是否可以作废,如果responseBuf === 1则执行步骤2
await fprinter.printCancel(custom.Cancel(
    docRefZ: '0021',
    docRefNumber: '0034',
    docDate: '011022', // DDMMYY
    printPreview: CustomProtocol.EnableType.DISABLE,
    fiscalSerial: 'STMTE500432',
    checkOnly: CustomProtocol.EnableType.ABLE, 
    codLottery: 'ASDSFES7',
));

// 步骤2:执行实际的作废请求
await fprinter.printCancel(custom.Cancel(
    docRefZ: '0021',
    docRefNumber: '0034',
    docDate: '011022', // DDMMYY
    printPreview: CustomProtocol.EnableType.DISABLE,
    fiscalSerial: 'STMTE500432',
    checkOnly: CustomProtocol.EnableType.DISABLE,
    codLottery: 'ASDSFES7',
));

// 财政报告
await fprinter.printFiscalReport(custom.Report(
    type: CustomProtocol.ReportType.DAILY_FISCAL_CLOUSE,
));

// 财政命令
await fprinter.executeCommand([custom.Command(
    code: CustomProtocol.CommandCode.OPEN_DRAWER
)]);

已实现功能

Epson 自定义
Fiscal ePOS-Print XML

### 示例代码

```dart
// example/example.dart
void main(List<String> args) {
  // 创建客户端
  final fprinter = new EpsonXmlHttpClient(epson.Config(
    host: '192.168.1.1',
    deviceId: 'local_printer',
    timeout: 10000
  ));

  // 财政收据
  fprinter.printFiscalReceipt(epson.Receipt(
    sales: [
      epson.Sale(
        type: Fiscal.ItemType.HOLD,
        description: 'A',
        quantity: 1,
        unitPrice: 5
      ),
      epson.Sale(
        type: Fiscal.ItemType.HOLD,
        description: 'B',
        quantity: 2,
        unitPrice: 2.5
      ),
      epson.Sale(
        type: Fiscal.ItemType.HOLD,
        description: 'C',
        quantity: 3,
        unitPrice: 3
      ),
    ],
    payments: [
      epson.Payment(
        description: 'Payment in cash',
        payment: 19
      )
    ]
  )).then((_) {
    // 财政报告
    fprinter.printFiscalReport(epson.Report(
      type: Fiscal.ReportType.DAILY_FISCAL_CLOUSE,
    ));
  });
}

更多关于Flutter财政打印机集成插件yjy_fiscal_printer的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter财政打印机集成插件yjy_fiscal_printer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


yjy_fiscal_printer 是一个用于在 Flutter 应用中集成财政打印机的插件。财政打印机通常用于打印收据、发票等财务相关的文档。以下是使用 yjy_fiscal_printer 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 yjy_fiscal_printer 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  yjy_fiscal_printer: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 以获取依赖。

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:yjy_fiscal_printer/yjy_fiscal_printer.dart';

3. 初始化打印机

在使用打印机之前,需要先初始化打印机。通常,你可以在应用的启动时进行初始化:

void initPrinter() async {
  try {
    await YjyFiscalPrinter.init();
    print("Printer initialized successfully");
  } catch (e) {
    print("Failed to initialize printer: $e");
  }
}

4. 打印收据

你可以使用 printReceipt 方法来打印收据。以下是一个简单的示例:

void printReceipt() async {
  try {
    await YjyFiscalPrinter.printReceipt(
      items: [
        {"name": "Item 1", "quantity": 1, "price": 10.0},
        {"name": "Item 2", "quantity": 2, "price": 15.0},
      ],
      total: 40.0,
      tax: 4.0,
    );
    print("Receipt printed successfully");
  } catch (e) {
    print("Failed to print receipt: $e");
  }
}

5. 处理错误

在实际使用中,可能会遇到各种错误,例如打印机未连接、纸张用完等。你可以在调用打印方法时捕获并处理这些错误。

6. 其他功能

yjy_fiscal_printer 插件可能还提供其他功能,例如打印发票、查询打印机状态等。你可以查阅插件的文档或源码以获取更多信息。

7. 示例代码

以下是一个完整的示例代码,展示了如何初始化和打印收据:

import 'package:flutter/material.dart';
import 'package:yjy_fiscal_printer/yjy_fiscal_printer.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fiscal Printer Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              initPrinter();
              printReceipt();
            },
            child: Text('Print Receipt'),
          ),
        ),
      ),
    );
  }
}

void initPrinter() async {
  try {
    await YjyFiscalPrinter.init();
    print("Printer initialized successfully");
  } catch (e) {
    print("Failed to initialize printer: $e");
  }
}

void printReceipt() async {
  try {
    await YjyFiscalPrinter.printReceipt(
      items: [
        {"name": "Item 1", "quantity": 1, "price": 10.0},
        {"name": "Item 2", "quantity": 2, "price": 15.0},
      ],
      total: 40.0,
      tax: 4.0,
    );
    print("Receipt printed successfully");
  } catch (e) {
    print("Failed to print receipt: $e");
  }
}
回到顶部