-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
intf.ZUGFeRDInvoiceDescriptor1Writer.pas
945 lines (817 loc) · 43.8 KB
/
intf.ZUGFeRDInvoiceDescriptor1Writer.pas
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
{* 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.ZUGFeRDInvoiceDescriptor1Writer;
interface
uses
System.SysUtils,System.Classes,System.StrUtils,Generics.Collections
,intf.ZUGFeRDInvoiceDescriptor
,intf.ZUGFeRDInvoiceDescriptorWriter
,intf.ZUGFeRDProfileAwareXmlTextWriter
,intf.ZUGFeRDExceptions
,intf.ZUGFeRDProfile
,intf.ZUGFeRDHelper
,intf.ZUGFeRDCurrencyCodes
,intf.ZUGFeRDVersion
,intf.ZUGFeRDInvoiceTypes
,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.ZUGFeRDSpecialServiceDescriptionCodes
,intf.ZUGFeRDAllowanceOrChargeIdentificationCodes
,intf.ZUGFeRDFormats
;
type
TZUGFeRDInvoiceDescriptor1Writer = class(TZUGFeRDInvoiceDescriptorWriter)
private
Writer: TZUGFeRDProfileAwareXmlTextWriter;
Descriptor: TZUGFeRDInvoiceDescriptor;
procedure _writeOptionalAmount(_writer : TZUGFeRDProfileAwareXmlTextWriter; _tagName : string; _value : ZUGFeRDNullable<Currency>; _numDecimals : Integer = 2);
procedure _writeNotes(_writer : TZUGFeRDProfileAwareXmlTextWriter; notes : TObjectList<TZUGFeRDNote>);
procedure _writeOptionalContact(_writer: TZUGFeRDProfileAwareXmlTextWriter; contactTag: String; contact: TZUGFeRDContact);
procedure _writeOptionalParty(_writer: TZUGFeRDProfileAwareXmlTextWriter; PartyTag: String; Party: TZUGFeRDParty; Contact: TZUGFeRDContact = nil; TaxRegistrations: TObjectList<TZUGFeRDTaxRegistration> = nil);
procedure _writeOptionalTaxes(_writer: TZUGFeRDProfileAwareXmlTextWriter);
procedure _writeElementWithAttribute(_writer: TZUGFeRDProfileAwareXmlTextWriter; tagName, attributeName,attributeValue, nodeValue: String);
function _translateInvoiceType(type_ : TZUGFeRDInvoiceType) : String;
function _encodeInvoiceType(type_ : TZUGFeRDInvoiceType) : Integer;
public
function Validate(_descriptor: TZUGFeRDInvoiceDescriptor; _throwExceptions: Boolean = True): Boolean; override;
/// <summary>
/// Saves the given invoice to the given stream.
/// Make sure that the stream is open and writeable. Otherwise, an IllegalStreamException will be thron.
/// </summary>
/// <param name="descriptor">The invoice object that should be saved</param>
/// <param name="stream">The target stream for saving the invoice</param>
/// <param name="format">Format of the target file</param>
procedure Save(_descriptor: TZUGFeRDInvoiceDescriptor; _stream: TStream; _format : TZUGFeRDFormats = TZUGFeRDFormats.CII); override;
end;
implementation
{ TZUGFeRDInvoiceDescriptor1Writer }
procedure TZUGFeRDInvoiceDescriptor1Writer.Save(
_descriptor: TZUGFeRDInvoiceDescriptor; _stream: TStream;
_format : TZUGFeRDFormats = TZUGFeRDFormats.CII);
var
streamPosition : Int64;
begin
if (_stream = nil) then
raise TZUGFeRDIllegalStreamException.Create('Cannot write to stream');
if _format = TZUGFeRDFormats.UBL then
raise TZUGFeRDUnsupportedException.Create('UBL format is not supported for ZUGFeRD 1.');
// validate data
if ((_descriptor.Profile = TZUGFeRDProfile.BasicWL) or (_descriptor.Profile = TZUGFeRDProfile.Minimum)) then
raise TZUGFeRDUnsupportedException.Create('Invalid profile used for ZUGFeRD 1.x invoice.');
// write data
streamPosition := _stream.Position;
Descriptor := _descriptor;
Writer := TZUGFeRDProfileAwareXmlTextWriter.Create(_stream,TEncoding.UTF8,Descriptor.Profile);
Writer.Formatting := TZUGFeRDXmlFomatting.xmlFormatting_Indented;
Writer.WriteStartDocument;
//#region Kopfbereich
Writer.WriteStartElement('rsm:CrossIndustryDocument');
Writer.WriteAttributeString('xmlns', 'xsi', '', 'http://www.w3.org/2001/XMLSchema-instance');
Writer.WriteAttributeString('xmlns', 'rsm', '', 'urn:ferd:CrossIndustryDocument:invoice:1p0');
Writer.WriteAttributeString('xmlns', 'ram', '', 'urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:12');
Writer.WriteAttributeString('xmlns', 'udt', '', 'urn:un:unece:uncefact:data:standard:UnqualifiedDataType:15');
//#endregion
//#region SpecifiedExchangedDocumentContext
Writer.WriteStartElement('rsm:SpecifiedExchangedDocumentContext');
if (Descriptor.IsTest) then
begin
Writer.WriteStartElement('ram:TestIndicator');
Writer.WriteElementString('udt:Indicator', ifthen(Descriptor.IsTest,'true','false'));
Writer.WriteEndElement(); // !ram:TestIndicator
end;
if (Descriptor.BusinessProcess <> '') then
begin
Writer.WriteStartElement('ram:BusinessProcessSpecifiedDocumentContextParameter', [TZUGFeRDProfile.Extended]);
Writer.WriteElementString('ram:ID', Descriptor.BusinessProcess, [TZUGFeRDProfile.Extended]);
Writer.WriteEndElement(); // !ram:BusinessProcessSpecifiedDocumentContextParameter
end;
Writer.WriteStartElement('ram:GuidelineSpecifiedDocumentContextParameter');
Writer.WriteElementString('ram:ID', TZUGFeRDProfileExtensions.EnumToString(Descriptor.Profile,TZUGFeRDVersion.Version1));
Writer.WriteEndElement(); // !ram:GuidelineSpecifiedDocumentContextParameter
Writer.WriteEndElement(); // !rsm:SpecifiedExchangedDocumentContext
Writer.WriteStartElement('rsm:HeaderExchangedDocument');
Writer.WriteElementString('ram:ID', Descriptor.InvoiceNo);
Writer.WriteElementString('ram:Name', ifthen(Descriptor.Name<>'',Descriptor.Name,_translateInvoiceType(Descriptor.Type_)));
Writer.WriteElementString('ram:TypeCode', Format('%d',[_encodeInvoiceType(Descriptor.Type_)]));
if (Trunc(Descriptor.InvoiceDate) > 0) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteStartElement('udt:DateTimeString');
Writer.WriteAttributeString('format', '102');
Writer.WriteValue(_formatDate(Descriptor.InvoiceDate));
Writer.WriteEndElement(); // !udt:DateTimeString
Writer.WriteEndElement(); // !IssueDateTime
end;
_writeNotes(Writer, Descriptor.Notes);
Writer.WriteEndElement(); // !rsm:HeaderExchangedDocument
//#region SpecifiedSupplyChainTradeTransaction
Writer.WriteStartElement('rsm:SpecifiedSupplyChainTradeTransaction');
Writer.WriteStartElement('ram:ApplicableSupplyChainTradeAgreement');
if (Descriptor.ReferenceOrderNo <> '') then
begin
Writer.WriteElementString('ram:BuyerReference', Descriptor.ReferenceOrderNo);
end;
_writeOptionalParty(Writer, 'ram:SellerTradeParty', Descriptor.Seller, Descriptor.SellerContact, Descriptor.SellerTaxRegistration);
_writeOptionalParty(Writer, 'ram:BuyerTradeParty', Descriptor.Buyer, Descriptor.BuyerContact, Descriptor.BuyerTaxRegistration);
if (Descriptor.OrderNo <> '') then
begin
Writer.WriteStartElement('ram:BuyerOrderReferencedDocument');
if (Descriptor.OrderDate.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
//Writer.WriteStartElement('udt:DateTimeString');
//Writer.WriteAttributeString('format', '102');
Writer.WriteValue(_formatDate(Descriptor.OrderDate.Value, false));
//Writer.WriteEndElement(); // !udt:DateTimeString
Writer.WriteEndElement(); // !IssueDateTime()
end;
Writer.WriteElementString('ram:ID', Descriptor.OrderNo);
Writer.WriteEndElement(); // !BuyerOrderReferencedDocument
end;
for var document : TZUGFeRDAdditionalReferencedDocument in Descriptor.AdditionalReferencedDocuments do
begin
Writer.WriteStartElement('ram:AdditionalReferencedDocument');
if (document.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
//Writer.WriteStartElement('udt:DateTimeString');
//Writer.WriteAttributeString('format', '102');
Writer.WriteValue(_formatDate(document.IssueDateTime.Value, false));
//Writer.WriteEndElement(); // !udt:DateTimeString
Writer.WriteEndElement(); // !IssueDateTime()
end;
if (document.ReferenceTypeCode <> TZUGFeRDReferenceTypeCodes.Unknown) then
begin
Writer.WriteElementString('ram:TypeCode', TZUGFeRDReferenceTypeCodesExtensions.EnumToString(document.ReferenceTypeCode));
end;
Writer.WriteElementString('ram:ID', document.ID);
Writer.WriteEndElement(); // !ram:AdditionalReferencedDocument
end; // !foreach(document)
Writer.WriteEndElement(); // !ApplicableSupplyChainTradeAgreement
Writer.WriteStartElement('ram:ApplicableSupplyChainTradeDelivery'); // Pflichteintrag
if (Descriptor.Profile = TZUGFeRDProfile.Extended) then
begin
_writeOptionalParty(Writer, 'ram:ShipToTradeParty', Descriptor.ShipTo);
_writeOptionalParty(Writer, 'ram:ShipFromTradeParty', Descriptor.ShipFrom);
end;
if (Descriptor.ActualDeliveryDate.HasValue) then
begin
Writer.WriteStartElement('ram:ActualDeliverySupplyChainEvent');
Writer.WriteStartElement('ram:OccurrenceDateTime');
Writer.WriteStartElement('udt:DateTimeString');
Writer.WriteAttributeString('format', '102');
Writer.WriteValue(_formatDate(Descriptor.ActualDeliveryDate.Value));
Writer.WriteEndElement(); // 'udt:DateTimeString
Writer.WriteEndElement(); // !OccurrenceDateTime()
Writer.WriteEndElement(); // !ActualDeliverySupplyChainEvent
end;
if (Descriptor.DeliveryNoteReferencedDocument <> nil) then
begin
Writer.WriteStartElement('ram:DeliveryNoteReferencedDocument');
if (Descriptor.DeliveryNoteReferencedDocument.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteValue(_formatDate(Descriptor.DeliveryNoteReferencedDocument.IssueDateTime.Value, false));
Writer.WriteEndElement(); // !IssueDateTime
end;
Writer.WriteElementString('ram:ID', Descriptor.DeliveryNoteReferencedDocument.ID);
Writer.WriteEndElement(); // !DeliveryNoteReferencedDocument
end;
Writer.WriteEndElement(); // !ApplicableSupplyChainTradeDelivery
Writer.WriteStartElement('ram:ApplicableSupplyChainTradeSettlement');
Writer.WriteElementString('ram:InvoiceCurrencyCode', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency));
if (Descriptor.Profile <> TZUGFeRDProfile.Basic) then
begin
_writeOptionalParty(Writer, 'ram:InvoiceeTradeParty', Descriptor.Invoicee);
end;
if (Descriptor.Profile <> TZUGFeRDProfile.Extended) then
begin
_writeOptionalParty(Writer, 'ram:PayeeTradeParty', Descriptor.Payee);
end;
Writer.WriteOptionalElementString('ram:PaymentReference', Descriptor.PaymentReference);
if (Descriptor.CreditorBankAccounts.Count = 0) and
(Descriptor.DebitorBankAccounts.Count = 0) then
begin
if (Descriptor.PaymentMeans<> nil) then
begin
Writer.WriteStartElement('ram:SpecifiedTradeSettlementPaymentMeans');
if (Descriptor.PaymentMeans <> nil) then
if (Descriptor.PaymentMeans.TypeCode <> TZUGFeRDPaymentMeansTypeCodes.Unknown) then
begin
Writer.WriteElementString('ram:TypeCode', TZUGFeRDPaymentMeansTypeCodesExtensions.EnumToString(Descriptor.PaymentMeans.TypeCode));
Writer.WriteOptionalElementString('ram:Information', Descriptor.PaymentMeans.Information);
//Evtl. Invalid
if (Descriptor.PaymentMeans <> nil) then
if (Descriptor.PaymentMeans.SEPACreditorIdentifier <> '') then
//TODO (Descriptor.PaymentMeans.SEPAMandateReference <> '') then
begin
Writer.WriteStartElement('ram:ID');
Writer.WriteAttributeString('schemeAgencyID', Descriptor.PaymentMeans.SEPACreditorIdentifier);
//TODO Writer.WriteValue(Descriptor.PaymentMeans.SEPAMandateReference);
Writer.WriteEndElement(); // !ram:ID
end;
end;
Writer.WriteEndElement(); // !SpecifiedTradeSettlementPaymentMeans
end;
end
else
begin
for var account : TZUGFeRDBankAccount in Descriptor.CreditorBankAccounts do
begin
Writer.WriteStartElement('ram:SpecifiedTradeSettlementPaymentMeans');
if (Descriptor.PaymentMeans<> nil) then
if (Descriptor.PaymentMeans.TypeCode <> TZUGFeRDPaymentMeansTypeCodes.Unknown) then
begin
Writer.WriteElementString('ram:TypeCode', TZUGFeRDPaymentMeansTypeCodesExtensions.EnumToString(Descriptor.PaymentMeans.TypeCode));
Writer.WriteOptionalElementString('ram:Information', Descriptor.PaymentMeans.Information);
if (Descriptor.PaymentMeans <> nil) then
if (Descriptor.PaymentMeans.SEPACreditorIdentifier <> '') then
//TODO (Descriptor.PaymentMeans.SEPAMandateReference <> '') then
begin
Writer.WriteStartElement('ram:ID');
Writer.WriteAttributeString('schemeAgencyID', Descriptor.PaymentMeans.SEPACreditorIdentifier);
//TODO Writer.WriteValue(Descriptor.PaymentMeans.SEPAMandateReference);
Writer.WriteEndElement(); // !ram:ID
end;
end;
Writer.WriteStartElement('ram:PayeePartyCreditorFinancialAccount');
Writer.WriteElementString('ram:IBANID', account.IBAN);
if (account.Name <> '') then
begin
Writer.WriteOptionalElementString('ram:AccountName', account.Name);
end;
Writer.WriteOptionalElementString('ram:ProprietaryID', account.ID);
Writer.WriteEndElement(); // !PayeePartyCreditorFinancialAccount
Writer.WriteStartElement('ram:PayeeSpecifiedCreditorFinancialInstitution');
Writer.WriteElementString('ram:BICID', account.BIC);
Writer.WriteOptionalElementString('ram:GermanBankleitzahlID', account.Bankleitzahl);
Writer.WriteOptionalElementString('ram:Name', account.BankName);
Writer.WriteEndElement(); // !PayeeSpecifiedCreditorFinancialInstitution
Writer.WriteEndElement(); // !SpecifiedTradeSettlementPaymentMeans
end;
for var account : TZUGFeRDBankAccount in Descriptor.DebitorBankAccounts do
begin
Writer.WriteStartElement('ram:SpecifiedTradeSettlementPaymentMeans');
if (Descriptor.PaymentMeans <> nil) then
if (Descriptor.PaymentMeans.TypeCode <> TZUGFeRDPaymentMeansTypeCodes.Unknown) then
begin
Writer.WriteElementString('ram:TypeCode', TZUGFeRDPaymentMeansTypeCodesExtensions.EnumToString(Descriptor.PaymentMeans.TypeCode));
Writer.WriteOptionalElementString('ram:Information', Descriptor.PaymentMeans.Information);
if (Descriptor.PaymentMeans <> nil) then
if (Descriptor.PaymentMeans.SEPACreditorIdentifier <> '') then
//TODO (Descriptor.PaymentMeans.SEPAMandateReference <> '') then
begin
Writer.WriteStartElement('ram:ID');
Writer.WriteAttributeString('schemeAgencyID', Descriptor.PaymentMeans.SEPACreditorIdentifier);
//TODO Writer.WriteValue(Descriptor.PaymentMeans.SEPAMandateReference);
Writer.WriteEndElement(); // !ram:ID
end;
end;
Writer.WriteStartElement('ram:PayerPartyDebtorFinancialAccount');
Writer.WriteElementString('ram:IBANID', account.IBAN);
Writer.WriteOptionalElementString('ram:ProprietaryID', account.ID);
Writer.WriteEndElement(); // !PayerPartyDebtorFinancialAccount
Writer.WriteStartElement('ram:PayerSpecifiedDebtorFinancialInstitution');
Writer.WriteElementString('ram:BICID', account.BIC);
Writer.WriteOptionalElementString('ram:GermanBankleitzahlID', account.Bankleitzahl);
Writer.WriteOptionalElementString('ram:Name', account.BankName);
Writer.WriteEndElement(); // !PayerSpecifiedDebtorFinancialInstitution
Writer.WriteEndElement(); // !SpecifiedTradeSettlementPaymentMeans
end;
end;
_writeOptionalTaxes(Writer);
for var tradeAllowanceCharge : TZUGFeRDTradeAllowanceCharge in Descriptor.TradeAllowanceCharges do
begin
Writer.WriteStartElement('ram:SpecifiedTradeAllowanceCharge');
Writer.WriteStartElement('ram:ChargeIndicator', [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteElementString('udt:Indicator', ifthen(tradeAllowanceCharge.ChargeIndicator,'true','false'));
Writer.WriteEndElement(); // !ram:ChargeIndicator
if tradeAllowanceCharge.BasisAmount <> 0.0 then
begin
Writer.WriteStartElement('ram:BasisAmount', [TZUGFeRDProfile.Extended]);
Writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(tradeAllowanceCharge.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.BasisAmount,4));
Writer.WriteEndElement();
end;
Writer.WriteStartElement('ram:ActualAmount', [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(tradeAllowanceCharge.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.ActualAmount, 4));
Writer.WriteEndElement();
if tradeAllowanceCharge.ChargeIndicator then
begin
Writer.WriteOptionalElementString('ram:ReasonCode',
TZUGFeRDSpecialServiceDescriptionCodesExtensions.EnumToString(
tradeAllowanceCharge.ReasonCodeCharge));
end else
begin
Writer.WriteOptionalElementString('ram:ReasonCode',
TZUGFeRDAllowanceOrChargeIdentificationCodesExtensions.EnumToString(
tradeAllowanceCharge.ReasonCodeAllowance));
end;
Writer.WriteOptionalElementString('ram:Reason', tradeAllowanceCharge.Reason, [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
if (tradeAllowanceCharge.Tax<> nil) then
begin
Writer.WriteStartElement('ram:CategoryTradeTax');
Writer.WriteElementString('ram:TypeCode', TZUGFeRDTaxTypesExtensions.EnumToString(tradeAllowanceCharge.Tax.TypeCode), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
if (tradeAllowanceCharge.Tax.CategoryCode <> TZUGFeRDTaxCategoryCodes.Unknown) then
Writer.WriteElementString('ram:CategoryCode', TZUGFeRDTaxCategoryCodesExtensions.EnumToString(tradeAllowanceCharge.Tax.CategoryCode), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteElementString('ram:ApplicablePercent', _formatDecimal(tradeAllowanceCharge.Tax.Percent), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteEndElement();
end;
Writer.WriteEndElement();
end;
for var serviceCharge : TZUGFeRDServiceCharge in Descriptor.ServiceCharges do
begin
Writer.WriteStartElement('ram:SpecifiedLogisticsServiceCharge');
Writer.WriteOptionalElementString('ram:Description', serviceCharge.Description, [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteElementString('ram:AppliedAmount', _formatDecimal(serviceCharge.Amount), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
if (serviceCharge.Tax<> nil) then
begin
Writer.WriteStartElement('ram:AppliedTradeTax');
Writer.WriteElementString('ram:TypeCode', TZUGFeRDTaxTypesExtensions.EnumToString(serviceCharge.Tax.TypeCode), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
if (serviceCharge.Tax.CategoryCode <> TZUGFeRDTaxCategoryCodes.Unknown) then
Writer.WriteElementString('ram:CategoryCode', TZUGFeRDTaxCategoryCodesExtensions.EnumToString(serviceCharge.Tax.CategoryCode), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteElementString('ram:ApplicablePercent', _formatDecimal(serviceCharge.Tax.Percent), [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteEndElement();
end;
Writer.WriteEndElement();
end;
for var PaymentTerms: TZUGFeRDPaymentTerms in Descriptor.PaymentTermsList do
begin
Writer.WriteStartElement('ram:SpecifiedTradePaymentTerms');
Writer.WriteOptionalElementString('ram:Description', PaymentTerms.Description);
if (PaymentTerms.DueDate.HasValue) then
begin
Writer.WriteStartElement('ram:DueDateDateTime');
_writeElementWithAttribute(Writer, 'udt:DateTimeString', 'format', '102', _formatDate(PaymentTerms.DueDate.Value));
Writer.WriteEndElement(); // !ram:DueDateDateTime
end;
Writer.WriteOptionalElementString('ram:DirectDebitMandateID', PaymentTerms.DirectDebitMandateID);
//TODO PaymentTerms.PartialPaymentAmount
//TODO PaymentTerms.ApplicableTradePaymentPenaltyTerms
if (PaymentTerms.ApplicableTradePaymentDiscountTerms.BasisAmount <> 0.0) and
(PaymentTerms.ApplicableTradePaymentDiscountTerms.CalculationPercent <> 0.0) then
begin
Writer.WriteStartElement('ram:ApplicableTradePaymentDiscountTerms');
_writeOptionalAmount(Writer, 'ram:BasisAmount', PaymentTerms.ApplicableTradePaymentDiscountTerms.BasisAmount);
_writeOptionalAmount(Writer, 'ram:CalculationPercent', PaymentTerms.ApplicableTradePaymentDiscountTerms.CalculationPercent,4);
Writer.WriteEndElement();
//TODO PaymentTerms.ApplicableTradePaymentDiscountTerms.ActualPenaltyAmount
end;
Writer.WriteEndElement();
end;
Writer.WriteStartElement('ram:SpecifiedTradeSettlementMonetarySummation');
_writeOptionalAmount(Writer, 'ram:LineTotalAmount', Descriptor.LineTotalAmount);
_writeOptionalAmount(Writer, 'ram:ChargeTotalAmount', Descriptor.ChargeTotalAmount);
_writeOptionalAmount(Writer, 'ram:AllowanceTotalAmount', Descriptor.AllowanceTotalAmount);
_writeOptionalAmount(Writer, 'ram:TaxBasisTotalAmount', Descriptor.TaxBasisAmount);
_writeOptionalAmount(Writer, 'ram:TaxTotalAmount', Descriptor.TaxTotalAmount);
_writeOptionalAmount(Writer, 'ram:GrandTotalAmount', Descriptor.GrandTotalAmount);
_writeOptionalAmount(Writer, 'ram:TotalPrepaidAmount', Descriptor.TotalPrepaidAmount);
_writeOptionalAmount(Writer, 'ram:DuePayableAmount', Descriptor.DuePayableAmount);
Writer.WriteEndElement(); // !ram:SpecifiedTradeSettlementMonetarySummation
Writer.WriteEndElement(); // !ram:ApplicableSupplyChainTradeSettlement
for var tradeLineItem :TZUGFeRDTradeLineItem in Descriptor.TradeLineItems do
begin
Writer.WriteStartElement('ram:IncludedSupplyChainTradeLineItem');
if (tradeLineItem.AssociatedDocument<> nil) then
if (tradeLineItem.AssociatedDocument.LineID <> '') then
begin
Writer.WriteStartElement('ram:AssociatedDocumentLineDocument');
Writer.WriteElementString('ram:LineID', tradeLineItem.AssociatedDocument.LineID);
Writer.WriteOptionalElementString('ram:LineStatusCode', tradeLineItem.AssociatedDocument.LineStatusCode);
Writer.WriteOptionalElementString('ram:LineStatusReasonCode', tradeLineItem.AssociatedDocument.LineStatusReasonCode);
_writeNotes(Writer, tradeLineItem.AssociatedDocument.Notes);
Writer.WriteEndElement(); // ram:AssociatedDocumentLineDocument
end;
// handelt es sich um einen Kommentar?
if tradeLineItem.AssociatedDocument <> nil then
if ((tradeLineItem.AssociatedDocument.Notes.Count > 0) and
(tradeLineItem.BilledQuantity = 0) and
(tradeLineItem.Description = '')) then
begin
Writer.WriteEndElement(); // !ram:IncludedSupplyChainTradeLineItem
continue;
end;
if (Descriptor.Profile <> TZUGFeRDProfile.Basic) then
begin
Writer.WriteStartElement('ram:SpecifiedSupplyChainTradeAgreement');
if (tradeLineItem.BuyerOrderReferencedDocument<> nil) then
begin
Writer.WriteStartElement('ram:BuyerOrderReferencedDocument');
if (tradeLineItem.BuyerOrderReferencedDocument.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteValue(_formatDate(tradeLineItem.BuyerOrderReferencedDocument.IssueDateTime.Value, false));
Writer.WriteEndElement(); // !ram:IssueDateTime
end;
Writer.WriteOptionalElementString('ram:LineID', tradeLineItem.BuyerOrderReferencedDocument.LineID);
Writer.WriteOptionalElementString('ram:ID', tradeLineItem.BuyerOrderReferencedDocument.ID);
Writer.WriteEndElement(); // !ram:BuyerOrderReferencedDocument
end;
if (tradeLineItem.ContractReferencedDocument<> nil) then
begin
Writer.WriteStartElement('ram:ContractReferencedDocument');
if (tradeLineItem.ContractReferencedDocument.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteValue(_formatDate(tradeLineItem.ContractReferencedDocument.IssueDateTime.Value, false));
Writer.WriteEndElement(); // !ram:IssueDateTime
end;
Writer.WriteOptionalElementString('ram:ID', tradeLineItem.ContractReferencedDocument.ID);
Writer.WriteEndElement(); // !ram:ContractReferencedDocument
end;
if (tradeLineItem.AdditionalReferencedDocuments<> nil) then
begin
for var document : TZUGFeRDAdditionalReferencedDocument in tradeLineItem.AdditionalReferencedDocuments do
begin
Writer.WriteStartElement('ram:AdditionalReferencedDocument');
if (document.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteValue(_formatDate(document.IssueDateTime.Value, false));
Writer.WriteEndElement(); // !ram:IssueDateTime
end;
Writer.WriteElementString('ram:LineID', tradeLineItem.AssociatedDocument.LineID);
Writer.WriteOptionalElementString('ram:ID', document.ID);
Writer.WriteElementString('ram:ReferenceTypeCode', TZUGFeRDReferenceTypeCodesExtensions.EnumToString(document.ReferenceTypeCode));
Writer.WriteEndElement(); // !ram:AdditionalReferencedDocument
end;
end;
Writer.WriteStartElement('ram:GrossPriceProductTradePrice');
_writeOptionalAmount(Writer, 'ram:ChargeAmount', tradeLineItem.GrossUnitPrice, 4);
if (tradeLineItem.UnitQuantity.HasValue) then
begin
_writeElementWithAttribute(Writer, 'ram:BasisQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.UnitCode), _formatDecimal(tradeLineItem.UnitQuantity.Value, 4));
end;
for var tradeAllowanceCharge : TZUGFeRDTradeAllowanceCharge in tradeLineItem.TradeAllowanceCharges do
begin
Writer.WriteStartElement('ram:AppliedTradeAllowanceCharge');
Writer.WriteStartElement('ram:ChargeIndicator', [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteElementString('udt:Indicator', ifthen(tradeAllowanceCharge.ChargeIndicator,'true','false'));
Writer.WriteEndElement(); // !ram:ChargeIndicator
Writer.WriteStartElement('ram:BasisAmount', [TZUGFeRDProfile.Extended]);
Writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(tradeAllowanceCharge.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.BasisAmount, 4));
Writer.WriteEndElement();
Writer.WriteStartElement('ram:ActualAmount', [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(tradeAllowanceCharge.Currency));
Writer.WriteValue(_formatDecimal(tradeAllowanceCharge.ActualAmount, 4));
Writer.WriteEndElement();
Writer.WriteOptionalElementString('ram:Reason', tradeAllowanceCharge.Reason, [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended]);
Writer.WriteEndElement(); // !AppliedTradeAllowanceCharge
end;
Writer.WriteEndElement(); // ram:GrossPriceProductTradePrice
Writer.WriteStartElement('ram:NetPriceProductTradePrice');
_writeOptionalAmount(Writer, 'ram:ChargeAmount', tradeLineItem.NetUnitPrice, 4);
if (tradeLineItem.UnitQuantity.HasValue) then
begin
_writeElementWithAttribute(Writer, 'ram:BasisQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.UnitCode), _formatDecimal(tradeLineItem.UnitQuantity.Value, 4));
end;
Writer.WriteEndElement(); // ram:NetPriceProductTradePrice
Writer.WriteEndElement(); // !ram:SpecifiedSupplyChainTradeAgreement
end;
if (Descriptor.Profile <> TZUGFeRDProfile.Basic) then
begin
Writer.WriteStartElement('ram:SpecifiedSupplyChainTradeDelivery');
_writeElementWithAttribute(Writer, 'ram:BilledQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.UnitCode), _formatDecimal(tradeLineItem.BilledQuantity, 4));
if tradeLineItem.PackageQuantity.HasValue then
_writeElementWithAttribute(Writer, 'ram:PackageQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.PackageUnitCode), _formatDecimal(tradeLineItem.PackageQuantity, 4));
if tradeLineItem.ChargeFreeQuantity.HasValue then
_writeElementWithAttribute(Writer, 'ram:ChargeFreeQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.ChargeFreeUnitCode), _formatDecimal(tradeLineItem.ChargeFreeQuantity, 4));
if (tradeLineItem.DeliveryNoteReferencedDocument<> nil) then
begin
Writer.WriteStartElement('ram:DeliveryNoteReferencedDocument');
if (tradeLineItem.DeliveryNoteReferencedDocument.IssueDateTime.HasValue) then
begin
Writer.WriteStartElement('ram:IssueDateTime');
Writer.WriteValue(_formatDate(tradeLineItem.DeliveryNoteReferencedDocument.IssueDateTime.Value, false));
Writer.WriteEndElement(); // !ram:IssueDateTime
end;
Writer.WriteOptionalElementString('ram:ID', tradeLineItem.DeliveryNoteReferencedDocument.ID);
Writer.WriteEndElement(); // !ram:DeliveryNoteReferencedDocument
end;
if (tradeLineItem.ActualDeliveryDate.HasValue) then
begin
Writer.WriteStartElement('ram:ActualDeliverySupplyChainEvent');
Writer.WriteStartElement('ram:OccurrenceDateTime');
Writer.WriteStartElement('udt:DateTimeString');
Writer.WriteAttributeString('format', '102');
Writer.WriteValue(_formatDate(tradeLineItem.ActualDeliveryDate.Value));
Writer.WriteEndElement(); // 'udt:DateTimeString
Writer.WriteEndElement(); // !OccurrenceDateTime()
Writer.WriteEndElement(); // !ActualDeliverySupplyChainEvent
end;
Writer.WriteEndElement(); // !ram:SpecifiedSupplyChainTradeDelivery
end
else
begin
Writer.WriteStartElement('ram:SpecifiedSupplyChainTradeDelivery');
_writeElementWithAttribute(Writer, 'ram:BilledQuantity', 'unitCode', TZUGFeRDQuantityCodesExtensions.EnumToString(tradeLineItem.UnitCode), _formatDecimal(tradeLineItem.BilledQuantity, 4));
Writer.WriteEndElement(); // !ram:SpecifiedSupplyChainTradeDelivery
end;
Writer.WriteStartElement('ram:SpecifiedSupplyChainTradeSettlement');
if (Descriptor.Profile <> TZUGFeRDProfile.Basic) then
begin
Writer.WriteStartElement('ram:ApplicableTradeTax');
Writer.WriteElementString('ram:TypeCode', TZUGFeRDTaxTypesExtensions.EnumToString(tradeLineItem.TaxType));
Writer.WriteElementString('ram:CategoryCode', TZUGFeRDTaxCategoryCodesExtensions.EnumToString(tradeLineItem.TaxCategoryCode));
Writer.WriteElementString('ram:ApplicablePercent', _formatDecimal(tradeLineItem.TaxPercent));
Writer.WriteEndElement(); // !ram:ApplicableTradeTax
end;
if (tradeLineItem.BillingPeriodStart.HasValue and tradeLineItem.BillingPeriodEnd.HasValue) then
begin
Writer.WriteStartElement('ram:BillingSpecifiedPeriod', [TZUGFeRDProfile.BasicWL,TZUGFeRDProfile.Basic,TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended,TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
Writer.WriteStartElement('ram:StartDateTime');
_writeElementWithAttribute(Writer, 'udt:DateTimeString', 'format', '102', _formatDate(tradeLineItem.BillingPeriodStart.Value));
Writer.WriteEndElement(); // !StartDateTime
Writer.WriteStartElement('ram:EndDateTime');
_writeElementWithAttribute(Writer, 'udt:DateTimeString', 'format', '102', _formatDate(tradeLineItem.BillingPeriodEnd.Value));
Writer.WriteEndElement(); // !EndDateTime
Writer.WriteEndElement(); // !BillingSpecifiedPeriod
end;
Writer.WriteStartElement('ram:SpecifiedTradeSettlementMonetarySummation');
var _total : double := 0;
if (tradeLineItem.LineTotalAmount.HasValue) then
begin
_total := tradeLineItem.LineTotalAmount.Value;
end
else if (tradeLineItem.NetUnitPrice.HasValue) then
begin
_total := tradeLineItem.NetUnitPrice.Value * tradeLineItem.BilledQuantity;
if tradeLineItem.UnitQuantity.HasValue then
if (tradeLineItem.UnitQuantity.Value <> 0) then
begin
_total := _total / tradeLineItem.UnitQuantity.Value;
end;
end;
_writeElementWithAttribute(Writer, 'ram:LineTotalAmount', 'currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency), _formatDecimal(_total));
Writer.WriteEndElement(); // ram:SpecifiedTradeSettlementMonetarySummation
Writer.WriteEndElement(); // !ram:SpecifiedSupplyChainTradeSettlement
Writer.WriteStartElement('ram:SpecifiedTradeProduct');
if (tradeLineItem.GlobalID<> nil) then
if (tradeLineItem.GlobalID.SchemeID <> TZUGFeRDGlobalIDSchemeIdentifiers.Unknown) and (tradeLineItem.GlobalID.ID <> '') then
begin
_writeElementWithAttribute(Writer, 'ram:GlobalID', 'schemeID', TZUGFeRDGlobalIDSchemeIdentifiersExtensions.EnumToString(tradeLineItem.GlobalID.SchemeID), tradeLineItem.GlobalID.ID);
end;
Writer.WriteOptionalElementString('ram:SellerAssignedID', tradeLineItem.SellerAssignedID);
Writer.WriteOptionalElementString('ram:BuyerAssignedID', tradeLineItem.BuyerAssignedID);
Writer.WriteOptionalElementString('ram:Name', tradeLineItem.Name);
Writer.WriteOptionalElementString('ram:Description', tradeLineItem.Description);
Writer.WriteEndElement(); // !ram:SpecifiedTradeProduct
Writer.WriteEndElement(); // !ram:IncludedSupplyChainTradeLineItem
end; // !foreach(tradeLineItem)
Writer.WriteEndElement(); // !ram:SpecifiedSupplyChainTradeTransaction
Writer.WriteEndElement(); // !ram:Invoice
Writer.WriteEndDocument();
Writer.Flush();
Writer.Free;
_stream.Seek(streamPosition, soFromBeginning);
end;// !Save()
procedure TZUGFeRDInvoiceDescriptor1Writer._writeElementWithAttribute(
_writer : TZUGFeRDProfileAwareXmlTextWriter;
tagName : String; attributeName : String;
attributeValue : String; nodeValue: String);
begin
_writer.WriteStartElement(tagName);
_writer.WriteAttributeString(attributeName, attributeValue);
_writer.WriteValue(nodeValue);
_writer.WriteEndElement(); // !tagName
end;
procedure TZUGFeRDInvoiceDescriptor1Writer._writeOptionalTaxes(
_writer : TZUGFeRDProfileAwareXmlTextWriter);
begin
for var tax : TZUGFeRDTax in Descriptor.Taxes do
begin
_writer.WriteStartElement('ram:ApplicableTradeTax');
_writer.WriteStartElement('ram:CalculatedAmount');
_writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency));
_writer.WriteValue(_formatDecimal(tax.TaxAmount));
_writer.WriteEndElement(); // !CalculatedAmount
_writer.WriteElementString('ram:TypeCode', TZUGFeRDTaxTypesExtensions.EnumToString(tax.TypeCode));
_writer.WriteStartElement('ram:BasisAmount');
_writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency));
_writer.WriteValue(_formatDecimal(tax.BasisAmount));
_writer.WriteEndElement(); // !BasisAmount
if (tax.AllowanceChargeBasisAmount <> 0.0) then
begin
_writer.WriteStartElement('ram:AllowanceChargeBasisAmount');
_writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency));
_writer.WriteValue(_formatDecimal(tax.AllowanceChargeBasisAmount));
_writer.WriteEndElement(); // !AllowanceChargeBasisAmount
end;
if (tax.CategoryCode <> TZUGFeRDTaxCategoryCodes.Unknown) then
begin
_writer.WriteElementString('ram:CategoryCode', TZUGFeRDTaxCategoryCodesExtensions.EnumToString(tax.CategoryCode));
end;
_writer.WriteElementString('ram:ApplicablePercent', _formatDecimal(tax.Percent));
_writer.WriteEndElement(); // !ApplicableTradeTax
end;
end;
procedure TZUGFeRDInvoiceDescriptor1Writer._writeNotes(
_writer : TZUGFeRDProfileAwareXmlTextWriter;
notes : TObjectList<TZUGFeRDNote>);
begin
if notes.Count = 0 then
exit;
for var note : TZUGFeRDNote in notes do
begin
_writer.WriteStartElement('ram:IncludedNote');
if (note.ContentCode <> TZUGFeRDContentCodes.Unknown) then
_writer.WriteElementString('ram:ContentCode', TZUGFeRDContentCodesExtensions.EnumToString(note.ContentCode));
_writer.WriteElementString('ram:Content', note.Content);
if (note.SubjectCode <> TZUGFeRDSubjectCodes.Unknown) then
_writer.WriteElementString('ram:SubjectCode', TZUGFeRDSubjectCodesExtensions.EnumToString(note.SubjectCode));
_writer.WriteEndElement();
end;
end;
procedure TZUGFeRDInvoiceDescriptor1Writer._writeOptionalParty(
_writer: TZUGFeRDProfileAwareXmlTextWriter;
PartyTag : String; Party : TZUGFeRDParty; Contact : TZUGFeRDContact = nil;
TaxRegistrations : TObjectList<TZUGFeRDTaxRegistration> = nil);
begin
if Party = nil then
exit;
_writer.WriteStartElement(PartyTag);
if (Party.ID <> nil) then
if (Party.ID.ID <> '') then
begin
if (Party.ID.SchemeID <> TZUGFeRDGlobalIDSchemeIdentifiers.Unknown) then
begin
_writer.WriteStartElement('ram:ID');
_writer.WriteAttributeString('schemeID', TZUGFeRDGlobalIDSchemeIdentifiersExtensions.EnumToString(Party.ID.SchemeID));
_writer.WriteValue(Party.ID.ID);
_writer.WriteEndElement();
end else
begin
_writer.WriteElementString('ram:ID', Party.ID.ID);
end;
end;
if (Party.GlobalID <> nil) then
if (Party.GlobalID.ID <> '') and (Party.GlobalID.SchemeID <> TZUGFeRDGlobalIDSchemeIdentifiers.Unknown) then
begin
_writer.WriteStartElement('ram:GlobalID');
_writer.WriteAttributeString('schemeID', TZUGFeRDGlobalIDSchemeIdentifiersExtensions.EnumToString(Party.GlobalID.SchemeID));
_writer.WriteValue(Party.GlobalID.ID);
_writer.WriteEndElement();
end;
_Writer.WriteOptionalElementString('ram:Name', Party.Name);
_Writer.WriteOptionalElementString('ram:Description', Party.Description, [TZUGFeRDProfile.Comfort,TZUGFeRDProfile.Extended,TZUGFeRDProfile.XRechnung1, TZUGFeRDProfile.XRechnung]);
_writeOptionalContact(_writer, 'ram:DefinedTradeContact', Contact);
_writer.WriteStartElement('ram:PostalTradeAddress');
_writer.WriteOptionalElementString('ram:PostcodeCode', Party.Postcode);
_writer.WriteOptionalElementString('ram:LineOne', ifthen(Party.ContactName = '', Party.Street,Party.ContactName));
if (Party.ContactName <> '') then
begin
_writer.WriteOptionalElementString('ram:LineTwo', Party.Street);
end;
_writer.WriteOptionalElementString('ram:CityName', Party.City);
_writer.WriteElementString('ram:CountryID', TZUGFeRDCountryCodesExtensions.EnumToString(Party.Country));
_writer.WriteEndElement(); // !PostalTradeAddress
if (TaxRegistrations <> nil) then
begin
for var _reg : TZUGFeRDTaxRegistration in TaxRegistrations do
if (_reg.No <> '') then
begin
_writer.WriteStartElement('ram:SpecifiedTaxRegistration');
_writer.WriteStartElement('ram:ID');
_writer.WriteAttributeString('schemeID', TZUGFeRDTaxRegistrationSchemeIDExtensions.EnumToString(_reg.SchemeID));
_writer.WriteValue(_reg.No);
_writer.WriteEndElement();
_writer.WriteEndElement();
end;
end;
_writer.WriteEndElement(); // !*TradeParty
end;
procedure TZUGFeRDInvoiceDescriptor1Writer._writeOptionalContact(
_writer: TZUGFeRDProfileAwareXmlTextWriter; contactTag : String;
contact : TZUGFeRDContact);
begin
if contact = nil then
exit;
_writer.WriteStartElement(contactTag);
_writer.WriteOptionalElementString('ram:PersonName', contact.Name);
_writer.WriteOptionalElementString('ram:DepartmentName', contact.OrgUnit);
if (contact.PhoneNo <> '') then
begin
_writer.WriteStartElement('ram:TelephoneUniversalCommunication');
_writer.WriteElementString('ram:CompleteNumber', contact.PhoneNo);
_writer.WriteEndElement();
end;
if (contact.FaxNo <> '') then
begin
_writer.WriteStartElement('ram:FaxUniversalCommunication');
_writer.WriteElementString('ram:CompleteNumber', contact.FaxNo);
_writer.WriteEndElement();
end;
if (contact.EmailAddress <> '') then
begin
_writer.WriteStartElement('ram:EmailURIUniversalCommunication');
_writer.WriteElementString('ram:URIID', contact.EmailAddress);
_writer.WriteEndElement();
end;
_writer.WriteEndElement();
end;
function TZUGFeRDInvoiceDescriptor1Writer._translateInvoiceType(type_ : TZUGFeRDInvoiceType) : String;
begin
case type_ of
Invoice: Result := 'RECHNUNG';
Correction: Result := 'KORREKTURRECHNUNG';
CreditNote: Result := 'GUTSCHRIFT';
DebitnoteRelatedToFinancialAdjustments: Result := 'WERTBELASTUNG';
DebitNote: Result := '';
SelfBilledInvoice: Result := '';
else Result := '';
end;
end;
function TZUGFeRDInvoiceDescriptor1Writer._encodeInvoiceType(type_ : TZUGFeRDInvoiceType) : Integer;
begin
if (Integer(type_) > 1000) then
type_ := TZUGFeRDInvoiceType(Integer(type_)-1000);
// only these types are allowed
// 84: 'Wertbelastung/Wertrechnung ohne Warenbezug'
// 380: 'Handelsrechnung (Rechnung für Waren und Dienstleistungen)'
// 389: 'Selbst ausgestellte Rechnung (Steuerrechtliche Gutschrift/Gutschriftsverfahren)'
//
// this is documented in ZUGFeRD-Format_1p0_c1p0_Codelisten.pdf
// all other types are mapped accordingly
case type_ of
TZUGFeRDInvoiceType.SelfBilledInvoice: Result := Integer(TZUGFeRDInvoiceType.SelfBilledInvoice);
TZUGFeRDInvoiceType.DebitnoteRelatedToFinancialAdjustments: Result := Integer(TZUGFeRDInvoiceType.DebitnoteRelatedToFinancialAdjustments);
TZUGFeRDInvoiceType.Unknown: Result := Integer(TZUGFeRDInvoiceType.Unknown);
else Result := Integer(TZUGFeRDInvoiceType.Invoice);
end;
end;
function TZUGFeRDInvoiceDescriptor1Writer.Validate(
_descriptor: TZUGFeRDInvoiceDescriptor; _throwExceptions: Boolean): Boolean;
begin
Result := false;
if not (_descriptor.Profile in [TZUGFeRDProfile.Basic,
TZUGFeRDProfile.Comfort,
TZUGFeRDProfile.Extended]) then
if (_throwExceptions) then
raise TZUGFeRDUnsupportedException.Create('Invalid TZUGFeRDProfile used for ZUGFeRD 1.0 invoice.')
else
exit;
if (_descriptor.Profile <> TZUGFeRDProfile.Extended) then // check tax types, only extended TZUGFeRDProfile allows tax types other than vat
begin
for var l : TZUGFeRDTradeLineItem in _descriptor.TradeLineItems do
if not ((l.TaxType = TZUGFeRDTaxTypes.Unknown) or
(l.TaxType = TZUGFeRDTaxTypes.VAT)) then
begin
if (_throwExceptions) then
raise TZUGFeRDUnsupportedException.Create('Tax types other than VAT only possible with extended TZUGFeRDProfile.')
else
exit;
end;
end;
Result := true;
end;
procedure TZUGFeRDInvoiceDescriptor1Writer._writeOptionalAmount(
_writer: TZUGFeRDProfileAwareXmlTextWriter; _tagName: string;
_value: ZUGFeRDNullable<Currency>;
_numDecimals: Integer);
begin
if (_value.HasValue) then // && (value.Value != decimal.MinValue))
begin
_writer.WriteStartElement(_tagName);
_writer.WriteAttributeString('currencyID', TZUGFeRDCurrencyCodesExtensions.EnumToString(Descriptor.Currency));
_writer.WriteValue(_formatDecimal(_value.Value, _numDecimals));
_writer.WriteEndElement; // !tagName
end;
end;
end.