|
1 |
package SL::Controller::CsvImport::Contact;
|
|
2 |
|
|
3 |
use strict;
|
|
4 |
|
|
5 |
use SL::Helper::Csv;
|
|
6 |
|
|
7 |
use parent qw(SL::Controller::CsvImport::Base);
|
|
8 |
|
|
9 |
use Rose::Object::MakeMethods::Generic
|
|
10 |
(
|
|
11 |
scalar => [ qw(table) ],
|
|
12 |
'scalar --get_set_init' => [ qw(all_vc) ],
|
|
13 |
);
|
|
14 |
|
|
15 |
sub init_class {
|
|
16 |
my ($self) = @_;
|
|
17 |
$self->class('SL::DB::Contact');
|
|
18 |
}
|
|
19 |
|
|
20 |
sub init_all_vc {
|
|
21 |
my ($self) = @_;
|
|
22 |
|
|
23 |
$self->all_vc({ customers => SL::DB::Manager::Customer->get_all(with_objects => [ 'contacts' ]),
|
|
24 |
vendors => SL::DB::Manager::Vendor->get_all( with_objects => [ 'contacts' ]) });
|
|
25 |
}
|
|
26 |
|
|
27 |
sub check_objects {
|
|
28 |
my ($self) = @_;
|
|
29 |
|
|
30 |
my %by_id = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} };
|
|
31 |
my %by_number = ( customers => { map { ( $_->customernumber => $_->id ) } @{ $self->all_vc->{customers} } },
|
|
32 |
vendors => { map { ( $_->vendornumber => $_->id ) } @{ $self->all_vc->{vendors} } } );
|
|
33 |
my %by_name = ( customers => { map { ( $_->name => $_->id ) } @{ $self->all_vc->{customers} } },
|
|
34 |
vendors => { map { ( $_->name => $_->id ) } @{ $self->all_vc->{vendors} } } );
|
|
35 |
|
|
36 |
foreach my $entry (@{ $self->controller->data }) {
|
|
37 |
my $object = $entry->{object};
|
|
38 |
my $raw_data = $entry->{raw_data};
|
|
39 |
|
|
40 |
my $name = $object->cp_name;
|
|
41 |
$name =~ s/^\s+//;
|
|
42 |
$name =~ s/\s+$//;
|
|
43 |
|
|
44 |
if (!$name) {
|
|
45 |
push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
|
|
46 |
next;
|
|
47 |
}
|
|
48 |
|
|
49 |
if ($object->cp_cv_id) {
|
|
50 |
$object->cp_cv_id(undef) if !$by_id{ $object->cp_cv_id };
|
|
51 |
}
|
|
52 |
|
|
53 |
if (!$object->cp_cv_id) {
|
|
54 |
$::lxdebug->message(0, "cnum" . $raw_data->{customernumber});
|
|
55 |
my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} };
|
|
56 |
$object->cp_cv_id($vc_id) if $vc_id;
|
|
57 |
}
|
|
58 |
|
|
59 |
if (!$object->cp_cv_id) {
|
|
60 |
my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} };
|
|
61 |
$object->cp_cv_id($vc_id) if $vc_id;
|
|
62 |
}
|
|
63 |
|
|
64 |
if (!$object->cp_cv_id) {
|
|
65 |
push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
|
|
66 |
next;
|
|
67 |
}
|
|
68 |
|
|
69 |
$entry->{vc} = $by_id{ $object->cp_cv_id };
|
|
70 |
|
|
71 |
if (($object->cp_gender ne 'm') && ($object->cp_gender ne 'f')) {
|
|
72 |
push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid');
|
|
73 |
next;
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
sub check_duplicates {
|
|
79 |
my ($self, %params) = @_;
|
|
80 |
|
|
81 |
my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
|
|
82 |
|
|
83 |
my %by_id_and_name;
|
|
84 |
if ('check_db' eq $self->controller->profile->get('duplicates')) {
|
|
85 |
foreach my $type (qw(customers vendors)) {
|
|
86 |
foreach my $vc (@{ $self->all_vc->{$type} }) {
|
|
87 |
$by_id_and_name{ $vc->id } = { map { ( $normalizer->($_->cp_name) => 'db' ) } @{ $vc->contacts } };
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
foreach my $entry (@{ $self->controller->data }) {
|
|
93 |
next if @{ $entry->{errors} };
|
|
94 |
|
|
95 |
my $name = $normalizer->($entry->{object}->cp_name);
|
|
96 |
|
|
97 |
$by_id_and_name{ $entry->{vc}->id } ||= { };
|
|
98 |
if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) {
|
|
99 |
$by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv';
|
|
100 |
|
|
101 |
} else {
|
|
102 |
push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
sub field_lengths {
|
|
108 |
return ( cp_title => 75,
|
|
109 |
cp_givenname => 75,
|
|
110 |
cp_name => 75,
|
|
111 |
cp_phone1 => 75,
|
|
112 |
cp_phone2 => 75,
|
|
113 |
cp_gender => 1,
|
|
114 |
);
|
|
115 |
}
|
|
116 |
|
|
117 |
1;
|
Import von Ansprechpartnern