Pflichtenheft Angebote/Aufträge: "Abbrechen" ohne AJAX-Call nur in JS
[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 init_class {
17   my ($self) = @_;
18   $self->class('SL::DB::Contact');
19 }
20
21 sub init_all_cvar_configs {
22   my ($self) = @_;
23
24   return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'Contacts' ]);
25 }
26
27 sub force_allow_columns {
28   return qw(cp_id);
29 }
30
31 sub check_objects {
32   my ($self) = @_;
33
34   $self->controller->track_progress(phase => 'building data', progress => 0);
35
36   my $i              = 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 };
42
43   foreach my $entry (@{ $self->controller->data }) {
44     $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
45
46     my $object = $entry->{object};
47     if ($object->cp_id) {
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;
51
52       } elsif ($update_policy eq 'skip') {
53         push(@{ $entry->{errors} }, $::locale->text('Skipping due to existing entry in database'));
54         next;
55
56       } elsif ($update_policy eq 'update_existing') {
57         # Update existing customer/vendor records.
58         $entry->{object_to_save} = $existing_contact;
59
60         $object->cp_cv_id($existing_contact->cp_cv_id);
61
62         foreach (qw(cp_name cp_gender)) {
63           $object->$_($existing_contact->$_) if !$object->$_;
64         }
65
66         $existing_contact->$_( $entry->{object}->$_ ) for @{ $methods };
67
68         push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
69
70       } else {
71         $object->cp_id(undef);
72       }
73     }
74
75     $self->check_name($entry);
76     $self->check_vc($entry, 'cp_cv_id');
77     $self->check_gender($entry);
78     $self->handle_cvars($entry);
79
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));
82
83     push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
84       if @cleaned_fields;
85   } continue {
86     $i++;
87   }
88
89   $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
90   $self->add_cvar_raw_data_columns;
91 }
92
93 sub check_name {
94   my ($self, $entry) = @_;
95
96   my $name     =  $entry->{object}->cp_name;
97   $name        =~ s/^\s+//;
98   $name        =~ s/\s+$//;
99
100   push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name;
101 }
102
103 sub check_gender {
104   my ($self, $entry) = @_;
105
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');
107 }
108
109 sub get_duplicate_check_fields {
110   return {
111     cp_name => {
112       label     => $::locale->text('Name'),
113       default   => 1,
114       maker     => sub {
115         my $o = shift;
116         return join(
117                  '--',
118                  $o->cp_cv_id,
119                  map(
120                    { s/[\s,\.\-]//g; $_ }
121                    $o->cp_name
122                  )
123         );
124       }
125     },
126   };
127 }
128
129 sub field_lengths {
130   return ( cp_title     => 75,
131            cp_givenname => 75,
132            cp_name      => 75,
133            cp_phone1    => 75,
134            cp_phone2    => 75,
135            cp_gender    =>  1,
136          );
137 }
138
139 sub setup_displayable_columns {
140   my ($self) = @_;
141
142   $self->SUPER::setup_displayable_columns;
143   $self->add_cvar_columns_to_displayable_columns;
144
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')             },
165
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')                 },
170                                 );
171 }
172
173 1;