1 package SL::Controller::CsvImport::Contact;
6 use SL::DB::CustomVariable;
7 use SL::DB::CustomVariableConfig;
9 use parent qw(SL::Controller::CsvImport::Base);
11 use Rose::Object::MakeMethods::Generic
13 scalar => [ qw(table) ],
18 $self->class('SL::DB::Contact');
21 sub init_all_cvar_configs {
24 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'Contacts' ]);
27 sub force_allow_columns {
34 $self->controller->track_progress(phase => 'building data', progress => 0);
37 my $num_data = scalar @{ $self->controller->data };
38 my $update_policy = $self->controller->profile->get('update_policy') || 'update_existing';
39 my %contacts_by_id = map { ( $_->cp_id => $_ ) } @{ $self->existing_objects };
40 my $methods = $self->controller->headers->{methods};
41 my %used_methods = map { ( $_ => 1 ) } @{ $methods };
43 foreach my $entry (@{ $self->controller->data }) {
44 $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
46 my $object = $entry->{object};
48 my $existing_contact = $contacts_by_id{ $object->cp_id };
49 if (!$existing_contact) {
50 $contacts_by_id{ $object->cp_id } = $object if $object->cp_id;
52 } elsif ($update_policy eq 'skip') {
53 push(@{ $entry->{errors} }, $::locale->text('Skipping due to existing entry in database'));
56 } elsif ($update_policy eq 'update_existing') {
57 # Update existing customer/vendor records.
58 $entry->{object_to_save} = $existing_contact;
60 $object->cp_cv_id($existing_contact->cp_cv_id);
62 foreach (qw(cp_name cp_gender)) {
63 $object->$_($existing_contact->$_) if !$object->$_;
66 $existing_contact->$_( $entry->{object}->$_ ) for @{ $methods };
68 push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
71 $object->cp_id(undef);
75 $self->check_name($entry);
76 $self->check_vc($entry, 'cp_cv_id');
77 $self->check_gender($entry);
78 $self->handle_cvars($entry);
80 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
81 cp_privatphone cp_privatemail cp_abteilung cp_street cp_zipcode cp_city cp_position));
83 push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
89 $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
90 $self->add_cvar_raw_data_columns;
94 my ($self, $entry) = @_;
96 my $name = $entry->{object}->cp_name;
100 push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name;
104 my ($self, $entry) = @_;
106 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');
109 sub get_duplicate_check_fields {
112 label => $::locale->text('Name'),
120 { s/[\s,\.\-]//g; $_ }
130 return ( cp_title => 75,
139 sub setup_displayable_columns {
142 $self->SUPER::setup_displayable_columns;
143 $self->add_cvar_columns_to_displayable_columns;
145 $self->add_displayable_columns({ name => 'cp_abteilung', description => $::locale->text('Department') },
146 { name => 'cp_birthday', description => $::locale->text('Birthday') },
147 { name => 'cp_cv_id', description => $::locale->text('Customer/Vendor (database ID)') },
148 { name => 'cp_email', description => $::locale->text('E-mail') },
149 { name => 'cp_fax', description => $::locale->text('Fax') },
150 { name => 'cp_gender', description => $::locale->text('Gender') },
151 { name => 'cp_givenname', description => $::locale->text('Given Name') },
152 { name => 'cp_id', description => $::locale->text('Database ID') },
153 { name => 'cp_mobile1', description => $::locale->text('Mobile1') },
154 { name => 'cp_mobile2', description => $::locale->text('Mobile2') },
155 { name => 'cp_name', description => $::locale->text('Name') },
156 { name => 'cp_phone1', description => $::locale->text('Phone1') },
157 { name => 'cp_phone2', description => $::locale->text('Phone2') },
158 { name => 'cp_privatemail', description => $::locale->text('Private E-mail') },
159 { name => 'cp_privatphone', description => $::locale->text('Private Phone') },
160 { name => 'cp_project', description => $::locale->text('Project') },
161 { name => 'cp_satfax', description => $::locale->text('Sat. Fax') },
162 { name => 'cp_satphone', description => $::locale->text('Sat. Phone') },
163 { name => 'cp_title', description => $::locale->text('Title') },
164 { name => 'cp_position', description => $::locale->text('Function/position') },
166 { name => 'customer', description => $::locale->text('Customer (name)') },
167 { name => 'customernumber', description => $::locale->text('Customer Number') },
168 { name => 'vendor', description => $::locale->text('Vendor (name)') },
169 { name => 'vendornumber', description => $::locale->text('Vendor Number') },