BankImport: Refactoring: Profil über init-Methode laden.
[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 use Rose::Object::MakeMethods::Generic
11 (
12  'scalar --get_set_init' => [ qw(profile) ],
13 );
14
15 __PACKAGE__->run_before('check_auth');
16
17 sub action_upload_mt940 {
18   my ($self, %params) = @_;
19
20   $self->setup_upload_mt940_action_bar;
21   $self->render('bankimport/form', title => $::locale->text('MT940 import'), profile => $self->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   die t8("The MT940 import needs an import profile called MT940") unless $self->profile;
37
38   $self->redirect_to(controller => 'controller.pl', action => 'CsvImport/test', 'profile.type' => 'bank_transactions', 'profile.id' => $self->profile->id, force_profile => 1);
39 }
40
41 sub check_auth {
42   $::auth->assert('bank_transaction');
43 }
44
45 sub init_profile {
46   my $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => $::myconfig{login});
47   if ( ! $profile ) {
48     $profile = SL::DB::Manager::CsvImportProfile->find_by(name => 'MT940', login => 'default');
49   }
50   return $profile;
51 }
52
53 sub setup_upload_mt940_action_bar {
54   my ($self) = @_;
55
56   for my $bar ($::request->layout->get('actionbar')) {
57     $bar->add(
58       action => [
59         $::locale->text('Preview'),
60         submit    => [ '#form', { action => 'BankImport/import_mt940' } ],
61         accesskey => 'enter',
62       ],
63     );
64   }
65 }
66
67 1;