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:
- XRechnung, in the UBL or the CII syntax.
- Peppol BIS Billing 3.0, in the UBL syntax.
- Factur-X / ZUGFeRD, the XML of the five profiles: Minimum, Basic WL, Basic, EN 16931 and Extended, through
TsgcInvoiceFacturX.
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:
- Facturae, Spain:
TsgcInvoiceFacturaewrites versions 3.2, 3.2.1 and 3.2.2. - FatturaPA, Italy:
TsgcInvoiceFatturaPAwrites version 1.2.3 for private recipients (FPR12) and for the public administration (FPA12). - KSeF, Poland:
TsgcInvoiceKSeFwrites the structured invoice, schema FA(3).
Two Spanish fiscal systems are covered as well:
- VeriFactu:
TsgcInvoiceVeriFactubuilds the registration and cancellation records with the chained SHA-256 hash, keeps the chain between records and produces the QR code URL and image. - TicketBAI:
TsgcInvoiceTicketBAIbuilds the 1.2.2 invoice and cancellation files with the chain to the previous invoice. After signing it gives the TBAI identifier and the QR code.
| Format | Class | Write | Read |
|---|---|---|---|
| UBL 2.1 Invoice and CreditNote | TsgcInvoiceUBL | Yes | Yes |
| UN/CEFACT CII D16B | TsgcInvoiceCII | Yes | Yes |
| XRechnung, UBL or CII | TsgcInvoiceDocument | Yes | Yes |
| Peppol BIS Billing 3.0, UBL | TsgcInvoiceDocument | Yes | Yes |
| Factur-X / ZUGFeRD XML, five profiles | TsgcInvoiceFacturX | Yes | Yes |
| Facturae 3.2, 3.2.1 and 3.2.2 | TsgcInvoiceFacturae | Yes | No |
| FatturaPA 1.2.3 | TsgcInvoiceFatturaPA | Yes | No |
| KSeF FA(3) | TsgcInvoiceKSeF | Yes | No |
| VeriFactu records | TsgcInvoiceVeriFactu | Yes | No |
| TicketBAI 1.2.2 | TsgcInvoiceTicketBAI | Yes | No |
Sending the Invoice
Three clients take the document to the tax authority:
TsgcVeriFactuClientsubmits VeriFactu records to the AEAT web service. It reads the result of each record, applies the record limit and the waiting time between submissions, queries submitted records and stores the receipts.TsgcSdIClientsends a FatturaPA file to the Italian Sistema di Interscambio (SdI) web service, checks the file name rule and reads the notification files that SdI returns.TsgcKSeFOnlineClientsends invoices to KSeF in an interactive session: it opens the session, sends the invoice, reads its status and KSeF number, closes the session and downloads the UPO receipt.
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:
TsgcPeppolSMPClientlooks up the participant through SML and SMP and verifies the signed SMP answer.TsgcPeppolSBDHandsgcPeppolWrapInvoicewrite the Peppol business message envelope, the SBDH.TsgcPeppolAS4MessageandTsgcPeppolAS4Receiptimplement the AS4 message of the Peppol AS4 profile: signed with RSA-SHA256, payload compressed with gzip and encrypted with AES-128-GCM, key transport RSA-OAEP.TsgcPeppolClientdoes lookup, envelope, send and receipt verification in one call, withSendInvoiceandResend. It stores the signed receipt and returns a result object with error codes.
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.
- The national formats are write only. UBL and CII, with their XRechnung, Peppol BIS and Factur-X variants, can be read back into the model. Facturae, FatturaPA, KSeF, VeriFactu and TicketBAI cannot.
- Peppol sending needs an accredited access point, and it is sending only. The library implements the protocol and does not provide the access point certificate or the accreditation. There is no receiving access point and no Peppol reporting.
- Factur-X is the XML only. A Factur-X or ZUGFeRD invoice is a PDF/A-3 file with this XML embedded. The component writes the XML and does not embed it in a PDF/A-3 file.
- KSeF invoices are sent, not signed, by the component, because KSeF does not sign the invoice XML.
- The SdI endpoints are not built in. The SdI gives its endpoints to the sender during the accreditation of the channel, and you set them on the client.
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.
