-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
intf.ZUGFeRDInvoiceValidator.pas
247 lines (211 loc) · 9.02 KB
/
intf.ZUGFeRDInvoiceValidator.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
{* 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.ZUGFeRDInvoiceValidator;
interface
uses
System.SysUtils,System.TypInfo,System.Classes,
System.Generics.Collections
,intf.ZUGFeRDInvoiceDescriptor
,intf.ZUGFeRDTradeLineItem
,intf.ZUGFeRDTradeAllowanceCharge
,intf.ZUGFeRDTax
;
type
/// <summary>
/// Validator for ZUGFeRD invoice descriptor.
///
/// Currently limited to summarizing line totals
///
/// Output syntax copied from Konik library (https://konik.io/)
/// </summary>
TZUGFeRDInvoiceValidator = class
public
class procedure ValidateAndPrint(descriptor: TZUGFeRDInvoiceDescriptor; filename: string = '');
class function Validate(descriptor: TZUGFeRDInvoiceDescriptor): TStringList;
end;
implementation
class procedure TZUGFeRDInvoiceValidator.ValidateAndPrint(
descriptor: TZUGFeRDInvoiceDescriptor; filename: string = '');
var
output: TStringList;
line: string;
begin
output := TZUGFeRDInvoiceValidator.Validate(descriptor);
try
if not filename.IsEmpty then
output.SaveToFile(filename);
for line in output do
Writeln(line);
finally
output.Free;
end;
end;
class function TZUGFeRDInvoiceValidator.Validate(
descriptor: TZUGFeRDInvoiceDescriptor): TStringList;
var
lineCounter: Integer;
lineTotal, allowanceTotal, taxTotal, grandTotal: Currency;
lineTotalPerTax: TDictionary<Currency, Currency>;
item: TZUGFeRDTradeLineItem;
charge: TZUGFeRDTradeAllowanceCharge;
kv: TPair<Currency, Currency>;
tax: TZUGFeRDTax;
allowance: TZUGFeRDTradeAllowanceCharge;
begin
Result := TStringList.Create;
if descriptor = nil then
begin
Result.Add('Invalid invoice descriptor');
exit;
end;
lineCounter := 0;
lineTotal := 0;
allowanceTotal := 0;
taxTotal := 0;
//grandTotal := 0;
lineTotalPerTax := TDictionary<Currency, Currency>.Create;
try
// line item summation
Result.Add('Validating invoice monetary summation');
Result.Add(Format('Starting recalculating line total from %d items...', [descriptor.TradeLineItems.Count]));
// foreach(TradeLineItem item in descriptor.TradeLineItems)
// {
//
//
// retval.Add(String.Format("{0};{1};{2}", ++lineCounter, item.Name, _total));
// }
for item in descriptor.TradeLineItems do
begin
var _total : Currency := 0;
if item.NetUnitPrice.HasValue then
begin
_total := (item.NetUnitPrice.Value * item.BilledQuantity);
lineTotal := lineTotal + _total;
end;
if not lineTotalPerTax.ContainsKey(item.TaxPercent) then
lineTotalPerTax.Add(item.TaxPercent, 0);
lineTotalPerTax[item.TaxPercent] := lineTotalPerTax[item.TaxPercent] + _total;
//retval.Add(String.Format("==> {0}:", ++lineCounter));
//retval.Add(String.Format("Recalculating item: [{0}]", item.Name));
//retval.Add(String.Format("Line total formula: {0:0.0000} EUR (net price) x {1:0.0000} (quantity)", item.NetUnitPrice, item.BilledQuantity));
//retval.Add(String.Format("Recalculated item line total = {0:0.0000} EUR", _total));
//retval.Add(String.Format("Recalculated item tax = {0:0.00} %", item.TaxPercent));
//retval.Add(String.Format("Current monetarySummation.lineTotal = {0:0.0000} EUR(the sum of all line totals)", lineTotal));
Inc(lineCounter);
Result.Add(Format('%d;%s;%f', [lineCounter, item.Name, _total]));
end;
Result.Add('==> DONE!');
Result.Add('Finished recalculating monetarySummation.lineTotal...');
Result.Add('Adding tax amounts from invoice allowance charge...');
for charge in descriptor.TradeAllowanceCharges do
begin
Result.Add(Format('==> added %f to %f%%', [-charge.Amount, charge.Tax.Percent]));
if not lineTotalPerTax.ContainsKey(charge.Tax.Percent) then
lineTotalPerTax.Add(charge.Tax.Percent, 0);
lineTotalPerTax[charge.Tax.Percent] := lineTotalPerTax[charge.Tax.Percent] - charge.Amount;
allowanceTotal := allowanceTotal + charge.Amount;
end;
Result.Add('Adding tax amounts from invoice service charge...');
// TODO
Result.Add(Format('Recalculated tax basis = %f', [lineTotal - allowanceTotal]));
Result.Add('Calculating tax total...');
for kv in lineTotalPerTax do
begin
var _taxTotal : Currency := (kv.Value * kv.Key / 100);
taxTotal := taxTotal + _taxTotal;
Result.Add(Format('===> %f x %f%% = %f', [kv.Value, kv.Key, _taxTotal]));
end;
grandTotal := lineTotal - allowanceTotal + taxTotal;
Result.Add(Format('Recalculated tax total = %f', [taxTotal]));
Result.Add(Format('Recalculated grand total = %f EUR(tax basis total + tax total)', [grandTotal]));
Result.Add('Recalculating invoice monetary summation DONE!');
Result.Add(Format('==> result: MonetarySummation[lineTotal = %f, chargeTotal = %f, allowanceTotal = %f, taxBasisTotal = %f, taxTotal = %f, grandTotal = %f, totalPrepaid = %f, duePayable = %f]',
[lineTotal,
0.0, // chargeTotal
allowanceTotal,
lineTotal - allowanceTotal, // - chargeTotal
taxTotal,
grandTotal,
0.0, // prepaid
lineTotal - allowanceTotal + taxTotal // - chargetotal + prepaid
]));
var _taxBasisTotal : Currency := 0;
for tax in descriptor.Taxes do
begin
_taxBasisTotal := _taxBasisTotal + tax.BasisAmount;
end;
var _allowanceTotal : Currency := 0;
for allowance in descriptor.TradeAllowanceCharges do
begin
_allowanceTotal := _allowanceTotal + allowance.ActualAmount;
end;
if not descriptor.TaxTotalAmount.HasValue then
begin
Result.Add(Format('trade.settlement.monetarySummation.taxTotal Message: Kein TaxTotalAmount vorhanden', []));
end
else if Abs(taxTotal - descriptor.TaxTotalAmount.Value) < 0.01 then
begin
Result.Add(Format('trade.settlement.monetarySummation.taxTotal Message: Berechneter Wert ist wie vorhanden:[%4f]', [taxTotal]));
end
else
begin
Result.Add(Format('trade.settlement.monetarySummation.taxTotal Message: Berechneter Wert ist[%4f] aber tatsächliche vorhander Wert ist[%4f] | Actual value: %4f)', [taxTotal, descriptor.TaxTotalAmount.GetValueOrDefault]));
end;
if Abs(lineTotal - descriptor.LineTotalAmount.Value) < 0.01 then
begin
Result.Add(Format('trade.settlement.monetarySummation.lineTotal Message: Berechneter Wert ist wie vorhanden:[%4f]', [lineTotal]));
end
else
begin
Result.Add(Format('trade.settlement.monetarySummation.lineTotal Message: Berechneter Wert ist[%4f] aber tatsächliche vorhander Wert ist[%4f] | Actual value: %4f)', [lineTotal, descriptor.LineTotalAmount.GetValueOrDefault]));
end;
if not descriptor.GrandTotalAmount.HasValue then
begin
Result.Add(Format('trade.settlement.monetarySummation.grandTotal Message: Kein GrandTotalAmount vorhanden', []));
end
else if Abs(grandTotal - descriptor.GrandTotalAmount.Value) < 0.01 then
begin
Result.Add(Format('trade.settlement.monetarySummation.grandTotal Message: Berechneter Wert ist wie vorhanden:[%4f]', [grandTotal]));
end
else
begin
Result.Add(Format('trade.settlement.monetarySummation.grandTotal Message: Berechneter Wert ist[%4f] aber tatsächliche vorhander Wert ist[%4f] | Actual value: %4f)', [grandTotal, descriptor.GrandTotalAmount.GetValueOrDefault]));
end;
{
* @todo Richtige Validierung implementieren
}
if Abs(_taxBasisTotal - _taxBasisTotal) < 0.01 then
begin
Result.Add(Format('trade.settlement.monetarySummation.taxBasisTotal Message: Berechneter Wert ist wie vorhanden:[%4f]', [_taxBasisTotal]));
end
else
begin
Result.Add(Format('trade.settlement.monetarySummation.taxBasisTotal Message: Berechneter Wert ist[%4f] aber tatsächliche vorhander Wert ist[%4f] | Actual value: %4f)', [_taxBasisTotal, _taxBasisTotal]));
end;
if Abs(allowanceTotal - _allowanceTotal) < 0.01 then
begin
Result.Add(Format('trade.settlement.monetarySummation.allowanceTotal Message: Berechneter Wert ist wie vorhanden:[%4f]', [_allowanceTotal]));
end
else
begin
Result.Add(Format('trade.settlement.monetarySummation.allowanceTotal Message: Berechneter Wert ist[%4f] aber tatsächliche vorhander Wert ist[%4f] | Actual value: %4f)', [allowanceTotal, _allowanceTotal]));
end;
finally
lineTotalPerTax.Free;
end;
end;
end.