|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of byrokrat\autogiro. |
|
5
|
|
|
* |
|
6
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
|
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
* |
|
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
declare(strict_types=1); |
|
23
|
|
|
|
|
24
|
|
|
namespace byrokrat\autogiro\Writer; |
|
25
|
|
|
|
|
26
|
|
|
use byrokrat\autogiro\Visitor\Visitor; |
|
27
|
|
|
use byrokrat\autogiro\Exception\RuntimeException; |
|
28
|
|
|
use byrokrat\autogiro\Tree\Node; |
|
29
|
|
|
use byrokrat\banking\AccountNumber; |
|
30
|
|
|
use byrokrat\id\IdInterface; |
|
31
|
|
|
use byrokrat\id\PersonalId; |
|
32
|
|
|
use byrokrat\id\OrganizationId; |
|
33
|
|
|
use Money\Money; |
|
34
|
|
|
use Money\MoneyFormatter; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Visitor that generates files to bgc from parse trees |
|
38
|
|
|
*/ |
|
39
|
|
|
class PrintingVisitor extends Visitor |
|
40
|
|
|
{ |
|
41
|
|
|
public const EOL = "\r\n"; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ?Output |
|
45
|
|
|
*/ |
|
46
|
|
|
private $output; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var MoneyFormatter |
|
50
|
|
|
*/ |
|
51
|
|
|
private $moneyFormatter; |
|
52
|
|
|
|
|
53
|
|
|
public function __construct(MoneyFormatter $moneyFormatter) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->moneyFormatter = $moneyFormatter; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setOutput(Output $output): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->output = $output; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function write(string $text): void |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->output) { |
|
66
|
|
|
$this->output->write($text); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function beforeDate(Node $node): void |
|
71
|
|
|
{ |
|
72
|
|
|
if ($node->getValue() instanceof \DateTimeInterface) { |
|
73
|
|
|
$this->write($node->getValue()->format('Ymd')); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function beforeImmediateDate(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->write('GENAST '); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function beforeText(Node $node): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->write($node->getValue()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function beforePayerNumber(Node $node): void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function beforePayeeBgcNumber(Node $node): void |
|
93
|
|
|
{ |
|
94
|
|
|
$this->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function beforePayeeBankgiro(Node $node): void |
|
98
|
|
|
{ |
|
99
|
|
|
if ($node->getValue() instanceof AccountNumber) { |
|
100
|
|
|
$account = $node->getValue(); |
|
101
|
|
|
$this->write( |
|
102
|
|
|
str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function beforeAccount(Node $node): void |
|
108
|
|
|
{ |
|
109
|
|
|
if ($node->getValue() instanceof AccountNumber) { |
|
110
|
|
|
$account = $node->getValue(); |
|
111
|
|
|
$this->write( |
|
112
|
|
|
$account->getClearingNumber() |
|
113
|
|
|
. str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function beforeInterval(Node $node): void |
|
119
|
|
|
{ |
|
120
|
|
|
if (!in_array((int)$node->getValue(), range('0', '8'))) { |
|
121
|
|
|
throw new RuntimeException('Interval must be between 0 and 8'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->write($node->getValue()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function beforeRepetitions(Node $node): void |
|
128
|
|
|
{ |
|
129
|
|
|
if (!ctype_digit($node->getValue()) || strlen($node->getValue()) > 3) { |
|
130
|
|
|
throw new RuntimeException("Invalid number of repitions: {$node->getValue()}"); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->write( |
|
134
|
|
|
$node->getValue() ? str_pad($node->getValue(), 3, '0', STR_PAD_LEFT) : ' ' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function beforeAmount(Node $node): void |
|
139
|
|
|
{ |
|
140
|
|
|
$amount = $node->getValue(); |
|
141
|
|
|
|
|
142
|
|
|
if ($amount instanceof Money) { |
|
143
|
|
|
if ($amount->getCurrency()->getCode() != 'SEK') { |
|
144
|
|
|
throw new RuntimeException('Printing visitor can only work with SEK'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($amount->greaterThan(Money::SEK('999999999999')) || $amount->lessThan(Money::SEK('-999999999999'))) { |
|
148
|
|
|
throw new RuntimeException('Amount must be between 9999999999.99 and -9999999999.99'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->write( |
|
152
|
|
|
str_pad($this->moneyFormatter->format($amount), 12, '0', STR_PAD_LEFT) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function beforeStateId(Node $node): void |
|
158
|
|
|
{ |
|
159
|
|
|
if ($node->getValue() instanceof PersonalId) { |
|
160
|
|
|
$this->write($node->getValue()->format('Ymdsk')); |
|
161
|
|
|
} |
|
162
|
|
|
if ($node->getValue() instanceof OrganizationId) { |
|
163
|
|
|
$this->write($node->getValue()->format('00Ssk')); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function beforeOpening(): void |
|
168
|
|
|
{ |
|
169
|
|
|
$this->write('01'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function beforeCreateMandateRequest(): void |
|
173
|
|
|
{ |
|
174
|
|
|
$this->write('04'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function beforeDeleteMandateRequest(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$this->write('03'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function beforeAcceptDigitalMandateRequest(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$this->write('04'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function beforeRejectDigitalMandateRequest(): void |
|
188
|
|
|
{ |
|
189
|
|
|
$this->write('04'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function beforeUpdateMandateRequest(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$this->write('05'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function beforeIncomingPaymentRequest(): void |
|
198
|
|
|
{ |
|
199
|
|
|
$this->write('82'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function beforeOutgoingPaymentRequest(): void |
|
203
|
|
|
{ |
|
204
|
|
|
$this->write('32'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function beforeAmendmentRequest(): void |
|
208
|
|
|
{ |
|
209
|
|
|
$this->write('23'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function afterRecord(): void |
|
213
|
|
|
{ |
|
214
|
|
|
$this->write(self::EOL); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|