Bugfixes Artikelimport
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
1 package SL::Controller::CsvImport::Base;
2
3 use strict;
4
5 use List::MoreUtils qw(pairwise);
6
7 use SL::Helper::Csv;
8
9 use parent qw(Rose::Object);
10
11 use Rose::Object::MakeMethods::Generic
12 (
13  scalar                  => [ qw(controller file csv) ],
14  'scalar --get_set_init' => [ qw(profile existing_objects class manager_class) ],
15 );
16
17 sub run {
18   my ($self) = @_;
19
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                                   strict_profile         => 1,
27                                   map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
28                                  ));
29   $self->csv->parse;
30
31   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
32
33   return unless $self->csv->header;
34
35   my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
36   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
37   $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
38   $self->controller->headers($headers);
39
40   # my @data;
41   # foreach my $object ($self->csv->get_objects)
42   my @objects  = $self->csv->get_objects;
43   my @raw_data = @{ $self->csv->get_data };
44   $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [] } } @objects, @raw_data ]);
45
46   $self->check_objects;
47   $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
48   $self->fix_field_lenghts;
49 }
50
51 sub add_columns {
52   my ($self, @columns) = @_;
53
54   my $h = $self->controller->headers;
55
56   foreach my $column (grep { !$h->{used}->{$_} } @columns) {
57     $h->{used}->{$column} = 1;
58     push @{ $h->{methods} }, $column;
59     push @{ $h->{headers} }, $column;
60   }
61 }
62
63 sub init_profile {
64   my ($self) = @_;
65
66   eval "require " . $self->class;
67
68   my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns });
69   my %profile;
70   for my $col ($self->class->meta->columns) {
71     next if $unwanted{$col};
72
73     my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric')   ? "$col\_as_number"
74       :        $col->isa('Rose::DB::Object::Metadata::Column::Date')      ? "$col\_as_date"
75       :        $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
76       :                                                                     $col->name;
77
78     $profile{$col} = $name;
79   }
80
81   $self->profile(\%profile);
82 }
83
84 sub init_existing_objects {
85   my ($self) = @_;
86
87   eval "require " . $self->class;
88   $self->existing_objects($self->manager_class->get_all);
89 }
90
91 sub init_class {
92   die "class not set";
93 }
94
95 sub init_manager_class {
96   my ($self) = @_;
97
98   $self->class =~ m/^SL::DB::(.+)/;
99   $self->manager_class("SL::DB::Manager::" . $1);
100 }
101
102 sub check_objects {
103 }
104
105 sub check_duplicates {
106 }
107
108 sub save_objects {
109   my ($self, %params) = @_;
110
111   my $data = $params{data} || $self->controller->data;
112
113   foreach my $entry (@{ $data }) {
114     next if @{ $entry->{errors} };
115
116     if (!$entry->{object}->save) {
117       push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
118     } else {
119       $self->controller->num_imported($self->controller->num_imported + 1);
120     }
121   }
122 }
123
124 sub field_lengths {
125   return ();
126 }
127
128 sub fix_field_lenghts {
129   my ($self) = @_;
130
131   my %field_lengths = $self->field_lengths;
132   foreach my $entry (@{ $self->controller->data }) {
133     next unless @{ $entry->{errors} };
134     map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
135   }
136 }
137
138 1;