X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FZUGFeRD.pm;h=699e5fe5ef7ac3e294c4a2bcde17219fff82419d;hb=874cdb18bd95f7a63accd77d6828cf54c320406f;hp=f03b5178943dc500cd1247c31c0229be25c76d9e;hpb=9077dc27c713758d631fe6d08c548613d4d2dde8;p=kivitendo-erp.git diff --git a/SL/Controller/ZUGFeRD.pm b/SL/Controller/ZUGFeRD.pm index f03b51789..699e5fe5e 100644 --- a/SL/Controller/ZUGFeRD.pm +++ b/SL/Controller/ZUGFeRD.pm @@ -5,6 +5,7 @@ use parent qw(SL::Controller::Base); use SL::DB::RecordTemplate; use SL::Locale::String qw(t8); use SL::Helper::DateTime; +use SL::VATIDNr; use SL::ZUGFeRD; use XML::LibXML; @@ -16,7 +17,7 @@ sub action_upload_zugferd { my ($self, %params) = @_; $self->setup_zugferd_action_bar; - $self->render('zugferd/form', title => $::locale->text('ZUGFeRD import')); + $self->render('zugferd/form', title => $::locale->text('Factur-X/ZUGFeRD import')); } sub action_import_zugferd { @@ -30,22 +31,53 @@ sub action_import_zugferd { 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 ZUGFeRD data, data and error message:") . $info->{message}; + die t8("Could not extract Factur-X/ZUGFeRD data, data and error message:") . $info->{message}; } # 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 ZUGFeRD invoice," . - " please ask your vendor to add this for his ZUGFeRD data.") unless $ustid; + 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; - $ustid =~ s/^\s+|\s+$//g; + $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); - die t8("Please add a valid VAT-ID for this vendor: " . $vc) unless (ref $vendor eq 'SL::DB::Vendor'); + my $vendor = SL::DB::Manager::Vendor->find_by( + ustid => $ustid, + 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 $vendors = SL::DB::Manager::Vendor->get_all( + where => [ + '!ustid' => undef, + '!ustid' => '', + or => [ + obsolete => undef, + obsolete => 0, + ], + ]); + + foreach my $other_vendor (@{ $vendors }) { + next unless SL::VATIDNr->normalize($other_vendor->ustid) eq $ustid; + + $vendor = $other_vendor; + last; + } + } + + die t8("Please add a valid VAT-ID for this vendor: #1", $vc) unless (ref $vendor eq 'SL::DB::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]);