bc65d17b79db811f282a9e838525e945bc30f4b5
[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 use SL::SessionFile::Random;
10
11 use Rose::Object::MakeMethods::Generic
12 (
13  'scalar --get_set_init' => [ qw(profile) ],
14 );
15
16 __PACKAGE__->run_before('check_auth');
17
18 sub action_upload_mt940 {
19   my ($self, %params) = @_;
20
21   $self->setup_upload_mt940_action_bar;
22   $self->render('bankimport/form', title => $::locale->text('MT940 import'), profile => $self->profile ? 1 : 0);
23 }
24
25 sub action_import_mt940 {
26   my ($self, %params) = @_;
27
28   die "missing file for action import" unless $::form->{file};
29
30   my $converted_data = SL::Helper::MT940::convert_mt940_data($::form->{file});
31
32   # store the converted data in a session file and create a temporary profile with it's name
33   my $file = SL::SessionFile::Random->new(mode => '>');
34   $file->fh->print($converted_data);
35   $file->fh->close;
36   $self->profile->set('file_name', $file->file_name);
37   $self->profile($self->profile->clone_and_reset_deep)->save;
38
39   die t8("The MT940 import needs an import profile called MT940") unless $self->profile;
40
41   $self->redirect_to(controller => 'controller.pl', action => 'CsvImport/test', 'profile.type' => 'bank_transactions', 'profile.id' => $self->profile->id, force_profile => 1);
42 }
43
44 sub check_auth {
45   $::auth->assert('bank_transaction');
46 }
47
48 sub init_profile {
49   my $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => $::myconfig{login});
50   if ( ! $profile ) {
51     $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => 'default');
52   }
53   return $profile;
54 }
55
56 sub setup_upload_mt940_action_bar {
57   my ($self) = @_;
58
59   for my $bar ($::request->layout->get('actionbar')) {
60     $bar->add(
61       action => [
62         $::locale->text('Preview'),
63         submit    => [ '#form', { action => 'BankImport/import_mt940' } ],
64         accesskey => 'enter',
65       ],
66     );
67   }
68 }
69
70 1;