|
1 |
package SL::Helper::QrBillParser;
|
|
2 |
|
|
3 |
use strict;
|
|
4 |
use warnings;
|
|
5 |
|
|
6 |
use Rose::Object::MakeMethods::Generic(
|
|
7 |
scalar => [ qw(is_valid error raw_data) ],
|
|
8 |
'scalar --get_set_init' => [ qw(spec) ],
|
|
9 |
);
|
|
10 |
|
|
11 |
our $VERSION = '0.01';
|
|
12 |
|
|
13 |
use constant {
|
|
14 |
REGEX_QRTYPE => qr{^SPC$},
|
|
15 |
REGEX_VERSION => qr{^0200$},
|
|
16 |
REGEX_CODING => qr{^1$},
|
|
17 |
REGEX_IBAN => qr{^(?:CH|LI)[0-9a-zA-Z]{19}$},
|
|
18 |
REGEX_ADDRESS_TYPE => qr{^[KS]$},
|
|
19 |
REGEX_NAME => qr{^.{1,70}$},
|
|
20 |
REGEX_ADDRESS_LINE => qr{^.{0,70}$},
|
|
21 |
REGEX_POSTAL_CODE => qr{^.{0,16}$},
|
|
22 |
REGEX_TOWN => qr{^.{0,35}$},
|
|
23 |
REGEX_COUNTRY => qr{^[A-Za-z]{2}$},
|
|
24 |
REGEX_AMOUNT => qr{^(?:(?:0|[1-9][0-9]{0,8})\.[0-9]{2})?$},
|
|
25 |
REGEX_CURRENCY => qr{^(?:CHF|EUR)$},
|
|
26 |
REGEX_REFERENCE_TYPE => qr{^(?:QRR|SCOR|NON)$},
|
|
27 |
REGEX_REFERENCE => qr{^.{0,27}$},
|
|
28 |
REGEX_UNSTRUCTURED_MESSAGE => qr{^.{0,140}$},
|
|
29 |
REGEX_TRAILER => qr{^EPD$},
|
|
30 |
REGEX_BILL_INFORMATION => qr{^.{0,140}$},
|
|
31 |
REGEX_ALTERNATIVE_SCHEME_PARAMETER => qr{^.{0,100}$},
|
|
32 |
REGEX_STREET_NAME_FROM_ADDRESS_LINE => qr{^(.*)\s+\d.*$},
|
|
33 |
REGEX_BUILDING_NUMBER_FROM_ADDRESS_LINE => qr{^.*\s+(\d+.*)$},
|
|
34 |
REGEX_POSTAL_CODE_FROM_ADDRESS_LINE => qr{^(\d+).*$},
|
|
35 |
REGEX_TOWN_FROM_ADDRESS_LINE => qr{^\d+\s(.*)$},
|
|
36 |
};
|
|
37 |
|
|
38 |
sub new {
|
|
39 |
my $class = shift;
|
|
40 |
|
|
41 |
my $self = bless {}, $class;
|
|
42 |
|
|
43 |
$self->init(@_);
|
|
44 |
|
|
45 |
return $self;
|
|
46 |
}
|
|
47 |
|
|
48 |
sub init {
|
|
49 |
my $self = shift;
|
|
50 |
my ($qrtext) = @_;
|
|
51 |
|
|
52 |
my @lines = split /(?:\n|\r\n)/, $qrtext;
|
|
53 |
|
|
54 |
$self->is_valid(1);
|
|
55 |
$self->error('');
|
|
56 |
$self->raw_data($qrtext);
|
|
57 |
|
|
58 |
for my $section ( @{$self->spec} ) {
|
|
59 |
for my $field ( @{$section->{fields}} ) {
|
|
60 |
my $value = $lines[$field->{line_number}];
|
|
61 |
|
|
62 |
if (!test_value($value, $field->{test}, $field->{status})) {
|
|
63 |
$self->error("Test failed: Section: '$section->{section}' Field: '$field->{name}' Value: '$value'");
|
|
64 |
$self->is_valid(0);
|
|
65 |
last;
|
|
66 |
}
|
|
67 |
|
|
68 |
$self->{$section->{section}} = {} if (!$self->{$section->{section}});
|
|
69 |
$self->{$section->{section}}->{$field->{name}} = $value;
|
|
70 |
}
|
|
71 |
last if $self->error;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
sub get_creditor_field {
|
|
76 |
my $self = shift;
|
|
77 |
my ($structured_field, $extract_field, $extract_regex) = @_;
|
|
78 |
|
|
79 |
if ($self->{creditor}->{address_type} eq 'S') {
|
|
80 |
return $self->{creditor}->{$structured_field};
|
|
81 |
}
|
|
82 |
# extract
|
|
83 |
$self->{creditor}->{$extract_field} =~ $extract_regex;
|
|
84 |
|
|
85 |
return $1 // '';
|
|
86 |
}
|
|
87 |
|
|
88 |
sub get_creditor_street_name {
|
|
89 |
# extract street name from street_or_address_line_1
|
|
90 |
# the regex matches everything until the first digit
|
|
91 |
return shift->get_creditor_field(
|
|
92 |
'street_or_address_line_1',
|
|
93 |
'street_or_address_line_1',
|
|
94 |
REGEX_STREET_NAME_FROM_ADDRESS_LINE
|
|
95 |
);
|
|
96 |
}
|
|
97 |
|
|
98 |
sub get_creditor_building_number {
|
|
99 |
# extract building number from street_or_address_line_1
|
|
100 |
# the regex matches the first digit and everything after
|
|
101 |
return shift->get_creditor_field(
|
|
102 |
'building_number_or_address_line_2',
|
|
103 |
'street_or_address_line_1',
|
|
104 |
REGEX_BUILDING_NUMBER_FROM_ADDRESS_LINE
|
|
105 |
);
|
|
106 |
}
|
|
107 |
|
|
108 |
sub get_creditor_post_code {
|
|
109 |
# extract post code from building_number_or_address_line_2
|
|
110 |
# the regex matches the first digits
|
|
111 |
return shift->get_creditor_field(
|
|
112 |
'postal_code',
|
|
113 |
'building_number_or_address_line_2',
|
|
114 |
REGEX_POSTAL_CODE_FROM_ADDRESS_LINE
|
|
115 |
);
|
|
116 |
}
|
|
117 |
|
|
118 |
sub get_creditor_town_name {
|
|
119 |
# extract town name from building_number_or_address_line_2
|
|
120 |
# the regex matches everything after the first digits
|
|
121 |
return shift->get_creditor_field(
|
|
122 |
'town',
|
|
123 |
'building_number_or_address_line_2',
|
|
124 |
REGEX_TOWN_FROM_ADDRESS_LINE
|
|
125 |
);
|
|
126 |
}
|
|
127 |
|
|
128 |
sub init_spec {
|
|
129 |
[
|
|
130 |
{
|
|
131 |
section => 'header',
|
|
132 |
fields => [
|
|
133 |
{
|
|
134 |
name => 'qrtype',
|
|
135 |
line_number => 0,
|
|
136 |
test => REGEX_QRTYPE,
|
|
137 |
status => 'M'
|
|
138 |
},
|
|
139 |
{
|
|
140 |
name => 'version',
|
|
141 |
line_number => 1,
|
|
142 |
test => REGEX_VERSION,
|
|
143 |
status => 'M'
|
|
144 |
},
|
|
145 |
{
|
|
146 |
name => 'coding',
|
|
147 |
line_number => 2,
|
|
148 |
test => REGEX_CODING,
|
|
149 |
status => 'M'
|
|
150 |
}
|
|
151 |
]
|
|
152 |
},
|
|
153 |
{
|
|
154 |
section => 'creditor_information',
|
|
155 |
fields => [
|
|
156 |
{
|
|
157 |
name => 'iban',
|
|
158 |
line_number => 3,
|
|
159 |
test => REGEX_IBAN,
|
|
160 |
status => 'M'
|
|
161 |
}
|
|
162 |
]
|
|
163 |
},
|
|
164 |
{
|
|
165 |
section => 'creditor',
|
|
166 |
fields => [
|
|
167 |
{
|
|
168 |
name => 'address_type',
|
|
169 |
line_number => 4,
|
|
170 |
test => REGEX_ADDRESS_TYPE,
|
|
171 |
status => 'M',
|
|
172 |
},
|
|
173 |
{
|
|
174 |
name => 'name',
|
|
175 |
line_number => 5,
|
|
176 |
test => REGEX_NAME,
|
|
177 |
status => 'M',
|
|
178 |
},
|
|
179 |
{
|
|
180 |
name => 'street_or_address_line_1',
|
|
181 |
line_number => 6,
|
|
182 |
test => REGEX_ADDRESS_LINE,
|
|
183 |
status => 'O'
|
|
184 |
},
|
|
185 |
{
|
|
186 |
name => 'building_number_or_address_line_2',
|
|
187 |
line_number => 7,
|
|
188 |
test => REGEX_ADDRESS_LINE,
|
|
189 |
status => 'O'
|
|
190 |
},
|
|
191 |
{
|
|
192 |
name => 'postal_code',
|
|
193 |
line_number => 8,
|
|
194 |
test => REGEX_POSTAL_CODE,
|
|
195 |
status => 'D'
|
|
196 |
},
|
|
197 |
{
|
|
198 |
name => 'town',
|
|
199 |
line_number => 9,
|
|
200 |
test => REGEX_TOWN,
|
|
201 |
status => 'D'
|
|
202 |
},
|
|
203 |
{
|
|
204 |
name => 'country',
|
|
205 |
line_number => 10,
|
|
206 |
test => REGEX_COUNTRY,
|
|
207 |
status => 'M'
|
|
208 |
}
|
|
209 |
]
|
|
210 |
},
|
|
211 |
{
|
|
212 |
section => 'ultimate_creditor',
|
|
213 |
fields => [
|
|
214 |
{
|
|
215 |
name => 'address_type',
|
|
216 |
line_number => 11,
|
|
217 |
test => REGEX_ADDRESS_TYPE,
|
|
218 |
status => 'X'
|
|
219 |
},
|
|
220 |
{
|
|
221 |
name => 'name',
|
|
222 |
line_number => 12,
|
|
223 |
test => REGEX_NAME,
|
|
224 |
status => 'X'
|
|
225 |
},
|
|
226 |
{
|
|
227 |
name => 'street_or_address_line_1',
|
|
228 |
line_number => 13,
|
|
229 |
test => REGEX_ADDRESS_LINE,
|
|
230 |
status => 'X'
|
|
231 |
},
|
|
232 |
{
|
|
233 |
name => 'building_number_or_address_line_2',
|
|
234 |
line_number => 14,
|
|
235 |
test => REGEX_ADDRESS_LINE,
|
|
236 |
status => 'X'
|
|
237 |
},
|
|
238 |
{
|
|
239 |
name => 'postal_code',
|
|
240 |
line_number => 15,
|
|
241 |
test => REGEX_POSTAL_CODE,
|
|
242 |
status => 'X'
|
|
243 |
},
|
|
244 |
{
|
|
245 |
name => 'town',
|
|
246 |
line_number => 16,
|
|
247 |
test => REGEX_TOWN,
|
|
248 |
status => 'X'
|
|
249 |
},
|
|
250 |
{
|
|
251 |
name => 'country',
|
|
252 |
line_number => 17,
|
|
253 |
test => REGEX_COUNTRY,
|
|
254 |
status => 'X'
|
|
255 |
}
|
|
256 |
]
|
|
257 |
},
|
|
258 |
{
|
|
259 |
section => 'payment_amount_information',
|
|
260 |
fields => [
|
|
261 |
{
|
|
262 |
name => 'amount',
|
|
263 |
line_number => 18,
|
|
264 |
test => REGEX_AMOUNT,
|
|
265 |
status => 'O'
|
|
266 |
},
|
|
267 |
{
|
|
268 |
name => 'currency',
|
|
269 |
line_number => 19,
|
|
270 |
test => REGEX_CURRENCY,
|
|
271 |
status => 'M'
|
|
272 |
}
|
|
273 |
]
|
|
274 |
},
|
|
275 |
{
|
|
276 |
section => 'ultimate_debtor',
|
|
277 |
fields => [
|
|
278 |
{
|
|
279 |
name => 'address_type',
|
|
280 |
line_number => 20,
|
|
281 |
test => REGEX_ADDRESS_TYPE,
|
|
282 |
status => 'D'
|
|
283 |
},
|
|
284 |
{
|
|
285 |
name => 'name',
|
|
286 |
line_number => 21,
|
|
287 |
test => REGEX_NAME,
|
|
288 |
status => 'D'
|
|
289 |
},
|
|
290 |
{
|
|
291 |
name => 'street_or_address_line_1',
|
|
292 |
line_number => 22,
|
|
293 |
test => REGEX_ADDRESS_LINE,
|
|
294 |
status => 'O'
|
|
295 |
},
|
|
296 |
{
|
|
297 |
name => 'building_number_or_address_line_2',
|
|
298 |
line_number => 23,
|
|
299 |
test => REGEX_ADDRESS_LINE,
|
|
300 |
status => 'O'
|
|
301 |
},
|
|
302 |
{
|
|
303 |
name => 'postal_code',
|
|
304 |
line_number => 24,
|
|
305 |
test => REGEX_POSTAL_CODE,
|
|
306 |
status => 'D'
|
|
307 |
},
|
|
308 |
{
|
|
309 |
name => 'town',
|
|
310 |
line_number => 25,
|
|
311 |
test => REGEX_TOWN,
|
|
312 |
status => 'D'
|
|
313 |
},
|
|
314 |
{
|
|
315 |
name => 'country',
|
|
316 |
line_number => 26,
|
|
317 |
test => REGEX_COUNTRY,
|
|
318 |
status => 'D'
|
|
319 |
}
|
|
320 |
]
|
|
321 |
},
|
|
322 |
{
|
|
323 |
section => 'payment_reference',
|
|
324 |
fields => [
|
|
325 |
{
|
|
326 |
name => 'reference_type',
|
|
327 |
line_number => 27,
|
|
328 |
test => REGEX_REFERENCE_TYPE,
|
|
329 |
status => 'M'
|
|
330 |
},
|
|
331 |
{
|
|
332 |
name => 'reference',
|
|
333 |
line_number => 28,
|
|
334 |
test => REGEX_REFERENCE,
|
|
335 |
status => 'D'
|
|
336 |
}
|
|
337 |
]
|
|
338 |
},
|
|
339 |
{
|
|
340 |
section => 'additional_information',
|
|
341 |
fields => [
|
|
342 |
{
|
|
343 |
name => 'unstructured_message',
|
|
344 |
line_number => 29,
|
|
345 |
test => REGEX_UNSTRUCTURED_MESSAGE,
|
|
346 |
status => 'O'
|
|
347 |
},
|
|
348 |
{
|
|
349 |
name => 'trailer',
|
|
350 |
line_number => 30,
|
|
351 |
test => REGEX_TRAILER,
|
|
352 |
status => 'M'
|
|
353 |
},
|
|
354 |
{
|
|
355 |
name => 'bill_information',
|
|
356 |
line_number => 31,
|
|
357 |
test => REGEX_BILL_INFORMATION,
|
|
358 |
status => 'A'
|
|
359 |
}
|
|
360 |
]
|
|
361 |
},
|
|
362 |
{
|
|
363 |
section => 'alternative_scheme',
|
|
364 |
fields => [
|
|
365 |
{
|
|
366 |
name => 'alternative_scheme_parameter1',
|
|
367 |
line_number => 32,
|
|
368 |
test => REGEX_ALTERNATIVE_SCHEME_PARAMETER,
|
|
369 |
status => 'A'
|
|
370 |
},
|
|
371 |
{
|
|
372 |
name => 'alternative_scheme_parameter2',
|
|
373 |
line_number => 33,
|
|
374 |
test => REGEX_ALTERNATIVE_SCHEME_PARAMETER,
|
|
375 |
status => 'A'
|
|
376 |
}
|
|
377 |
]
|
|
378 |
}
|
|
379 |
];
|
|
380 |
}
|
|
381 |
|
|
382 |
### helper
|
|
383 |
|
|
384 |
sub test_value {
|
|
385 |
my ($value, $test, $status) = @_;
|
|
386 |
|
|
387 |
# mandatory fields must have a content
|
|
388 |
return 0 if $status eq 'M' && length $value <= 0;
|
|
389 |
|
|
390 |
# optional fields can be empty
|
|
391 |
return 1 if $status eq 'O' && length $value == 0;
|
|
392 |
|
|
393 |
# dependent fields can be empty
|
|
394 |
return 1 if $status eq 'D' && length $value == 0;
|
|
395 |
|
|
396 |
# "do not fill" fields cannot have a content
|
|
397 |
if ($status eq 'X') {
|
|
398 |
return 1 if ($value eq '');
|
|
399 |
return 0;
|
|
400 |
}
|
|
401 |
|
|
402 |
# additional fields can be undefined
|
|
403 |
if ($status eq 'A') {
|
|
404 |
return 1 if !defined($value);
|
|
405 |
return 0 if $value !~ $test;
|
|
406 |
return 1;
|
|
407 |
}
|
|
408 |
|
|
409 |
return 0 if !defined($value);
|
|
410 |
return 0 if $value !~ $test;
|
|
411 |
return 1;
|
|
412 |
}
|
|
413 |
|
|
414 |
1;
|
|
415 |
|
|
416 |
__END__
|
|
417 |
|
|
418 |
=pod
|
|
419 |
|
|
420 |
=encoding utf8
|
|
421 |
|
|
422 |
=head1 NAME
|
|
423 |
|
|
424 |
SL::Helper::QrBillParser - Helper for parsing QR bill data
|
|
425 |
|
|
426 |
=head1 SYNOPSIS
|
|
427 |
|
|
428 |
use SL::Helper::QrBillParser;
|
|
429 |
|
|
430 |
my $qr_obj = SL::Helper::QrBillParser->new($item->{qrbill_data});
|
|
431 |
|
|
432 |
my $valid = $qr_obj->is_valid;
|
|
433 |
my $error_message = $qr_obj->error;
|
|
434 |
my $qrtext = $qr_obj->raw_data;
|
|
435 |
|
|
436 |
# data for remittance information
|
|
437 |
my $reference = $qr_obj->{payment_reference}->{reference};
|
|
438 |
my $unstructured_message = $qr_obj->{additional_information}->{unstructured_message}
|
|
439 |
|
|
440 |
# set currency and amount
|
|
441 |
my $currency = $qr_obj->{payment_amount_information}->{currency};
|
|
442 |
my $amount = $qr_obj->{payment_amount_information}->{amount}
|
|
443 |
|
|
444 |
# set creditor name and address from qr data
|
|
445 |
my $creditor_name = $qr_obj->{creditor}->{name};
|
|
446 |
my $creditor_street_name = $qr_obj->get_creditor_street_name;
|
|
447 |
my $creditor_building_number = $qr_obj->get_creditor_building_number;
|
|
448 |
my $creditor_postal_code = $qr_obj->get_creditor_post_code;
|
|
449 |
my $creditor_town_name = $qr_obj->get_creditor_town_name;
|
|
450 |
my $creditor_country = $qr_obj->{creditor}->{country}
|
|
451 |
|
|
452 |
# set creditor iban
|
|
453 |
my $creditor_iban = $qr_obj->{creditor_information}->{iban};
|
|
454 |
|
|
455 |
=head1 DESCRIPTION
|
|
456 |
|
|
457 |
This is simple helper to parse swiss qr bill data from a string into an object.
|
|
458 |
|
|
459 |
Some methods are provided to easily retrieve the creditor address data.
|
|
460 |
|
|
461 |
=head1 FUNCTIONS
|
|
462 |
|
|
463 |
=over 4
|
|
464 |
|
|
465 |
=item C<new>
|
|
466 |
|
|
467 |
my $qr_obj = SL::Helper::QrBillParser->new($item->{qrbill_data});
|
|
468 |
|
|
469 |
Creates a new object from the qr bill data string.
|
|
470 |
|
|
471 |
=item C<is_valid>
|
|
472 |
|
|
473 |
my $valid = $qr_obj->is_valid;
|
|
474 |
|
|
475 |
Returns true if the qr bill data is valid.
|
|
476 |
|
|
477 |
=item C<error>
|
|
478 |
|
|
479 |
my $error_message = $qr_obj->error;
|
|
480 |
|
|
481 |
Returns the error message if the qr bill data is invalid.
|
|
482 |
|
|
483 |
=item C<raw_data>
|
|
484 |
|
|
485 |
my $qrtext = $qr_obj->raw_data;
|
|
486 |
|
|
487 |
Returns the raw qr bill data string.
|
|
488 |
|
|
489 |
=item C<get_creditor_street_name>
|
|
490 |
|
|
491 |
my $creditor_street_name = $qr_obj->get_creditor_street_name;
|
|
492 |
|
|
493 |
Returns the creditor street name.
|
|
494 |
|
|
495 |
=item C<get_creditor_building_number>
|
|
496 |
|
|
497 |
my $creditor_building_number = $qr_obj->get_creditor_building_number;
|
|
498 |
|
|
499 |
Returns the creditor building number.
|
|
500 |
|
|
501 |
=item C<get_creditor_post_code>
|
|
502 |
|
|
503 |
my $creditor_postal_code = $qr_obj->get_creditor_post_code;
|
|
504 |
|
|
505 |
Returns the creditor postal code.
|
|
506 |
|
|
507 |
=item C<get_creditor_town_name>
|
|
508 |
|
|
509 |
my $creditor_town_name = $qr_obj->get_creditor_town_name;
|
|
510 |
|
|
511 |
Returns the creditor town name.
|
|
512 |
|
|
513 |
=back
|
|
514 |
|
|
515 |
=head1 TESTS
|
|
516 |
|
|
517 |
Tests for functions see t/helper/qrbill_parser.t.
|
|
518 |
|
|
519 |
Run: C<t/test.pl t/helper/qrbill_parser.t>
|
|
520 |
|
|
521 |
=head1 LIMITATIONS
|
|
522 |
|
|
523 |
Basic validation is performed based on the status code and regular expressions.
|
|
524 |
However complete checks of dependent fields would require more elaborate logic.
|
|
525 |
|
|
526 |
=head1 BUGS
|
|
527 |
|
|
528 |
Nothing here yet.
|
|
529 |
|
|
530 |
=head1 AUTHOR
|
|
531 |
|
|
532 |
Cem Aydin E<lt>cem.aydin@revamp-it.chE<gt>
|
|
533 |
Steven Schubiger E<lt>stsc@refcnt.orgE<gt>
|
|
534 |
|
|
535 |
=cut
|
Schweizer QR-Rechnung: Modul zum Parsen der QR daten erstellt, inklusive tests