blib/lib/Finance/Bank/NL/CLIEOP03.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 252 CC 37 SCAR 46.0
CC
37
Cov
0%
as_string
182.0
add_incasso
156.0
batch
56.0
CRAP 99.6
1 #
2 # Finance::Bank::NL::CLIEOP03
3 #
4 # Copyright (C) 2007 Sebastiaan Hoogeveen. All rights reserved.
5 #
6  
7 =pod
8  
9 =head1 NAME
10  
11 Finance::Bank::NL::CLIEOP03 - Generate CLIEOP03 files for Dutch banks.
12  
13 =head1 SYNOPSIS
14  
15 use Finance::Bank::NL::CLIEOP03;
16  
17 $c = Finance::Bank::NL::CLIEOP03->new;
18  
19 $batch = $c->batch (
20 account => '1234567',
21 sender_name => 'My Company',
22 type => Finance::Bank::NL::CLIEOP03::T_INCASSI
23 );
24  
25 $batch->add (
26 amount => 25,
27 account_number => '1234567',
28 account_name => 'Ms. Example',
29 description => 'Taking your money'
30 );
31  
32 $c->write ( 'CLIEOP03' );
33  
34 =head1 DESCRIPTION
35  
36 This module allows for easy creation of CLIEOP03 batch transaction files
37 which can be processed by Dutch banks. CLIEOP03 files can be used for
38 automatic debiting ("automatische incasso") or batch transfers
39 ("verzamelgiro"). You must have a business bank account and (usually) an
40 additional agreement with your bank for using this.
41  
42 =head1 DISCLAIMER
43  
44 This module is provided as-is. Due to the nature of this module you should
45 check for yourself that it does its work correctly, e.g. by auditing the
46 source code and checking the generated files.
47  
48 =head1 METHODS
49  
50 =head2 Finance::Bank::NL::CLIEOP03
51  
52 =cut
53  
54 package Finance::Bank::NL::CLIEOP03;
55  
56 our $VERSION = '0.03';
57  
58 use strict;
59 use warnings;
60  
61 use constant T_INCASSI => '10';
62 use constant T_BETALINGEN => '00';
63  
64 use Carp;
65  
66 =pod
67  
68 =head3 new
69  
70 $c = Finance::Bank::NL::CLIEOP03->new (
71 sender_id => 'CMPNY'
72 );
73  
74 Create a new CLIEOP03 file. The following parameter can be specified:
75  
76 =over
77  
78 =item * always_include_name
79  
80 This tells the module to always include the name of the account holder, even
81 when the CLIEOP03 documentation says it shouldn't do that. Use this to
82 overcome compatibility problems with the software of the Postbank.
83  
84 =item * sender_id
85  
86 An identifier that is used to identify you. This may be anything of upto 5
87 characters. Optional, defaults to ''.
88  
89 =item * serial
90  
91 When more than one file is created on a single day, each must have a
92 different serial. Specify the serial here. Optional, defaults to 1.
93  
94 =back
95  
96 =cut
97  
98 sub new {
99  
100 my $class = shift;
101 my %param = @_;
102  
103 Carp::croak 'Required parameter sender_id not specified' unless ( $param{sender_id} );
104  
105 $param{serial} ||= 1;
106 Carp::croak 'Specified serial is invalid (must be 1 to 99)' unless (( $param{serial} >= 1 ) && ( $param{serial} <= 99 ));
107
108 my $self = {
109 always_include_name => $param{always_include_name},
110 sender_id => $param{sender_id},
111 serial => $param{serial},
112 batches => []
113 };
114  
115 bless $self, $class;
116  
117 }
118  
119 =pod
120  
121 =head3 batch
122  
123 $b = $c->batch (
124 account => '1234567',
125 fixed_description => 'Costs for your hosting',
126 sender_name => 'Hosting Corporation',
127 type => Finance::Bank::NL::CLIEOP03::T_INCASSI
128 )
129  
130 Add a batch to the specified CLIEOP03 file. The batch is returned as a
131 reference to a Finance::Bank::NL::CLIEOP03::Batch object. The following
132 parameters can be specified:
133  
134 =over
135  
136 =item * account
137  
138 The account number of the party that is offering the transactions for
139 processing (thus, your account number). This is either a 9-digit bank
140 account or a 3 to 7 digit giro account. Required.
141  
142 =item * currency
143  
144 Indicates the currency that is to be used in this batch. Valid values are
145 either 'NLG' for Dutch Guilder, or 'EUR' for Euro. Optional, defaults to
146 'EUR'.
147  
148 =item * fixed_description
149  
150 Indicate a description to be added to every transaction in this batch. This
151 field is specified as a string, but will be processed as a maximum of four
152 lines containing a maximum of 32 characters each. You can use newlines to
153 split the lines, but the string will be capped if it exceeds the limits
154 specified here. Optional, defaults to no fixed description.
155  
156 =item * is_test
157  
158 If this parameter is specified with a true value, the CLIEOP03 file is
159 created as a 'test' file only. Optional, defaults to writing production
160 files.
161  
162 =item * schedule_for
163  
164 Indicate the date on which the transactions from this batch are to be
165 executed. Specify the date in the format DDMMYY, with the date no more
166 than 30 days in the future. Optional, defaults to immediate transfer.
167  
168 =item * sender_name
169  
170 The name of the organisation creating the CLIEOP03 file, as it should appear
171 on the bank statements of the other parties. The maximum length of this
172 field is 35 characters, although the Postbank will only process the first 32
173 characters. Required.
174  
175 =item * type
176  
177 The type of transaction that this batch contains, required. The value for
178 this can be specified using one of the following constants:
179  
180 =over
181  
182 =item * Finance::Bank::NL::CLIEOP03::T_BETALINGEN to indicate "verzamelgiro" bank transfers.
183  
184 =item * Finance::Bank::NL::CLIEOP03::T_INCASSI to indicate "automatische incasso" transactions.
185  
186 =back
187  
188 =back
189  
190 =cut
191  
192 sub batch {
193  
194 my $c = shift;
195 my %param = @_;
196  
197 Carp::croak 'Account number not specified' unless ( $param{account} );
198 Carp::croak 'Invalid account number' unless (( $param{account} =~ /^[0-9]{9}$/ ) || ( $param{account} =~ /^[0-9]{3,7}$/ ));
199  
200 $param{currency} ||= 'EUR';
201 Carp::croak 'Invalid currency' unless (( $param{currency} eq 'NLG' ) || ( $param{currency} eq 'EUR' ));
202  
203 if ( $param{schedule_for} ) {
204 Carp::croak 'Invalid schedule date' unless ( $param{schedule_for} =~ /^[0-3][0-9][0-1][0-9]{3}$/ );
205 }
206  
207 Carp::croak 'No sender name specified' unless ( $param{sender_name} );
208  
209 my $batch = {
210 account => $param{account},
211 currency => $param{currency},
212 fixed_description => $param{fixed_description},
213 is_test => $param{is_test},
214 schedule_for => $param{schedule_for},
215 sender_name => $param{sender_name},
216 type => $param{type},
217 transactions => []
218 };
219  
220 bless $batch, 'Finance::Bank::NL::CLIEOP03::Batch';
221 push @{$c->{batches}}, $batch;
222 return $batch;
223  
224 }
225  
226 =pod
227  
228 =head3 as_string
229  
230 $s = $c->as_string;
231  
232 Return the CLIEOP03 file as a single string, containing the records
233 specified by a \r\n pair.
234  
235 =cut
236  
237 sub as_string {
238  
239 my $self = shift;
240 my $s;
241  
242 # Bestandsvoorloopinfo (0001)
243  
244 my @now = localtime;
245 $self->{sender_id} = substr ( $self->{sender_id}, 0, 5 ) if ( length ( $self->{sender_id} ) > 5 );
246  
247 $s = sprintf (
248 "%04d%1s%02d%02d%02d%-8s%-5s%02d%02d%1d%21s\r\n",
249 '0001', # Infocode
250 'A', # Variantcode
251 $now[3], # Aanmaakdatumbestand (day)
252 $now[4] + 1, # Aanmaakdatumbestand (month)
253 $now[5] % 100, # Aanmaakdatumbestand (year without century)
254 'CLIEOP03', # Bestandsnaam
255 $self->{sender_id} || '', # Inzenderidentificatie
256 $now[3], # Bestandsidentificatie (day)
257 $self->{serial} || 1, # Bestandsidentificatie (serial)
258 1, # Duplicaatcode
259 '' # Filler
260 );
261  
262 my $batch_serial = 1;
263  
264 foreach my $batch ( @{$self->{batches}} ) {
265  
266 # Batchvoorloopinfo (0010)
267  
268 $s .= sprintf (
269 "%04d%1s%02d%010d%04d%-3s%26s\r\n",
270 '0010', # Infocode
271 'B', # Variantcode
272 $batch->{type}, # Transactiegroep
273 $batch->{account}, # Reknropdrachtgever
274 $batch_serial, # Batchvolgnummer
275 $batch->{currency}, # Aanleveringsmuntsoort
276 '' # Filler
277 );
278  
279 if ( $batch->{fixed_description} ) {
280 foreach my $desc_line ( $self->_split_description ( $batch->{fixed_description} )) {
281  
282 # Vasteomschrijvinginfo (0020)
283  
284 $s .= sprintf (
285 "%04d%1s%-32s%13s\r\n",
286 '0020', # Infocode
287 'A', # Variantcode
288 $desc_line, # Vasteomschrijving
289 '' # Filler
290 );
291
292 }
293 }
294  
295 # Opdrachtgeverinfo (0030)
296  
297 $batch->{sender_name} = substr ( $batch->{sender_name}, 0, 35 ) if ( length ( $batch->{sender_name} ) > 35 );
298  
299 $s .= sprintf (
300 "%04d%1s%1d%06d%-35s%1s%2s\r\n",
301 '0030', # Infocode
302 'B', # Variantcode
303 1, # NAWcode
304 $batch->{schedule_for} || 0, # Gewensteverwerkingsdatum
305 $batch->{sender_name}, # Naamopdrachtgever
306 $batch->{is_test} ? 'T' : 'P', # Testcode
307 '' # Filler
308 );
309  
310 my ( $totalcents, $totalaccounts, $totaltransactions ) = ( 0, 0, 0 );
311  
312 if ( $batch->{type} == Finance::Bank::NL::CLIEOP03::T_INCASSI ) {
313  
314 # INCASSI transactions
315  
316 foreach my $inc ( @{$batch->{transactions}} ) {
317  
318 # Transactieinfo (0100)
319  
320 $s .= sprintf (
321 "%04d%1s%04d%012.0f%010d%010d%9s\r\n",
322 '0100', # Infocode
323 'A', # Variantcode
324 ( $inc->{check} ? 1002 : 1001 ), # Transactiesoort
325 $inc->{amount} * 100, # Bedrag
326 $inc->{account_number}, # Reknrbetaler
327 $batch->{account}, # Reknrbegunstigde
328 '' # Filler
329 );
330  
331 $totalcents += int ( $inc->{amount} * 100 );
332 $totalaccounts += $inc->{account_number} + $batch->{account};
333 $totaltransactions++;
334  
335 if ( $inc->{check} || $self->{always_include_name} ) {
336  
337 # Naambetalerinfo (0110)
338  
339 $inc->{account_name} = substr ( $inc->{account_name}, 0, 35 ) if ( length ( $inc->{account_name} ) > 35 );
340  
341 $s .= sprintf (
342 "%04d%1s%-35s%10s\r\n",
343 '0110', # Infocode
344 'B', # Variantcode
345 $inc->{account_name}, # Naambetaler
346 '' # Filler
347 );
348  
349 if ( $inc->{account_city} ) {
350  
351 # Woonplaatsbetalerinfo (0113)
352  
353 $inc->{account_city} = substr ( $inc->{account_city}, 0, 35 ) if ( length ( $inc->{account_city} ) > 35 );
354  
355 $s .= sprintf (
356 "%04d%1s%-35s%10s\r\n",
357 '0113', # Infocode
358 'B', # Variantcode
359 $inc->{account_city}, # Woonplaatsbetaler
360 '' # Filler
361 );
362  
363 }
364 }
365  
366 if ( $inc->{identifier} ) {
367  
368 # Betalingskenmerkinfo (0150)
369  
370 $s .= sprintf (
371 "%04d%1s%016.0f%29s\r\n",
372 '0150', # Infocode
373 'A', # Variantcode
374 $inc->{identifier}, # Betalingskenmerk
375 '' # Filler
376 );
377  
378 }
379  
380 if ( $inc->{description} ) {
381  
382 foreach my $desc_line ( $self->_split_description ( $inc->{description} )) {
383  
384 # Omschrijvingsinfo (0160)
385  
386 $s .= sprintf (
387 "%04d%1s%-32s%13s\r\n",
388 '0160', # Infocode
389 'A', # Variantcode
390 $desc_line, # Omschrijving
391 '' # Filler
392 );
393  
394 }
395 }
396 }
397 }
398  
399 # Batchsluitinfo (9990)
400  
401 $s .= sprintf (
402 "%04d%1s%018.0f%010.0f%07.0f%10s\r\n",
403 '9990', # Infocode
404 'A', # Variantcode
405 $totalcents % 10**18, # Totaalbedrag
406 $totalaccounts % 10**10, # Totaalreknrs
407 $totaltransactions % 10**7, # Aantal posten
408 '' # Filler
409 );
410  
411 }
412  
413 # Bestandssluitinfo (9999)
414  
415 $s .= sprintf (
416 "%04d%1s%45s\r\n",
417 '9999', # Infocode
418 'A', # Variantcode
419 '' # Filler
420 );
421  
422 }
423  
424 =pod
425  
426 =head3 write
427  
428 $c->write ( 'CLIEOP03' );
429  
430 Write the CLIEOP03 file to the filename specified.
431  
432 =cut
433  
434 sub write {
435  
436 my $self = shift;
437 my ( $filename ) = @_;
438  
439 Carp::croak 'Required filename not specified' unless ( $filename );
440 open ( CLIEOPFILE, ">$filename" ) or Carp::croak "Cannot open $filename for writing: $!";
441 print CLIEOPFILE $self->as_string;
442 close ( CLIEOPFILE );
443  
444 }
445  
446 #
447 # PRIVATE METHODS
448 #
449  
450 #
451 # @lines = $self->_split_description ( $description )
452 #
453 # Split a description to a maximum of 4 lines containing a maximum of 32
454 # characters each. Note that we do not do any fancy wrapping here; however,
455 # any newlines or returns in the description can be used to wrap text.
456 #
457  
458 sub _split_description {
459  
460 my $self = shift;
461 my ( $desc ) = @_;
462  
463 my @lines;
464 foreach ( split /(\n|\r)+/, $desc ) {
465  
466 chomp;
467  
468 while ( length ( $_ ) > 32 && ( @lines < 4 )) {
469 push @lines, substr ( $_, 0, 32 );
470 $_ = substr ( $_, 32 );
471 }
472  
473 push @lines, $_ if ( $_ );
474  
475 last if ( @lines >= 4 );
476  
477 }
478  
479 @lines = @lines[0..3] if ( @lines > 4 );
480 return @lines;
481  
482 }
483  
484 package Finance::Bank::NL::CLIEOP03::Batch;
485  
486 =pod
487  
488 head2 Finance::Bank::NL::CLIEOP03::Batch
489  
490 =head3 add
491  
492 Add a transaction to the current batch. This method is a frontend for
493 add_betaling and add_incasso, and will select which method to be eventually
494 executed based on the type of the current batch. Note that it is not
495 possible to add transactions of a different type to a batch.
496  
497 =cut
498  
499 sub add {
500  
501 my $self = shift;
502 if ( $self->{type} == Finance::Bank::NL::CLIEOP03::T_INCASSI ) {
503 $self->add_incasso ( @_ );
504 }
505  
506 }
507  
508 =pod
509  
510 =head3 add_incasso
511  
512 $batch->add_incasso (
513 account_number => '1234556789',
514 amount => 10.00,
515 description => 'Customer 1234',
516 identifier => 6435893353335
517 );
518  
519 For payments in a batch of type T_INCASSI the following parameters can be
520 specified:
521  
522 =over
523  
524 =item * account_city
525  
526 The city of residence of the payee, consisting of no more than 32
527 characters. This field is optional if 'check' is true, and ignored
528 otherwise.
529  
530 =item * account_name
531  
532 The name of the payee, consisting of no more than 32 characters. This field
533 is required if 'check' is true, and ignored otherwise.
534  
535 =item * account_number
536  
537 The account number of the payee, either as a 9-digit bank account number or
538 a 3 to 7 digit giro number.
539  
540 =item * amount
541  
542 The amount to be debited, specified in the batch's currency. Required.
543  
544 =item * check
545  
546 If specified with a true value, the account number is to be checked against
547 the specified name. This parameter is ignored with 9-digit bank accounts.
548 Optional, defaults to true.
549  
550 =item * description
551  
552 The description of this transaction. This is added to the fixed_description
553 of the batch, and should adhere to the exact same rules. Note that banks
554 will usually only process the first four lines of the fixed_description and
555 the description together. Optional.
556  
557 =item * identifier
558  
559 A unique identifier for this transaction, which will be reported by the bank
560 on any feedback. Consists of no more than 16 digits (if less than 16 digits
561 are specified, the identifier is padded with zeros at the left). Optional.
562  
563 =back
564  
565 =cut
566  
567 sub add_incasso {
568  
569 my $self = shift;
570 my %param = @_;
571  
572 unless ( exists $param{check} ) {
573 $param{check} = 1 if ( $param{account_number} !~ /^[0-9]{9}$/ );
574 }
575  
576 if ( $param{check} ) {
577 Carp::croak ( 'The parameter account_name is required when check is true' ) unless ( $param{account_name} );
578 }
579  
580 Carp::croak ( 'The parameter account_number is required' ) unless ( $param{account_number} );
581 Carp::croak ( 'The parameter account_number is invalid' ) unless (( $param{account_number} =~ /^[0-9]{9}$/ ) || ( $param{account_number} =~ /^[0-9]{3,7}$/ ));
582  
583 Carp::croak ( 'The parameter amount is required' ) unless ( $param{amount} );
584 Carp::croak ( 'The parameter amount is invalid' ) unless ( $param{amount} =~ /^[0-9\.]*$/ );
585  
586 if ( $param{identifier} ) {
587 Carp::croak ( 'The parameter identifier is invalid' ) unless ( $param{identifier} =~ /^[0-9]+$/ );
588 }
589  
590 my $transaction = {
591 account_city => $param{account_city},
592 account_name => $param{account_name},
593 account_number => $param{account_number},
594 amount => $param{amount},
595 check => ( length ( $param{account_number} ) < 9 ? $param{check} : 0),
596 description => $param{description},
597 identifier => $param{identifier}
598 };
599  
600 push @{$self->{transactions}}, $transaction;
601  
602 }
603  
604 =pod
605  
606 =head1 CAVEATS
607  
608 This module currently only supports the T_INCASSI transaction type.
609  
610 Please note the version number; this module has not yet been extensively
611 tested. However, since implementing CLIEOP03 is both tedious and boring, I
612 thought I should make this module available ASAP.
613  
614 =head1 AUTHOR
615  
616 Sebastiaan Hoogeveen <pause-zebaz@nederhost.nl>
617  
618 =head1 COPYRIGHT
619  
620 Copyright (c) 2007 Sebastiaan Hoogeveen. All rights reserved. This program
621 is free software; you can redistribute it and/or modify it under the same
622 terms as Perl itself.
623  
624 See http://www.perl.com/perl/misc/Artistic.html
625  
626 =cut
627  
628 1;