From 2c6717d9653f11fd560fdac0a2aa06b4096ba2b6 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 3 Mar 2011 10:29:49 +0100 Subject: [PATCH] Refactoring: gemeinsamen Code ausgelagert; Ansprechpersonen/Lieferadressen: Kunden-/Lieferantennamen anzeigen --- SL/Controller/CsvImport.pm | 2 +- SL/Controller/CsvImport/Base.pm | 69 +++++++++++++++++++-- SL/Controller/CsvImport/Contact.pm | 64 +++++-------------- SL/Controller/CsvImport/Shipto.pm | 45 ++------------ locale/de/all | 1 + templates/webpages/csv_import/_preview.html | 6 ++ 6 files changed, 94 insertions(+), 93 deletions(-) diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 73a97faea..c0967ac0c 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -18,7 +18,7 @@ use parent qw(SL::Controller::Base); use Rose::Object::MakeMethods::Generic ( scalar => [ qw(type profile file all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen - import_status errors headers raw_data_headers data num_imported num_importable displayable_columns) ], + import_status errors headers raw_data_headers info_headers data num_imported num_importable displayable_columns) ], ); __PACKAGE__->run_before('check_auth'); diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 44b1e471a..b6bf80117 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -5,15 +5,17 @@ use strict; use List::MoreUtils qw(pairwise); use SL::Helper::Csv; +use SL::DB::Customer; use SL::DB::Language; use SL::DB::PaymentTerm; +use SL::DB::Vendor; use parent qw(Rose::Object); use Rose::Object::MakeMethods::Generic ( scalar => [ qw(controller file csv) ], - 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by) ], + 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ], ); sub run { @@ -39,13 +41,11 @@ sub run { $headers->{used} = { map { ($_ => 1) } @{ $headers->{headers} } }; $self->controller->headers($headers); $self->controller->raw_data_headers({ used => { }, headers => [ ] }); + $self->controller->info_headers({ used => { }, headers => [ ] }); - # 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 => [], information => [] } } @objects, @raw_data ]); - $::lxdebug->dump(0, "DATA", $self->controller->data); + $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]); $self->check_objects; $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check'; @@ -64,6 +64,18 @@ sub add_columns { } } +sub add_info_columns { + my ($self, @columns) = @_; + + my $h = $self->controller->info_headers; + + foreach my $column (grep { !$h->{used}->{ $_->{method} } } map { ref $_ eq 'HASH' ? $_ : { method => $_, header => $_ } } @columns) { + $h->{used}->{ $column->{method} } = 1; + push @{ $h->{methods} }, $column->{method}; + push @{ $h->{headers} }, $column->{header}; + } +} + sub add_raw_data_columns { my ($self, @columns) = @_; @@ -100,6 +112,53 @@ sub init_payment_terms_by { return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) }; } +sub init_all_vc { + my ($self) = @_; + + return { customers => SL::DB::Manager::Customer->get_all, + vendors => SL::DB::Manager::Vendor->get_all }; +} + +sub init_vc_by { + my ($self) = @_; + + my %by_id = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} }; + my %by_number = ( customers => { map { ( $_->customernumber => $_ ) } @{ $self->all_vc->{customers} } }, + vendors => { map { ( $_->vendornumber => $_ ) } @{ $self->all_vc->{vendors} } } ); + my %by_name = ( customers => { map { ( $_->name => $_ ) } @{ $self->all_vc->{customers} } }, + vendors => { map { ( $_->name => $_ ) } @{ $self->all_vc->{vendors} } } ); + + return { id => \%by_id, + number => \%by_number, + name => \%by_name, }; +} + +sub check_vc { + my ($self, $entry, $id_column) = @_; + + if ($entry->{object}->$id_column) { + $entry->{object}->$id_column(undef) if !$self->vc_by->{id}->{ $entry->{object}->$id_column }; + } + + if (!$entry->{object}->$id_column) { + my $vc = $self->vc_by->{number}->{customers}->{ $entry->{raw_data}->{customernumber} } + || $self->vc_by->{number}->{vendors}->{ $entry->{raw_data}->{vendornumber} }; + $entry->{object}->$id_column($vc->id) if $vc; + } + + if (!$entry->{object}->$id_column) { + my $vc = $self->vc_by->{name}->{customers}->{ $entry->{raw_data}->{customer} } + || $self->vc_by->{name}->{vendors}->{ $entry->{raw_data}->{vendor} }; + $entry->{object}->$id_column($vc->id) if $vc; + } + + if ($entry->{object}->$id_column) { + $entry->{info_data}->{vc_name} = $self->vc_by->{id}->{ $entry->{object}->$id_column }->name; + } else { + push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found'); + } +} + sub handle_cvars { my ($self, $entry) = @_; diff --git a/SL/Controller/CsvImport/Contact.pm b/SL/Controller/CsvImport/Contact.pm index 04697cb9b..e5dadbbeb 100644 --- a/SL/Controller/CsvImport/Contact.pm +++ b/SL/Controller/CsvImport/Contact.pm @@ -8,8 +8,7 @@ use parent qw(SL::Controller::CsvImport::Base); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(table) ], - 'scalar --get_set_init' => [ qw(all_vc) ], + scalar => [ qw(table) ], ); sub init_class { @@ -17,61 +16,32 @@ sub init_class { $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; - } + $self->check_name($entry); + $self->check_vc($entry, 'cp_cv_id'); + $self->check_gender($entry); + } - if ($object->cp_cv_id) { - $object->cp_cv_id(undef) if !$by_id{ $object->cp_cv_id }; - } + $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' }); +} - if (!$object->cp_cv_id) { - my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} }; - $object->cp_cv_id($vc_id) if $vc_id; - } +sub check_name { + my ($self, $entry) = @_; - 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; - } + my $name = $entry->{object}->cp_name; + $name =~ s/^\s+//; + $name =~ s/\s+$//; - if (!$object->cp_cv_id) { - push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found'); - next; - } + push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name; +} - $entry->{vc} = $by_id{ $object->cp_cv_id }; +sub check_gender { + my ($self, $entry) = @_; - if (($object->cp_gender ne 'm') && ($object->cp_gender ne 'f')) { - push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid'); - next; - } - } + 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'); } sub check_duplicates { diff --git a/SL/Controller/CsvImport/Shipto.pm b/SL/Controller/CsvImport/Shipto.pm index 9c6a2e922..f46c09d33 100644 --- a/SL/Controller/CsvImport/Shipto.pm +++ b/SL/Controller/CsvImport/Shipto.pm @@ -8,8 +8,7 @@ use parent qw(SL::Controller::CsvImport::Base); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(table) ], - 'scalar --get_set_init' => [ qw(all_vc) ], + scalar => [ qw(table) ], ); sub init_class { @@ -17,49 +16,15 @@ sub init_class { $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 }; + $self->check_vc($entry, 'trans_id'); + $entry->{object}->module('CT'); } + + $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' }); } sub check_duplicates { diff --git a/locale/de/all b/locale/de/all index 1fa97becb..d0c4b1482 100644 --- a/locale/de/all +++ b/locale/de/all @@ -490,6 +490,7 @@ $self->{texts} = { 'Customer not on file!' => 'Kunde ist nicht in der Datenbank!', 'Customer saved!' => 'Kunde gespeichert!', 'Customer type' => 'Kundentyp', + 'Customer/Vendor' => 'Kunde/Lieferant', 'Customer/Vendor (database ID)' => 'Kunde/Lieferant (Datenbank-ID)', 'Customername' => 'Kundenname', 'Customernumberinit' => 'Kunden-/Lieferantennummernkreis', diff --git a/templates/webpages/csv_import/_preview.html b/templates/webpages/csv_import/_preview.html index d36000cc9..fe088e25e 100644 --- a/templates/webpages/csv_import/_preview.html +++ b/templates/webpages/csv_import/_preview.html @@ -12,6 +12,9 @@ + [%- FOREACH column = SELF.info_headers.headers %] + + [%- END %] [%- FOREACH column = SELF.headers.headers %] [%- END %] @@ -23,6 +26,9 @@ [%- FOREACH row = SELF.data %] + [%- FOREACH method = SELF.info_headers.methods %] + + [%- END %] [%- FOREACH method = SELF.headers.methods %] [%- END %] -- 2.20.1
[%- HTML.escape(column) %][%- HTML.escape(column) %]
[%- HTML.escape(row.info_data.$method) %][%- HTML.escape(row.object.$method) %]