Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision d472ce56

Von Johannes Grassler vor mehr als 1 Jahr hinzugefügt

  • ID d472ce560fa4732ee8b16213a7bdb614703551aa
  • Vorgänger 34e66c3e
  • Nachfolger bb889114

ZUGFeRD-Import auf SL::XMLInvoice umgestellt

Wichtigste Aenderung dieses Commits ist die Umstellung des
ZUGFeRD-Imports in der Finanzbuchhaltung auf das neu
hinzugefuegte Modul SL::XMLInvoice, das auch die Verabeitung
von Rechnungen im XRechnung-Format erlaubt. Darueber hinaus
gibt es einige weitere Aenderungen:

  • Datenformate: Neben ZUGFeRD wird nun auch XRechnung
    unterstuetzt.
  • Fehlertoleranz: Fehlende ZUGFeRD-Metadaten erzeugen nur noch
    Warnungen, saemtliche XML-Anhaenge an PDF-Dateien werden
    automatisch erkannt und verarbeitet (egal ob es sich um
    ZUGFeRD/Faktur-X- oder XRechnung-Daten handelt).
  • Upload von reinen XML-Dateien ist nun auch moeglich (fuer
    Rechnungen im XRechnung-Format wichtig).
  • Die Posten der Rechnung werden nun auch automatisch in das
    Formular eingetragen, nicht mehr nur Rechnungsnummer und
    Datum.
  • Es muss keine Belegvorlage mehr fuer den Lieferanten
    existieren. Eine eigene Belegvorlage wird fuer jede
    importierte Rechnung automatisch angelegt.
  • Es wird automatisch ein Gegenkonto fuer die Buchung
    aus den Verbindlichkeitskonten ausgewaehlt.
  • Das Faelligkeitsdatum der Rechnung wird immer gesetzt.
    Enthaelt die Rechnung keines, wird es auf das Rechnungsdatum
    gesetzt.

Unterschiede anzeigen:

