From 9e152755a340c813514eb132de27349ae7a9e028 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 1 Mar 2011 17:15:47 +0100 Subject: [PATCH] Import von Lieferadressen. --- SL/Controller/CsvImport.pm | 2 + SL/Controller/CsvImport/Shipto.pm | 109 ++++++++++++++++++++++++++++++ SL/DB/Customer.pm | 8 ++- SL/DB/Vendor.pm | 2 +- menu.ini | 8 +-- 5 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 SL/Controller/CsvImport/Shipto.pm diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 022861508..6556c5b91 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -8,6 +8,7 @@ use SL::Helper::Flash; use SL::SessionFile; use SL::Controller::CsvImport::Contact; use SL::Controller::CsvImport::CustomerVendor; +use SL::Controller::CsvImport::Shipto; use List::MoreUtils qw(none); @@ -144,6 +145,7 @@ sub test_and_import { my $worker = $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(controller => $self, file => $file) : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new( controller => $self, file => $file) + : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new( controller => $self, file => $file) : die "Program logic error"; $worker->run; diff --git a/SL/Controller/CsvImport/Shipto.pm b/SL/Controller/CsvImport/Shipto.pm new file mode 100644 index 000000000..5f99386d7 --- /dev/null +++ b/SL/Controller/CsvImport/Shipto.pm @@ -0,0 +1,109 @@ +package SL::Controller::CsvImport::Shipto; + +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::Shipto'); +} + +sub init_all_vc { + my ($self) = @_; + + $self->all_vc({ customers => SL::DB::Manager::Customer->get_all, + vendors => SL::DB::Manager::Vendor->get_all }); +} + +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}; + + if ($object->trans_id) { + $object->trans_id(undef) if !$by_id{ $object->trans_id }; + } + + if (!$object->trans_id) { + my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} }; + $object->trans_id($vc_id) if $vc_id; + } + + if (!$object->trans_id) { + my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} }; + $object->trans_id($vc_id) if $vc_id; + } + + if (!$object->trans_id) { + push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found'); + next; + } + + $object->module('CT'); + + $entry->{vc} = $by_id{ $object->trans_id }; + } +} + +sub check_duplicates { + my ($self, %params) = @_; + + my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; }; + my $name_maker = sub { return $normalizer->($_[0]->shiptoname) . '--' . $normalizer->($_[0]->shiptostreet) }; + + 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 { ( $name_maker->($_) => 'db' ) } @{ $vc->shipto } }; + } + } + } + + foreach my $entry (@{ $self->controller->data }) { + next if @{ $entry->{errors} }; + + my $name = $name_maker->($entry->{object}); + + $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 ( shiptoname => 75, + shiptodepartment_1 => 75, + shiptodepartment_2 => 75, + shiptostreet => 75, + shiptozipcode => 75, + shiptocity => 75, + shiptocountry => 75, + shiptocontact => 75, + shiptophone => 30, + shiptofax => 30, + ); +} + +1; diff --git a/SL/DB/Customer.pm b/SL/DB/Customer.pm index 85d699933..f2893ae74 100644 --- a/SL/DB/Customer.pm +++ b/SL/DB/Customer.pm @@ -14,7 +14,13 @@ __PACKAGE__->meta->add_relationship( class => 'SL::DB::Shipto', column_map => { id => 'trans_id' }, manager_args => { sort_by => 'lower(shipto.shiptoname)' }, - query_args => [ 'module' => 'CT' ], + query_args => [ module => 'CT' ], + }, + contacts => { + type => 'one to many', + class => 'SL::DB::Contact', + column_map => { id => 'cp_cv_id' }, + manager_args => { sort_by => 'lower(contacts.cp_name)' }, }, business => { type => 'one to one', diff --git a/SL/DB/Vendor.pm b/SL/DB/Vendor.pm index 42e6ca690..13b70ebe2 100644 --- a/SL/DB/Vendor.pm +++ b/SL/DB/Vendor.pm @@ -13,7 +13,7 @@ __PACKAGE__->meta->add_relationship( class => 'SL::DB::Shipto', column_map => { id => 'trans_id' }, manager_args => { sort_by => 'lower(shipto.shiptoname)' }, - query_args => [ 'shipto.module' => 'CT' ], + query_args => [ module => 'CT' ], }, contacts => { type => 'one to many', diff --git a/menu.ini b/menu.ini index b89693a7e..8dcdf0121 100644 --- a/menu.ini +++ b/menu.ini @@ -789,10 +789,10 @@ module=controller.pl action=CsvImport/new profile.type=contacts -# [System--Import CSV2--Shipto] -# module=controller.pl -# action=CsvImport/new -# profile.type=addresses +[System--Import CSV2--Shipto] +module=controller.pl +action=CsvImport/new +profile.type=addresses # [System--Import CSV2--Parts] # module=controller.pl -- 2.20.1