sgcSign 2026.12 中的电子发票:生成、校验并签署发票

· 组件
sgcSign 2026.12 中的电子发票,在 Delphi 和 .NET 中生成、校验并签署发票

到目前为止,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,为下一位客户写成 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 的写入器:

同时还支持两个西班牙税务系统:

格式类写入读取
UBL 2.1 Invoice 和 CreditNoteTsgcInvoiceUBL是是
UN/CEFACT CII D16BTsgcInvoiceCII是是
XRechnung,UBL 或 CIITsgcInvoiceDocument是是
Peppol BIS Billing 3.0,UBLTsgcInvoiceDocument是是
Factur-X / ZUGFeRD XML,五个配置文件TsgcInvoiceFacturX是是
Facturae 3.2、3.2.1 和 3.2.2TsgcInvoiceFacturae是否
FatturaPA 1.2.3TsgcInvoiceFatturaPA是否
KSeF FA(3)TsgcInvoiceKSeF是否
VeriFactu 记录TsgcInvoiceVeriFactu是否
TicketBAI 1.2.2TsgcInvoiceTicketBAI是否

发送发票

三个客户端负责把文档送到税务机关:

通过 Peppol 网络发送

2026.12 版本还可以在 Peppol 网络上发送发票,支持 Delphi 和 .NET。各个部分是独立的类,因此可以使用整个流程,也可以只使用其中一步:

无需外部库:加密、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 设置为文件对应的格式并填充模型,因此收到的发票可以先校验,再用另一种语法重新写出。

不包含的内容

第一个版本的范围有一定限制,在围绕它规划项目之前最好先了解这些限制。

如何获取

电子发票自 2026.12 版本起成为 sgcSign 的一部分。它不是单独的版本:这些单元随 sgcSign 一起提供,附带完整源代码,适用于 Delphi 7 至 Delphi 13 和 .NET。Demos 文件夹中新增了 EInvoice 演示,它在一个窗体中填写一张类型化发票,并用一个组件以每种格式生成该发票。

产品页面上的电子发票 · 下载试用版 · 更新日志

有问题或反馈?联系我们,您将收到编写这些代码的人员的回复。