Leistungsdatum: Order-Controller
[kivitendo-erp.git] / SL / Controller / CsvImport / Contact.pm
1 package SL::Controller::CsvImport::Contact;
2
3 use strict;
4
5 use SL::Helper::Csv;
6 use SL::DB::CustomVariable;
7 use SL::DB::CustomVariableConfig;
8
9 use parent qw(SL::Controller::CsvImport::Base);
10
11 use Rose::Object::MakeMethods::Generic
12 (
13  scalar => [ qw(table) ],
14 );
15
16 sub set_profile_defaults {
17 };
18
19 sub init_class {
20   my ($self) = @_;
21   $self->class('SL::DB::Contact');
22 }
23
24 sub init_all_cvar_configs {
25   my ($self) = @_;
26
27   return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'Contacts' ]);
28 }
29
30 sub force_allow_columns {
31   return qw(cp_id);
32 }
33
34 sub check_objects {
35   my ($self) = @_;
36
37   $self->controller->track_progress(phase => 'building data', progress => 0);
38
39   my $i              = 0;
40   my $num_data       = scalar @{ $self->controller->data };
41   my $update_policy  = $self->controller->profile->get('update_policy') || 'update_existing';
42   my %contacts_by_id = map { ( $_->cp_id => $_ ) } @{ $self->existing_objects };
43   my $methods        = $self->controller->headers->{methods};
44   my %used_methods   = map { ( $_ => 1 ) } @{ $methods };
45
46   foreach my $entry (@{ $self->controller->data }) {
47     $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
48
49     my $object = $entry->{object};
50     if ($object->cp_id) {
51       my $existing_contact = $contacts_by_id{ $object->cp_id };
52       if (!$existing_contact) {
53         $contacts_by_id{ $object->cp_id } = $object if $object->cp_id;
54
55       } elsif ($update_policy eq 'skip') {
56         push(@{ $entry->{errors} }, $::locale->text('Skipping due to existing entry in database'));
57         next;
58
59       } elsif ($update_policy eq 'update_existing') {
60         # Update existing customer/vendor records.
61         $entry->{object_to_save} = $existing_contact;
62
63         $object->cp_cv_id($existing_contact->cp_cv_id);
64
65         foreach (qw(cp_name cp_gender)) {
66           $object->$_($existing_contact->$_) if !$object->$_;
67         }
68
69         $existing_contact->$_( $entry->{object}->$_ ) for @{ $methods };
70
71         push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
72
73       } else {
74         $object->cp_id(undef);
75       }
76     }
77
78     $self->check_name($entry);
79     $self->check_vc($entry, 'cp_cv_id');
80     $self->check_gender($entry);
81     $self->handle_cvars($entry);
82
83     my @cleaned_fields = $self->clean_fields(qr{[\r\n]}, $object, qw(cp_title cp_givenname cp_name cp_email cp_phone1 cp_phone2 cp_fax cp_mobile1 cp_mobile2 cp_satphone cp_satfax
84                                                                      cp_privatphone cp_privatemail cp_abteilung cp_street cp_zipcode cp_city cp_position));
85
86     push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
87       if @cleaned_fields;
88   } continue {
89     $i++;
90   }
91
92   $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
93   $self->add_cvar_raw_data_columns;
94 }
95
96 sub check_name {
97   my ($self, $entry) = @_;
98
99   my $name     =  $entry->{object}->cp_name;
100   $name        =~ s/^\s+//;
101   $name        =~ s/\s+$//;
102
103   push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name;
104 }
105
106 sub check_gender {
107   my ($self, $entry) = @_;
108
109   push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid') if ($entry->{object}->cp_gender ne 'm') && ($entry->{object}->cp_gender ne 'f');
110 }
111
112 sub get_duplicate_check_fields {
113   return {
114     cp_name => {
115       label     => $::locale->text('Name'),
116       default   => 1,
117       maker     => sub {
118         my $o = shift;
119         return join(
120                  '--',
121                  $o->cp_cv_id,
122                  map(
123                    { s/[\s,\.\-]//g; $_ }
124                    $o->cp_name
125                  )
126         );
127       }
128     },
129   };
130 }
131
132 sub setup_displayable_columns {
133   my ($self) = @_;
134
135   $self->SUPER::setup_displayable_columns;
136   $self->add_cvar_columns_to_displayable_columns;
137
138   $self->add_displayable_columns({ name => 'cp_abteilung',   description => $::locale->text('Department')                    },
139                                  { name => 'cp_birthday',    description => $::locale->text('Birthday')                      },
140                                  { name => 'cp_cv_id',       description => $::locale->text('Customer/Vendor (database ID)') },
141                                  { name => 'cp_email',       description => $::locale->text('E-mail')                        },
142                                  { name => 'cp_fax',         description => $::locale->text('Fax')                           },
143                                  { name => 'cp_gender',      description => $::locale->text('Gender')                        },
144                                  { name => 'cp_givenname',   description => $::locale->text('Given Name')                    },
145                                  { name => 'cp_id',          description => $::locale->text('Database ID')                   },
146                                  { name => 'cp_mobile1',     description => $::locale->text('Mobile1')                       },
147                                  { name => 'cp_mobile2',     description => $::locale->text('Mobile2')                       },
148                                  { name => 'cp_name',        description => $::locale->text('Name')                          },
149                                  { name => 'cp_phone1',      description => $::locale->text('Phone1')                        },
150                                  { name => 'cp_phone2',      description => $::locale->text('Phone2')                        },
151                                  { name => 'cp_privatemail', description => $::locale->text('Private E-mail')                },
152                                  { name => 'cp_privatphone', description => $::locale->text('Private Phone')                 },
153                                  { name => 'cp_project',     description => $::locale->text('Project')                       },
154                                  { name => 'cp_satfax',      description => $::locale->text('Sat. Fax')                      },
155                                  { name => 'cp_satphone',    description => $::locale->text('Sat. Phone')                    },
156                                  { name => 'cp_title',       description => $::locale->text('Title')                         },
157                                  { name => 'cp_position',    description => $::locale->text('Function/position')             },
158
159                                  { name => 'customer',       description => $::locale->text('Customer (name)')               },
160                                  { name => 'customernumber', description => $::locale->text('Customer Number')               },
161                                  { name => 'customer_gln',   description => $::locale->text('Customer GLN')                  },
162                                  { name => 'vendor',         description => $::locale->text('Vendor (name)')                 },
163                                  { name => 'vendornumber',   description => $::locale->text('Vendor Number')                 },
164                                  { name => 'vendor_gln',     description => $::locale->text('Vendor GLN')                    },
165                                 );
166 }
167
168 1;