Kunden-/Lieferantenstammdatenimport: zumindest das Testen funktioniert
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
1 package SL::Controller::CsvImport::Base;
2
3 use strict;
4
5 use SL::Helper::Csv;
6
7 use parent qw(Rose::Object);
8
9 use Rose::Object::MakeMethods::Generic
10 (
11  scalar                  => [ qw(controller file csv) ],
12  'scalar --get_set_init' => [ qw(profile existing_objects class manager_class) ],
13 );
14
15 sub run {
16   my ($self) = @_;
17
18   $::lxdebug->dump(0, "file", $self->file);
19   $::lxdebug->dump(0, "profile", $self->controller->profile);
20   my $profile = $self->profile;
21   $self->csv(SL::Helper::Csv->new(file                   => $self->file->file_name,
22                                   encoding               => $self->controller->profile->get('charset'),
23                                   class                  => $self->class,
24                                   profile                => $profile,
25                                   ignore_unknown_columns => 1,
26                                   map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
27                                  ));
28   $self->csv->parse;
29
30   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
31
32   $::lxdebug->dump(0, "err", $self->csv->errors);
33
34   return unless $self->csv->header;
35
36   my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
37   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
38   $self->controller->headers($headers);
39
40   $self->controller->data([ map { { object => $_, errors => [] } } $self->csv->get_objects ]);
41
42   $self->check_objects;
43   $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
44 }
45
46 sub init_profile {
47   my ($self) = @_;
48
49   eval "require " . $self->class;
50
51   my %profile;
52   for my $col ($self->class->meta->columns) {
53     my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric')   ? "$col\_as_number"
54       :        $col->isa('Rose::DB::Object::Metadata::Column::Date')      ? "$col\_as_date"
55       :        $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
56       :                                                                     $col->name;
57
58     $profile{$col} = $name;
59   }
60
61   $self->profile(\%profile);
62 }
63
64 sub init_existing_objects {
65   my ($self) = @_;
66
67   eval "require " . $self->class;
68   $self->existing_objects($self->manager_class->get_all);
69 }
70
71 sub init_class {
72   die "class not set";
73 }
74
75 sub init_manager_class {
76   my ($self) = @_;
77
78   $self->class =~ m/^SL::DB::(.+)/;
79   $self->manager_class("SL::DB::Manager::" . $1);
80 }
81
82 sub check_objects {
83 }
84
85 sub check_duplicates {
86 }
87
88 sub save_objects {
89   my ($self, %params) = @_;
90
91   my $data = $params{data} || $self->controller->data;
92
93   foreach my $entry (@{ $data }) {
94     if (!$entry->{object}->save) {
95       push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
96     } else {
97       $self->controller->num_imported($self->controller->num_imported + 1);
98     }
99   }
100 }
101
102 1;