epic-ts
[kivitendo-erp.git] / SL / Helper / MT940.pm
1 package SL::Helper::MT940;
2
3 use strict;
4
5 sub convert_mt940_data {
6   my ($mt940_data) = @_;
7
8   # takes the data from an uploaded mt940 file, converts it to csv via aqbanking and returns the converted data
9   # The uploaded file data is stored as a session file, just like the aqbanking settings file.
10
11   my $import_filename = 'bank_transfer.940';
12   my $sfile = SL::SessionFile->new($import_filename, mode => '>');
13   $sfile->fh->print($mt940_data);
14   $sfile->fh->close;
15
16   my $aqbin = $::lx_office_conf{applications}->{aqbanking};
17   die "Can't find aqbanking-cli, please check your configuration file.\n" unless -f $aqbin;
18   my $cmd = "$aqbin --cfgdir=\"" . $sfile->get_path . "\" import --importer=\"swift\" --profile=\"SWIFT-MT940\" -f " . $sfile->get_path . "/$import_filename | $aqbin --cfgdir=\"" . $sfile->get_path . "\" listtrans --exporter=\"csv\" --profile=\"AqMoney2\" ";
19
20   my $converted_data = '"empty";"local_bank_code";"local_account_number";"remote_bank_code";"remote_account_number";"transdate";"valutadate";"amount";"currency";"remote_name";"remote_name_1";"purpose";"purpose1";"purpose2";"purpose3";"purpose4";"purpose5";"purpose6";"purpose7";"purpose8";"purpose9";"purpose10";"purpose11"' . "\n";
21
22   open my $mt, "-|", "$cmd" || die "Problem with executing aqbanking\n";
23   my $headerline = <$mt>;  # discard original aqbanking header line
24   while (<$mt>) {
25     $converted_data .= $_;
26   };
27   close $mt;
28   return $converted_data;
29 };
30
31 1;