-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathintf.ZUGFeRDIInvoiceDescriptorWriter.pas
More file actions
146 lines (126 loc) · 5.05 KB
/
Copy pathintf.ZUGFeRDIInvoiceDescriptorWriter.pas
File metadata and controls
146 lines (126 loc) · 5.05 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
{* 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.ZUGFeRDIInvoiceDescriptorWriter;
interface
uses
System.Classes, System.SysUtils, System.DateUtils, System.StrUtils
,System.Math, System.Generics.Collections
,intf.ZUGFeRDInvoiceDescriptor
,intf.ZUGFeRDProfileAwareXmlTextWriter
,intf.ZUGFeRDProfile
,intf.ZUGFeRDFormats
,intf.ZUGFeRDInvoiceFormatOptions
,intf.ZUGFeRDHelper
;
type
TZUGFeRDIInvoiceDescriptorWriter = class abstract
public
procedure Save(descriptor: TZUGFeRDInvoiceDescriptor; stream: TStream; format: TZUGFeRDFormats = TZUGFeRDFormats.CII; options: TZUGFeRDInvoiceFormatOptions = Nil); overload; virtual; abstract;
procedure Save(descriptor: TZUGFeRDInvoiceDescriptor; const filename: string; format: TZUGFeRDFormats = TZUGFeRDFormats.CII; options: TZUGFeRDInvoiceFormatOptions = Nil); overload;
function Validate(descriptor: TZUGFeRDInvoiceDescriptor; throwExceptions: Boolean = True): Boolean; virtual; abstract;
procedure WriteOptionalElementString(writer: TZUGFeRDProfileAwareXmlTextWriter; const tagName, value: string; profile: TZUGFeRDProfiles = TZUGFERDPROFILES_DEFAULT);
/// <summary>
/// Write header comments to XML writer
/// </summary>
procedure WriteHeaderComments(Writer: TZUGFeRDProfileAwareXmlTextWriter; Options: TZUGFeRDInvoiceFormatOptions);
/// <summary>
/// Write single comment to XML writer
/// </summary>
procedure WriteComment(Writer: TZUGFeRDProfileAwareXmlTextWriter; Options: TZUGFeRDInvoiceFormatOptions; const Comment: string);
/// <summary>
/// Format decimal value with specified number of decimals
/// </summary>
function _formatDecimal(const Value: Currency; NumDecimals: Integer = 2): string;
/// <summary>
/// Format date value
/// </summary>
function _formatDate(const Date: TDateTime; FormatAs102: Boolean = True; ToUBLDate: Boolean = False): string;
end;
implementation
procedure TZUGFeRDIInvoiceDescriptorWriter.Save(descriptor: TZUGFeRDInvoiceDescriptor; const filename: string; format:TZUGFeRDFormats; options: TZUGFeRDInvoiceFormatOptions);
var
fs: TFileStream;
begin
if Validate(descriptor, True) then
begin
fs := TFileStream.Create(filename, fmCreate or fmOpenWrite);
try
Save(descriptor, fs, format, options);
//fs.Flush;
//fs.Close;
finally
fs.Free;
end;
end;
end;
procedure TZUGFeRDIInvoiceDescriptorWriter.WriteOptionalElementString(
writer: TZUGFeRDProfileAwareXmlTextWriter;
const tagName, value: string;
profile: TZUGFeRDProfiles = TZUGFERDPROFILES_DEFAULT);
begin
if not value.IsEmpty then
writer.WriteElementString(tagName, value, profile);
end;
procedure TZUGFeRDIInvoiceDescriptorWriter.WriteHeaderComments(Writer: TZUGFeRDProfileAwareXmlTextWriter; Options: TZUGFeRDInvoiceFormatOptions);
var
Comment: string;
begin
if (Writer = nil) or (Options = nil) then
Exit;
if not Options.IncludeXmlComments or (Options.XmlHeaderComments.Count = 0) then
Exit;
for Comment in Options.XmlHeaderComments do
if Comment <> '' then
Writer.WriteComment(Comment);
end;
procedure TZUGFeRDIInvoiceDescriptorWriter.WriteComment(Writer: TZUGFeRDProfileAwareXmlTextWriter; Options: TZUGFeRDInvoiceFormatOptions; const Comment: string);
begin
if (Writer = nil) or (Options = nil) or (Comment = '') then
Exit;
if not Options.IncludeXmlComments then
Exit;
Writer.WriteComment(Comment);
end;
function TZUGFeRDIInvoiceDescriptorWriter._formatDecimal(const Value: Currency; NumDecimals: Integer): string;
var
FormatSettings: TFormatSettings;
RoundedValue: Currency;
FormatStr: string;
begin
FormatSettings := TFormatSettings.Invariant;
RoundedValue := RoundTo(Value, -NumDecimals);
FormatStr := '%.' + IntToStr(NumDecimals) + 'f';
Result := Format(FormatStr, [RoundedValue], FormatSettings);
end;
function TZUGFeRDIInvoiceDescriptorWriter._formatDate(const Date: TDateTime; FormatAs102: Boolean; ToUBLDate: Boolean): string;
var
FormatSettings: TFormatSettings;
begin
FormatSettings := TFormatSettings.Invariant;
if FormatAs102 then
begin
Result := FormatDateTime('yyyymmdd', Date, FormatSettings);
end
else
begin
if ToUBLDate then
Result := FormatDateTime('yyyy-mm-dd', Date, FormatSettings)
else
Result := FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Date, FormatSettings);
end;
end;
end.