« MBS @ FMTraining.TV -… | Home | Company's 24th Birthd… »

Read Invoices from P7M files in Italy

Electronic invoices in Italia are nowadays sent as P7M files, digitally signed with a XML and PDF content. Checkout the details about FatturaPA format if you are interested. For storing XML in a PDF or extracting it, please check our DynaPDF functions. Since you may need to receive and decode such P7M invoices, even if you are not in Italy yourself, we have some sample code to handle this below:

 

First thing is to use PKCS7.ReadFromContainer or PKCS7.ReadFromFile to read in the P7M file and get the reference number. If that works, we can query the main content block of the P7M file with the PKCS7.SignedDataContent function. That block is the XML embedded in the P7M file.

Second, we use XML.GetPathValue functions to query the XML for the attachment name with "FatturaElettronica.FatturaElettronicaBody.Allegati.NomeAttachment" and the content with "FatturaElettronica.FatturaElettronicaBody.Allegati.Attachment". You may also query all the other values from the invoice since you have the XML.

 

Third, we use Base64Decode function to get the PDF decoded and put it in a field. This allows us to look on the invoice and optionally print it. You don't need to parse the PDF since the data in the XML is sufficient to fill in records about the invoice.

 

e.g. the invoice number with:

MBS( "XML.GetPathValue"; $XML; "FatturaElettronica.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Numero")

Or the date of the invoice:

MBS( "XML.GetPathValue"; $XML; "FatturaElettronica.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Data")

 

The full sample script is here:

 

# Extract in file Read Italian Invoice

 

Set Variable []

If [ IsEmpty ( Read Italian Invoice::P7M File ) ]

Insert File [ Filters ; Insert ; Never compress ]

End If

If [ not IsEmpty ( Read Italian Invoice::P7M File ) ]

Set Variable [ $p ; Value: MBS( "PKCS7.ReadFromContainer"; Read Italian Invoice::P7M File ) ]

If [ MBS("IsError") ]

Show Custom Dialog [ "Failed to read P7M file" ; $p ]

Else

Set Variable [ $xml ; Value: MBS( "PKCS7.SignedDataContent"; $p ; "UTF-8") ]

If [ MBS("IsError") ]

Show Custom Dialog [ "Failed to read XML file" ; $xml ]

Else

Set Field [ Read Italian Invoice::XML Read Italian Invoice::XML ; $xml $xml ]

Set Variable [ $name ; Value: MBS( "XML.GetPathValue"; Read Italian Invoice::XML; "FatturaElettronica.FatturaElettronicaBody.Allegati.NomeAttachment") ]

Set Field [ Read Italian Invoice::Name Read Italian Invoice::Name ; $name $name ]

Set Variable [ $attachment ; Value: MBS( "XML.GetPathValue"; Read Italian Invoice::XML; "FatturaElettronica.FatturaElettronicaBody.Allegati.Attachment") ]

Set Field [ Read Italian Invoice::PDF Read Italian Invoice::PDF ; Base64Decode ( $attachment; $name ) Base64Decode ( $attachment; $name ) ]

End If

Set Variable [ $r ; Value: MBS( "PKCS7.Release"; $p ) ]

End If

End If

 

The example file will be included for future plugin releases. Let us know if you have questions.

Claris FileMaker Plugin
24 03 24 - 08:11