到目前为止,sgcSign 签署的是由您的应用程序自行构建的发票 XML。各国配置文件可以签署 Facturae、FatturaPA 或 TicketBAI 文件,但文件必须先存在,而生成文件是您的工作。
从 sgcSign 2026.12 开始,该库同样可以生成发票。您填写一张类型化的发票,库会计算合计金额,检查业务规则,并按接收方或税务机关要求的格式写出文档。各国配置文件对结果进行签署,三个客户端负责发送。本文介绍的所有功能均自 sgcSign 2026.12 起提供,适用于 Delphi 7 至 Delphi 13 和 .NET,并且已包含在 sgcSign 中。
一分钟了解 sgcSign 中的电子发票:发票模型、格式、校验以及通过 Peppol 发送。 YouTube 上也有。
基于 EN 16931 的类型化发票
TsgcInvoice 是 EN 16931 语义数据模型的类型化模型,涵盖卖方、买方、收款方、付款、明细行、折扣与附加费、增值税明细和合计。该模型不含 XML,因此同一张发票可以用多种语法写出。CalculateTotals 会填写各行净额、增值税明细和单据合计,应用程序只需设置数量、单价和增值税率,不必自己汇总。
TsgcInvoiceUBL 写入和读取 UBL 2.1 的 Invoice 和 CreditNote(贷项通知单)。TsgcInvoiceCII 写入和读取 UN/CEFACT Cross Industry Invoice D16B。下面的示例填写一张发票,计算合计,检查 EN 16931 规则,并写出一个 UBL 2.1 文件。
uses
Classes, SysUtils,
sgcSign_Invoice, sgcSign_Invoice_UBL, sgcSign_Invoice_Validator;
procedure CreateInvoice;
var
oInvoice: TsgcInvoice;
oLine: TsgcInvoiceLine;
oValidator: TsgcInvoiceValidator;
oUBL: TsgcInvoiceUBL;
begin
oInvoice := TsgcInvoice.Create;
oValidator := TsgcInvoiceValidator.Create;
oUBL := TsgcInvoiceUBL.Create;
try
oInvoice.Number := 'INV-2026-0001';
oInvoice.IssueDate := EncodeDate(2026, 11, 11);
oInvoice.DueDate := EncodeDate(2026, 12, 11);
oInvoice.CurrencyCode := 'EUR';
oInvoice.SpecificationIdentifier :=
CS_SGC_INVOICE_UBL_CUSTOMIZATION_EN16931;
oInvoice.BuyerReference := 'PO-7781';
oInvoice.Seller.Name := 'Seller Company SL';
oInvoice.Seller.VATID := 'ESB12345674';
oInvoice.Seller.Address.City := 'Madrid';
oInvoice.Seller.Address.CountryCode := 'ES';
oInvoice.Buyer.Name := 'Buyer Company GmbH';
oInvoice.Buyer.VATID := 'DE123456789';
oInvoice.Buyer.Address.City := 'Berlin';
oInvoice.Buyer.Address.CountryCode := 'DE';
oLine := oInvoice.Lines.Add;
oLine.ID := '1';
oLine.Item.Name := 'Consulting';
oLine.Quantity := 10;
oLine.UnitCode := CS_SGC_INVOICE_UNIT_HOUR;
oLine.Price.NetPrice := 85;
oLine.VATCategoryCode := CS_SGC_INVOICE_VAT_STANDARD;
oLine.VATRate := 21;
// line amounts, VAT breakdown and totals
oInvoice.CalculateTotals;
// EN 16931 business rules
if not oValidator.Validate(oInvoice) then
raise Exception.Create(oValidator.AsText);
// UBL 2.1 document
oUBL.WriteToFile(oInvoice, 'invoice.xml');
finally
oUBL.Free;
oValidator.Free;
oInvoice.Free;
end;
end;
XRechnung、Peppol BIS 和 Factur-X
大多数接收方要求的并不是纯粹的 EN 16931,而是它的某个配置文件。sgcSign 2026.12 可以基于同一个模型写入和读取其中三种:
- XRechnung,支持 UBL 或 CII 语法。
- Peppol BIS Billing 3.0,使用 UBL 语法。
- Factur-X / ZUGFeRD,五个配置文件的 XML:Minimum、Basic WL、Basic、EN 16931 和 Extended,通过
TsgcInvoiceFacturX实现。
写入某个配置文件不会改变发票本身,因此同一个模型可以为一位客户写成 XRechnung,为下一位客户写成 Peppol BIS。收到发票时,sgcInvoiceReadAny 会检测文件的语法和配置文件,并将其加载到模型中。
使用官方规则编号进行校验
TsgcInvoiceValidator 根据 EN 16931 业务规则检查发票,需要时还会根据 XRechnung 和 Peppol BIS Billing 3.0 规则集检查。这些规则以原生方式编码,在写出任何 XML 之前直接在模型上运行。每条消息都带有官方规则标识符、严重级别、业务术语、字段在模型中的路径以及规则文本,因此应用程序可以告诉用户应更正哪个字段。
下面的示例对同一张发票校验两次。它先通过 EN 16931 规则,然后按 XRechnung 检查,而 XRechnung 要求提供卖方联系人,核心模型并不要求这一项。FillInvoice 填写第一个示例中的发票,并补充双方的街道和邮政编码,以及 XRechnung 所要求的 SEPA 转账付款。Log 输出一行文本。
uses
SysUtils, Classes,
sgcSign_Invoice, sgcSign_Invoice_Validator, sgcSign_Invoice_Variants;
procedure RuleSetsSample;
var
oInvoice: TsgcInvoice;
oValidator: TsgcInvoiceValidator;
i: Integer;
begin
oInvoice := TsgcInvoice.Create;
oValidator := TsgcInvoiceValidator.Create;
try
FillInvoice(oInvoice);
oInvoice.CalculateTotals;
Log(BoolToStr(oValidator.Validate(oInvoice), True)); // True
// the same invoice as XRechnung
sgcInvoiceApplyVariant(oInvoice, ivXRechnung, isUBL);
oValidator.RuleSets := [vrsEN16931, vrsXRechnung];
if not oValidator.Validate(oInvoice) then
for i := 0 to oValidator.Messages.Count - 1 do
Log(oValidator.Messages[i].RuleID + ' ' + oValidator.Messages[i].Path);
// what XRechnung asks for and the core model does not
oInvoice.Seller.Contact.Name := 'Ana Lopez';
oInvoice.Seller.Contact.Telephone := '+34 910 000 000';
oInvoice.Seller.Contact.Email := 'ana@seller.example';
oInvoice.Delivery.ActualDeliveryDate := EncodeDate(2026, 11, 10);
Log(BoolToStr(oValidator.Validate(oInvoice), True)); // True
Log(IntToStr(oValidator.WarningCount)); // 0
finally
oValidator.Free;
oInvoice.Free;
end;
end;
第二次校验输出两条消息,分别是 BR-DE-2 Seller.Contact 和 BR-DE-TMP-32 Delivery.ActualDeliveryDate。前者是致命错误,后者是警告。设置好联系人和交付日期后,发票即可通过校验,且没有任何警告。
各国格式
有三个国家使用自己的发票格式,每种格式都有一个接收同一个 TsgcInvoice 的写入器:
- Facturae,西班牙:
TsgcInvoiceFacturae写入 3.2、3.2.1 和 3.2.2 版本。 - FatturaPA,意大利:
TsgcInvoiceFatturaPA写入 1.2.3 版本,适用于私营接收方 (FPR12) 和公共行政机构 (FPA12)。 - KSeF,波兰:
TsgcInvoiceKSeF写入结构化发票,模式为 FA(3)。
同时还支持两个西班牙税务系统:
- VeriFactu:
TsgcInvoiceVeriFactu生成带有链式 SHA-256 哈希的登记记录和取消记录,维护记录之间的链条,并生成二维码的 URL 和图像。 - TicketBAI:
TsgcInvoiceTicketBAI生成 1.2.2 版的发票文件和取消文件,并链接到上一张发票。签署之后,它会给出 TBAI 标识符和二维码。
| 格式 | 类 | 写入 | 读取 |
|---|---|---|---|
| UBL 2.1 Invoice 和 CreditNote | TsgcInvoiceUBL | 是 | 是 |
| UN/CEFACT CII D16B | TsgcInvoiceCII | 是 | 是 |
| XRechnung,UBL 或 CII | TsgcInvoiceDocument | 是 | 是 |
| Peppol BIS Billing 3.0,UBL | TsgcInvoiceDocument | 是 | 是 |
| Factur-X / ZUGFeRD XML,五个配置文件 | TsgcInvoiceFacturX | 是 | 是 |
| Facturae 3.2、3.2.1 和 3.2.2 | TsgcInvoiceFacturae | 是 | 否 |
| FatturaPA 1.2.3 | TsgcInvoiceFatturaPA | 是 | 否 |
| KSeF FA(3) | TsgcInvoiceKSeF | 是 | 否 |
| VeriFactu 记录 | TsgcInvoiceVeriFactu | 是 | 否 |
| TicketBAI 1.2.2 | TsgcInvoiceTicketBAI | 是 | 否 |
发送发票
三个客户端负责把文档送到税务机关:
TsgcVeriFactuClient向 AEAT Web 服务提交 VeriFactu 记录。它读取每条记录的结果,遵守记录数量上限和提交之间的等待时间,查询已提交的记录,并保存回执。TsgcSdIClient将 FatturaPA 文件发送到意大利 Sistema di Interscambio (SdI) 的 Web 服务,检查文件名规则,并读取 SdI 返回的通知文件。TsgcKSeFOnlineClient在交互式会话中向 KSeF 发送发票:打开会话,发送发票,读取其状态和 KSeF 编号,关闭会话,并下载 UPO 回执。
通过 Peppol 网络发送
2026.12 版本还可以在 Peppol 网络上发送发票,支持 Delphi 和 .NET。各个部分是独立的类,因此可以使用整个流程,也可以只使用其中一步:
TsgcPeppolSMPClient通过 SML 和 SMP 查询参与者并验证已签名的 SMP 应答。TsgcPeppolSBDH和sgcPeppolWrapInvoice写入 Peppol 业务消息信封,即 SBDH。TsgcPeppolAS4Message和TsgcPeppolAS4Receipt实现 Peppol AS4 配置文件的 AS4 消息:使用 RSA-SHA256 签名,负载用 gzip 压缩并用 AES-128-GCM 加密,密钥传输使用 RSA-OAEP。TsgcPeppolClient一次调用即可完成查询、信封封装、发送和回执验证,使用SendInvoice和Resend。它会保存已签名的回执,并返回带有错误代码的结果对象。
无需外部库:加密、XML 和 AS4 代码均包含在库中,适用于 Delphi 7 到 Delphi 13。下面的示例使用 TsgcPeppolClient 发送一个发票文件。
uses
Classes, SysUtils,
sgcSign_Interfaces, sgcSign_Peppol_Client;
procedure SendInvoiceToPeppol(const aAccessPointKey: IsgcKeyProvider);
var
oClient: TsgcPeppolClient;
oResult: TsgcPeppolSendResult;
begin
oClient := TsgcPeppolClient.Create(nil);
oResult := TsgcPeppolSendResult.Create;
try
// key and certificate of the sending access point
oClient.KeyProvider := aAccessPointKey;
oClient.TestNetwork := True;
// the company that issues the invoice and its country
oClient.SenderID := '0088:7315458756324';
oClient.CountryC1 := 'ES';
// lookup, envelope, AS4 message, POST and receipt in one call
if oClient.SendInvoiceFile('invoice.xml', '9915:receiver', oResult) then
WriteLn('Delivered, message ' + oResult.MessageId)
else
WriteLn(oResult.ErrorCode + ': ' + oResult.ErrorText);
finally
oResult.Free;
oClient.Free;
end;
end;
在围绕此功能规划项目之前,请先阅读这些要求。要在 Peppol 网络上发送,必须是经认证的 Peppol 接入点(服务提供商),并持有 OpenPeppol 签发的接入点证书。该库实现协议,但不提供证书或认证。使用服务提供商的公司通常通过该提供商发送。该库仅负责发送:没有接收接入点,也没有 Peppol 报告。参与者查询不需要证书。
发送方已针对开源 phase4 AS4 服务器(版本 4.9.0)在本地测试 PKI 上完成互操作性测试:Delphi 和 .NET 库发出的每条消息都被接受,每个回执都通过验证。它尚未在生产接入点上运行,也未使用真实的 Peppol 证书。Demos\Delphi\Peppol 和 Demos\CBuilder\Peppol 演示随库一起提供。
一个组件完成整个流程
TsgcInvoiceDocument 把这些步骤组合在一起。选择格式,填写发票,然后调用 Build。组件会计算合计,使用该格式的规则集进行校验,写出文档,并用该格式的签名配置文件对其签名。校验失败时不会写出任何内容,并且任何 sgcSign 密钥提供程序都可以为文档签名。
下面的示例以 EN 16931 配置文件写出 Factur-X XML,并使用 PFX 文件中的证书对其签名。
uses
Classes, SysUtils,
sgcSign_Types, sgcSign_KeyProvider_PFX,
sgcSign_Invoice, sgcSign_Invoice_Document;
procedure BuildSignedInvoice;
var
oKeyProvider: TsgcPFXKeyProvider;
oDocument: TsgcInvoiceDocument;
begin
oKeyProvider := TsgcPFXKeyProvider.Create(nil);
oDocument := TsgcInvoiceDocument.Create(nil);
try
oKeyProvider.FileName := 'certificate.pfx';
oKeyProvider.Password := 'secret';
oKeyProvider.LoadFromFile;
// FillInvoice sets seller, buyer and lines as in the previous example
FillInvoice(oDocument.Invoice);
oDocument.Format := ifFacturXEN16931;
oDocument.Sign := True;
oDocument.KeyProvider := oKeyProvider;
// totals, validation, Factur-X XML and XAdES signature
oDocument.BuildToFile('factur-x.xml');
finally
oDocument.Free;
oKeyProvider.Free;
end;
end;
该组件同样可以读取。LoadFromFile 接收 UBL 或 CII 文件,把 Format 设置为文件对应的格式并填充模型,因此收到的发票可以先校验,再用另一种语法重新写出。
不包含的内容
第一个版本的范围有一定限制,在围绕它规划项目之前最好先了解这些限制。
- 各国格式只能写入。UBL 和 CII 及其 XRechnung、Peppol BIS 和 Factur-X 变体可以读回到模型中,而 Facturae、FatturaPA、KSeF、VeriFactu 和 TicketBAI 不能。
- Peppol 发送需要经认证的接入点,且仅支持发送。该库实现协议,但不提供接入点证书或认证。没有接收接入点,也没有 Peppol 报告。
- Factur-X 只是 XML。Factur-X 或 ZUGFeRD 发票是嵌入了该 XML 的 PDF/A-3 文件。组件写出 XML,但不会把它嵌入 PDF/A-3 文件。
- 组件只发送 KSeF 发票,不对其签名,因为 KSeF 不对发票 XML 签名。
- 未内置 SdI 端点。SdI 在通道认证期间把端点提供给发送方,您需要在客户端上设置它们。
如何获取
电子发票自 2026.12 版本起成为 sgcSign 的一部分。它不是单独的版本:这些单元随 sgcSign 一起提供,附带完整源代码,适用于 Delphi 7 至 Delphi 13 和 .NET。Demos 文件夹中新增了 EInvoice 演示,它在一个窗体中填写一张类型化发票,并用一个组件以每种格式生成该发票。
产品页面上的电子发票 · 下载试用版 · 更新日志
有问题或反馈?联系我们,您将收到编写这些代码的人员的回复。
