Quiq/Zugferd.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 97.0% 65 / 67 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Quiq/ContentProcessor.pm ↑ Summary Quiq/Database/Api.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Zugferd - Generiere das XML von ZUGFeRD-Rechnungen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Diese Klasse dient der Generung des XMLs von E-Rechnungen nach dem
16 ZUGFeRD/Factur-X-Standard. Sie kapselt die Profile des Standards,
17 sowohl die XSD-Dateien (XML-Schemadefinition) als auch fertige
18 Templates.
19  
20 Die Generierung eines Rechnungs-XMLs erfolgt durch Einsetzung der
21 Rechnungswerte in das Template des jeweiligen Profils.
22  
23 =head1 EXAMPLES
24  
25 Zeige das Template des Profils EN16931:
26  
27 $ perl -MQuiq::Zugferd -E 'print Quiq::Zugferd->new("en16931")->template'
28  
29 Zeige den Template-Baum des Profils EN16931:
30  
31 $ perl -MQuiq::Zugferd -MQuiq::Dumper -E '$t = Quiq::Zugferd->new("en16931")->tree; say Quiq::Dumper->dump($t)'
32  
33 =cut
34  
35 # -----------------------------------------------------------------------------
36  
37 package Quiq::Zugferd;
38 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 our $VERSION = '1.238';
46  
47 1 ▶use Quiq::PerlModule;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Quiq::Zugferd::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Quiq::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use XML::Compile::Schema ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use XML::Compile::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Quiq::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Quiq::AnsiColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Quiq::Xml;
Subroutine BEGIN: called CC=1 SCAR=2.6
59 use Quiq::Tree;
60 use MIME::Base64 ();
61 use Quiq::Hash;
62  
63 # -----------------------------------------------------------------------------
64  
65 =head1 METHODS
66  
67 =head2 Konstruktor
68  
69 =head3 new() - Instantiiere Objekt
70  
71 =head4 Synopsis
72  
73 $zug = $class->new($profile,%options);
74  
75 =head4 Arguments
76  
77 =over 4
78  
79 =item $profile
80  
81 Name des ZUGFeRD-Profils. Es existieren die ZUGFeRD-Profile: C<minimum>,
82 C<basicwl>, C<basic>, C<en16931>, C<extended>.
83  
84 =back
85  
86 =head4 Options
87  
88 =over 4
89  
90 =item -version => $version (Default: '2.3.2')
91  
92 Die ZUGFeRD-Version.
93  
94 =back
95  
96 =head4 Returns
97  
98 Object
99  
100 =head4 Description
101  
102 Instantiiere ein ZUGFeRD-Objekt des Profils $profile und der
103 ZUGFeRD-Version $version und liefere dieses zurück.
104  
105 =cut
106  
107 # -----------------------------------------------------------------------------
108  
109 sub new {
110 my $class = shift;
111  
112 # Optionen und Argumente
113  
114 my $version = '2.3.2';
115  
116 my $argA = $class->parameters(1,1,\@_,
117 -version => \$version,
118 );
119 my $profile = shift @$argA;
120  
121 my $mod = Quiq::PerlModule->new('Quiq::Zugferd');
122 my $modDir = $mod->moduleDir;
123  
124 my $zugferdDir = "$modDir/$version";
125 my $xsdDir = "$zugferdDir/profile/$profile";
126 my $xmlTemplateFile = "$xsdDir/template.xml";
127  
128 my $p = Quiq::Path->new;
129  
130 if (!$p->exists($xsdDir)) {
131 $class->throw(
132 'ZUGFERD-00099: XSD directory does not exist',
133 Dir => $xsdDir,
134 );
135 }
136 if (!$p->exists($xmlTemplateFile)) {
137 $class->throw(
138 'ZUGFERD-00099: XML template does not exist',
139 Template => $xmlTemplateFile,
140 );
141 }
142  
143 # Ermittele .xsd-Dateien
144 my @xsdFiles = Quiq::Path->find($xsdDir,-pattern=>'\.xsd$');
145  
146 # Instantiiere Schema-Objekt
147  
148 my $sch = XML::Compile::Schema->new;
149 for my $file (@xsdFiles) {
150 $sch->importDefinitions($file);
151 }
152  
153 # Lies Template-Datei
154 my $template = $p->read($xmlTemplateFile);
155  
156 # Entferne Zeilen mit # am Anfang
157 $template =~ s|^\s*#.*\n||gm;
158  
159 # Ermittele den Typ des Wurzelelements
160  
161 my $doc = XML::LibXML->load_xml(
162 string => $template,
163 no_blanks => 1,
164 );
165 my $top = $doc->documentElement;
166 my $rootType = XML::Compile::Util::type_of_node($top);
167  
168 # Erzeuge Template-Baum
169  
170 my $rdr = $sch->compile(READER=>$rootType,
171 sloppy_floats => 1, # Wir wollen keine BigFloat-Elemente
172 sloppy_integers => 1, # Wir wollen keine BigInt-Elemente
173 validation => 0,
174 );
175  
176 my $h = $rdr->($template);
177 my $tree = Quiq::Zugferd::Tree->new($h);
178  
179 # direkte Zuweisung, da XML::Compile diese Platzhalter überschreibt
180  
181 $tree->setDeep('SupplyChainTradeTransaction.'.
182 'ApplicableHeaderTradeSettlement.SpecifiedTradeAllowanceCharge.[0].'.
183 'ChargeIndicator.Indicator','BG-21-1');
184 $tree->setDeep('SupplyChainTradeTransaction.'.
185 'IncludedSupplyChainTradeLineItem.[0].SpecifiedLineTradeSettlement.'.
186 'SpecifiedTradeAllowanceCharge.[0].ChargeIndicator.Indicator',
187 'BG-27-1_BG-28-1');
188 $tree->setDeep('SupplyChainTradeTransaction.'.
189 'ApplicableHeaderTradeAgreement.AdditionalReferencedDocument.[0].'.
190 'AttachmentBinaryObject._','BT-125');
191  
192 # say Quiq::Dumper->dump($tree);
193  
194 return bless {
195 zugferdDir => $zugferdDir,
196 sch => $sch,
197 rootType => $rootType,
198 template => $template,
199 tree => $tree,
200 version => $version,
201 bgH => undef, # wird bei Bedarf geladen
202 },$class;
203 }
204  
205 # -----------------------------------------------------------------------------
206  
207 =head3 createTemplate() - Erzeuge XML-Template zu Schemadefinition
208  
209 =head4 Synopsis
210  
211 $xml = $class->createTemplate($xsdDir);
212  
213 =head4 Returns
214  
215 (String) XML-Template
216  
217 =head4 Description
218  
219 Erzeuge auf Basis der XSD-Dateien in $xsdDir mit %CL<lt>XML::Compile::Schema>%
220 ein XML Template ohne Werte und liefere dieses zurück.
221  
222 B<ACHTUNG:> Mit der originalen Version von XML::Compile werden erneut
223 auftauchende Elemente ELEMENT abgekürzt mit <ELEMENT/>. Das Ergebnis ist
224 dann als Template nicht wirklich brauchbar.
225  
226 =head4 Examples
227  
228 Profil EN16931 der ZUGFeRD-Version 2.4:
229  
230 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->createTemplate("ZF24_DE/Schema/3_Factur-X_1.08_EN16931")' # 562 Zeilen
231  
232 Profil EXTENDED der ZUGFeRD-Version 2.4:
233  
234 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->createTemplate("ZF24_DE/Schema/4_Factur-X_1.08_EXTENDED")' # 1690 Zeilen
235  
236 =cut
237  
238 # -----------------------------------------------------------------------------
239  
240 sub createTemplate {
241 my ($this,$xsdDir) = @_;
242  
243 # Ermittele .xsd-Dateien im XSD-Verzeichnis
244 my @xsdFiles = Quiq::Path->find($xsdDir,-pattern=>'\.xsd$');
245  
246 # Instantiiere Schema-Objekt
247  
248 my $sch = XML::Compile::Schema->new;
249 for my $file (@xsdFiles) {
250 $sch->importDefinitions($file);
251 }
252  
253 my $ns = 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100';
254 my $type = 'CrossIndustryInvoice';
255 my $rootType = "{$ns}$type";
256  
257 my $xml = $sch->template(XML=>$rootType,
258 show_comments => 'NONE',
259 skip_header => 1,
260 show_all => 1,
261 prefixes => [
262 rsm => $ns,
263 ],
264 );
265  
266 $xml =~ s|"token"|""|g;
267 $xml =~ s|>\s*token\s*<|><|g;
268 $xml =~ s|"example"|""|g;
269 $xml =~ s|>\s*example\s*<|><|g;
270 $xml =~ s|"example"|""|g;
271 $xml =~ s|>\s*example\s*<|><|g;
272 $xml =~ s|"3.1415"|""|g;
273 $xml =~ s|>\s*3.1415\s*<|><|g;
274 $xml =~ s|>\s*decoded bytes\s*<|><|g;
275 $xml =~ s|>\s*true\s*<|><|g;
276  
277 return $xml;
278 }
279  
280 # -----------------------------------------------------------------------------
281  
282 =head2 Objektmethoden
283  
284 =head3 checkAttributes() - Prüfe die Attribute des Rechnungsobjekts
285  
286 =head4 Synopsis
287  
288 $msg = $zug->checkAttributes($rch);
289  
290 =head4 Arguments
291  
292 =over 4
293  
294 =item $rch
295  
296 (Object) Das Rechnungsobjekt
297  
298 =back
299  
300 =head4 Returns
301  
302 (String) Prüfungsbericht
303  
304 =head4 Description
305  
306 Prüfe die Felder des Rechnungsobjekts auf mögliche Fehler und liefere
307 einen Bericht über das Ergebnis. Falls keine Fehler festgestellt wurden,
308 lieferet die Methode einen Leerstring ('').
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 sub checkAttributes {
315 my ($self,$rch) = @_;
316  
317 my $text = '';
318  
319 # Skalare Attribute
320  
321 my $btH = $self->bt;
322 for my $bt (values %$btH) {
323 if (!$bt->mandatory) {
324 next;
325 }
326 my $key = $bt->attribute;
327 if ($key) {
328 my $val = $rch->getDeep($key);
329 if (!$val) {
330 $text .= sprintf q|ERROR: Rechnungsattribut "%s" (%s)|.
331 " hat keinen Wert\n",$key,$bt->name;
332 }
333 }
334 }
335  
336 # Listen
337  
338 my $arr = $rch->positionen;
339 if (!@$arr) {
340 $text .= sprintf "ERROR: Rechnung hat keine Positionen\n";
341 }
342  
343 $arr = $rch->umsatzsteuern;
344 if (!@$arr) {
345 $text .= sprintf "ERROR: Umsatzsteueraufschlüsselung fehlt\n";
346 }
347  
348 return $text;
349 }
350  
351 # -----------------------------------------------------------------------------
352  
353 =head3 combine() - Erzeuge ZUGFeRD PDF
354  
355 =head4 Synopsis
356  
357 $zug->combine($pdfFile,$xmlFile,$zugferdFile);
358  
359 =head4 Arguments
360  
361 =over 4
362  
363 =item $pdfFile
364  
365 (String) Der Pfad zum PDF der Rechnung
366  
367 =item $xmlFile
368  
369 (String) Der Pfad zum ZUGFeRD XML der Rechnung
370  
371 =item $zugferdFile
372  
373 (String) Der Pfad zur resultierenden ZUGFeRD Rechnung
374  
375 =back
376  
377 =head4 Description
378  
379 Füge das Rechnungs-PDF $pdfFile und das ZUGFeRD XML $xmlFile
380 zusammen und schreibe das Ergebnis nach $zugferdFile.
381  
382 =cut
383  
384 # -----------------------------------------------------------------------------
385  
386 sub combine {
387 my ($self,$pdfFile,$xmlFile,$zugferdFile) = @_;
388  
389 my $p = Quiq::Path->new;
390  
391 my $dir = $p->tempDir;
392 $p->copy($xmlFile,"$dir/factur-x.xml");
393  
394 my $sh = Quiq::Shell->new;
395 $sh->cd($dir);
396 $sh->exec("pdftk $pdfFile attach_files 'factur-x.xml' output $zugferdFile");
397 $sh->back;
398  
399 return;
400 }
401  
402 # -----------------------------------------------------------------------------
403  
404 =head3 createInvoice() - Erzeuge das XML einer ZUGFeRD-Rechnung (abstrakt)
405  
406 =head4 Synopsis
407  
408 $xml = $zug->createInvoice(@args);
409  
410 =head4 Arguments
411  
412 =over 4
413  
414 =item @args
415  
416 Beliebige Argumente, die für das Ausfüllen des ZUGFeRD XML-Templates
417 benötigt werden.
418  
419 =back
420  
421 =head4 Returns
422  
423 (String) XML
424  
425 =cut
426  
427 # -----------------------------------------------------------------------------
428  
429 sub createInvoice {
430 my $self = shift;
431  
432 # Dies ist eine abstrakte Methode, die in einer abgeleiteten
433 # Klasse überschrieben werden muss
434 $self->throw;
435  
436 # Spezifikationskennung
437 my $bt24 = 'urn:cen.eu:en16931:2017';
438  
439 return $self->resolvePlaceholders(
440 -validate => 1,
441 -showPlaceholders => 1,
442 -showTree => 1,
443 '--',
444 'BT-24' => $bt24,
445 );
446 }
447  
448 # -----------------------------------------------------------------------------
449  
450 =head3 processSubTree() - Verarbeite Subbaum
451  
452 =head4 Synopsis
453  
454 $treeA = $zug->processSubTree($name,\@arr,sub {
455 my ($zug,$t,$h,$i) = @_;
456 ...
457 $t->resolvePlaceholders(
458 ...
459 );
460
461 return $t;
462 });
463  
464 =head4 Arguments
465  
466 =over 4
467  
468 =item $zug
469  
470 ZUGFeRD-Objekt
471  
472 =item $name
473  
474 Name des Subbaums
475  
476 =item @arr
477  
478 Liste der Elemente, über die iteriert wird, um Teilbäume (mit
479 ersetzten Platzhaltern) zu erzeugen.
480  
481 =item sub {}
482  
483 Subroutine, die die Einsetzung in jeweils einen Subbaum vornimmt
484  
485 =back
486  
487 =head4 Returns
488  
489 (Object) (Sub-)Baum mit ersetzen Platzhaltern
490  
491 =head4 Description
492  
493 Ersetze im Subbaum $name die Platzhalter aus den Elementen von @arr.
494  
495 =cut
496  
497 # -----------------------------------------------------------------------------
498  
499 sub processSubTree {
500 my ($self,$name,$arr,$sub) = @_;
501  
502 my $path = $self->bg($name)->path;
503 my $t0 = $self->tree->getSubTree($path,$name);
504  
505 my $i = 0;
506 my @arr;
507 for my $e (@$arr) {
508 my $t = Quiq::Storable->clone($t0);
509 push @arr,$sub->($self,$t,$e,$i++);
510 }
511  
512 return \@arr;
513 }
514  
515 # -----------------------------------------------------------------------------
516  
517 =head3 resolvePlaceholders() - Ersetze Platzhalter im Template
518  
519 =head4 Synopsis
520  
521 ($xml,$status,$debugText) = $zug->resolvePlaceholders(@keyVal,%options);
522  
523 =head4 Arguments
524  
525 =over 4
526  
527 =item @keyVal
528  
529 Liste der Platzhalter/Wert-Paare
530  
531 =back
532  
533 =head4 Options
534  
535 =over 4
536  
537 =item -label => $text (Default: '')
538  
539 Versieh den Abschnitt der Platzhalter (bei -showPlaceHolders=>1) mit
540 der Beschriftung $label.
541  
542 =item -showPlaceholders => $bool (Default: 0)
543  
544 Gibt die Liste der Platzhalter auf STDOUT aus
545  
546 =item -showTree => $bool (Default: 0)
547  
548 Gib den resultierenden ZUGFeRD-Baum auf STDOUT aus.
549  
550 =item -subTree => $tree (Default: undef)
551  
552 Führe die Ersetzung auf dem Teilbaum $tree aus.
553  
554 =item -validate => $bool (Default: 0)
555  
556 Aktiviere die Validierung durch XML::Compile
557  
558 =back
559  
560 =head4 Returns
561  
562 (Boolean,String,String) XML nach Platzhalter-Ersetzung und Debug-Text
563  
564 =head4 Description
565  
566 Ersetze die Platzhalter im Template des ZUGFeRD-Profils und liefere
567 das resultierende XML sowie etwaigen Debug-Text zurück.
568  
569 =cut
570  
571 # -----------------------------------------------------------------------------
572  
573 my $ans = Quiq::AnsiColor->new(1);
574  
575 sub resolvePlaceholders {
576 my $self = shift;
577 # @_: @keyVal,%options
578  
579 # Optionen und Argumente
580  
581 my $label = '';
582 my $showPlaceholders = 0;
583 my $showTree = 0;
584 my $subTree => undef;
585 my $validate = 0;
586  
587 my $argA = $self->parameters(0,undef,\@_,
588 -label => \$label,
589 -showPlaceholders => \$showPlaceholders,
590 -showTree => \$showTree,
591 -subTree => \$subTree,
592 -validate => \$validate,
593 );
594 # @$argA;
595  
596 my $text = '';
597 if ($showPlaceholders) {
598 $text .= sprintf "--%s--\n",$ans->str('bold red',$label);
599 for (my $i = 0; $i < @$argA; $i += 2) {
600 my $key = $argA->[$i];
601 my $val = $argA->[$i+1];
602 my $method = lc substr($key,0,2);
603 my $bt = $self->$method($key);
604 $text .= sprintf "%s = %s - %s%s\n",$key,
605 defined($val)? "'$val'": 'undef',
606 $bt->mandatory? $ans->str('bold dark green','*').' ': '',
607 $ans->str('dark green',$bt->text);
608 }
609 }
610  
611 if ($subTree) {
612 $subTree->resolvePlaceholders(
613 '--',
614 @$argA
615 );
616 return (0,'','');;
617 }
618  
619 my ($sch,$rootType,$tree) = $self->get(qw/sch rootType tree/);
620  
621 # Wir operieren auf einer Kopie des Baums
622 $tree = Quiq::Storable->clone($tree);
623  
624 $tree->resolvePlaceholders(
625 '--',
626 @$argA
627 );
628 $tree->reduceTree;
629  
630 if ($showTree) {
631 $text .= "-----\n";
632 $text .= Quiq::Dumper->dump($tree)."\n";
633 $text .= "-----\n";
634 }
635  
636 # (Leeren) XML-Baum instantiieren
637 my $doc = XML::LibXML::Document->new('1.0','UTF-8');
638  
639 # Erzeuge XML-Writer
640  
641 my $wrt = $sch->compile(WRITER=>$rootType,
642 validation => $validate,
643 # Hiermit verhindern wir, dass x0 statt rsm als Präfix gesetzt wird
644 prefixes => [rsm =>
645 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'],
646 );
647  
648 # Wandele den Baum nach XML. Auch wenn eine Validierung abgeschaltet
649 # ist (s.o.), kann es passieren, dass der Writer eine Exception wirft.
650 # Er schreibt dann auch eine Meldung nach STDERR, was störend ist.
651 # Wir fangen beides mit folgendem Code ab.
652  
653 my $error = '';
654 my $xml = do {
655 local *STDERR;
656 Quiq::FileHandle->captureStderr(\$error);
657 eval {$wrt->($doc,$tree)};
658 };
659 $text .= $error;
660  
661 if ($@) {
662 return (1,'',$text);
663 }
664
665 # Liefere das XML formatiert
666  
667 $xml =~ s/\x2//g; # CTRL-B herausfiltern => Quiq Fix
668 return (0,Quiq::Xml->print($xml),$text);
669 }
670  
671 # -----------------------------------------------------------------------------
672  
673 =head3 showPaths() - Zeige die Pfade aller Blattknoten
674  
675 =head4 Synopsis
676  
677 $str = $zug->showPaths;
678  
679 =head4 Returns
680  
681 (String) Liste der Pfade des Baums
682  
683 =head4 Description
684  
685 Zeige die Liste der Pfade aller Blattkonten des ZUGFeRD-Baums
686 (siehe auch die Beispiele Quiq::Tree->leafNodes()).
687  
688 =cut
689  
690 # -----------------------------------------------------------------------------
691  
692 sub showPaths {
693 my $self = shift;
694  
695 my $str = '';
696 my @arr = sort {$a->[0] cmp $b->[0]} Quiq::Tree->leafPaths($self->tree);
697 for (@arr) {
698 my ($path,$val) = @$_;
699 $str .= "$path $val\n";
700 }
701  
702 return $str;
703 }
704  
705 # -----------------------------------------------------------------------------
706  
707 =head3 template() - Liefere das ZUGFeRD-Template
708  
709 =head4 Synopsis
710  
711 $xml = $zug->template;
712  
713 =head4 Returns
714  
715 (String) XML
716  
717 =cut
718  
719 # -----------------------------------------------------------------------------
720  
721 # Accessor-Methode
722  
723 # -----------------------------------------------------------------------------
724  
725 =head3 toXml() - Liefere das Zugferd-XML einer Rechnung
726  
727 =head4 Synopsis
728  
729 $xml = $zug->toXml($rch,%options);
730  
731 =head4 Arguments
732  
733 =over 4
734  
735 =item $rch
736  
737 (object) Rechnung
738  
739 =back
740  
741 =head4 Options
742  
743 =over 4
744  
745 =item -debug => $bool (Default: 0)
746  
747 Gib Detailinformation auf STDOUT aus.
748  
749 =back
750  
751 =head4 Returns
752  
753 (String) XML
754  
755 =head4 Description
756  
757 Erzeuge eine ZUGFeRD XML Repräsentation des Rechnungs-Objekts $rch
758 und liefere diese zurück.
759  
760 =cut
761  
762 # -----------------------------------------------------------------------------
763  
764 sub toXml {
765 my $self = shift;
766 # @_: $rch,%options
767  
768 # Argumente und Optionen
769  
770 my $debug = 0;
771  
772 my $argA = $self->parameters(1,1,\@_,
773 -debug => \$debug,
774 );
775 my $rch = shift @$argA;
776  
777 # Operation ausführen
778  
779 my $debugText = '';
780 if ($debug) {
781 my $a = Quiq::AnsiColor->new(1);
782 $debugText .= sprintf "---%s---\n",$a->str('bold red','Objects');
783 $debugText .= Quiq::Dumper->dump($rch);
784 $debugText .= "---";
785 }
786  
787 # Freitexte
788  
789 my $bg1 = $self->processSubTree('BG-1',$rch->freitexte,sub {
790 my ($zug,$t,$frt,$i) = @_;
791  
792 $zug->resolvePlaceholders(
793 -subTree => $t,
794 -showPlaceholders => $debug,
795 -label => 'Freitext',
796 '--',
797 'BT-21' => $frt->code,
798 'BT-22' => $frt->text,
799 );
800  
801 return $t;
802 });
803  
804 # Rechnungsbegründende Anlagen
805
806 my $bg24 = $self->processSubTree('BG-24',$rch->kaeufer->anhaenge,sub {
807 my ($zug,$t,$anh,$i) = @_;
808 # die Quiq::Dumper->dump($t);
809  
810 $zug->resolvePlaceholders(
811 -subTree => $t,
812 -showPlaceholders => $debug,
813 -label => 'Anhänge',
814 '--',
815 'BT-122' => $anh->id,
816 'BT-122-0' => 916,
817 'BT-123' => $anh->name,
818 'BT-125-1' => $anh->mimeType,
819 'BT-125-2' => $anh->filename,
820 'BT-125' => MIME::Base64::encode_base64($anh->data,''),
821 );
822
823 return $t;
824 });
825  
826 # Positionen
827  
828 my $bg25 = $self->processSubTree('BG-25',$rch->positionen,sub {
829 my ($zug,$t,$pos,$i) = @_;
830  
831 # Artikelattribute
832  
833 my $path = 'SpecifiedTradeProduct.ApplicableProductCharacteristic';
834 my $bg32 = $t->processSubTree($path,'BG-32',$pos->attribute,sub {
835 my ($ztr,$t,$atr,$i) = @_;
836  
837 $t->resolvePlaceholders(
838 -showPlaceholders => 0,
839 '--',
840 'BT-160' => $atr->name,
841 'BT-161' => $atr->wert,
842 );
843  
844 return $t;
845 });
846  
847 # Zu- und Abschläge
848  
849 $path = 'SpecifiedLineTradeSettlement.SpecifiedTradeAllowanceCharge';
850 my $bg27_bg28 = $t->processSubTree($path,'BG-27_BG-28',$pos->zuAbschlaege,sub {
851 my ($ztr,$t,$zap,$i) = @_;
852  
853 $t->resolvePlaceholders(
854 -showPlaceholders => 0,
855 '--',
856 'BG-27-1_BG-28-1' => $zap->typ,
857 'BT-138_BT-143' => $zap->prozent,
858 'BT-137_BT-142' => $zap->grundbetrag,
859 'BT-136_BT-141' => $zap->differenz,
860 'BT-140_BT-145' => $zap->grundCode,
861 'BT-139_BT-144' => $zap->grund,
862 );
863  
864 return $t;
865 });;
866  
867 my $proz = $pos->umsatzsteuersatz // '';
868  
869 $zug->resolvePlaceholders(
870 -subTree => $t,
871 -showPlaceholders => $debug,
872 -label => 'Position',
873 '--',
874 'BT-126' => $pos->positionsnummer,
875 'BT-153' => $pos->artikelname,
876 'BT-154' => $pos->artikelbeschreibung,
877 $pos->transferDate('abrechnungszeitraumVon','BT-134'),
878 $pos->transferDate('abrechnungszeitraumBis','BT-135'),
879 'BT-129' => $pos->menge,
880 'BT-130' => $pos->einheit,
881 'BT-146' => $pos->preisProEinheitNetto,
882 'BT-131' => $pos->gesamtpreisNetto,
883 'BT-152' => $proz,
884 'BT-151' => $proz eq '0.00'? 'E': 'S',
885 'BT-151-0' => 'VAT',
886 # # Zuschläge
887 # 'BT-141' => $pos->zuschlagNetto,
888 # 'BT-144' => $pos->zuschlagGrund,
889 # Artikelattribute
890 'BG-32' => $bg32,
891 # Zu- und Abschläge
892 'BG-27_BG-28' => $bg27_bg28,
893 );
894  
895 return $t;
896 });
897  
898 # Umsatzsteuern
899  
900 my $bg23 = $self->processSubTree('BG-23',$rch->umsatzsteuern,sub {
901 my ($zug,$t,$ums,$i) = @_;
902
903 $zug->resolvePlaceholders(
904 -subTree => $t,
905 -showPlaceholders => $debug,
906 -label => 'Umstzsteueraufschlüsselung',
907 '--',
908 'BT-117' => $ums->umsatzsteuerbetrag,
909 'BT-118-0' => 'VAT',
910 'BT-118' => $ums->umsatzsteuerkategorie,
911 'BT-116' => $ums->summeBetraege,
912 'BT-119' => $ums->prozentsatz,
913 'BT-120' => $ums->befreiungsgrund,
914 );
915  
916 return $t;
917 });
918  
919 my ($status,$xml,$text) = $self->resolvePlaceholders(
920 -label => 'Rechnung',
921 -validate => !$debug,
922 -showPlaceholders => $debug,
923 -showTree => $debug,
924 '--',
925 # Rechnung (allgemeine Angaben)
926 'BT-24' => $rch->profilKennung,
927 'BT-23' => $rch->geschaeftsprozessTyp,
928 'BT-3' => $rch->rechnungsart,
929 'BT-1' => $rch->rechnungsnummer,
930 $rch->transferDate('rechnungsdatum','BT-2'),
931 'BT-5' => $rch->waehrung,
932 'BT-10' => $rch->leitwegId,
933 'BT-12' => $rch->vertragsnummer,
934 'BT-14' => $rch->auftragsnummer,
935 'BT-20' => $rch->zahlungsbedingungen,
936 $rch->transferDate('faelligkeitsdatum','BT-9'),
937 $rch->transferDate('abrechnungszeitraumVon','BT-73'),
938 $rch->transferDate('abrechnungszeitraumBis','BT-74'),
939 'BT-82' => $rch->zahlungsmittel,
940 'BT-83' => $rch->verwendungszweck,
941 'BT-81' => $rch->zahlungsart,
942 'BT-84' => $rch->iban,
943 'BT-86' => $rch->bic,
944 # Verkäufer
945 'BT-27' => $rch->verkaeufer->name,
946 'BT-29' => $rch->verkaeufer->kennung,
947 'BT-30' => $rch->verkaeufer->id,
948 'BT-33' => $rch->verkaeufer->information,
949 'BT-38' => $rch->verkaeufer->plz,
950 'BT-35' => $rch->verkaeufer->strasse,
951 'BT-37' => $rch->verkaeufer->ort,
952 'BT-40' => $rch->verkaeufer->land,
953 'BT-41' => $rch->verkaeufer->kontakt,
954 'BT-42' => $rch->verkaeufer->telefon,
955 'BT-34' => $rch->verkaeufer->contactEmail,
956 'BT-34-1' => $rch->verkaeufer->contactEmail? '0088': undef,
957 # 'BT-34' => $rch->verkaeufer->umsatzsteuernummer,
958 # 'BT-34-1' => $rch->verkaeufer->umsatzsteuernummer? '9930': undef,
959 'BT-43' => $rch->verkaeufer->contactEmail,
960 # wg. Vertauschung UST-ID und Steuernummer
961 # 'BT-14' => $rch->verkaeufer->auftragsreferenz,
962 # 'BT-31' => $rch->verkaeufer->umsatzsteuernummer,
963 # 'BT-31-0' => 'VA',
964 # 'BT-32' => $rch->verkaeufer->steuernummer,
965 # 'BT-32-0' => 'FC',
966 'BT-31' => $rch->verkaeufer->umsatzsteuernummer,
967 'BT-31-0' => 'VA',
968 'BT-32' => $rch->verkaeufer->steuernummer,
969 'BT-32-0' => 'FC',
970 # Käufer (Zahler)
971 'BT-44' => $rch->kaeufer->name,
972 'BT-56' => $rch->kaeufer->kontakt,
973 'BT-46' => $rch->kaeufer->kundennummer,
974 # 'BT-49' => $rch->kaeufer->email,
975 # 'BT-49-1' => $rch->kaeufer->email? '0088': undef,
976 'BT-49' => $rch->leitwegId,
977 'BT-49-1' => $rch->leitwegId? '0204': undef,
978 'BT-50' => $rch->kaeufer->strasse,
979 'BT-53' => $rch->kaeufer->plz,
980 'BT-52' => $rch->kaeufer->ort,
981 'BT-55' => $rch->kaeufer->land,
982 'BT-13' => $rch->kaeufer->auftragsreferenz,
983 'BT-48' => $rch->kaeufer->umsatzsteuernummer,
984 'BT-48-0' => $rch->kaeufer->umsatzsteuernummer? 'VA': undef,
985 # Empfänger
986 'BT-70' => $rch->empfaenger->name,
987 'BT-76' => $rch->empfaenger->kontakt,
988 'BT-75' => $rch->empfaenger->strasse,
989 'BT-78' => $rch->empfaenger->plz,
990 'BT-77' => $rch->empfaenger->ort,
991 'BT-80' => $rch->empfaenger->land,
992 $rch->empfaenger->transferDate('lieferdatum','BT-72'),
993 # Beträge
994 'BT-106' => $rch->summePositionenNetto,
995 'BT-109' => $rch->gesamtsummeNetto,
996 'BT-110' => $rch->summeUmsatzsteuer,
997 'BT-110-0' => $rch->waehrung,
998 'BT-112' => $rch->gesamtsummeBrutto,
999 'BT-115' => $rch->faelligerBetrag,
1000 # Freitexte
1001 'BG-1' => $bg1,
1002 # Positionen
1003 'BG-25' => $bg25,
1004 # Umsatzsteuern
1005 'BG-23' => $bg23,
1006 # Rechnungsbegründende Unterlagen
1007 'BG-24' => $bg24,
1008 );
1009 $debugText .= $text;
1010 # $debugText .= $self->checkAttributes($rch);
1011  
1012 return ($status,$xml,$debugText);
1013 }
1014  
1015 # -----------------------------------------------------------------------------
1016  
1017 =head3 tree() - Liefere den ZUGFeRD-Baum
1018  
1019 =head4 Synopsis
1020  
1021 $tree = $zug->tree;
1022  
1023 =head4 Returns
1024  
1025 (Object) Baum
1026  
1027 =head4 Example
1028  
1029 $ perl -MQuiq::Zugferd -MQuiq::Dumper -E '$t = Quiq::Zugferd->new("en16931")->tree; say Quiq::Dumper->dump($t)'
1030  
1031 =cut
1032  
1033 # -----------------------------------------------------------------------------
1034  
1035 # Accessor-Methode
1036  
1037 # -----------------------------------------------------------------------------
1038  
1039 =head2 Information
1040  
1041 =head3 bg() - Business Group
1042  
1043 =head4 Synopsis
1044  
1045 $bg = $zug->bg($name);
1046  
1047 =head4 Arguments
1048  
1049 =over 4
1050  
1051 =item $name
1052  
1053 Name der Business Group. Beispiel: C<BG-25> (Rechnungsposition)
1054  
1055 =back
1056  
1057 =head4 Returns
1058  
1059 (Object) Business Group
1060  
1061 =head4 Description
1062  
1063 Liefere die Business Group $name. Ist die Business Group nicht definiert,
1064 wirf eine Exception.
1065  
1066 Das Objekt hat die Attribute:
1067  
1068 =over 4
1069  
1070 =item name
1071  
1072 Name der Business Group.
1073  
1074 =item text
1075  
1076 Kurzbeschreibung der Business Group.
1077  
1078 =item path
1079  
1080 Pfad zum Knoten im ZUGFeRD-Baum.
1081  
1082 =back
1083  
1084 =head4 Example
1085  
1086 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->new("en16931")->bg("BG-25")->path'
1087 SupplyChainTradeTransaction.IncludedSupplyChainTradeLineItem
1088  
1089 =cut
1090  
1091 # -----------------------------------------------------------------------------
1092  
1093 sub bg {
1094 my ($self,$name) = @_;
1095  
1096 my $bgH = $self->memoize('bgH',sub {
1097 my ($self,$key) = @_;
1098  
1099 my %h;
1100 my $file = $self->zugferdDir('business-group.csv');
1101 my $fh = Quiq::FileHandle->new('<',$file);
1102 $fh->setEncoding('UTF-8');
1103 while (<$fh>) {
1104 if (/^\s*#/) {
1105 next;
1106 }
1107 chomp;
1108 my ($name,$mandatory,$text,$path) = split /;/;
1109 $h{$name} = Quiq::Hash->new(
1110 name => $name,
1111 mandatory => $mandatory,
1112 path => $path,
1113 text => $text,
1114 );
1115 }
1116 $fh->close;
1117  
1118 return \%h;
1119 });
1120  
1121 my $bg = $bgH->{$name} // $self->throw(
1122 'ZUGFERD-00099: Business group does not exist',
1123 BusinessGroup => $name,
1124 );
1125  
1126 return $bg;
1127 }
1128  
1129 # -----------------------------------------------------------------------------
1130  
1131 =head3 bt() - Business Term(s)
1132  
1133 =head4 Synopsis
1134  
1135 %bt | $btH = $zug->bt;
1136 $bt = $zug->bt($name);
1137  
1138 =head4 Arguments
1139  
1140 =over 4
1141  
1142 =item $name
1143  
1144 Name des Business Terms. Beispiel: C<BT-1> (Rechnungsnummer)
1145  
1146 =back
1147  
1148 =head4 Returns
1149  
1150 (Object) Business Term
1151  
1152 =head4 Description
1153  
1154 Liefere den Business Term $name. Ist der Business Term nicht definiert,
1155 wirf eine Exception.
1156  
1157 Das Objekt hat die Attribute:
1158  
1159 =over 4
1160  
1161 =item name
1162  
1163 Name des Business Terms.
1164  
1165 =item mandatory
1166  
1167 Pflichtfeldstatus. 0=optional, 1=XSD-Pflicht, 2=Mustang-Pflicht
1168  
1169 =item text
1170  
1171 Kurzbeschreibung des Business Terms.
1172  
1173 =item attribute
1174  
1175 Name des Objektattributs
1176  
1177 =item path
1178  
1179 Pfad zum Blattknoten im ZUGFeRD-Baum.
1180  
1181 =back
1182  
1183 =head4 Example
1184  
1185 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->new("en16931")->bt("BT-1")->text'
1186 Rechnungsnummer
1187  
1188 =cut
1189  
1190 # -----------------------------------------------------------------------------
1191  
1192 sub bt {
1193 my ($self,$name) = @_;
1194  
1195 my $btH = $self->memoize('btH',sub {
1196 my ($self,$key) = @_;
1197  
1198 my %h;
1199 my $file = $self->zugferdDir('business-term.csv');
1200 my $fh = Quiq::FileHandle->new('<',$file);
1201 $fh->setEncoding('UTF-8');
1202 while (<$fh>) {
1203 if (/^\s*#/) {
1204 next;
1205 }
1206 chomp;
1207 my ($name,$mandatory,$attribute,$text) = split /;/;
1208 $h{$name} = Quiq::Hash->new(
1209 name => $name,
1210 attribute => $attribute,
1211 mandatory => $mandatory,
1212 text => $text,
1213 path => undef,
1214 );
1215 }
1216 $fh->close;
1217  
1218 $file = $self->zugferdDir('business-term-path.csv');
1219 $fh = Quiq::FileHandle->new('<',$file);
1220 $fh->setEncoding('UTF-8');
1221 while (<$fh>) {
1222 if (/^\s*#/) {
1223 next;
1224 }
1225 chomp;
1226 my ($name,$path) = split /;/;
1227 $h{$name}{'path'} = $path;
1228 }
1229 $fh->close;
1230  
1231 return \%h;
1232 });
1233  
1234 if (!$name) {
1235 my $h = $self->btH;
1236 return wantarray? %$h: $h;
1237 }
1238  
1239 my $bt = $btH->{$name} // $self->throw(
1240 'ZUGFERD-00099: Business term does not exist',
1241 BusinessGroup => $name,
1242 );
1243  
1244 return $bt;
1245 }
1246  
1247 # -----------------------------------------------------------------------------
1248  
1249 =head3 zugferdDir() - Pfad des ZUGFeRD-Verzeichnisses
1250  
1251 =head4 Synopsis
1252  
1253 $path = $zug->zugferdDir;
1254 $path = $zug->zugferdDir($subPath);
1255  
1256 =head4 Arguments
1257  
1258 =over 4
1259  
1260 =item $subPath
1261  
1262 Subpfad ins Verzeichnis
1263  
1264 =back
1265  
1266 =head4 Returns
1267  
1268 (String) Dateipfad
1269  
1270 =head4 Description
1271  
1272 Liefere den Dateipfad des ZUGFeRD-Verzeichnisses, optional ergänzt um
1273 Subpfad $subPath.
1274  
1275 =cut
1276  
1277 # -----------------------------------------------------------------------------
1278  
1279 sub zugferdDir {
1280 my $self = shift;
1281  
1282 my $path = $self->{'zugferdDir'};
1283 if (@_) {
1284 $path .= "/$_[0]";
1285 }
1286  
1287 return $path;
1288 }
1289  
1290 # -----------------------------------------------------------------------------
1291  
1292 =head1 DETAILS
1293  
1294 =head2 Vorgehen bei der Generierung einer ZUGFeRD E-Rechnung
1295  
1296 Der Inhalt einer Rechnung setzt sich aus verschiedenen Bestandteilen
1297 zusammen:
1298  
1299 =over 2
1300  
1301 =item *
1302  
1303 Rechnungsdaten
1304  
1305 =over 2
1306  
1307 =item *
1308  
1309 Allgemeine Rechnungsdaten
1310  
1311 =item *
1312  
1313 Rechnungsreferenzen
1314  
1315 =back
1316  
1317 =item *
1318  
1319 Verkäufer
1320  
1321 =over 2
1322  
1323 =item *
1324  
1325 Informationen zum Verkäufer
1326  
1327 =item *
1328  
1329 Steuervertreter des Verkäufers
1330  
1331 =item *
1332  
1333 Postanschrift des Verkäufers
1334  
1335 =item *
1336  
1337 Kontaktdaten des Verkäufers
1338  
1339 =item *
1340  
1341 Vom Verkäufer abweichender Zahlungsempfänger
1342  
1343 =back
1344  
1345 =item *
1346  
1347 Käufer
1348  
1349 =over 2
1350  
1351 =item *
1352  
1353 Informationen zum Käufer
1354  
1355 =item *
1356  
1357 Postanschrift des Käufers
1358  
1359 =item *
1360  
1361 Kontaktdaten des Käufers
1362  
1363 =item *
1364  
1365 Lieferinformation
1366  
1367 =back
1368  
1369 =item *
1370  
1371 Rechnungspositionen
1372  
1373 =over 2
1374  
1375 =item *
1376  
1377 Rechnungsposition
1378  
1379 =item *
1380  
1381 Weitere Daten zur Position
1382  
1383 =item *
1384  
1385 Nachlässe auf Ebene der Rechnungsposition
1386  
1387 =item *
1388  
1389 Zuschläge auf Ebene der Rechnungsposition
1390  
1391 =back
1392  
1393 =item *
1394  
1395 Rechnungsbeträge
1396  
1397 =over 2
1398  
1399 =item *
1400  
1401 Nachlässe auf Ebene der Rechnung
1402  
1403 =item *
1404  
1405 Zuschläge auf Ebene der Rechnung
1406  
1407 =item *
1408  
1409 Aufschlüsselung der Umsatzsteuer auf Ebene der Rechnung
1410  
1411 =item *
1412  
1413 Rechnungsbeträge
1414  
1415 =back
1416  
1417 =item *
1418  
1419 Zahlungsdaten
1420  
1421 =over 2
1422  
1423 =item *
1424  
1425 Zahlungsdaten
1426  
1427 =item *
1428  
1429 Zahlungsmittel: Überweisung
1430  
1431 =item *
1432  
1433 Zahlungsmittel: Lastschrift
1434  
1435 =back
1436  
1437 =item *
1438  
1439 Anhänge
1440  
1441 =item *
1442  
1443 Verweise
1444  
1445 =item *
1446  
1447 Empfänger
1448  
1449 =item *
1450  
1451 Texte auf Rechnungs- und Positionsebene
1452  
1453 =back
1454  
1455 =head2 Abschläge und Zuschläge
1456  
1457 Abschläge und Zuschläge gibt es auf Positions- und Rechnungsebene.
1458 Diese haben jeweils eine Kardinalität 0..n, sind also als Listen
1459 unterhalb der Position bzw. der Rechnung repräsentiert.
1460 Der Attribute sind bei Ab- und Zuschlägen identisch.
1461 Auf Rechnungsebene haben die Ab- bzw. Zuschläge zusätzlich
1462 Angaben zur Umsatzsteuer.
1463  
1464 Klassen:
1465  
1466 =over 4
1467  
1468 =item ...::ZuAbschlaegeRechnung
1469  
1470 =item ...::ZuAbschlaegePosition
1471  
1472 =back
1473  
1474 =head1 VERSION
1475  
1476 1.238
1477  
1478 =head1 AUTHOR
1479  
1480 Frank Seitz, L<http://fseitz.de/>
1481  
1482 =head1 COPYRIGHT
1483  
1484 Copyright (C) 2026 Frank Seitz
1485  
1486 =head1 LICENSE
1487  
1488 This code is free software; you can redistribute it and/or modify
1489 it under the same terms as Perl itself.
1490  
1491 =cut
1492  
1493 # -----------------------------------------------------------------------------
1494  
1495 1;
1496  
1497 # eof
« Quiq/ContentProcessor.pm ↑ Summary Quiq/Database/Api.pm »