E-Invoicing in sgcSign 2026.12: Build, Validate and Sign the Invoice

· Components
E-invoicing in sgcSign 2026.12, build, validate and sign invoices in Delphi and .NET

Until now sgcSign signed an invoice XML that your application had to build itself. The country profiles knew how to sign a Facturae, a FatturaPA or a TicketBAI file, but the file had to exist first, and writing it was your job.

From sgcSign 2026.12 the library also builds the invoice. You fill a typed invoice, the library calculates the totals, checks the business rules and writes the document in the format that the receiver or the tax authority asks for. The country profiles sign the result, and three clients send it. Everything described in this post is available from sgcSign 2026.12, for Delphi 7 to Delphi 13 and .NET, and it is included in sgcSign.

E-invoicing in sgcSign in one minute: the invoice model, the formats, validation and Peppol sending. Also on YouTube.

A Typed Invoice on EN 16931

TsgcInvoice is a typed model of the EN 16931 semantic data model: seller, buyer, payee, payment, lines, allowances and charges, VAT breakdown and totals. The model holds no XML, so the same invoice can be written in more than one syntax. CalculateTotals fills the line net amounts, the VAT breakdown and the document totals, so the application sets quantities, prices and VAT rates and does not add anything up.

TsgcInvoiceUBL writes and reads the UBL 2.1 Invoice and CreditNote. TsgcInvoiceCII writes and reads the UN/CEFACT Cross Industry Invoice D16B. The example below fills an invoice, calculates the totals, checks the EN 16931 rules and writes a UBL 2.1 file.

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 and Factur-X

Most receivers do not ask for plain EN 16931. They ask for a profile of it. sgcSign 2026.12 writes and reads three of them from the same model:

Writing a profile does not change the invoice, so one model can be written as XRechnung for one customer and as Peppol BIS for the next. When an invoice arrives, sgcInvoiceReadAny detects the syntax and the profile of the file and loads it into the model.

Validation with the Official Rule Ids

TsgcInvoiceValidator checks an invoice against the EN 16931 business rules, and against the XRechnung and Peppol BIS Billing 3.0 rule sets when you ask for them. The rules are coded natively and run on the model, before any XML is written. Every message carries the official rule identifier, the severity, the business term, the path of the field in the model and the rule text, so the application can show the user which field to correct.

The example below validates the same invoice twice. It passes the EN 16931 rules, then it is checked as an XRechnung, which asks for a seller contact that the core model does not require. FillInvoice fills the invoice of the first example and adds the street and postal code of both parties and a SEPA credit transfer, which XRechnung requires. Log prints a line.

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;

The second validation prints two messages, BR-DE-2 Seller.Contact and BR-DE-TMP-32 Delivery.ActualDeliveryDate. The first is a fatal error, the second is a warning. Once the contact and the delivery date are set, the invoice passes with no warnings.

National Formats

Three countries use an invoice format of their own, and each one has a writer that takes the same TsgcInvoice:

Two Spanish fiscal systems are covered as well:

FormatClassWriteRead
UBL 2.1 Invoice and CreditNoteTsgcInvoiceUBLYesYes
UN/CEFACT CII D16BTsgcInvoiceCIIYesYes
XRechnung, UBL or CIITsgcInvoiceDocumentYesYes
Peppol BIS Billing 3.0, UBLTsgcInvoiceDocumentYesYes
Factur-X / ZUGFeRD XML, five profilesTsgcInvoiceFacturXYesYes
Facturae 3.2, 3.2.1 and 3.2.2TsgcInvoiceFacturaeYesNo
FatturaPA 1.2.3TsgcInvoiceFatturaPAYesNo
KSeF FA(3)TsgcInvoiceKSeFYesNo
VeriFactu recordsTsgcInvoiceVeriFactuYesNo
TicketBAI 1.2.2TsgcInvoiceTicketBAIYesNo

Sending the Invoice

Three clients take the document to the tax authority:

Sending on the Peppol Network

Version 2026.12 also sends the invoice on the Peppol network, in Delphi and .NET. The parts are separate classes, so you can use the whole flow or one step of it:

No external libraries are needed: the cryptography, XML and AS4 code are part of the library, for Delphi 7 to Delphi 13. The example below sends an invoice file with 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;

Read the requirements before you plan a project around this. Sending on the Peppol network requires being an accredited Peppol access point (service provider) with an access point certificate issued by OpenPeppol. The library implements the protocol and does not provide the certificate or the accreditation. Companies that use a service provider normally send through that provider. The library sends only: there is no receiving access point and no Peppol reporting. The participant lookup needs no certificate.

The sender was tested for interoperability against the open source phase4 AS4 server (version 4.9.0) on a local test PKI: every message was accepted and every receipt verified, with the Delphi and .NET libraries. It has not been run against a production access point or with real Peppol certificates. The Demos\Delphi\Peppol and Demos\CBuilder\Peppol demos come with the library.

One Component for the Whole Flow

TsgcInvoiceDocument puts the steps together. Choose the format, fill the invoice and call Build. The component calculates the totals, validates with the rule sets of the format, writes the document and signs it with the signing profile of the format. Nothing is written when the validation fails, and any sgcSign key provider can sign the document.

The example below writes the Factur-X XML in the EN 16931 profile and signs it with a certificate from a PFX file.

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;

The component also reads. LoadFromFile takes a UBL or a CII file, sets Format to what the file is and fills the model, so a received invoice can be validated and written again in the other syntax.

What Is Not Included

The scope of this first version has limits, and it is better to know them before you plan a project around it.

Getting It

E-invoicing is part of sgcSign from version 2026.12. It is not a separate edition: the units ship with sgcSign, with full source code, for Delphi 7 to Delphi 13 and .NET. A new EInvoice demo in the Demos folder fills one typed invoice in a form and builds it in each of the formats with a single component.

E-invoicing on the product page · Download the trial · Changelog

Questions or feedback? Get in touch, you will get a reply from the people who wrote the code.