Repository navigation
Expand file tree
/
Copy pathCheckInvoice.dpr
More file actions
110 lines (95 loc) · 3.11 KB
/
Copy pathCheckInvoice.dpr
File metadata and controls
110 lines (95 loc) · 3.11 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
{
EInvoice-for-Pascal
Copyright (C) 2026 Landrix Software GmbH & Co. KG
This file is dual-licensed under:
- GNU GPL version 3 or later; or
- the Landrix Software Commercial License.
See LICENSE and COMMERCIAL-LICENSE.md.
SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Landrix-Commercial
}
{ Example: check an invoice against the business rules of its
specification, then break it and see the check and the writer refuse it.
Usage: CheckInvoice [file] (default: xrechnung-ubl.xml)
Exit code 0 when the file was read and the check behaved as shown; step 2
expects an error for XRechnung only (BR-DE-16). }
program CheckInvoice;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$IFDEF MSWINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
{$IFDEF UNIX}
cwstring,
{$ENDIF}
SysUtils,
LandrixXmlBase,
LandrixEInvoiceTypes,
LandrixEInvoiceDiagnostics,
LandrixEInvoiceSpecs,
LandrixEInvoiceInvoice,
LandrixEInvoice;
{ Runs the checks of SaveTo* without writing and prints the result. }
function RunCheck(Invoice: TLandrixInvoice; const ATarget: TLandrixEInvoiceTarget;
AOptions: TLandrixEInvoiceOptions): Integer;
var
D: TLandrixEInvoiceDiagnostics;
I: Integer;
begin
D := TLandrixEInvoiceDiagnostics.Create;
try
Result := TLandrixEInvoice.Check(Invoice, ATarget, AOptions, D);
WriteLn(string(LandrixEInvoiceTargetToStr(ATarget)), ': ', Result, ' error(s)');
for I := 0 to Integer(D.Count) - 1 do
if D[I].Severity = eds_Error then
WriteLn(' ', string(D[I].RuleID), ' ', string(D[I].Message));
finally
D.Free;
end;
end;
var
FileName: TLandrixEInvoiceString;
Doc: TLandrixEInvoiceDocument;
Target: TLandrixEInvoiceTarget;
Xml: TLandrixXmlUtf8;
Ok: Boolean;
begin
FileName := 'xrechnung-ubl.xml';
if ParamCount > 0 then
FileName := TLandrixEInvoiceString(ParamStr(1));
Doc := TLandrixEInvoice.LoadFromFile(FileName);
try
if not Doc.Info.SpecFound then
begin
WriteLn('Unknown specification ', string(Doc.Info.CustomizationID), ': nothing to check against');
ExitCode := 1;
Exit;
end;
Target := Doc.Info.Spec.Target;
{ 1. the invoice as read; eio_Strict also counts content the target
cannot hold as an error }
Ok := RunCheck(Doc.Invoice, Target, []) = 0;
RunCheck(Doc.Invoice, Target, [eio_Strict]);
{ 2. without the seller's VAT identifier a standard-rated XRechnung
violates BR-DE-16 }
Doc.Invoice.AccountingSupplierParty.VATCompanyID := '';
if RunCheck(Doc.Invoice, Target, []) = 0 then
Ok := Ok and (Target.Standard <> est_XRechnung);
{ 3. writing refuses an invoice with errors and writes nothing;
eio_WriteDespiteErrors is the escape hatch }
try
TLandrixEInvoice.SaveToUtf8(Doc.Invoice, Target);
WriteLn('Written: no error for this specification');
except
on E: ELandrixEInvoiceWriteError do
WriteLn('Refused: ', E.Message);
end;
Xml := TLandrixEInvoice.SaveToUtf8(Doc.Invoice, Target, [eio_WriteDespiteErrors]);
WriteLn('Written despite errors: ', Length(Xml), ' bytes');
finally
Doc.Free;
end;
if not Ok then
ExitCode := 1;
end.