_replace_special_chars in Helper ausgelagert.
[kivitendo-erp.git] / SL / SEPA / XML / Transaction.pm
1 package SL::SEPA::XML::Transaction;
2
3 use strict;
4
5 use SL::DB::Helper::ReplaceSpecialChars qw(replace_special_chars);
6
7 use Carp;
8 use Encode;
9 use List::Util qw(first);
10 use POSIX qw(strftime);
11
12 sub new {
13   my $class = shift;
14   my $self  = {};
15
16   bless $self, $class;
17
18   $self->_init(@_);
19
20   return $self;
21 }
22
23 sub _init {
24   my $self       = shift;
25   my %params     = @_;
26
27   $self->{sepa}  = $params{sepa};
28   delete $params{sepa};
29
30   my $missing_parameter = first { !$params{$_} } qw(src_iban src_bic dst_iban dst_bic company reference amount end_to_end_id);
31   croak "Missing parameter: $missing_parameter" if ($missing_parameter);
32
33   $params{end_to_end_id}  ||= 'NOTPROVIDED';
34   $params{execution_date} ||= strftime "%Y-%m-%d", localtime;
35
36   croak "Execution date format wrong for '$params{execution_date}': not YYYY-MM-DD." if ($params{execution_date} !~ /^\d{4}-\d{2}-\d{2}$/);
37
38   map { $self->{$_} = $self->{sepa}->{iconv}->convert($params{$_})       } keys %params;
39   map { $self->{$_} =~ s/\s+//g                                          } qw(src_iban src_bic dst_iban dst_bic);
40   map { $self->{$_} = replace_special_chars($self->{$_}) } qw(company reference end_to_end_id);
41 }
42
43 sub get {
44   my $self    = shift;
45   my $key     = shift;
46   my $max_len = shift;
47
48   return undef if (!defined $self->{$key});
49
50   my $str = $max_len ? substr($self->{$key}, 0, $max_len) : $self->{$key};
51
52   return encode('UTF-8', $str);
53 }
54
55 1;