MT940-Import: Vordefiniertes Default Profil
[kivitendo-erp.git] / SL / Controller / BankImport.pm
1 package SL::Controller::BankImport;
2 use strict;
3 use Data::Dumper;
4 use parent qw(SL::Controller::Base);
5
6 use SL::Locale::String qw(t8);
7 use SL::DB::CsvImportProfile;
8 use SL::Helper::MT940;
9
10 __PACKAGE__->run_before('check_auth');
11
12 sub action_upload_mt940 {
13   my ($self, %params) = @_;
14
15   my $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => $::myconfig{login});
16   if ( ! $profile ) {
17     $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => 'default');
18   }
19   $self->render('bankimport/form', title => $::locale->text('MT940 import'), profile => $profile ? 1 : 0);
20
21 }
22
23 sub action_import_mt940 {
24   my ($self, %params) = @_;
25
26   die "missing file for action import" unless $::form->{file};
27
28   my $converted_data = SL::Helper::MT940::convert_mt940_data($::form->{file});
29
30   # store the converted data in a session file with a name expected by the profile type "bank_transactions"
31   my $file = SL::SessionFile->new("csv-import-bank_transactions.csv", mode => '>');
32   $file->fh->print($converted_data);
33   $file->fh->close;
34
35   my $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => $::myconfig{login});
36   if ( ! $profile ) {
37     $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => 'default');
38   }
39   die t8("The MT940 import needs an import profile called MT940") unless $profile;
40
41   $self->redirect_to(controller => 'controller.pl', action => 'CsvImport/test', 'profile.type' => 'bank_transactions', 'profile.id' => $profile->id, force_profile => 1);
42
43 };
44
45 sub check_auth {
46   $::auth->assert('bank_transaction');
47 }
48
49 1;
50