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.240';
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 0 ▶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 'BT-125' => $anh->data,
822 );
823
824 return $t;
825 });
826  
827 # Positionen
828  
829 my $bg25 = $self->processSubTree('BG-25',$rch->positionen,sub {
830 my ($zug,$t,$pos,$i) = @_;
831  
832 # Artikelattribute
833  
834 my $path = 'SpecifiedTradeProduct.ApplicableProductCharacteristic';
835 my $bg32 = $t->processSubTree($path,'BG-32',$pos->attribute,sub {
836 my ($ztr,$t,$atr,$i) = @_;
837  
838 $t->resolvePlaceholders(
839 -showPlaceholders => 0,
840 '--',
841 'BT-160' => $atr->name,
842 'BT-161' => $atr->wert,
843 );
844  
845 return $t;
846 });
847  
848 # Zu- und Abschläge
849  
850 $path = 'SpecifiedLineTradeSettlement.SpecifiedTradeAllowanceCharge';
851 my $bg27_bg28 = $t->processSubTree($path,'BG-27_BG-28',$pos->zuAbschlaege,sub {
852 my ($ztr,$t,$zap,$i) = @_;
853  
854 $t->resolvePlaceholders(
855 -showPlaceholders => 0,
856 '--',
857 'BG-27-1_BG-28-1' => $zap->typ,
858 'BT-138_BT-143' => $zap->prozent,
859 'BT-137_BT-142' => $zap->grundbetrag,
860 'BT-136_BT-141' => $zap->differenz,
861 'BT-140_BT-145' => $zap->grundCode,
862 'BT-139_BT-144' => $zap->grund,
863 );
864  
865 return $t;
866 });;
867  
868 my $proz = $pos->umsatzsteuersatz // '';
869  
870 $zug->resolvePlaceholders(
871 -subTree => $t,
872 -showPlaceholders => $debug,
873 -label => 'Position',
874 '--',
875 'BT-126' => $pos->positionsnummer,
876 'BT-153' => $pos->artikelname,
877 'BT-154' => $pos->artikelbeschreibung,
878 $pos->transferDate('abrechnungszeitraumVon','BT-134'),
879 $pos->transferDate('abrechnungszeitraumBis','BT-135'),
880 'BT-129' => $pos->menge,
881 'BT-130' => $pos->einheit,
882 'BT-146' => $pos->preisProEinheitNetto,
883 'BT-131' => $pos->gesamtpreisNetto,
884 'BT-152' => $proz,
885 'BT-151' => $proz eq '0.00'? 'E': 'S',
886 'BT-151-0' => 'VAT',
887 # # Zuschläge
888 # 'BT-141' => $pos->zuschlagNetto,
889 # 'BT-144' => $pos->zuschlagGrund,
890 # Artikelattribute
891 'BG-32' => $bg32,
892 # Zu- und Abschläge
893 'BG-27_BG-28' => $bg27_bg28,
894 );
895  
896 return $t;
897 });
898  
899 # Umsatzsteuern
900  
901 my $bg23 = $self->processSubTree('BG-23',$rch->umsatzsteuern,sub {
902 my ($zug,$t,$ums,$i) = @_;
903
904 $zug->resolvePlaceholders(
905 -subTree => $t,
906 -showPlaceholders => $debug,
907 -label => 'Umstzsteueraufschlüsselung',
908 '--',
909 'BT-117' => $ums->umsatzsteuerbetrag,
910 'BT-118-0' => 'VAT',
911 'BT-118' => $ums->umsatzsteuerkategorie,
912 'BT-116' => $ums->summeBetraege,
913 'BT-119' => $ums->prozentsatz,
914 'BT-120' => $ums->befreiungsgrund,
915 );
916  
917 return $t;
918 });
919  
920 my ($status,$xml,$text) = $self->resolvePlaceholders(
921 -label => 'Rechnung',
922 -validate => !$debug,
923 -showPlaceholders => $debug,
924 -showTree => $debug,
925 '--',
926 # Rechnung (allgemeine Angaben)
927 'BT-24' => $rch->profilKennung,
928 'BT-23' => $rch->geschaeftsprozessTyp,
929 'BT-3' => $rch->rechnungsart,
930 'BT-1' => $rch->rechnungsnummer,
931 $rch->transferDate('rechnungsdatum','BT-2'),
932 'BT-5' => $rch->waehrung,
933 'BT-10' => $rch->leitwegId,
934 'BT-12' => $rch->vertragsnummer,
935 'BT-14' => $rch->auftragsnummer,
936 'BT-20' => $rch->zahlungsbedingungen,
937 $rch->transferDate('faelligkeitsdatum','BT-9'),
938 $rch->transferDate('abrechnungszeitraumVon','BT-73'),
939 $rch->transferDate('abrechnungszeitraumBis','BT-74'),
940 'BT-82' => $rch->zahlungsmittel,
941 'BT-83' => $rch->verwendungszweck,
942 'BT-81' => $rch->zahlungsart,
943 'BT-84' => $rch->iban,
944 'BT-86' => $rch->bic,
945 # Verkäufer
946 'BT-27' => $rch->verkaeufer->name,
947 'BT-29' => $rch->verkaeufer->kennung,
948 'BT-30' => $rch->verkaeufer->id,
949 'BT-33' => $rch->verkaeufer->information,
950 'BT-38' => $rch->verkaeufer->plz,
951 'BT-35' => $rch->verkaeufer->strasse,
952 'BT-37' => $rch->verkaeufer->ort,
953 'BT-40' => $rch->verkaeufer->land,
954 'BT-41' => $rch->verkaeufer->kontakt,
955 'BT-42' => $rch->verkaeufer->telefon,
956 'BT-34' => $rch->verkaeufer->contactEmail,
957 'BT-34-1' => $rch->verkaeufer->contactEmail? '0088': undef,
958 # 'BT-34' => $rch->verkaeufer->umsatzsteuernummer,
959 # 'BT-34-1' => $rch->verkaeufer->umsatzsteuernummer? '9930': undef,
960 'BT-43' => $rch->verkaeufer->contactEmail,
961 # wg. Vertauschung UST-ID und Steuernummer
962 # 'BT-14' => $rch->verkaeufer->auftragsreferenz,
963 # 'BT-31' => $rch->verkaeufer->umsatzsteuernummer,
964 # 'BT-31-0' => 'VA',
965 # 'BT-32' => $rch->verkaeufer->steuernummer,
966 # 'BT-32-0' => 'FC',
967 'BT-31' => $rch->verkaeufer->umsatzsteuernummer,
968 'BT-31-0' => 'VA',
969 'BT-32' => $rch->verkaeufer->steuernummer,
970 'BT-32-0' => 'FC',
971 # Käufer (Zahler)
972 'BT-44' => $rch->kaeufer->name,
973 'BT-56' => $rch->kaeufer->kontakt,
974 'BT-46' => $rch->kaeufer->kundennummer,
975 # 'BT-49' => $rch->kaeufer->email,
976 # 'BT-49-1' => $rch->kaeufer->email? '0088': undef,
977 'BT-49' => $rch->leitwegId,
978 'BT-49-1' => $rch->leitwegId? '0204': undef,
979 'BT-50' => $rch->kaeufer->strasse,
980 'BT-53' => $rch->kaeufer->plz,
981 'BT-52' => $rch->kaeufer->ort,
982 'BT-55' => $rch->kaeufer->land,
983 'BT-13' => $rch->kaeufer->auftragsreferenz,
984 'BT-48' => $rch->kaeufer->umsatzsteuernummer,
985 'BT-48-0' => $rch->kaeufer->umsatzsteuernummer? 'VA': undef,
986 # Empfänger
987 'BT-70' => $rch->empfaenger->name,
988 'BT-76' => $rch->empfaenger->kontakt,
989 'BT-75' => $rch->empfaenger->strasse,
990 'BT-78' => $rch->empfaenger->plz,
991 'BT-77' => $rch->empfaenger->ort,
992 'BT-80' => $rch->empfaenger->land,
993 $rch->empfaenger->transferDate('lieferdatum','BT-72'),
994 # Beträge
995 'BT-106' => $rch->summePositionenNetto,
996 'BT-109' => $rch->gesamtsummeNetto,
997 'BT-110' => $rch->summeUmsatzsteuer,
998 'BT-110-0' => $rch->waehrung,
999 'BT-112' => $rch->gesamtsummeBrutto,
1000 'BT-115' => $rch->faelligerBetrag,
1001 # Freitexte
1002 'BG-1' => $bg1,
1003 # Positionen
1004 'BG-25' => $bg25,
1005 # Umsatzsteuern
1006 'BG-23' => $bg23,
1007 # Rechnungsbegründende Unterlagen
1008 'BG-24' => $bg24,
1009 );
1010 $debugText .= $text;
1011 # $debugText .= $self->checkAttributes($rch);
1012  
1013 return ($status,$xml,$debugText);
1014 }
1015  
1016 # -----------------------------------------------------------------------------
1017  
1018 =head3 tree() - Liefere den ZUGFeRD-Baum
1019  
1020 =head4 Synopsis
1021  
1022 $tree = $zug->tree;
1023  
1024 =head4 Returns
1025  
1026 (Object) Baum
1027  
1028 =head4 Example
1029  
1030 $ perl -MQuiq::Zugferd -MQuiq::Dumper -E '$t = Quiq::Zugferd->new("en16931")->tree; say Quiq::Dumper->dump($t)'
1031  
1032 =cut
1033  
1034 # -----------------------------------------------------------------------------
1035  
1036 # Accessor-Methode
1037  
1038 # -----------------------------------------------------------------------------
1039  
1040 =head2 Information
1041  
1042 =head3 bg() - Business Group
1043  
1044 =head4 Synopsis
1045  
1046 $bg = $zug->bg($name);
1047  
1048 =head4 Arguments
1049  
1050 =over 4
1051  
1052 =item $name
1053  
1054 Name der Business Group. Beispiel: C<BG-25> (Rechnungsposition)
1055  
1056 =back
1057  
1058 =head4 Returns
1059  
1060 (Object) Business Group
1061  
1062 =head4 Description
1063  
1064 Liefere die Business Group $name. Ist die Business Group nicht definiert,
1065 wirf eine Exception.
1066  
1067 Das Objekt hat die Attribute:
1068  
1069 =over 4
1070  
1071 =item name
1072  
1073 Name der Business Group.
1074  
1075 =item text
1076  
1077 Kurzbeschreibung der Business Group.
1078  
1079 =item path
1080  
1081 Pfad zum Knoten im ZUGFeRD-Baum.
1082  
1083 =back
1084  
1085 =head4 Example
1086  
1087 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->new("en16931")->bg("BG-25")->path'
1088 SupplyChainTradeTransaction.IncludedSupplyChainTradeLineItem
1089  
1090 =cut
1091  
1092 # -----------------------------------------------------------------------------
1093  
1094 sub bg {
1095 my ($self,$name) = @_;
1096  
1097 my $bgH = $self->memoize('bgH',sub {
1098 my ($self,$key) = @_;
1099  
1100 my %h;
1101 my $file = $self->zugferdDir('business-group.csv');
1102 my $fh = Quiq::FileHandle->new('<',$file);
1103 $fh->setEncoding('UTF-8');
1104 while (<$fh>) {
1105 if (/^\s*#/) {
1106 next;
1107 }
1108 chomp;
1109 my ($name,$mandatory,$text,$path) = split /;/;
1110 $h{$name} = Quiq::Hash->new(
1111 name => $name,
1112 mandatory => $mandatory,
1113 path => $path,
1114 text => $text,
1115 );
1116 }
1117 $fh->close;
1118  
1119 return \%h;
1120 });
1121  
1122 my $bg = $bgH->{$name} // $self->throw(
1123 'ZUGFERD-00099: Business group does not exist',
1124 BusinessGroup => $name,
1125 );
1126  
1127 return $bg;
1128 }
1129  
1130 # -----------------------------------------------------------------------------
1131  
1132 =head3 bt() - Business Term(s)
1133  
1134 =head4 Synopsis
1135  
1136 %bt | $btH = $zug->bt;
1137 $bt = $zug->bt($name);
1138  
1139 =head4 Arguments
1140  
1141 =over 4
1142  
1143 =item $name
1144  
1145 Name des Business Terms. Beispiel: C<BT-1> (Rechnungsnummer)
1146  
1147 =back
1148  
1149 =head4 Returns
1150  
1151 (Object) Business Term
1152  
1153 =head4 Description
1154  
1155 Liefere den Business Term $name. Ist der Business Term nicht definiert,
1156 wirf eine Exception.
1157  
1158 Das Objekt hat die Attribute:
1159  
1160 =over 4
1161  
1162 =item name
1163  
1164 Name des Business Terms.
1165  
1166 =item mandatory
1167  
1168 Pflichtfeldstatus. 0=optional, 1=XSD-Pflicht, 2=Mustang-Pflicht
1169  
1170 =item text
1171  
1172 Kurzbeschreibung des Business Terms.
1173  
1174 =item attribute
1175  
1176 Name des Objektattributs
1177  
1178 =item path
1179  
1180 Pfad zum Blattknoten im ZUGFeRD-Baum.
1181  
1182 =back
1183  
1184 =head4 Example
1185  
1186 $ perl -MQuiq::Zugferd -E 'say Quiq::Zugferd->new("en16931")->bt("BT-1")->text'
1187 Rechnungsnummer
1188  
1189 =cut
1190  
1191 # -----------------------------------------------------------------------------
1192  
1193 sub bt {
1194 my ($self,$name) = @_;
1195  
1196 my $btH = $self->memoize('btH',sub {
1197 my ($self,$key) = @_;
1198  
1199 my %h;
1200 my $file = $self->zugferdDir('business-term.csv');
1201 my $fh = Quiq::FileHandle->new('<',$file);
1202 $fh->setEncoding('UTF-8');
1203 while (<$fh>) {
1204 if (/^\s*#/) {
1205 next;
1206 }
1207 chomp;
1208 my ($name,$mandatory,$attribute,$text) = split /;/;
1209 $h{$name} = Quiq::Hash->new(
1210 name => $name,
1211 attribute => $attribute,
1212 mandatory => $mandatory,
1213 text => $text,
1214 path => undef,
1215 );
1216 }
1217 $fh->close;
1218  
1219 $file = $self->zugferdDir('business-term-path.csv');
1220 $fh = Quiq::FileHandle->new('<',$file);
1221 $fh->setEncoding('UTF-8');
1222 while (<$fh>) {
1223 if (/^\s*#/) {
1224 next;
1225 }
1226 chomp;
1227 my ($name,$path) = split /;/;
1228 $h{$name}{'path'} = $path;
1229 }
1230 $fh->close;
1231  
1232 return \%h;
1233 });
1234  
1235 if (!$name) {
1236 my $h = $self->btH;
1237 return wantarray? %$h: $h;
1238 }
1239  
1240 my $bt = $btH->{$name} // $self->throw(
1241 'ZUGFERD-00099: Business term does not exist',
1242 BusinessGroup => $name,
1243 );
1244  
1245 return $bt;
1246 }
1247  
1248 # -----------------------------------------------------------------------------
1249  
1250 =head3 zugferdDir() - Pfad des ZUGFeRD-Verzeichnisses
1251  
1252 =head4 Synopsis
1253  
1254 $path = $zug->zugferdDir;
1255 $path = $zug->zugferdDir($subPath);
1256  
1257 =head4 Arguments
1258  
1259 =over 4
1260  
1261 =item $subPath
1262  
1263 Subpfad ins Verzeichnis
1264  
1265 =back
1266  
1267 =head4 Returns
1268  
1269 (String) Dateipfad
1270  
1271 =head4 Description
1272  
1273 Liefere den Dateipfad des ZUGFeRD-Verzeichnisses, optional ergänzt um
1274 Subpfad $subPath.
1275  
1276 =cut
1277  
1278 # -----------------------------------------------------------------------------
1279  
1280 sub zugferdDir {
1281 my $self = shift;
1282  
1283 my $path = $self->{'zugferdDir'};
1284 if (@_) {
1285 $path .= "/$_[0]";
1286 }
1287  
1288 return $path;
1289 }
1290  
1291 # -----------------------------------------------------------------------------
1292  
1293 =head1 DETAILS
1294  
1295 =head2 Vorgehen bei der Generierung einer ZUGFeRD E-Rechnung
1296  
1297 Der Inhalt einer Rechnung setzt sich aus verschiedenen Bestandteilen
1298 zusammen:
1299  
1300 =over 2
1301  
1302 =item *
1303  
1304 Rechnungsdaten
1305  
1306 =over 2
1307  
1308 =item *
1309  
1310 Allgemeine Rechnungsdaten
1311  
1312 =item *
1313  
1314 Rechnungsreferenzen
1315  
1316 =back
1317  
1318 =item *
1319  
1320 Verkäufer
1321  
1322 =over 2
1323  
1324 =item *
1325  
1326 Informationen zum Verkäufer
1327  
1328 =item *
1329  
1330 Steuervertreter des Verkäufers
1331  
1332 =item *
1333  
1334 Postanschrift des Verkäufers
1335  
1336 =item *
1337  
1338 Kontaktdaten des Verkäufers
1339  
1340 =item *
1341  
1342 Vom Verkäufer abweichender Zahlungsempfänger
1343  
1344 =back
1345  
1346 =item *
1347  
1348 Käufer
1349  
1350 =over 2
1351  
1352 =item *
1353  
1354 Informationen zum Käufer
1355  
1356 =item *
1357  
1358 Postanschrift des Käufers
1359  
1360 =item *
1361  
1362 Kontaktdaten des Käufers
1363  
1364 =item *
1365  
1366 Lieferinformation
1367  
1368 =back
1369  
1370 =item *
1371  
1372 Rechnungspositionen
1373  
1374 =over 2
1375  
1376 =item *
1377  
1378 Rechnungsposition
1379  
1380 =item *
1381  
1382 Weitere Daten zur Position
1383  
1384 =item *
1385  
1386 Nachlässe auf Ebene der Rechnungsposition
1387  
1388 =item *
1389  
1390 Zuschläge auf Ebene der Rechnungsposition
1391  
1392 =back
1393  
1394 =item *
1395  
1396 Rechnungsbeträge
1397  
1398 =over 2
1399  
1400 =item *
1401  
1402 Nachlässe auf Ebene der Rechnung
1403  
1404 =item *
1405  
1406 Zuschläge auf Ebene der Rechnung
1407  
1408 =item *
1409  
1410 Aufschlüsselung der Umsatzsteuer auf Ebene der Rechnung
1411  
1412 =item *
1413  
1414 Rechnungsbeträge
1415  
1416 =back
1417  
1418 =item *
1419  
1420 Zahlungsdaten
1421  
1422 =over 2
1423  
1424 =item *
1425  
1426 Zahlungsdaten
1427  
1428 =item *
1429  
1430 Zahlungsmittel: Überweisung
1431  
1432 =item *
1433  
1434 Zahlungsmittel: Lastschrift
1435  
1436 =back
1437  
1438 =item *
1439  
1440 Anhänge
1441  
1442 =item *
1443  
1444 Verweise
1445  
1446 =item *
1447  
1448 Empfänger
1449  
1450 =item *
1451  
1452 Texte auf Rechnungs- und Positionsebene
1453  
1454 =back
1455  
1456 =head2 Abschläge und Zuschläge
1457  
1458 Abschläge und Zuschläge gibt es auf Positions- und Rechnungsebene.
1459 Diese haben jeweils eine Kardinalität 0..n, sind also als Listen
1460 unterhalb der Position bzw. der Rechnung repräsentiert.
1461 Der Attribute sind bei Ab- und Zuschlägen identisch.
1462 Auf Rechnungsebene haben die Ab- bzw. Zuschläge zusätzlich
1463 Angaben zur Umsatzsteuer.
1464  
1465 Klassen:
1466  
1467 =over 4
1468  
1469 =item ...::ZuAbschlaegeRechnung
1470  
1471 =item ...::ZuAbschlaegePosition
1472  
1473 =back
1474  
1475 =head1 VERSION
1476  
1477 1.240
1478  
1479 =head1 AUTHOR
1480  
1481 Frank Seitz, L<http://fseitz.de/>
1482  
1483 =head1 COPYRIGHT
1484  
1485 Copyright (C) 2026 Frank Seitz
1486  
1487 =head1 LICENSE
1488  
1489 This code is free software; you can redistribute it and/or modify
1490 it under the same terms as Perl itself.
1491  
1492 =cut
1493  
1494 # -----------------------------------------------------------------------------
1495  
1496 1;
1497  
1498 # eof
« Quiq/ContentProcessor.pm ↑ Summary Quiq/Database/Api.pm »