6204e736aa44367e6cae1c3f4d3724d041d46c89
[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
20   $self->setup_upload_mt940_action_bar;
21   $self->render('bankimport/form', title => $::locale->text('MT940 import'), profile => $profile ? 1 : 0);
22 }
23
24 sub action_import_mt940 {
25   my ($self, %params) = @_;
26
27   die "missing file for action import" unless $::form->{file};
28
29   my $converted_data = SL::Helper::MT940::convert_mt940_data($::form->{file});
30
31   # store the converted data in a session file with a name expected by the profile type "bank_transactions"
32   my $file = SL::SessionFile->new("csv-import-bank_transactions.csv", mode => '>');
33   $file->fh->print($converted_data);
34   $file->fh->close;
35
36   my $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => $::myconfig{login});
37   if ( ! $profile ) {
38     $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => 'default');
39   }
40   die t8("The MT940 import needs an import profile called MT940") unless $profile;
41
42   $self->redirect_to(controller => 'controller.pl', action => 'CsvImport/test', 'profile.type' => 'bank_transactions', 'profile.id' => $profile->id, force_profile => 1);
43 }
44
45 sub check_auth {
46   $::auth->assert('bank_transaction');
47 }
48
49 sub setup_upload_mt940_action_bar {
50   my ($self) = @_;
51
52   for my $bar ($::request->layout->get('actionbar')) {
53     $bar->add(
54       action => [
55         $::locale->text('Preview'),
56         submit    => [ '#form', { action => 'BankImport/import_mt940' } ],
57         accesskey => 'enter',
58       ],
59     );
60   }
61 }
62
63 1;