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) ],
16 sub set_profile_defaults {
21 $self->class('SL::DB::Contact');
24 sub init_all_cvar_configs {
27 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'Contacts' ]);
30 sub force_allow_columns {
37 $self->controller->track_progress(phase => 'building data', progress => 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 };
46 foreach my $entry (@{ $self->controller->data }) {
47 $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
49 my $object = $entry->{object};
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;
55 } elsif ($update_policy eq 'skip') {
56 push(@{ $entry->{errors} }, $::locale->text('Skipping due to existing entry in database'));
59 } elsif ($update_policy eq 'update_existing') {
60 # Update existing customer/vendor records.
61 $entry->{object_to_save} = $existing_contact;
63 $object->cp_cv_id($existing_contact->cp_cv_id);
65 foreach (qw(cp_name cp_gender)) {
66 $object->$_($existing_contact->$_) if !$object->$_;
69 $existing_contact->$_( $entry->{object}->$_ ) for @{ $methods };
71 push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
74 $object->cp_id(undef);
78 $self->check_name($entry);
79 $self->check_vc($entry, 'cp_cv_id');
80 $self->check_gender($entry);
81 $self->handle_cvars($entry);
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));
86 push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
92 $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
93 $self->add_cvar_raw_data_columns;
97 my ($self, $entry) = @_;
99 my $name = $entry->{object}->cp_name;
103 push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name;
107 my ($self, $entry) = @_;
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');
112 sub get_duplicate_check_fields {
115 label => $::locale->text('Name'),
123 { s/[\s,\.\-]//g; $_ }
132 sub setup_displayable_columns {
135 $self->SUPER::setup_displayable_columns;
136 $self->add_cvar_columns_to_displayable_columns;
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') },
159 { name => 'customer', description => $::locale->text('Customer (name)') },
160 { name => 'customernumber', description => $::locale->text('Customer Number') },
161 { name => 'vendor', description => $::locale->text('Vendor (name)') },
162 { name => 'vendornumber', description => $::locale->text('Vendor Number') },