Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b9bb0b44

Von Cem Aydin vor etwa 1 Jahr hinzugefügt

  • ID b9bb0b4449aff603d8152bea3bd34121ede35d22
  • Vorgänger 2cb40cda
  • Nachfolger 67b0bbf5

Schweizer QR-Rechnung: Modul zum Parsen der QR daten erstellt, inklusive tests

Unterschiede anzeigen:

SL/Helper/QrBillParser.pm
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
t/helper/qrbill_parser.t
1
use Test::More tests => 40;
2

  
3
use strict;
4

  
5
use lib 't';
6
use Support::TestSetup;
7

  
8
use_ok('SL::Helper::QrBillParser');
9

  
10
Support::TestSetup::login();
11

  
12
my $code1 = "SPC\n0200\n1\nCH5204835012345671000\nS\nSample Foundation\nPO Box\n\n3001\nBern\nCH\n\n\n\n\n\n\n\n\nCHF\n\n\n\n\n\n\n\nNON\n\n\nEPD\n";
13
my $obj1 = SL::Helper::QrBillParser->new($code1);
14

  
15
is($obj1->is_valid, 1, 'code1valid');
16
is($obj1->{creditor_information}->{iban}, "CH5204835012345671000", 'code1iban');
17
is($obj1->{creditor}->{name}, "Sample Foundation", 'code1name');
18
is($obj1->{payment_amount_information}->{amount}, "", 'code1amount');
19

  
20
my $code2 = "SPC\r\n0200\r\n1\r\nCH4431999123000889012\r\nS\r\nMax Muster & Söhne\r\nMusterstrasse\r\n123\r\n8000\r\nSeldwyla\r\nCH\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n1949.75\r\nCHF\r\nS\r\nSimon Muster\r\nMusterstrasse\r\n1\r\n8000\r\nSeldwyla\r\nCH\r\nQRR\r\n210000000003139471430009017\r\nOrder from 15.10.2020\r\nEPD\r\n//S1/10/1234/11/201021/30/102673386/32/7.7/40/0:30\r\nName AV1: UV;UltraPay005;12345\r\nName AV2: XY;XYService;54321";
21
my $obj2 = SL::Helper::QrBillParser->new($code2);
22

  
23
is($obj2->is_valid, 1, 'code2valid');
24
is($obj2->{creditor_information}->{iban}, "CH4431999123000889012", 'code2iban');
25
is($obj2->{creditor}->{name}, "Max Muster & Söhne", 'code2name');
26
is($obj2->{payment_amount_information}->{amount}, "1949.75", 'code2amount');
27
is($obj2->{payment_reference}->{reference}, "210000000003139471430009017", 'code2reference');
28
is($obj2->{additional_information}->{unstructured_message}, "Order from 15.10.2020", 'code2unstructured_message');
29

  
30
is($obj2->get_creditor_street_name(), 'Musterstrasse', 'code2street_name');
31
is($obj2->get_creditor_building_number(), '123', 'code2building_number');
32
is($obj2->get_creditor_post_code(), '8000', 'code2post_code');
33
is($obj2->get_creditor_town_name(), 'Seldwyla', 'code2town_name');
34

  
35
my $code3 = "SPC\n0200\n1\nCH5800791123000889012\nS\nMuster Krankenkasse\nMusterstrasse\n12\n8000\nSeldwyla\nCH\n\n\n\n\n\n\n\n211.00\nCHF\nS\nSarah Beispiel\nMusterstrasse\n1\n8000\nSeldwyla\nCH\nSCOR\nRF240191230100405JSH0438\n\nEPD\n";
36
my $obj3 = SL::Helper::QrBillParser->new($code3);
37

  
38
is($obj3->is_valid, 1, 'code3valid');
39
is($obj3->{creditor_information}->{iban}, "CH5800791123000889012", 'code3iban');
40
is($obj3->{creditor}->{name}, "Muster Krankenkasse", 'code3name');
41
is($obj3->{payment_amount_information}->{amount}, "211.00", 'code3amount');
42
is($obj3->{payment_reference}->{reference}, "RF240191230100405JSH0438", 'code3reference');
43

  
44
my $code4 = "SPC\n0200\n1\nCH5800791123000889012\nS\nMax Muster & Söhne\nMusterstrasse\n123\n8000\nSeldwyla\nCH\n\n\n\n\n\n\n\n199.95\nCHF\nS\nSarah Beispiel\nMusterstrasse\n1\n78462\nKonstanz\nDE\nSCOR\nRF18539007547034\n\nEPD\n";
45
my $obj4 = SL::Helper::QrBillParser->new($code4);
46

  
47
is($obj4->is_valid, 1, 'code4valid');
48
is($obj4->{creditor_information}->{iban}, "CH5800791123000889012", 'code4iban');
49
is($obj4->{creditor}->{name}, "Max Muster & Söhne", 'code4name');
50
is($obj4->{payment_amount_information}->{amount}, "199.95", 'code4amount');
51
is($obj4->{payment_reference}->{reference}, "RF18539007547034", 'code4reference');
52

  
53
my $code5 = "SP\n0200\n1\nCH5800791123000889012\nS\nMax Muster & Söhne\nMusterstrasse\n123\n8000\nSeldwyla\nCH\n\n\n\n\n\n199.95\nCHF\nS\nSarah Beispiel\nMusterstrasse\n1\n78462\nKonstanz\nDE\nSCOR\nRF18539007547034\n\nEPD\n";
54
my $obj5 = SL::Helper::QrBillParser->new($code5);
55

  
56
is($obj5->is_valid, 0, 'code5invalid');
57
is($obj5->error, "Test failed: Section: 'header' Field: 'qrtype' Value: 'SP'", 'code5error');
58

  
59
my $code6 = "SPC\n0200\n1\nCH5800791123889012\nS\nMax Muster & Söhne\nMusterstrasse\n123\n8000\nSeldwyla\nCH\n\n\n\n\n\n\n\n199.95\nCHF\nS\nSarah Beispiel\nMusterstrasse\n1\n78462\nKonstanz\nDE\nSCOR\nRF18539007547034\n\nEPD\n";
60
my $obj6 = SL::Helper::QrBillParser->new($code6);
61

  
62
is($obj6->is_valid, 0, 'code6invalid');
63
is($obj6->error, "Test failed: Section: 'creditor_information' Field: 'iban' Value: 'CH5800791123889012'", 'code6error');
64

  
65
my $code7 = "SPC\n0200\n1\nCH5204835012345671000\nK\nSample Foundation\nMusterstrasse 55\n3005 Bern\n\n\nCH\n\n\n\n\n\n\n\n\nCHF\n\n\n\n\n\n\n\nNON\n\n\nEPD\n";
66
my $obj7 = SL::Helper::QrBillParser->new($code7);
67

  
68
is($obj7->is_valid, 1, 'code7valid');
69
is($obj7->get_creditor_street_name(), 'Musterstrasse', 'code7street_name');
70
is($obj7->get_creditor_building_number(), '55', 'code7building_number');
71
is($obj7->get_creditor_post_code(), '3005', 'code7post_code');
72
is($obj7->get_creditor_town_name(), 'Bern', 'code7town_name');
73

  
74
my $code8 = "SPC\n0200\n1\nCH5204835012345671000\nK\nSample Foundation\nMusterstrasse 25b\n3005 Bern\n\n\nCH\n\n\n\n\n\n\n\n\nCHF\n\n\n\n\n\n\n\nNON\n\n\nEPD\n";
75
my $obj8 = SL::Helper::QrBillParser->new($code8);
76

  
77
is($obj8->is_valid, 1, 'code8valid');
78
is($obj8->get_creditor_street_name(), 'Musterstrasse', 'code8street_name');
79
is($obj8->get_creditor_building_number(), '25b', 'code8building_number');
80

  
81
my $code9 = "SPC\n0200\n1\nCH5204835012345671000\nK\nSample Foundation\nMusterstrasse 25 c\n3005 Bern\n\n\nCH\n\n\n\n\n\n\n\n\nCHF\n\n\n\n\n\n\n\nNON\n\n\nEPD\n";
82
my $obj9 = SL::Helper::QrBillParser->new($code9);
83

  
84
is($obj9->is_valid, 1, 'code9valid');
85
is($obj9->get_creditor_street_name(), 'Musterstrasse', 'code9street_name');
86
is($obj9->get_creditor_building_number(), '25 c', 'code9building_number');

Auch abrufbar als: Unified diff