-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathintf.ZUGFeRDInvoiceDescriptor22UBLWriter.pas
More file actions
1225 lines (1047 loc) · 52.7 KB
/
Copy pathintf.ZUGFeRDInvoiceDescriptor22UBLWriter.pas
File metadata and controls
1225 lines (1047 loc) · 52.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.}
unit intf.ZUGFeRDInvoiceDescriptor22UBLWriter;
interface
uses
System.SysUtils,System.Classes,System.StrUtils,System.Generics.Collections
,intf.ZUGFeRDInvoiceDescriptor
,intf.ZUGFeRDInvoiceTypes
,intf.ZUGFeRDProfileAwareXmlTextWriter
,intf.ZUGFeRDIInvoiceDescriptorwriter
,intf.ZUGFeRDProfile
,intf.ZUGFeRDExceptions
,intf.ZUGFeRDHelper
,intf.ZUGFeRDCurrencyCodes
,intf.ZUGFeRDVersion
,intf.ZUGFeRDNote
,intf.ZUGFeRDContentCodes
,intf.ZUGFeRDSubjectCodes
,intf.ZUGFeRDContact
,intf.ZUGFeRDParty
,intf.ZUGFeRDTaxRegistration
,intf.ZUGFeRDGlobalIDSchemeIdentifiers
,intf.ZUGFeRDCountryCodes
,intf.ZUGFeRDTaxRegistrationSchemeID
,intf.ZUGFeRDTax
,intf.ZUGFeRDTaxTypes
,intf.ZUGFeRDTaxCategoryCodes
,intf.ZUGFeRDTradeLineItem
,intf.ZUGFeRDAdditionalReferencedDocument
,intf.ZUGFeRDAdditionalReferencedDocumentTypeCodes
,intf.ZUGFeRDReferenceTypeCodes
,intf.ZUGFeRDPaymentMeansTypeCodes
,intf.ZUGFeRDBankAccount
,intf.ZUGFeRDTradeAllowanceCharge
,intf.ZUGFeRDPaymentTerms
,intf.ZUGFeRDServiceCharge
,intf.ZUGFeRDQuantityCodes
,intf.ZUGFeRDLegalOrganization
,intf.ZUGFeRDPartyTypes
,intf.ZUGFeRDElectronicAddress
,intf.ZUGFeRDElectronicAddressSchemeIdentifiers
,intf.ZUGFeRDTaxExemptionReasonCodes
,intf.ZUGFeRDApplicableProductCharacteristic
,intf.ZUGFeRDReceivableSpecifiedTradeAccountingAccount
,intf.ZUGFeRDAccountingAccountTypeCodes
,intf.ZUGFeRDMimeTypeMapper
,intf.ZUGFeRDSpecialServiceDescriptionCodes
,intf.ZUGFeRDFormats
,intf.ZUGFeRDDesignatedProductClassification
,intf.ZUGFeRDDesignatedProductClassificationClassCodes
,intf.ZUGFeRDIncludedReferencedProduct
,intf.ZUGFeRDInvoiceReferencedDocument
,intf.ZUGFeRDTransportmodeCodes
,intf.ZUGFeRDChargeReasonCodes
,intf.ZUGFeRDAllowanceReasonCodes
,intf.ZUGFeRDInvoiceFormatOptions
,intf.ZUGFeRDInvoiceCommentConstants
,intf.ZUGFeRDLineStatusCodes
,intf.ZUGFeRDLineStatusReasonCodes
,intf.ZUGFeRDTradeDeliveryTermCodes
;
type
TZUGFeRDInvoiceDescriptor22UBLWriter = class(TZUGFeRDIInvoiceDescriptorWriter)
private
Writer: TZUGFeRDProfileAwareXmlTextWriter;
Descriptor: TZUGFeRDInvoiceDescriptor;
procedure _writeOptionalAmount(_writer : TZUGFeRDProfileAwareXmlTextWriter; tagName : string; value : ZUGFeRDNullable<Currency>; numDecimals : Integer = 2; forceCurrency : Boolean = false; profile : TZUGFeRDProfiles = TZUGFERDPROFILES_DEFAULT);
procedure _writeNotes(_writer : TZUGFeRDProfileAwareXmlTextWriter; notes : TObjectList<TZUGFeRDNote>);
procedure _writeOptionalParty(_writer: TZUGFeRDProfileAwareXmlTextWriter; partyType : TZUGFeRDPartyTypes; party : TZUGFeRDParty; contact : TZUGFeRDContact = nil; electronicAddress : TZUGFeRDElectronicAddress = nil; taxRegistrations : TObjectList<TZUGFeRDTaxRegistration> = nil);
procedure _writeApplicableProductCharacteristics(_writer: TZUGFeRDProfileAwareXmlTextWriter; productCharacteristics : TObjectList<TZUGFeRDApplicableProductCharacteristic>);
procedure _writeIncludedReferencedProducts(_writer: TZUGFeRDProfileAwareXmlTextWriter; includedReferencedProducts : TObjectList<TZUGFeRDIncludedReferencedProduct>);
procedure _WriteDocumentLevelAllowanceCharges(tradeAllowanceCharge: TZUGFeRDAbstractTradeAllowanceCharge);
procedure _WriteTradeLineItem(tradeLineItem: TZUGFeRDTradeLineItem; isInvoice: Boolean; options: TZUGFeRDInvoiceFormatOptions);
procedure _WriteItemLevelSpecifiedTradeAllowanceCharge(specifiedTradeAllowanceCharge: TZUGFeRDAbstractTradeAllowanceCharge);
procedure _WriteCommodityClassification(_writer: TZUGFeRDProfileAwareXmlTextWriter; designatedProductClassifications: TObjectList<TZUGFeRDDesignatedProductClassification>);
function _IsInvoiceAccordingToUBLSpecification(type_: TZUGFeRDInvoiceType): Boolean;
function _mapTaxRegistrationSchemeID(schemeID: TZUGFeRDTaxRegistrationSchemeID): string;
function GetNameSpaces(isInvoice: Boolean): TDictionary<string, string>;
private const
ALL_PROFILES = [TZUGFeRDProfile.Minimum,
TZUGFeRDProfile.BasicWL,
TZUGFeRDProfile.Basic,
TZUGFeRDProfile.Comfort,
TZUGFeRDProfile.Extended,
TZUGFeRDProfile.XRechnung1,
TZUGFeRDProfile.XRechnung,
TZUGFeRDProfile.EReporting];
public
function Validate(_descriptor: TZUGFeRDInvoiceDescriptor; _throwExceptions: Boolean = True): Boolean; override;
procedure Save(_descriptor: TZUGFeRDInvoiceDescriptor; _stream: TStream; _format : TZUGFeRDFormats = TZUGFeRDFormats.CII; options: TZUGFeRDInvoiceFormatOptions = Nil); override;
end;
implementation
{ TZUGFeRDInvoiceDescriptor22UBLWriter }
function TZUGFeRDInvoiceDescriptor22UBLWriter.GetNameSpaces(isInvoice: Boolean): TDictionary<string, string>;
begin
Result := TDictionary<string, string>.Create;
Result.Add('cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
Result.Add('cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
Result.Add('ext', 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2');
Result.Add('xs', 'http://www.w3.org/2001/XMLSchema');
if isInvoice then
Result.Add('ubl', 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2')
else
Result.Add('ubl', 'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2');
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter.Save(
_descriptor: TZUGFeRDInvoiceDescriptor; _stream: TStream;
_format: TZUGFeRDFormats; options: TZUGFeRDInvoiceFormatOptions);
var
streamPosition : Int64;
isInvoice : Boolean;
dueDate : TDateTime;
dueDateFound : Boolean;
paymentTerms : TZUGFeRDPaymentTerms;
invoiceReferencedDocument : TZUGFeRDInvoiceReferencedDocument;
document : TZUGFeRDAdditionalReferencedDocument;
traceAccountingAccount : TZUGFeRDReceivableSpecifiedTradeAccountingAccount;
account : TZUGFeRDBankAccount;
tax : TZUGFeRDTax;
tradeAllowanceCharge : TZUGFeRDAbstractTradeAllowanceCharge;
tradeLineItem : TZUGFeRDTradeLineItem;
begin
if (_stream = nil) then
raise TZUGFeRDIllegalStreamException.Create('Cannot write to stream');
streamPosition := _stream.Position;
Descriptor := _descriptor;
isInvoice := _IsInvoiceAccordingToUBLSpecification(Descriptor.Type_);
var automaticallyCleanInvalidXmlCharacters: Boolean := False;
if options <> nil then
automaticallyCleanInvalidXmlCharacters := options.AutomaticallyCleanInvalidCharacters;
Writer := TZUGFeRDProfileAwareXmlTextWriter.Create(_stream, TEncoding.UTF8, Descriptor.Profile, automaticallyCleanInvalidXmlCharacters);
Writer.SetNamespaces(GetNameSpaces(isInvoice));
Writer.WriteStartDocument;
WriteHeaderComments(Writer, options);
// #region Kopfbereich
// UBL has different namespace for different types
if isInvoice then
begin
Writer.WriteStartElement('ubl', 'Invoice', '');
Writer.WriteAttributeString('xmlns', 'ubl', '', 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2');
end
else
begin
Writer.WriteStartElement('ubl', 'CreditNote', '');
Writer.WriteAttributeString('xmlns', 'ubl', '', 'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2');
end;
Writer.WriteAttributeString('xmlns', 'cac', '', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
Writer.WriteAttributeString('xmlns', 'cbc', '', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
Writer.WriteAttributeString('xmlns', 'ext', '', 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2');
Writer.WriteAttributeString('xmlns', 'xs', '', 'http://www.w3.org/2001/XMLSchema');
// #endregion
Writer.WriteElementString('cbc:CustomizationID', 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0');
Writer.WriteElementString('cbc:ProfileID', 'urn:fdc:peppol.eu:2017:poacc:billing:01:1.0');
Writer.WriteElementString('cbc:ID', Descriptor.InvoiceNo); // Rechnungsnummer
if Descriptor.InvoiceDate.HasValue then
begin
Writer.WriteElementString('cbc:IssueDate', _formatDate(Descriptor.InvoiceDate.Value, false, true));
end;
if isInvoice then
begin
// DueDate (BT-9) - cardinality 0..1
dueDateFound := false;
for paymentTerms in Descriptor.PaymentTermsList do
begin
if paymentTerms.DueDate.HasValue then
begin
dueDate := paymentTerms.DueDate.Value;
dueDateFound := true;
Break;
end;
end;
if dueDateFound then
Writer.WriteElementString('cbc:DueDate', _formatDate(dueDate, false, true));
end;
if isInvoice then
Writer.WriteElementString('cbc:InvoiceTypeCode', TEnumExtensions<TZUGFeRDInvoiceType>.EnumToString(Descriptor.Type_))
else
Writer.WriteElementString('cbc:CreditNoteTypeCode', TEnumExtensions<TZUGFeRDInvoiceType>.EnumToString(Descriptor.Type_));
_writeNotes(Writer, Descriptor.Notes);
Writer.WriteElementString('cbc:DocumentCurrencyCode', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
// BT-6
if Descriptor.TaxCurrency.HasValue then
Writer.WriteElementString('cbc:TaxCurrencyCode', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.TaxCurrency.Value));
// BT-19
if Descriptor.ReceivableSpecifiedTradeAccountingAccounts.Count > 0 then
begin
for traceAccountingAccount in Descriptor.ReceivableSpecifiedTradeAccountingAccounts do
begin
if traceAccountingAccount.TradeAccountID.Trim = '' then
Continue;
Writer.WriteOptionalElementString('cbc:AccountingCost', traceAccountingAccount.TradeAccountID);
Break; // Cardinality 0..1
end;
end;
Writer.WriteOptionalElementString('cbc:BuyerReference', Descriptor.ReferenceOrderNo);
if Descriptor.BillingPeriodStart.HasValue or Descriptor.BillingPeriodEnd.HasValue then
begin
Writer.WriteStartElement('cac:InvoicePeriod');
if Descriptor.BillingPeriodStart.HasValue then
Writer.WriteElementString('cbc:StartDate', _formatDate(Descriptor.BillingPeriodStart.Value, false, true));
if Descriptor.BillingPeriodEnd.HasValue then
Writer.WriteElementString('cbc:EndDate', _formatDate(Descriptor.BillingPeriodEnd.Value, false, true));
Writer.WriteEndElement; // !InvoicePeriod
end;
// OrderReference is optional
if Descriptor.OrderNo <> '' then
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.BuyerOrderReferencedDocumentComment);
Writer.WriteStartElement('cac:OrderReference');
Writer.WriteElementString('cbc:ID', Descriptor.OrderNo);
if (Descriptor.SellerOrderReferencedDocument <> nil) then
Writer.WriteOptionalElementString('cbc:SalesOrderID', Descriptor.SellerOrderReferencedDocument.ID);
Writer.WriteEndElement; // !OrderReference
end;
// BillingReference
if Descriptor.InvoiceReferencedDocuments.Count > 0 then
begin
Writer.WriteStartElement('cac:BillingReference');
for invoiceReferencedDocument in Descriptor.InvoiceReferencedDocuments do
begin
Writer.WriteStartElement('cac:InvoiceDocumentReference', [TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
Writer.WriteOptionalElementString('cbc:ID', invoiceReferencedDocument.ID);
if invoiceReferencedDocument.IssueDateTime.HasValue then
Writer.WriteElementString('cbc:IssueDate', _formatDate(invoiceReferencedDocument.IssueDateTime.Value, false, true));
Writer.WriteEndElement; // !cac:InvoiceDocumentReference
Break; // only one reference allowed in UBL
end;
Writer.WriteEndElement; // !cac:BillingReference
end;
// DespatchDocumentReference
if Descriptor.DespatchAdviceReferencedDocument <> nil then
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.DespatchAdviceReferencedDocumentComment);
Writer.WriteStartElement('cac:DespatchDocumentReference');
Writer.WriteOptionalElementString('cbc:ID', Descriptor.DespatchAdviceReferencedDocument.ID);
Writer.WriteEndElement; // !DespatchDocumentReference
end;
// ContractDocumentReference
if Descriptor.ContractReferencedDocument <> nil then
begin
Writer.WriteStartElement('cac:ContractDocumentReference');
Writer.WriteOptionalElementString('cbc:ID', Descriptor.ContractReferencedDocument.ID);
Writer.WriteEndElement; // !ContractDocumentReference
end;
// AdditionalDocumentReference
if Descriptor.AdditionalReferencedDocuments.Count > 0 then
begin
for document in Descriptor.AdditionalReferencedDocuments do
begin
Writer.WriteStartElement('cac:AdditionalDocumentReference');
Writer.WriteStartElement('cbc:ID'); // BT-18, BT-22
if document.ReferenceTypeCode.HasValue then
Writer.WriteAttributeString('schemeID', TEnumExtensions<TZUGFeRDReferenceTypeCodes>.EnumToString(document.ReferenceTypeCode.Value)); // BT-18-1
Writer.WriteValue(document.ID);
Writer.WriteEndElement; // !cbc:ID
if document.TypeCode.HasValue then
Writer.WriteElementString('cbc:DocumentTypeCode', TEnumExtensions<TZUGFeRDAdditionalReferencedDocumentTypeCode>.EnumToString(document.TypeCode.Value));
Writer.WriteOptionalElementString('cbc:DocumentDescription', document.Name); // BT-123
if document.AttachmentBinaryObject <> nil then
begin
Writer.WriteStartElement('cac:Attachment');
Writer.WriteStartElement('cbc:EmbeddedDocumentBinaryObject'); // BT-125
Writer.WriteAttributeString('filename', document.Filename);
Writer.WriteAttributeString('mimeCode', TZUGFeRDMimeTypeMapper.GetMimeType(document.Filename));
Writer.WriteValue(TZUGFeRDHelper.GetDataAsBase64(document.AttachmentBinaryObject));
Writer.WriteEndElement; // !cbc:EmbeddedDocumentBinaryObject
Writer.WriteEndElement; // !cac:Attachment
end;
Writer.WriteEndElement; // !AdditionalDocumentReference
end;
end;
// ProjectReference
if Descriptor.SpecifiedProcuringProject <> nil then
begin
Writer.WriteStartElement('cac:ProjectReference');
Writer.WriteOptionalElementString('cbc:ID', Descriptor.SpecifiedProcuringProject.ID);
Writer.WriteEndElement; // !ProjectReference
end;
// #region SellerTradeParty
// AccountingSupplierParty = PartyTypes.SellerTradeParty
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.SellerTradePartyComment);
_writeOptionalParty(Writer, TZUGFeRDPartyTypes.SellerTradeParty, Descriptor.Seller, Descriptor.SellerContact, Descriptor.SellerElectronicAddress, Descriptor.SellerTaxRegistration);
// #endregion
// #region BuyerTradeParty
// AccountingCustomerParty = PartyTypes.BuyerTradeParty
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.BuyerTradePartyComment);
_writeOptionalParty(Writer, TZUGFeRDPartyTypes.BuyerTradeParty, Descriptor.Buyer, Descriptor.BuyerContact, Descriptor.BuyerElectronicAddress, Descriptor.BuyerTaxRegistration);
// #endregion
if Descriptor.SellerTaxRepresentative <> nil then
_writeOptionalParty(Writer, TZUGFeRDPartyTypes.SellerTaxRepresentativeTradeParty, Descriptor.SellerTaxRepresentative);
// Delivery = ShipToTradeParty
if (Descriptor.ShipTo <> nil) or (Descriptor.ActualDeliveryDate.HasValue) then
begin
Writer.WriteStartElement('cac:Delivery');
if Descriptor.ActualDeliveryDate.HasValue then
begin
Writer.WriteStartElement('cbc:ActualDeliveryDate');
Writer.WriteValue(_formatDate(Descriptor.ActualDeliveryDate.Value, false, true));
Writer.WriteEndElement; // !ActualDeliveryDate
end;
if Descriptor.ShipTo <> nil then
begin
Writer.WriteStartElement('cac:DeliveryLocation');
if Descriptor.ShipTo.ID <> nil then
Writer.WriteOptionalElementString('cbc:ID', Descriptor.ShipTo.ID.ID);
Writer.WriteStartElement('cac:Address');
Writer.WriteOptionalElementString('cbc:StreetName', Descriptor.ShipTo.Street);
Writer.WriteOptionalElementString('cbc:AdditionalStreetName', Descriptor.ShipTo.Street2);
Writer.WriteOptionalElementString('cbc:CityName', Descriptor.ShipTo.City);
Writer.WriteOptionalElementString('cbc:PostalZone', Descriptor.ShipTo.Postcode);
Writer.WriteOptionalElementString('cbc:CountrySubentity', Descriptor.ShipTo.CountrySubdivisionName);
Writer.WriteStartElement('cac:Country');
if Descriptor.ShipTo.Country.HasValue then
Writer.WriteElementString('cbc:IdentificationCode', TEnumExtensions<TZUGFeRDCountryCodes>.EnumToString(Descriptor.ShipTo.Country.Value));
Writer.WriteEndElement; // !Country
Writer.WriteEndElement; // !Address
Writer.WriteEndElement; // !DeliveryLocation
if Descriptor.ShipTo.Name <> '' then
begin
Writer.WriteStartElement('cac:DeliveryParty');
Writer.WriteStartElement('cac:PartyName');
Writer.WriteStartElement('cbc:Name');
Writer.WriteValue(Descriptor.ShipTo.Name);
Writer.WriteEndElement; // !Name
Writer.WriteEndElement; // !PartyName
Writer.WriteEndElement; // !DeliveryParty
end;
end;
Writer.WriteEndElement; // !Delivery
end;
// PaymentMeans
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.ApplicableHeaderTradeSettlementComment);
if (Descriptor.CreditorBankAccounts.Count = 0) and (Descriptor.DebitorBankAccounts.Count = 0) then
begin
if Descriptor.PaymentMeans <> nil then
begin
if (Descriptor.PaymentMeans <> nil) and Descriptor.PaymentMeans.TypeCode.HasValue then
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.SpecifiedTradeSettlementPaymentMeansComment);
Writer.WriteStartElement('cac:PaymentMeans', [TZUGFeRDProfile.BasicWL, TZUGFeRDProfile.Basic, TZUGFeRDProfile.Comfort, TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
Writer.WriteElementString('cbc:PaymentMeansCode', TEnumExtensions<TZUGFeRDPaymentMeansTypeCodes>.EnumToString(Descriptor.PaymentMeans.TypeCode.Value));
Writer.WriteOptionalElementString('cbc:PaymentID', Descriptor.PaymentReference);
if Descriptor.PaymentMeans.FinancialCard <> nil then
begin
Writer.WriteStartElement('cac:CardAccount', [TZUGFeRDProfile.BasicWL, TZUGFeRDProfile.Basic, TZUGFeRDProfile.Comfort, TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
Writer.WriteElementString('cbc:PrimaryAccountNumberID', Descriptor.PaymentMeans.FinancialCard.Id);
Writer.WriteOptionalElementString('cbc:HolderName', Descriptor.PaymentMeans.FinancialCard.CardholderName);
Writer.WriteEndElement; // !CardAccount
end;
Writer.WriteEndElement; // !PaymentMeans
end;
end;
end
else
begin
for account in Descriptor.CreditorBankAccounts do
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.SpecifiedTradeSettlementPaymentMeansComment);
Writer.WriteStartElement('cac:PaymentMeans', [TZUGFeRDProfile.BasicWL, TZUGFeRDProfile.Basic, TZUGFeRDProfile.Comfort, TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
if (Descriptor.PaymentMeans <> nil) and Descriptor.PaymentMeans.TypeCode.HasValue then
begin
Writer.WriteElementString('cbc:PaymentMeansCode', TEnumExtensions<TZUGFeRDPaymentMeansTypeCodes>.EnumToString(Descriptor.PaymentMeans.TypeCode.Value));
Writer.WriteOptionalElementString('cbc:PaymentID', Descriptor.PaymentReference);
if Descriptor.PaymentMeans.FinancialCard <> nil then
begin
Writer.WriteStartElement('cac:CardAccount', [TZUGFeRDProfile.BasicWL, TZUGFeRDProfile.Basic, TZUGFeRDProfile.Comfort, TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
Writer.WriteElementString('cbc:PrimaryAccountNumberID', Descriptor.PaymentMeans.FinancialCard.Id);
Writer.WriteOptionalElementString('cbc:HolderName', Descriptor.PaymentMeans.FinancialCard.CardholderName);
Writer.WriteEndElement; // !CardAccount
end;
end;
// PayeeFinancialAccount
Writer.WriteStartElement('cac:PayeeFinancialAccount');
Writer.WriteElementString('cbc:ID', account.IBAN);
Writer.WriteOptionalElementString('cbc:Name', account.Name);
if account.BIC <> '' then
begin
Writer.WriteStartElement('cac:FinancialInstitutionBranch');
Writer.WriteElementString('cbc:ID', account.BIC);
Writer.WriteEndElement; // !FinancialInstitutionBranch
end;
Writer.WriteEndElement; // !PayeeFinancialAccount
Writer.WriteEndElement; // !PaymentMeans
end;
// [BR-67] - An Invoice shall contain maximum one Payment Mandate (BG-19).
for account in Descriptor.DebitorBankAccounts do
begin
Writer.WriteStartElement('cac:PaymentMeans', [TZUGFeRDProfile.BasicWL, TZUGFeRDProfile.Basic, TZUGFeRDProfile.Comfort, TZUGFeRDProfile.Extended, TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
if (Descriptor.PaymentMeans <> nil) and Descriptor.PaymentMeans.TypeCode.HasValue then
begin
Writer.WriteElementString('cbc:PaymentMeansCode', TEnumExtensions<TZUGFeRDPaymentMeansTypeCodes>.EnumToString(Descriptor.PaymentMeans.TypeCode.Value));
Writer.WriteOptionalElementString('cbc:PaymentID', Descriptor.PaymentReference);
end;
Writer.WriteStartElement('cac:PaymentMandate');
// PEPPOL-EN16931-R061: Mandate reference MUST be provided for direct debit.
Writer.WriteElementString('cbc:ID', Descriptor.PaymentMeans.SEPAMandateReference);
Writer.WriteStartElement('cac:PayerFinancialAccount');
Writer.WriteElementString('cbc:ID', account.IBAN);
Writer.WriteEndElement; // !PayerFinancialAccount
Writer.WriteEndElement; // !PaymentMandate
Writer.WriteEndElement; // !PaymentMeans
end;
end;
// PaymentTerms (optional)
var hasPaymentTermsDescription := false;
for paymentTerms in Descriptor.PaymentTermsList do
begin
if paymentTerms.Description <> '' then
begin
hasPaymentTermsDescription := true;
Break;
end;
end;
if hasPaymentTermsDescription then
begin
Writer.WriteStartElement('cac:PaymentTerms');
var hasAnyDescription := false;
for paymentTerms in Descriptor.PaymentTermsList do
begin
if paymentTerms.Description.Trim <> '' then
begin
hasAnyDescription := true;
Break;
end;
end;
if hasAnyDescription then
begin
Writer.WriteStartElement('cbc:Note');
for paymentTerms in Descriptor.PaymentTermsList do
begin
if paymentTerms.Description <> '' then
begin
Writer.WriteRawString(sLineBreak);
Writer.WriteRawIndention;
Writer.WriteValue(paymentTerms.Description);
end;
end;
Writer.WriteRawString(sLineBreak);
Writer.WriteEndElement; // !Note
end;
Writer.WriteEndElement; // !PaymentTerms
end;
// #region AllowanceCharge
for tradeAllowanceCharge in Descriptor.TradeAllowanceCharges do
_WriteDocumentLevelAllowanceCharges(tradeAllowanceCharge);
// #endregion
// Tax Total
if (Descriptor.Taxes.Count > 0) and (Descriptor.TaxTotalAmount.HasValue) then
begin
Writer.WriteStartElement('cac:TaxTotal');
_writeOptionalAmount(Writer, 'cbc:TaxAmount', Descriptor.TaxTotalAmount, 2, true);
for tax in Descriptor.Taxes do
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.ApplicableTradeTaxComment);
Writer.WriteStartElement('cac:TaxSubtotal');
_writeOptionalAmount(Writer, 'cbc:TaxableAmount', tax.BasisAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:TaxAmount', tax.TaxAmount, 2, true);
Writer.WriteStartElement('cac:TaxCategory');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxCategoryCodes>.EnumToString(tax.CategoryCode));
if tax.CategoryCode <> TZUGFeRDTaxCategoryCodes.O then // BR-O-05
Writer.WriteElementString('cbc:Percent', _formatDecimal(tax.Percent));
if tax.ExemptionReasonCode.HasValue then
Writer.WriteElementString('cbc:TaxExemptionReasonCode', TEnumExtensions<TZUGFeRDTaxExemptionReasonCodes>.EnumToString(tax.ExemptionReasonCode.Value));
Writer.WriteOptionalElementString('cbc:TaxExemptionReason', tax.ExemptionReason);
Writer.WriteStartElement('cac:TaxScheme');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxTypes>.EnumToString(tax.TypeCode));
Writer.WriteEndElement; // !TaxScheme
Writer.WriteEndElement; // !TaxCategory
Writer.WriteEndElement; // !TaxSubtotal
end;
Writer.WriteEndElement; // !TaxTotal
end;
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.SpecifiedTradeSettlementHeaderMonetarySummationComment);
Writer.WriteStartElement('cac:LegalMonetaryTotal');
_writeOptionalAmount(Writer, 'cbc:LineExtensionAmount', Descriptor.LineTotalAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:TaxExclusiveAmount', Descriptor.TaxBasisAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:TaxInclusiveAmount', Descriptor.GrandTotalAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:AllowanceTotalAmount', Descriptor.AllowanceTotalAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:ChargeTotalAmount', Descriptor.ChargeTotalAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:PrepaidAmount', Descriptor.TotalPrepaidAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:PayableRoundingAmount', Descriptor.RoundingAmount, 2, true);
_writeOptionalAmount(Writer, 'cbc:PayableAmount', Descriptor.DuePayableAmount, 2, true);
Writer.WriteEndElement; // !LegalMonetaryTotal
for tradeLineItem in Descriptor.TradeLineItems do
begin
// Skip items with parent line id because these are written recursively in _WriteTradeLineItem
if tradeLineItem.AssociatedDocument.ParentLineID = '' then
begin
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.IncludedSupplyChainTradeLineItemComment);
_WriteTradeLineItem(tradeLineItem, isInvoice, options);
end;
end;
Writer.WriteEndDocument;
Writer.Flush;
_stream.Seek(streamPosition, TSeekOrigin.soBeginning);
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter._WriteDocumentLevelAllowanceCharges(
tradeAllowanceCharge: TZUGFeRDAbstractTradeAllowanceCharge);
begin
if tradeAllowanceCharge = nil then
Exit;
Writer.WriteStartElement('cac:AllowanceCharge');
Writer.WriteElementString('cbc:ChargeIndicator', IfThen(tradeAllowanceCharge.ChargeIndicator, 'true', 'false'));
if (tradeAllowanceCharge is TZUGFeRDTradeAllowance) and
TZUGFeRDTradeAllowance(tradeAllowanceCharge).ReasonCode.HasValue then
begin
Writer.WriteStartElement('cbc:AllowanceChargeReasonCode'); // BT-97
Writer.WriteValue(TEnumExtensions<TZUGFeRDAllowanceReasonCodes>.EnumToString(TZUGFeRDTradeAllowance(tradeAllowanceCharge).ReasonCode.Value));
Writer.WriteEndElement;
end
else if (tradeAllowanceCharge is TZUGFeRDTradeCharge) and
TZUGFeRDTradeCharge(tradeAllowanceCharge).ReasonCode.HasValue then
begin
Writer.WriteStartElement('cbc:AllowanceChargeReasonCode'); // BT-104
Writer.WriteValue(TEnumExtensions<TZUGFeRDChargeReasonCodes>.EnumToString(TZUGFeRDTradeCharge(tradeAllowanceCharge).ReasonCode.Value));
Writer.WriteEndElement;
end;
if tradeAllowanceCharge.Reason <> '' then
begin
Writer.WriteStartElement('cbc:AllowanceChargeReason'); // BT-97 / BT-104
Writer.WriteValue(tradeAllowanceCharge.Reason);
Writer.WriteEndElement;
end;
if tradeAllowanceCharge.ChargePercentage.HasValue and tradeAllowanceCharge.BasisAmount.HasValue then
begin
Writer.WriteStartElement('cbc:MultiplierFactorNumeric');
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.ChargePercentage.Value, 2));
Writer.WriteEndElement;
end;
Writer.WriteStartElement('cbc:Amount'); // BT-92 / BT-99
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.ActualAmount));
Writer.WriteEndElement;
if tradeAllowanceCharge.BasisAmount.HasValue then
begin
Writer.WriteStartElement('cbc:BaseAmount'); // BT-93 / BT-100
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.BasisAmount.Value));
Writer.WriteEndElement;
end;
Writer.WriteStartElement('cac:TaxCategory');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxCategoryCodes>.EnumToString(tradeAllowanceCharge.Tax.CategoryCode));
Writer.WriteElementString('cbc:Percent', _formatDecimal(tradeAllowanceCharge.Tax.Percent));
Writer.WriteStartElement('cac:TaxScheme');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxTypes>.EnumToString(tradeAllowanceCharge.Tax.TypeCode));
Writer.WriteEndElement; // !cac:TaxScheme
Writer.WriteEndElement; // !cac:TaxCategory
Writer.WriteEndElement; // !AllowanceCharge
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter._WriteTradeLineItem(
tradeLineItem: TZUGFeRDTradeLineItem; isInvoice: Boolean;
options: TZUGFeRDInvoiceFormatOptions);
var
document : TZUGFeRDAdditionalReferencedDocument;
specifiedTradeAllowance : TZUGFeRDTradeAllowance;
specifiedTradeCharge : TZUGFeRDTradeCharge;
subTradeLineItem : TZUGFeRDTradeLineItem;
charges : TObjectList<TZUGFeRDAbstractTradeAllowanceCharge>;
begin
if tradeLineItem.AssociatedDocument.ParentLineID = '' then
begin
if isInvoice then
Writer.WriteStartElement('cac:InvoiceLine')
else
Writer.WriteStartElement('cac:CreditNoteLine');
end
else
begin
if isInvoice then
Writer.WriteStartElement('cac:SubInvoiceLine')
else
Writer.WriteStartElement('cac:SubCreditNoteLine');
end;
Writer.WriteElementString('cbc:ID', tradeLineItem.AssociatedDocument.LineID);
if (tradeLineItem.AssociatedDocument <> nil) and
(tradeLineItem.AssociatedDocument.Notes <> nil) and
(tradeLineItem.AssociatedDocument.Notes.Count > 0) then
begin
// BT-127
var noteContent := '';
for var note : TZUGFeRDNote in tradeLineItem.AssociatedDocument.Notes do
begin
if noteContent <> '' then
noteContent := noteContent + sLineBreak;
noteContent := noteContent + note.Content;
end;
Writer.WriteStartElement('cbc:Note');
Writer.WriteValue(noteContent);
Writer.WriteEndElement; // !cbc:Note
end;
if isInvoice then
Writer.WriteStartElement('cbc:InvoicedQuantity')
else
Writer.WriteStartElement('cbc:CreditedQuantity');
Writer.WriteAttributeString('unitCode', TEnumExtensions<TZUGFeRDQuantityCodes>.EnumToString(tradeLineItem.UnitCode));
Writer.WriteValue(_formatDecimal(tradeLineItem.BilledQuantity, 4));
Writer.WriteEndElement; // !InvoicedQuantity || CreditedQuantity
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.SpecifiedTradeSettlementLineMonetarySummationComment);
_writeOptionalAmount(Writer, 'cbc:LineExtensionAmount', tradeLineItem.LineTotalAmount, 2, true);
if tradeLineItem.BillingPeriodStart.HasValue or tradeLineItem.BillingPeriodEnd.HasValue then
begin
Writer.WriteStartElement('cac:InvoicePeriod');
if tradeLineItem.BillingPeriodStart.HasValue then
Writer.WriteElementString('cbc:StartDate', _formatDate(tradeLineItem.BillingPeriodStart.Value, false, true));
if tradeLineItem.BillingPeriodEnd.HasValue then
Writer.WriteElementString('cbc:EndDate', _formatDate(tradeLineItem.BillingPeriodEnd.Value, false, true));
Writer.WriteEndElement; // !InvoicePeriod
end;
if tradeLineItem.AdditionalReferencedDocuments.Count > 0 then
begin
for document in tradeLineItem.AdditionalReferencedDocuments do
begin
Writer.WriteStartElement('cac:DocumentReference');
Writer.WriteStartElement('cbc:ID'); // BT-18, BT-22
if document.ReferenceTypeCode.HasValue then
Writer.WriteAttributeString('schemeID', TEnumExtensions<TZUGFeRDReferenceTypeCodes>.EnumToString(document.ReferenceTypeCode.Value)); // BT-18-1
Writer.WriteValue(document.ID);
Writer.WriteEndElement; // !cbc:ID
if document.TypeCode.HasValue then
Writer.WriteElementString('cbc:DocumentTypeCode', TEnumExtensions<TZUGFeRDAdditionalReferencedDocumentTypeCode>.EnumToString(document.TypeCode.Value));
Writer.WriteOptionalElementString('cbc:DocumentDescription', document.Name); // BT-123
Writer.WriteEndElement; // !DocumentReference
end;
end;
for specifiedTradeAllowance in tradeLineItem.GetSpecifiedTradeAllowances do
_WriteItemLevelSpecifiedTradeAllowanceCharge(specifiedTradeAllowance);
for specifiedTradeCharge in tradeLineItem.GetSpecifiedTradeCharges do
_WriteItemLevelSpecifiedTradeAllowanceCharge(specifiedTradeCharge);
Writer.WriteStartElement('cac:Item');
Writer.WriteOptionalElementString('cbc:Description', tradeLineItem.Description);
Writer.WriteElementString('cbc:Name', tradeLineItem.Name);
if tradeLineItem.BuyerAssignedID <> '' then
begin
Writer.WriteStartElement('cac:BuyersItemIdentification');
Writer.WriteElementString('cbc:ID', tradeLineItem.BuyerAssignedID);
Writer.WriteEndElement; // !BuyersItemIdentification
end;
if tradeLineItem.SellerAssignedID <> '' then
begin
Writer.WriteStartElement('cac:SellersItemIdentification');
Writer.WriteElementString('cbc:ID', tradeLineItem.SellerAssignedID);
Writer.WriteEndElement; // !SellersItemIdentification
end;
_writeIncludedReferencedProducts(Writer, tradeLineItem.IncludedReferencedProducts);
_WriteCommodityClassification(Writer, tradeLineItem.DesignedProductClassifications);
// [UBL-SR-48] - Invoice lines shall have one and only one classified tax category.
Writer.WriteStartElement('cac:ClassifiedTaxCategory');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxCategoryCodes>.EnumToString(tradeLineItem.TaxCategoryCode));
Writer.WriteElementString('cbc:Percent', _formatDecimal(tradeLineItem.TaxPercent));
Writer.WriteStartElement('cac:TaxScheme');
Writer.WriteElementString('cbc:ID', TEnumExtensions<TZUGFeRDTaxTypes>.EnumToString(tradeLineItem.TaxType));
Writer.WriteEndElement; // !TaxScheme
Writer.WriteEndElement; // !ClassifiedTaxCategory
_writeApplicableProductCharacteristics(Writer, tradeLineItem.ApplicableProductCharacteristics);
Writer.WriteEndElement; // !Item
Writer.WriteStartElement('cac:Price'); // BG-29
WriteComment(Writer, options, TZUGFeRDInvoiceCommentConstants.NetPriceProductTradePriceComment);
Writer.WriteStartElement('cbc:PriceAmount');
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
// UBL-DT-01 explicitly excempts the price amount from the 2 decimal rule for amount elements,
// thus allowing for 4 decimal places (needed for e.g. fuel prices)
Writer.WriteValue(_formatDecimal(tradeLineItem.NetUnitPrice.Value, 4));
Writer.WriteEndElement;
if tradeLineItem.NetQuantity.HasValue then
begin
Writer.WriteStartElement('cbc:BaseQuantity'); // BT-149
Writer.WriteAttributeString('unitCode', TEnumExtensions<TZUGFeRDQuantityCodes>.EnumToString(tradeLineItem.UnitCode)); // BT-150
Writer.WriteValue(_formatDecimal(tradeLineItem.NetQuantity.Value));
Writer.WriteEndElement;
end;
charges := tradeLineItem.TradeAllowanceCharges;
if charges.Count > 0 then // only one charge possible in UBL
begin
var tradeAllowanceCharge := charges[0];
Writer.WriteStartElement('cac:AllowanceCharge');
if tradeAllowanceCharge is TZUGFeRDTradeAllowance then
Writer.WriteElementString('cbc:ChargeIndicator', 'false')
else
Writer.WriteElementString('cbc:ChargeIndicator', 'true');
Writer.WriteStartElement('cbc:Amount'); // BT-147
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.ActualAmount));
Writer.WriteEndElement;
if tradeAllowanceCharge.BasisAmount.HasValue then // BT-148 is optional
begin
Writer.WriteStartElement('cbc:BaseAmount'); // BT-148
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.BasisAmount.Value));
Writer.WriteEndElement;
end;
Writer.WriteEndElement; // !AllowanceCharge
end;
Writer.WriteEndElement; // !Price
// Write sub invoice lines recursively
for subTradeLineItem in Descriptor.TradeLineItems do
begin
if subTradeLineItem.AssociatedDocument.ParentLineID = tradeLineItem.AssociatedDocument.LineID then
_WriteTradeLineItem(subTradeLineItem, isInvoice, options);
end;
Writer.WriteEndElement; // !InvoiceLine
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter._WriteItemLevelSpecifiedTradeAllowanceCharge(
specifiedTradeAllowanceCharge: TZUGFeRDAbstractTradeAllowanceCharge);
begin
Writer.WriteStartElement('cac:AllowanceCharge');
Writer.WriteElementString('cbc:ChargeIndicator',
IfThen(specifiedTradeAllowanceCharge.ChargeIndicator, 'true', 'false')); // BG-28-0
if (specifiedTradeAllowanceCharge is TZUGFeRDTradeAllowance) and
TZUGFeRDTradeAllowance(specifiedTradeAllowanceCharge).ReasonCode.HasValue then
begin
Writer.WriteOptionalElementString('cbc:AllowanceChargeReasonCode',
TEnumExtensions<TZUGFeRDAllowanceReasonCodes>.EnumToString(
TZUGFeRDTradeAllowance(specifiedTradeAllowanceCharge).ReasonCode.Value)); // BT-140
end
else if (specifiedTradeAllowanceCharge is TZUGFeRDTradeCharge) and
TZUGFeRDTradeCharge(specifiedTradeAllowanceCharge).ReasonCode.HasValue then
begin
Writer.WriteOptionalElementString('cbc:AllowanceChargeReasonCode',
TEnumExtensions<TZUGFeRDChargeReasonCodes>.EnumToString(
TZUGFeRDTradeCharge(specifiedTradeAllowanceCharge).ReasonCode.Value)); // BT-145
end;
Writer.WriteOptionalElementString('cbc:AllowanceChargeReason',
specifiedTradeAllowanceCharge.Reason); // BT-139, BT-144
if specifiedTradeAllowanceCharge.ChargePercentage.HasValue then
begin
Writer.WriteOptionalElementString('cbc:MultiplierFactorNumeric',
_formatDecimal(specifiedTradeAllowanceCharge.ChargePercentage.Value));
end;
Writer.WriteStartElement('cbc:Amount');
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(specifiedTradeAllowanceCharge.ActualAmount));
Writer.WriteEndElement; // !Amount
if specifiedTradeAllowanceCharge.BasisAmount.HasValue then
begin
Writer.WriteStartElement('cbc:BaseAmount'); // BT-137, BT-142
Writer.WriteAttributeString('currencyID', TEnumExtensions<TZUGFeRDCurrencyCodes>.EnumToString(Descriptor.Currency));
Writer.WriteValue(_formatDecimal(specifiedTradeAllowanceCharge.BasisAmount.Value));
Writer.WriteEndElement; // !BaseAmount
end;
Writer.WriteEndElement; // !AllowanceCharge
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter._WriteCommodityClassification(
_writer: TZUGFeRDProfileAwareXmlTextWriter;
designatedProductClassifications: TObjectList<TZUGFeRDDesignatedProductClassification>);
var
classification : TZUGFeRDDesignatedProductClassification;
begin
if (designatedProductClassifications = nil) or (designatedProductClassifications.Count = 0) then
Exit;
_writer.WriteStartElement('cac:CommodityClassification');
for classification in designatedProductClassifications do
begin
if not classification.ListID.HasValue then
Continue;
_writer.WriteStartElement('cbc:ItemClassificationCode'); // BT-158
Writer.WriteAttributeString('listID', TEnumExtensions<TZUGFeRDDesignatedProductClassificationClassCodes>.EnumToString(classification.ListID.Value)); // BT-158-1
if classification.ListVersionID <> '' then
Writer.WriteAttributeString('listVersionID', classification.ListVersionID); // BT-158-2
// no name attribute in Peppol Billing!
_writer.WriteValue(classification.ClassCode, ALL_PROFILES);
_writer.WriteEndElement;
end;
_writer.WriteEndElement;
end;
procedure TZUGFeRDInvoiceDescriptor22UBLWriter._writeOptionalParty(
_writer: TZUGFeRDProfileAwareXmlTextWriter;
partyType: TZUGFeRDPartyTypes; party: TZUGFeRDParty;
contact: TZUGFeRDContact; electronicAddress: TZUGFeRDElectronicAddress;
taxRegistrations: TObjectList<TZUGFeRDTaxRegistration>);
var
taxReg : TZUGFeRDTaxRegistration;
begin
// filter according to https://github.com/stephanstapel/ZUGFeRD-csharp/pull/221
case partyType of
TZUGFeRDPartyTypes.Unknown: Exit;
TZUGFeRDPartyTypes.SellerTradeParty: ;
TZUGFeRDPartyTypes.BuyerTradeParty: ;
TZUGFeRDPartyTypes.SellerTaxRepresentativeTradeParty: ;
TZUGFeRDPartyTypes.ShipFromTradeParty: Exit;
TZUGFeRDPartyTypes.ShipToTradeParty: Exit; // ship to has minimized info, cannot use generic _writeOptionalParty()
else
Exit;
end;
if party <> nil then
begin
case partyType of
TZUGFeRDPartyTypes.SellerTradeParty:
_writer.WriteStartElement('cac:AccountingSupplierParty', ALL_PROFILES);
TZUGFeRDPartyTypes.BuyerTradeParty:
_writer.WriteStartElement('cac:AccountingCustomerParty', ALL_PROFILES);
TZUGFeRDPartyTypes.SellerTaxRepresentativeTradeParty:
_writer.WriteStartElement('cac:TaxRepresentativeParty', ALL_PROFILES);
end;
// TaxRepresentativeParty is already of type PartyType in UBL, no nested cac:Party needed
if partyType <> TZUGFeRDPartyTypes.SellerTaxRepresentativeTradeParty then
_writer.WriteStartElement('cac:Party', ALL_PROFILES);
if (electronicAddress <> nil) and (ElectronicAddress.Address<>'') then
begin
_writer.WriteStartElement('cbc:EndpointID');
_writer.WriteAttributeString('schemeID', TEnumExtensions<TZUGFeRDElectronicAddressSchemeIdentifiers>.EnumToString(electronicAddress.ElectronicAddressSchemeID));
_writer.WriteValue(electronicAddress.Address);
_writer.WriteEndElement;
end;
if partyType = TZUGFeRDPartyTypes.SellerTradeParty then
begin
// SEPA Creditor Identifier
if (Descriptor.PaymentMeans <> nil) and (Descriptor.PaymentMeans.SEPACreditorIdentifier <> '') then
begin
_writer.WriteStartElement('cac:PartyIdentification');
_writer.WriteStartElement('cbc:ID'); // BT-90
_writer.WriteAttributeString('schemeID', 'SEPA');
_writer.WriteValue(Descriptor.PaymentMeans.SEPACreditorIdentifier);
_writer.WriteEndElement; // !ID
_writer.WriteEndElement; // !PartyIdentification
end;
// no 'else' because the cardinality is 0..n
if (party.ID <> nil) and (party.ID.ID <> '') then
begin
_writer.WriteStartElement('cac:PartyIdentification');
_writer.WriteStartElement('cbc:ID'); // BT-29
// 'SchemeID' is optional
if party.ID.SchemeID.HasValue then
_writer.WriteAttributeString('schemeID', TEnumExtensions<TZUGFeRDGlobalIDSchemeIdentifiers>.EnumToString(party.ID.SchemeID.Value));
_writer.WriteValue(party.ID.ID);
_writer.WriteEndElement; // !ID
_writer.WriteEndElement; // !PartyIdentification
end;
end
else if partyType = TZUGFeRDPartyTypes.BuyerTradeParty then
begin
if (party.GlobalID <> nil) and (party.GlobalID.ID <> '') then
begin
_writer.WriteStartElement('cac:PartyIdentification');
_writer.WriteStartElement('cbc:ID');
if party.GlobalID.SchemeID.HasValue then