From b16b33c220a567bf8a8b998ff69d44d380b0a181 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 23 Apr 2013 09:01:21 +0200 Subject: [PATCH] CSV-Import Ansprechpersonen: Aktualisieren von Existierenden via Spalte "cp_id" Conflicts: SL/Controller/CsvImport/Contact.pm --- SL/Controller/CsvImport/Base.pm | 6 +++ SL/Controller/CsvImport/Contact.pm | 49 ++++++++++++++++++- locale/de/all | 3 ++ .../webpages/csv_import/_form_contacts.html | 8 +++ templates/webpages/csv_import/form.html | 3 ++ 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 templates/webpages/csv_import/_form_contacts.html diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index e7889d7b8..7f4326a07 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -153,6 +153,10 @@ sub init_all_vc { vendors => SL::DB::Manager::Vendor->get_all }; } +sub force_allow_columns { + return (); +} + sub init_vc_by { my ($self) = @_; @@ -222,6 +226,8 @@ sub init_profile { eval "require " . $self->class; my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns }); + delete $unwanted{$_} for ($self->force_allow_columns); + my %profile; for my $col ($self->class->meta->columns) { next if $unwanted{$col}; diff --git a/SL/Controller/CsvImport/Contact.pm b/SL/Controller/CsvImport/Contact.pm index 193f87d8e..5ca2ae848 100644 --- a/SL/Controller/CsvImport/Contact.pm +++ b/SL/Controller/CsvImport/Contact.pm @@ -24,20 +24,64 @@ sub init_all_cvar_configs { return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'Contacts' ]); } +sub force_allow_columns { + return qw(cp_id); +} + sub check_objects { my ($self) = @_; $self->controller->track_progress(phase => 'building data', progress => 0); - my $i; - my $num_data = scalar @{ $self->controller->data }; + my $i = 0; + my $num_data = scalar @{ $self->controller->data }; + my $update_policy = $self->controller->profile->get('update_policy') || 'update_existing'; + my %contacts_by_id = map { ( $_->cp_id => $_ ) } @{ $self->existing_objects }; + my $methods = $self->controller->headers->{methods}; + my %used_methods = map { ( $_ => 1 ) } @{ $methods }; + foreach my $entry (@{ $self->controller->data }) { $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0; + my $object = $entry->{object}; + if ($object->cp_id) { + my $existing_contact = $contacts_by_id{ $object->cp_id }; + if (!$existing_contact) { + $contacts_by_id{ $object->cp_id } = $object; + + } elsif ($update_policy eq 'skip') { + push(@{ $entry->{errors} }, $::locale->text('Skipping due to existing entry in database')); + next; + + } elsif ($update_policy eq 'update_existing') { + # Update existing customer/vendor records. + $entry->{object_to_save} = $existing_contact; + + $object->cp_cv_id($existing_contact->cp_cv_id); + + foreach (qw(cp_name cp_gender)) { + $object->$_($existing_contact->$_) if !$object->$_; + } + + $existing_contact->$_( $entry->{object}->$_ ) for @{ $methods }; + + push @{ $entry->{information} }, $::locale->text('Updating existing entry in database'); + + } else { + $object->cp_id(undef); + } + } + $self->check_name($entry); $self->check_vc($entry, 'cp_cv_id'); $self->check_gender($entry); $self->handle_cvars($entry); + + 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 + cp_privatphone cp_privatemail cp_abteilung cp_street cp_zipcode cp_city cp_position)); + + push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields)) + if @cleaned_fields; } continue { $i++; } @@ -105,6 +149,7 @@ sub setup_displayable_columns { { name => 'cp_fax', description => $::locale->text('Fax') }, { name => 'cp_gender', description => $::locale->text('Gender') }, { name => 'cp_givenname', description => $::locale->text('Given Name') }, + { name => 'cp_id', description => $::locale->text('Database ID') }, { name => 'cp_mobile1', description => $::locale->text('Mobile1') }, { name => 'cp_mobile2', description => $::locale->text('Mobile2') }, { name => 'cp_name', description => $::locale->text('Name') }, diff --git a/locale/de/all b/locale/de/all index 8c6685acf..02aaf87ff 100644 --- a/locale/de/all +++ b/locale/de/all @@ -856,6 +856,7 @@ $self->{texts} = { 'Execution type' => 'Ausführungsart', 'Existing Buchungsgruppen' => 'Existierende Buchungsgruppen', 'Existing Datasets' => 'Existierende Datenbanken', + 'Existing contacts (with column \'cp_id\')' => 'Existierende Ansprechpersonen (mit Spalte \'cp_id\')', 'Existing customers/vendors with same customer/vendor number' => 'Existierende Kunden/Lieferanten mit derselben Kunden-/Lieferantennummer', 'Existing file on server' => 'Auf dem Server existierende Datei', 'Existing pending follow-ups for this item' => 'Noch nicht erledigte Wiedervorlagen für dieses Dokument', @@ -1032,6 +1033,7 @@ $self->{texts} = { 'Individual Items' => 'Einzelteile', 'Information' => 'Information', 'Insert with new customer/vendor number' => 'Mit neuer Kunden-/Lieferantennummer anlegen', + 'Insert with new database ID' => 'Neu anlegen mit neuer Datenbank-ID', 'Insert with new part number' => 'Mit neuer Artikelnummer einfügen', 'Interest' => 'Zinsen', 'Interest Rate' => 'Zinssatz', @@ -2321,6 +2323,7 @@ $self->{texts} = { 'You can either create a new database or chose an existing database.' => 'Sie können entweder eine neue Datenbank erstellen oder eine existierende auswählen.', 'You can find information on the migration in the upgrade chapter of the documentation.' => 'Informationen über die Migration sind in der Upgrade Kapitel in der Dokumentation zu finden.', 'You can only delete datasets that are not in use.' => 'Sie können nur Datenbanken löschen, die momentan nicht in Benutzung sind.', + 'You can update existing contacts by providing the \'cp_id\' column with their database IDs. Otherwise: ' => 'Sie können existierende Einträge aktualisieren, indem Sie eine Spalte \'cp_id\' mit der Datenbank-ID des Eintrags mitgeben. Andernfalls: ', 'You can use the following strings in the long description and all translations. They will be replaced by their actual values by kivitendo before they\'re output.' => 'Sie können die folgenden Begriffe in den Langtexten und allen Übersetzungen benutzen. Sie werden von kivitendo vor der Ausgabe durch ihren tatsächlichen Wert ersetzt.', 'You cannot adjust the price for pricegroup "#1" by a negative percentage.' => 'Sie können den Preis für Preisgruppe "#1" um einen negativen Prozentwert anpassen.', 'You cannot continue before all required modules are installed.' => 'Sie können nicht fortfahren, bevor alle benötigten Pakete installiert sind.', diff --git a/templates/webpages/csv_import/_form_contacts.html b/templates/webpages/csv_import/_form_contacts.html new file mode 100644 index 000000000..c3325b60f --- /dev/null +++ b/templates/webpages/csv_import/_form_contacts.html @@ -0,0 +1,8 @@ +[%- USE LxERP -%][%- USE L -%] + + [%- LxERP.t8("Existing contacts (with column 'cp_id')") %]: + + [% opts = [ [ 'update_existing', LxERP.t8('Update properties of existing entries') ], [ 'insert_new', LxERP.t8('Insert with new database ID') ], [ 'skip', LxERP.t8('Skip entry') ] ] %] + [% L.select_tag('settings.update_policy', opts, default = SELF.profile.get('update_policy'), style = 'width: 300px') %] + + diff --git a/templates/webpages/csv_import/form.html b/templates/webpages/csv_import/form.html index 26db240d7..9fd30ac1d 100644 --- a/templates/webpages/csv_import/form.html +++ b/templates/webpages/csv_import/form.html @@ -73,6 +73,7 @@ [%- IF SELF.type == 'contacts' %]

+ [%- LxERP.t8("You can update existing contacts by providing the 'cp_id' column with their database IDs. Otherwise: ") %] [%- LxERP.t8('At least one of the columns #1, customer, customernumber, vendor, vendornumber (depending on the target table) is required for matching the entry to an existing customer or vendor.', 'cp_cv_id') %]

@@ -203,6 +204,8 @@ [%- INCLUDE 'csv_import/_form_parts.html' %] [%- ELSIF SELF.type == 'customers_vendors' %] [%- INCLUDE 'csv_import/_form_customers_vendors.html' %] +[%- ELSIF SELF.type == 'contacts' %] + [%- INCLUDE 'csv_import/_form_contacts.html' %] [%- END %] -- 2.20.1