SL/Controller/ZUGFeRD.pm
package SL::Controller::ZUGFeRD;
use strict;
use warnings;
use parent qw(SL::Controller::Base);
use SL::DB::RecordTemplate;
use SL::Locale::String qw(t8);
use SL::Helper::DateTime;
use SL::XMLInvoice;
use SL::VATIDNr;
use SL::ZUGFeRD;
use SL::SessionFile;
......
$self->render('zugferd/form', title => $::locale->text('Factur-X/ZUGFeRD import'));
}
sub action_import_zugferd {
my ($self, %params) = @_;
my $file = $::form->{file};
my $file_name = $::form->{file_name};
sub find_vendor_by_taxnumber {
my $taxnumber = shift @_;
die t8("missing file for action import") unless $file;
die t8("can only parse a pdf file") unless $file =~ m/^%PDF/;
# 1.1 check if we a have a vendor with this tax number (vendor.taxnumber)
my $vendor = SL::DB::Manager::Vendor->find_by(
taxnumber => $taxnumber,
or => [
obsolete => undef,
obsolete => 0,
]);
if (!$vendor) {
# 1.2 If no vendor with the exact VAT ID number is found, the
# number might be stored slightly different in the database
# (e.g. with spaces breaking up groups of numbers). Iterate over
# all existing vendors with VAT ID numbers, normalize their
# representation and compare those.
my $info = SL::ZUGFeRD->extract_from_pdf($file);
my $vendors = SL::DB::Manager::Vendor->get_all(
where => [
'!taxnumber' => undef,
'!taxnumber' => '',
or => [
obsolete => undef,
obsolete => 0,
],
]);
if ($info->{result} != SL::ZUGFeRD::RES_OK()) {
# An error occurred; log message from parser:
$::lxdebug->message(LXDebug::DEBUG1(), "Could not extract ZUGFeRD data, error message: " . $info->{message});
die t8("Could not extract Factur-X/ZUGFeRD data, data and error message:") . $info->{message};
foreach my $other_vendor (@{ $vendors }) {
next unless $other_vendor->taxnumber eq $taxnumber;
$vendor = $other_vendor;
last;
}
}
# valid ZUGFeRD metadata
my $dom = XML::LibXML->load_xml(string => $info->{invoice_xml});
}
# 1. check if ZUGFeRD SellerTradeParty has a VAT-ID
my $ustid = $dom->findnodes('//ram:SellerTradeParty/ram:SpecifiedTaxRegistration')->string_value;
die t8("No VAT Info for this Factur-X/ZUGFeRD invoice," .
" please ask your vendor to add this for his Factur-X/ZUGFeRD data.") unless $ustid;
sub find_vendor_by_ustid {
my $ustid = shift @_;
$ustid = SL::VATIDNr->normalize($ustid);
# 1.1 check if we a have a vendor with this VAT-ID (vendor.ustid)
my $vc = $dom->findnodes('//ram:SellerTradeParty/ram:Name')->string_value;
my $vendor = SL::DB::Manager::Vendor->find_by(
ustid => $ustid,
or => [
......
}
}
die t8("Please add a valid VAT-ID for this vendor: #1", $vc) unless (ref $vendor eq 'SL::DB::Vendor');
return $vendor;
}
# 2. check if we have a ap record template for this vendor (TODO only the oldest template is choosen)
my $template_ap = SL::DB::Manager::RecordTemplate->get_first(where => [vendor_id => $vendor->id]);
die t8("No AP Record Template for this vendor found, please add one") unless (ref $template_ap eq 'SL::DB::RecordTemplate');
sub find_vendor {
my ($ustid, $taxnumber) = @_;
my $vendor;
if ( $ustid ) {
$vendor = find_vendor_by_ustid($ustid);
}
# 3. parse the zugferd data and fill the ap record template
# -> no need to check sign (credit notes will be negative) just record thei ZUGFeRD type in ap.notes
# -> check direct debit (defaults to no)
# -> set amount (net amount) and unset taxincluded
# (template and user cares for tax and if there is more than one booking accno)
# -> date (can be empty)
# -> duedate (may be empty)
# -> compare record iban and generate a warning if this differs from vendor's master data iban
my $total = $dom->findnodes('//ram:SpecifiedTradeSettlementHeaderMonetarySummation' .
'/ram:TaxBasisTotalAmount')->string_value;
if (ref $vendor eq 'SL::DB::Vendor') { return $vendor; }
my $invnumber = $dom->findnodes('//rsm:ExchangedDocument/ram:ID')->string_value;
if ( $taxnumber ) {
$vendor = find_vendor_by_taxnumber($taxnumber);
}
# parse dates to kivi if set/valid
my ($transdate, $duedate, $dt_to_kivi, $due_dt_to_kivi);
$transdate = $dom->findnodes('//ram:IssueDateTime')->string_value;
$duedate = $dom->findnodes('//ram:DueDateDateTime')->string_value;
$transdate =~ s/^\s+|\s+$//g;
$duedate =~ s/^\s+|\s+$//g;
if ($transdate =~ /^[0-9]{8}$/) {
$dt_to_kivi = DateTime->new(year => substr($transdate,0,4),
month => substr ($transdate,4,2),
day => substr($transdate,6,2))->to_kivitendo;
if (ref $vendor eq 'SL::DB::Vendor') { return $vendor; }
return undef;
}
sub action_import_zugferd {
my ($self, %params) = @_;
my $file = $::form->{file};
my $file_name = $::form->{file_name};
my %res; # result data structure returned by SL::ZUGFeRD->extract_from_{pdf,xml}()
my $parser; # SL::XMLInvoice object created by SL::ZUGFeRD->extract_from_{pdf,xml}()
my $dom; # DOM object for parsed XML data
my $template_ap; # SL::DB::RecordTemplate object
my $vendor; # SL::DB::Vendor object
my $ibanmessage; # Message to display if vendor's database and invoice IBANs don't match up
die t8("missing file for action import") unless $file;
die t8("can only parse a pdf or xml file") unless $file =~ m/^%PDF|<\?xml/;
if ( $::form->{file} =~ m/^%PDF/ ) {
%res = %{SL::ZUGFeRD->extract_from_pdf($::form->{file})}
} else {
%res = %{SL::ZUGFeRD->extract_from_xml($::form->{file})};
}
if ($duedate =~ /^[0-9]{8}$/) {
$due_dt_to_kivi = DateTime->new(year => substr($duedate,0,4),
month => substr ($duedate,4,2),
day => substr($duedate,6,2))->to_kivitendo;
if ($res{'result'} != SL::ZUGFeRD::RES_OK()) {
# An error occurred; log message from parser:
$::lxdebug->message(LXDebug::DEBUG1(), "Could not extract ZUGFeRD data, error message: " . $res{'message'});
die(t8("Could not extract Factur-X/ZUGFeRD data, data and error message:") . " $res{'message'}");
}
my $type = $dom->findnodes('//rsm:ExchangedDocument/ram:TypeCode')->string_value;
$parser = $res{'invoice_xml'};
# Shouldn't be neccessary with SL::XMLInvoice doing the heavy lifting, but
# let's grab it, just in case.
$dom = $parser->{dom};
my %metadata = %{$parser->metadata};
my @items = @{$parser->items};
my $dd = $dom->findnodes('//ram:ApplicableHeaderTradeSettlement' .
'/ram:SpecifiedTradeSettlementPaymentMeans/ram:TypeCode')->string_value;
my $direct_debit = $dd == 59 ? 1 : 0;
my $iban = $metadata{'iban'};
my $invnumber = $metadata{'invnumber'};
my $iban = $dom->findnodes('//ram:ApplicableHeaderTradeSettlement/ram:SpecifiedTradeSettlementPaymentMeans' .
'/ram:PayeePartyCreditorFinancialAccount/ram:IBANID')->string_value;
my $ibanmessage;
if ( ! ($metadata{'ustid'} or $metadata{'taxnumber'}) ) {
die t8("Cannot process this invoice: neither VAT ID nor tax ID present.");
}
$vendor = find_vendor($metadata{'ustid'}, $metadata{'taxnumber'});
die t8("Please add a valid VAT ID or tax number for this vendor: #1", $metadata{'vendor_name'}) unless $vendor;
# Create a record template for this imported invoice
$template_ap = SL::DB::RecordTemplate->new(
vendor_id=>$vendor->id,
);
# Check IBAN specified on bill matches the one we've got in
# the database for this vendor.
$ibanmessage = $iban ne $vendor->iban ? "Record IBAN $iban doesn't match vendor IBAN " . $vendor->iban : $iban if $iban;
# save the zugferd file to session file for reuse in ap.pl
......
$session_file->fh->print($file);
$session_file->fh->close;
# Use invoice creation date as due date if there's no due date
$metadata{'duedate'} = $metadata{'transdate'} unless defined $metadata{'duedate'};
# parse dates to kivi if set/valid
foreach my $key ( qw(transdate duedate) ) {
next unless defined $metadata{$key};
$metadata{$key} =~ s/^\s+|\s+$//g;
if ($metadata{$key} =~ /^([0-9]{4})-?([0-9]{2})-?([0-9]{2})$/) {
$metadata{$key} = DateTime->new(year => $1,
month => $2,
day => $3)->to_kivitendo;
}
}
# Try to fill in AP account to book against
my $ap_chart_id = $::instance_conf->get_ap_chart_id;
unless ( defined $ap_chart_id ) {
# If no default account is configured, just use the first AP account found.
my $ap_chart = SL::DB::Manager::Chart->get_all(
where => [ link => 'AP' ],
sort_by => [ 'accno' ],
);
$ap_chart_id = ${$ap_chart}[0]->id;
}
my $currency = SL::DB::Manager::Currency->find_by(
name => $metadata{'currency'},
);
$template_ap->assign_attributes(
template_name => "Faktur-X/ZUGFeRD/XRechnung Import $vendor->name, $invnumber",
template_type => 'ap_transaction',
direct_debit => $metadata{'direct_debit'},
notes => "Faktur-X/ZUGFeRD/XRechnung Import. Type: $metadata{'type'}\nIBAN: " . $ibanmessage,
taxincluded => 0,
currency_id => $currency->id,
ar_ap_chart_id => $ap_chart_id,
);
$template_ap->save;
my $default_ap_amount_chart = SL::DB::Manager::Chart->find_by(charttype => 'A');
foreach my $i ( @items )
{
my %item = %{$i};
my $net_total = $item{'subtotal'};
my $desc = $item{'description'};
my $tax_rate = $item{'tax_rate'} / 100; # XML data is usually in percent
my $taxes = SL::DB::Manager::Tax->get_all(
where => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' },
rate => $tax_rate,
],
);
# If we really can't find any tax definition (a simple rounding error may
# be sufficient for that to happen), grab the first tax fitting the default
# category, just like the AP form would do it for manual entry.
if ( scalar @{$taxes} == 0 ) {
$taxes = SL::D::ManagerTax->get_all(
where => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' } ],
);
}
my $tax = ${$taxes}[0];
my $item_obj = SL::DB::RecordTemplateItem
->new(amount1 => $net_total,
record_template_id => $template_ap->id,
chart_id => $default_ap_amount_chart->id,
tax_id => $tax->id,
);
$item_obj->save;
}
$self->redirect_to(
controller => 'ap.pl',
action => 'load_record_template',
id => $template_ap->id,
'form_defaults.amount_1' => $::form->format_amount(\%::myconfig, $total, 2),
'form_defaults.transdate' => $dt_to_kivi,
'form_defaults.invnumber' => $invnumber,
'form_defaults.duedate' => $due_dt_to_kivi,
'form_defaults.no_payment_bookings' => 0,
'form_defaults.paid_1_suggestion' => $::form->format_amount(\%::myconfig, $total, 2),
'form_defaults.notes' => "ZUGFeRD Import. Type: $type\nIBAN: " . $ibanmessage,
'form_defaults.paid_1_suggestion' => $::form->format_amount(\%::myconfig, $metadata{'total'}, 2),
'form_defaults.invnumber' => $invnumber,
'form_defaults.duedate' => $metadata{'duedate'},
'form_defaults.transdate' => $metadata{'transdate'},
'form_defaults.notes' => "ZUGFeRD Import. Type: $metadata{'type'}\nIBAN: " . $ibanmessage,
'form_defaults.taxincluded' => 0,
'form_defaults.direct_debit' => $direct_debit,
'form_defaults.direct_debit' => $metadata{'direct_debit'},
'form_defaults.zugferd_session_file' => $file_name,
);
......
=head1 NAME
SL::Controller::ZUGFeRD
Controller for importing ZUGFeRD pdf files to kivitendo
SL::Controller::ZUGFeRD - Controller for importing ZUGFeRD PDF files or XML invoices to kivitendo
=head1 FUNCTIONS
......
Creates a web from with a single upload dialog.
=item C<action_import_zugferd $pdf>
=item C<action_import_zugferd $file>
Expects a single pdf with ZUGFeRD 2.0 metadata.
Checks if the param <C$pdf> is set and a valid pdf file.
Calls helper functions to validate and extract the ZUGFeRD data.
Needs a valid VAT ID (EU) for this vendor and
expects one ap template for this vendor in kivitendo.
Expects a single PDF with ZUGFeRD, Factur-X or XRechnung
metadata. Alternatively, it can also process said data as a
standalone XML file.
Parses some basic ZUGFeRD data (invnumber, total net amount,
transdate, duedate, vendor VAT ID, IBAN) and uses the first
found ap template for this vendor to fill this template with
ZUGFeRD data.
If the vendor's master data contain a IBAN and the
ZUGFeRD record has a IBAN also these values will be compared.
If they don't match a warning will be writte in ap.notes.
Furthermore the ZUGFeRD type code will be written to ap.notes.
No callback implemented.
Checks if the param <C$pdf> is set and a valid PDF or XML
file. Calls helper functions to validate and extract the
ZUGFeRD/Factur-X/XRechnung data. The invoice needs to have a
valid VAT ID (EU) or tax number (Germany) and a vendor with
the same VAT ID or tax number enrolled in Kivitendo.
It parses some basic ZUGFeRD data (invnumber, total net amount,
transdate, duedate, vendor VAT ID, IBAN, etc.) and also
extracts the invoice's items.
If the invoice has a IBAN also, it will be be compared to the
IBAN saved for the vendor (if any). If they don't match a
warning will be writte in ap.notes. Furthermore the ZUGFeRD
type code will be written to ap.notes. No callback
implemented.
=back
=head1 TODO and CAVEAT
=head1 CAVEAT
This is just a very basic Parser for ZUGFeRD/Factur-X/XRechnung invoices.
We assume that the invoice's creator is a company with a valid
European VAT ID or German tax number and enrolled in
Kivitendo. Currently, implementation is a bit hacky because
invoice import uses AP record templates as a vessel for
generating the AP record form with the imported data filled
in.
=head1 TODO
This implementation could be improved as follows:
=over 4
=item Direct creation of the filled in AP record form
Creating an AP record template in the database is not
very elegant, since it will spam the database with record
templates that become redundant once the invoice has been
booked. It would be preferable to fill in the form directly.
=item Automatic upload of invoice
Right now, one has to use the "Book and upload" button to
upload the raw invoice document to WebDAV or DMS and attach it
to the invoice. This should be a simple matter of setting a
check box when uploading.
=item Handling of vendor invoices
There is no reason this functionality could not be used to
import vendor invoices as well. Since these tend to be very
lengthy, the ability to import them would be very beneficial.
This is just a very basic Parser for ZUGFeRD data.
We assume that the ZUGFeRD generator is a company with a
valid European VAT ID. Furthermore this vendor needs only
one and just noe ap template (the first match will be used).
=item Automatic handling of payment purpose
The ZUGFeRD data should also be extracted in the helper package
and maybe a model should be used for this.
The user should set one ap template as a default for ZUGFeRD.
The ZUGFeRD pdf should be written to WebDAV or DMS.
If the ZUGFeRD data has a payment purpose set, this should
be the default for the SEPA-XML export.
=back
=head1 AUTHORS
=over 4
=item Jan Büren E<lt>jan@kivitendo-premium.deE<gt>,
=head1 AUTHOR
=item Johannes Graßler E<lt>info@computer-grassler.deE<gt>,
Jan Büren E<lt>jan@kivitendo-premium.deE<gt>,
=back
=cut
SL/ZUGFeRD.pm
use XML::LibXML;
use SL::Locale::String qw(t8);
use SL::XMLInvoice;
use parent qw(Exporter);
our @EXPORT_PROFILES = qw(PROFILE_FACTURX_EXTENDED PROFILE_XRECHNUNG);
......
use constant PROFILE_XRECHNUNG => 1;
use constant RES_OK => 0;
use constant RES_ERR_FILE_OPEN => 1;
use constant RES_ERR_NO_XMP_METADATA => 2;
use constant RES_ERR_NO_XML_INVOICE => 3;
use constant RES_ERR_NOT_ZUGFERD => 4;
use constant RES_ERR_UNSUPPORTED_ZUGFERD_VERSION => 5;
use constant RES_ERR_FILE_OPEN => -1;
use constant RES_ERR_NO_ATTACHMENT => -2;
our @customer_settings = (
[ 0, t8('Do not create Factur-X/ZUGFeRD invoices') ],
......
sub _extract_zugferd_invoice_xml {
my $doc = shift;
my $names_dict = $doc->getValue($doc->getRootDict->{Names}) or return {};
my $files_tree = $names_dict->{EmbeddedFiles} or return {};
my %res_fail;
$res_fail{'result'} = RES_ERR_NO_ATTACHMENT();
$res_fail{'message'} = "PDF does not have a Names dictionary.";
my $names_dict = $doc->getValue($doc->getRootDict->{Names}) or return \%res_fail;
$res_fail{'message'} = "PDF does not have a EmbeddedFiles tree.";
my $files_tree = $names_dict->{EmbeddedFiles} or return \%res_fail;
my @agenda = $files_tree;
my $ret = {};
my $parser; # SL::XMLInvoice object used as return value
my @res; # Temporary storage for error messages encountered during
# attempts to process attachments.
# Hardly ever more than single leaf, but...
......
my $obj_node = $doc->dereference($any_num);
my $content = $doc->decodeOne($obj_node->{value}, 0) // '';
#print "1\n";
next if $content !~ m{<rsm:CrossIndustryInvoice};
#print "2\n";
my $dom = eval { XML::LibXML->load_xml(string => $content) };
return $content if $dom && ($dom->documentElement->nodeName eq 'rsm:CrossIndustryInvoice');
$parser = $parser = SL::XMLInvoice->new($content);
# Caveat: this will only ever catch the first attachment looking like
# an XML invoice.
if ( $parser->{status} == SL::XMLInvoice::RES_OK ){
return $parser;
} else {
push @res, t8("Could not parse PDF embedded attachment #1: #2",
$k,
$parser->{result});
}
}
}
}
return undef;
# There's going to be at least one attachment that failed to parse as XML by
# this point - if there were no attachments at all, we would have bailed out
# a lot earlier.
%res_fail = ( result => RES_ERR_FILE_OPEN(),
message => join("; ", @res),
);
return \%res_fail;
}
sub _get_xmp_metadata {
......
if ($node && $node->{StreamData} && defined($node->{StreamData}->{value})) {
return $node->{StreamData}->{value};
}
return undef;
}
sub extract_from_pdf {
my ($self, $file_name) = @_;
my @warnings;
my $pdf_doc = CAM::PDF->new($file_name);
if (!$pdf_doc) {
return {
return \{
result => RES_ERR_FILE_OPEN(),
message => $::locale->text('The file \'#1\' could not be opened for reading.', $file_name),
};
}
my $xmp = _get_xmp_metadata($pdf_doc);
if (!defined $xmp) {
return {
result => RES_ERR_NO_XMP_METADATA(),
message => $::locale->text('The file \'#1\' does not contain the required XMP meta data.', $file_name),
};
}
push @warnings, $::locale->text('The file \'#1\' does not contain the required XMP meta data.', $file_name);
} else {
my $dom = eval { XML::LibXML->load_xml(string => $xmp) };
my $bad = {
result => RES_ERR_NO_XMP_METADATA(),
message => $::locale->text('Parsing the XMP metadata failed.'),
};
push @warnings, $::locale->text('Parsing the XMP metadata failed.'), if !$dom;
my $dom = eval { XML::LibXML->load_xml(string => $xmp) };
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
return $bad if !$dom;
my $zugferd_version;
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
my $test = $xpc->findnodes('/x:xmpmeta/rdf:RDF/rdf:Description');
my $zugferd_version;
foreach my $node ($xpc->findnodes('/x:xmpmeta/rdf:RDF/rdf:Description')) {
my $ns = first { ref($_) eq 'XML::LibXML::Namespace' } $node->attributes;
next unless $ns;
foreach my $node ($xpc->findnodes('/x:xmpmeta/rdf:RDF/rdf:Description')) {
my $ns = first { ref($_) eq 'XML::LibXML::Namespace' } $node->attributes;
next unless $ns;
if ($ns->getData =~ m{urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0}) {
$zugferd_version = 'zugferd:2p0';
last;
}
if ($ns->getData =~ m{urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0}) {
$zugferd_version = 'zugferd:2p0';
last;
}
if ($ns->getData =~ m{urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0}) {
$zugferd_version = 'factur-x:1p0';
last;
}
if ($ns->getData =~ m{urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0}) {
$zugferd_version = 'factur-x:1p0';
last;
if ($ns->getData =~ m{zugferd|factur-x}i) {
$zugferd_version = 'unsupported';
last;
}
}
if ($ns->getData =~ m{zugferd|factur-x}i) {
$zugferd_version = 'unsupported';
last;
if (!$zugferd_version) {
push @warnings, $::locale->text('The XMP metadata does not declare the Factur-X/ZUGFeRD data.'),
}
}
if (!$zugferd_version) {
return {
result => RES_ERR_NOT_ZUGFERD(),
message => $::locale->text('The XMP metadata does not declare the Factur-X/ZUGFeRD data.'),
};
}
if ($zugferd_version eq 'unsupported') {
return {
result => RES_ERR_UNSUPPORTED_ZUGFERD_VERSION(),
message => $::locale->text('The Factur-X/ZUGFeRD version used is not supported.'),
};
if ($zugferd_version eq 'unsupported') {
push @warnings, $::locale->text('The Factur-X/ZUGFeRD version used is not supported.'),
}
}
my $invoice_xml = _extract_zugferd_invoice_xml($pdf_doc);
if (!defined $invoice_xml) {
return {
result => RES_ERR_NO_XML_INVOICE(),
message => $::locale->text('The Factur-X/ZUGFeRD XML invoice was not found.'),
};
}
my %res;
return {
result => RES_OK(),
%res = (
result => $invoice_xml->{result},
message => $invoice_xml->{message},
metadata_xmp => $xmp,
invoice_xml => $invoice_xml,
};
warnings => \@warnings,
);
return \%res;
}
sub extract_from_xml {
my ($self, $data) = @_;
my %res;
my $invoice_xml = SL::XMLInvoice->new($data);
%res = (
result => $invoice_xml->{result},
message => $invoice_xml->{message},
metadata_xmp => undef,
invoice_xml => $invoice_xml,
warnings => (),
);
return \%res;
}
1;
......
my $pdf = '/path/to/my.pdf';
my $info = SL::ZUGFeRD->extract_from_pdf($pdf);
my $xml = '<?xml version="1.0" encoding="UTF-8"?> ...';
my $info = SL::ZUGFeRD->extract_from_xml($xml);
if ($info->{result} != SL::ZUGFeRD::RES_OK()) {
# An error occurred; log message from parser:
$::lxdebug->message(LXDebug::DEBUG1(), "Could not extract ZUGFeRD data from $pdf: " . $info->{message});
return;
}
# Parse & handle invoice XML:
my $dom = XML::LibXML->load_xml(string => $info->{invoice_xml});
# Access invoice XML data:
my $inv = ${$info}{'invoice_xml};
my %metadata = %{$inv->metadata};
my @items = @{$inv->items};
my $dom = $inv->dom;
=head1 FUNCTIONS
=over 4
=item C<extract_from_pdf> C<$file_name>
=head2 extract_from_pdf E<lt>file_nameE<gt>
Opens an existing PDF in the file system and tries to extract
Factur-X/ZUGFeRD invoice data from it. First it'll parse the XMP
Opens an existing PDF file in the file system and tries to extract
Factur-X/XRechnung/ZUGFeRD invoice data from it. First it'll parse the XMP
metadata and look for the Factur-X/ZUGFeRD declaration inside. If the
declaration isn't found or the declared version isn't 2p0, an error is
returned.
declaration isn't found or the declared version isn't 2p0, an warning is
recorded in the returned data structure's C<warnings> key.
Otherwise it'll continue to look through all embedded files in the
PDF. The first embedded XML file with a root node of
C<rsm:CrossCountryInvoice> will be returnd.
Regardless of metadata presence, it will continue to iterate over all files
embedded in the PDF and attempt to parse them with SL::XMLInvoice. If it
succeeds, the first SL::XMLInvoice object that indicates successful parsing is
returned.
Always returns a hash ref containing the key C<result>, a number that
can be one of the following constants:
=over 4
=item C<RES_OK> (0): parsing was OK; the returned hash will also
contain the keys C<xmp_metadata> and C<invoice_xml> which will contain
the XML text of the metadata & the Factur-X/ZUGFeRD invoice.
=item C<RES_OK> (0): parsing was OK.
=item C<RES_ERR_…> (all values E<!=> 0): parsing failed. Values > 0 indicate a failure
in C<SL::XMLInvoice>, Values < 0 indicate a failure in C<SL::ZUGFeRD>.
=back
Other than that, the hash ref contains the following keys:
=over 4
=item C<message> - An error message detailing the problem upon nonzero C<result>, undef otherwise.
=item C<metadata_xmp> - The XMP metadata extracted from the Factur-X/ZUGFeRD invoice (if present)
=item C<invoice_xml> - An SL::XMLInvoice object holding the data extracted from the parsed XML invoice.
=item C<warnings> - Warnings encountered upon extracting/parsing XML files (if any)
=back
=head2 extract_from_xml E<lt>stringE<gt>
Takes a string containing an XML document with Factur-X/XRechnung/ZUGFeRD
invoice data and attempts to parse it using C<SL::XMLInvoice>.
If parsing is successful, an SL::XMLInvoice object containing the document's
parsed data is returned.
This method always returns a hash ref containing the key C<result>, a number that
can be one of the following constants:
=over 4
=item C<RES_OK> (0): parsing was OK.
=item C<RES_ERR_…> (all values E<gt> 0): parsing failed; the hash will
also contain a key C<message> which contains a human-readable
information about what exactly failed.
=item C<RES_ERR_…> (all values E<!=> 0): parsing failed. Values > 0 indicate a failure
in C<SL::XMLInvoice>, Values < 0 indicate a failure in C<SL::ZUGFeRD>.
=back
Other than that, the hash ref contains the following keys:
=over 4
=item C<message> - An error message detailing the problem upon nonzero C<result>, undef otherwise.
=item C<metadata_xmp> - Always undef and only present to let downstream code expecting its presence fail gracefully.
=item C<invoice_xml> - An SL::XMLInvoice object holding the data extracted from the parsed XML invoice.
=item C<warnings> - Warnings encountered upon extracting/parsing XML data (if any)
=back
=head1 BUGS
Nothing here yet.
=head1 AUTHOR
=head1 AUTHORS
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
=over 4
=item Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
=item Johannes Graßler E<lt>info@computer-grassler.deE<gt>
=back
=cut
templates/webpages/zugferd/form.html
</p>
<form method="post" action="controller.pl" enctype="multipart/form-data" id="form">
[% L.input_tag('file', '', type => 'file', accept => '.pdf', onchange='kivi.ZUGFeRD.update_file_name();') %]
[% L.input_tag('file', '', type => 'file', accept => '.pdf,.xml', onchange='kivi.ZUGFeRD.update_file_name();') %]
[% L.hidden_tag('file_name', '') %]
</form>

Auch abrufbar als: Unified diff