From 7b9d1666e389bfedba43b2d0fd5d1b0e3f129e88 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 1 Mar 2011 16:13:48 +0100 Subject: [PATCH] Import von Ansprechpartnern --- SL/Controller/CsvImport.pm | 1 + SL/Controller/CsvImport/Base.pm | 8 +- SL/Controller/CsvImport/Contact.pm | 117 +++++++++++++++++++++++++++++ menu.ini | 8 +- 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 SL/Controller/CsvImport/Contact.pm diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 7717e2cee..bebd3516d 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -6,6 +6,7 @@ use SL::DB::Buchungsgruppe; use SL::DB::CsvImportProfile; use SL::Helper::Flash; use SL::SessionFile; +use SL::Controller::CsvImport::Contact; use SL::Controller::CsvImport::CustomerVendor; use List::MoreUtils qw(none); diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 175392240..e865a8612 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -2,6 +2,8 @@ package SL::Controller::CsvImport::Base; use strict; +use List::MoreUtils qw(pairwise); + use SL::Helper::Csv; use parent qw(Rose::Object); @@ -33,7 +35,11 @@ sub run { $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ]; $self->controller->headers($headers); - $self->controller->data([ map { { object => $_, errors => [] } } $self->csv->get_objects ]); + # my @data; + # foreach my $object ($self->csv->get_objects) + my @objects = $self->csv->get_objects; + my @raw_data = @{ $self->csv->get_data }; + $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [] } } @objects, @raw_data ]); $self->check_objects; $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check'; diff --git a/SL/Controller/CsvImport/Contact.pm b/SL/Controller/CsvImport/Contact.pm new file mode 100644 index 000000000..ace665401 --- /dev/null +++ b/SL/Controller/CsvImport/Contact.pm @@ -0,0 +1,117 @@ +package SL::Controller::CsvImport::Contact; + +use strict; + +use SL::Helper::Csv; + +use parent qw(SL::Controller::CsvImport::Base); + +use Rose::Object::MakeMethods::Generic +( + scalar => [ qw(table) ], + 'scalar --get_set_init' => [ qw(all_vc) ], +); + +sub init_class { + my ($self) = @_; + $self->class('SL::DB::Contact'); +} + +sub init_all_vc { + my ($self) = @_; + + $self->all_vc({ customers => SL::DB::Manager::Customer->get_all(with_objects => [ 'contacts' ]), + vendors => SL::DB::Manager::Vendor->get_all( with_objects => [ 'contacts' ]) }); +} + +sub check_objects { + my ($self) = @_; + + my %by_id = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} }; + my %by_number = ( customers => { map { ( $_->customernumber => $_->id ) } @{ $self->all_vc->{customers} } }, + vendors => { map { ( $_->vendornumber => $_->id ) } @{ $self->all_vc->{vendors} } } ); + my %by_name = ( customers => { map { ( $_->name => $_->id ) } @{ $self->all_vc->{customers} } }, + vendors => { map { ( $_->name => $_->id ) } @{ $self->all_vc->{vendors} } } ); + + foreach my $entry (@{ $self->controller->data }) { + my $object = $entry->{object}; + my $raw_data = $entry->{raw_data}; + + my $name = $object->cp_name; + $name =~ s/^\s+//; + $name =~ s/\s+$//; + + if (!$name) { + push @{ $entry->{errors} }, $::locale->text('Error: Name missing'); + next; + } + + if ($object->cp_cv_id) { + $object->cp_cv_id(undef) if !$by_id{ $object->cp_cv_id }; + } + + if (!$object->cp_cv_id) { + $::lxdebug->message(0, "cnum" . $raw_data->{customernumber}); + my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} }; + $object->cp_cv_id($vc_id) if $vc_id; + } + + if (!$object->cp_cv_id) { + my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} }; + $object->cp_cv_id($vc_id) if $vc_id; + } + + if (!$object->cp_cv_id) { + push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found'); + next; + } + + $entry->{vc} = $by_id{ $object->cp_cv_id }; + + if (($object->cp_gender ne 'm') && ($object->cp_gender ne 'f')) { + push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid'); + next; + } + } +} + +sub check_duplicates { + my ($self, %params) = @_; + + my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; }; + + my %by_id_and_name; + if ('check_db' eq $self->controller->profile->get('duplicates')) { + foreach my $type (qw(customers vendors)) { + foreach my $vc (@{ $self->all_vc->{$type} }) { + $by_id_and_name{ $vc->id } = { map { ( $normalizer->($_->cp_name) => 'db' ) } @{ $vc->contacts } }; + } + } + } + + foreach my $entry (@{ $self->controller->data }) { + next if @{ $entry->{errors} }; + + my $name = $normalizer->($entry->{object}->cp_name); + + $by_id_and_name{ $entry->{vc}->id } ||= { }; + if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) { + $by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv'; + + } else { + push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file'); + } + } +} + +sub field_lengths { + return ( cp_title => 75, + cp_givenname => 75, + cp_name => 75, + cp_phone1 => 75, + cp_phone2 => 75, + cp_gender => 1, + ); +} + +1; diff --git a/menu.ini b/menu.ini index 37005f27a..b89693a7e 100644 --- a/menu.ini +++ b/menu.ini @@ -784,10 +784,10 @@ module=controller.pl action=CsvImport/new profile.type=customers_vendors -# [System--Import CSV2--Contacts] -# module=controller.pl -# action=CsvImport/new -# profile.type=contacts +[System--Import CSV2--Contacts] +module=controller.pl +action=CsvImport/new +profile.type=contacts # [System--Import CSV2--Shipto] # module=controller.pl -- 2.20.1