X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FZUGFeRD.pm;h=f989d3d949c3853a834400039a2e680cccd9df43;hb=4dc48e117cfdb6e59c2d8b9d8087ef8a3bc245a4;hp=5455398cd9c948464521d1c0b1bf07474e93eba9;hpb=ec4cffa8a9fe604ecaab2a0af734b5e28c040935;p=kivitendo-erp.git diff --git a/SL/DB/Helper/ZUGFeRD.pm b/SL/DB/Helper/ZUGFeRD.pm index 5455398cd..f989d3d94 100644 --- a/SL/DB/Helper/ZUGFeRD.pm +++ b/SL/DB/Helper/ZUGFeRD.pm @@ -14,6 +14,7 @@ use SL::Helper::ISO3166; use SL::Helper::ISO4217; use SL::Helper::UNECERecommendation20; use SL::VATIDNr; +use SL::ZUGFeRD qw(:PROFILES); use Carp; use Encode qw(encode); @@ -24,6 +25,16 @@ use XML::Writer; my @line_names = qw(LineOne LineTwo LineThree); +my %standards_ids = ( + PROFILE_FACTURX_EXTENDED() => 'urn:cen.eu:en16931:2017#conformant#urn:factur-x.eu:1p0:extended', + PROFILE_XRECHNUNG() => 'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.0', +); + +sub _is_profile { + my ($self, @profiles) = @_; + return any { $self->{_zugferd}->{profile} == $_ } @profiles; +} + sub _u8 { my ($value) = @_; return encode('UTF-8', $value // ''); @@ -280,6 +291,17 @@ sub _format_payment_terms_description { $description =~ s{<\%$_\%>}{ $params{vars}->{$_} }ge for keys %{ $params{vars} }; $description =~ s{<\%$_\%>}{ $params{formatted_amounts}->{$_} }ge for keys %{ $params{formatted_amounts} }; + if (_is_profile($self, PROFILE_XRECHNUNG())) { + my @terms; + + if ($self->payment_terms->terms_skonto && ($self->payment_terms->percent_skonto * 1)) { + push @terms, sprintf("#SKONTO#TAGE=\%d#PROZENT=\%.2f#\n", $self->payment_terms->terms_skonto, $self->payment_terms->percent_skonto * 100); + } + + $description =~ s{#}{_}g; + $description = join('', @terms) . $description; + } + return $description; } @@ -301,7 +323,9 @@ sub _payment_terms { $params{xml}->endTag; # - if ($self->payment_terms->percent_skonto && $self->payment_terms->terms_skonto) { + if ( _is_profile($self, PROFILE_FACTURX_EXTENDED()) + && $self->payment_terms->percent_skonto + && $self->payment_terms->terms_skonto) { my $currency_id = _u8(SL::Helper::ISO4217::map_currency_name_to_code($self->currency->name) // 'EUR'); # @@ -340,14 +364,14 @@ sub _exchanged_document_context { # $params{xml}->startTag("rsm:ExchangedDocumentContext"); - if ($self->customer->create_zugferd_invoices_for_this_customer == 2) { + if ($self->{_zugferd}->{test_mode}) { $params{xml}->startTag("ram:TestIndicator"); $params{xml}->dataElement("udt:Indicator", "true"); $params{xml}->endTag; } $params{xml}->startTag("ram:GuidelineSpecifiedDocumentContextParameter"); - $params{xml}->dataElement("ram:ID", "urn:cen.eu:en16931:2017#conformant#urn:zugferd.de:2p0:extended"); + $params{xml}->dataElement("ram:ID", $standards_ids{ $self->{_zugferd}->{profile} }); $params{xml}->endTag; $params{xml}->endTag; # @@ -368,7 +392,7 @@ sub _exchanged_document { $params{xml}->startTag("rsm:ExchangedDocument"); $params{xml}->dataElement("ram:ID", _u8($self->invnumber)); - $params{xml}->dataElement("ram:Name", _u8(_type_name($self))); + $params{xml}->dataElement("ram:Name", _u8(_type_name($self))) if _is_profile($self, PROFILE_FACTURX_EXTENDED()); $params{xml}->dataElement("ram:TypeCode", _u8(_type_code($self))); # @@ -377,7 +401,9 @@ sub _exchanged_document { $params{xml}->endTag; # - if ($self->language && (($self->language->template_code // '') =~ m{^(de|en)}i)) { + if ( _is_profile($self, PROFILE_FACTURX_EXTENDED()) + && $self->language + && (($self->language->template_code // '') =~ m{^(de|en)}i)) { $params{xml}->dataElement("ram:LanguageID", uc($1)); } @@ -499,6 +525,8 @@ sub _applicable_header_trade_agreement { # $params{xml}->startTag("ram:ApplicableHeaderTradeAgreement"); + $params{xml}->dataElement("ram:BuyerReference", _u8($self->customer->c_vendor_routing_id)) if $self->customer->c_vendor_routing_id; + _seller_trade_party($self, %params); _buyer_trade_party($self, %params); @@ -608,7 +636,13 @@ sub _validate_data { $result{bank_account} = scalar(@{ $bank_accounts }) == 1 ? $bank_accounts->[0] : first { $_->use_for_zugferd } @{ $bank_accounts }; if (!$result{bank_account}) { - SL::X::ZUGFeRDValidation->throw(message => $prefix . $::locale->text('No bank account flagged for ZUGFeRD usage was found.')); + SL::X::ZUGFeRDValidation->throw(message => $prefix . $::locale->text('No bank account flagged for Factur-X/ZUGFeRD usage was found.')); + } + } + + if (_is_profile($self, PROFILE_XRECHNUNG())) { + if (!$self->customer->c_vendor_routing_id) { + SL::X::ZUGFeRDValidation->throw(message => $prefix . $::locale->text('The value \'our routing id at customer\' must be set in the customer\'s master data for profile #1.', 'XRechnung 2.0')); } } @@ -617,6 +651,11 @@ sub _validate_data { sub create_zugferd_data { my ($self) = @_; + $self->{_zugferd} = { SL::ZUGFeRD->convert_customer_setting($self->customer->create_zugferd_invoices_for_this_customer) }; + + if (!$standards_ids{ $self->{_zugferd}->{profile} }) { + croak "Profile '" . $self->{_zugferd}->{profile} . "' is not supported"; + } my $output = ''; @@ -655,7 +694,7 @@ sub create_zugferd_xmp_data { return { conformance_level => 'EXTENDED', - document_file_name => 'ZUGFeRD-invoice.xml', + document_file_name => 'factur-x.xml', document_type => 'INVOICE', version => '1.0', };