From 59274d46487423806bdc4c76ea405c57d9e45c0a Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 11 Nov 2021 16:37:24 +0100 Subject: [PATCH] =?utf8?q?CSV-Import=20von=20zus=C3=A4tzlichen=20Rechnungs?= =?utf8?q?adressen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/CsvImport.pm | 5 +- .../CsvImport/AdditionalBillingAddress.pm | 90 +++++++++++++++++++ SL/DB/AdditionalBillingAddress.pm | 18 ++++ locale/de/all | 2 + menus/user/00-erp.yaml | 7 ++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 SL/Controller/CsvImport/AdditionalBillingAddress.pm diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 06f4bc2cc..d1ff7199f 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -13,6 +13,7 @@ use SL::Helper::Flash; use SL::Locale::String; use SL::SessionFile; use SL::SessionFile::Random; +use SL::Controller::CsvImport::AdditionalBillingAddress; use SL::Controller::CsvImport::Contact; use SL::Controller::CsvImport::CustomerVendor; use SL::Controller::CsvImport::Part; @@ -307,7 +308,7 @@ sub check_auth { sub check_type { my ($self) = @_; - die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts inventories customers_vendors addresses contacts projects orders delivery_orders bank_transactions ar_transactions); + die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts inventories customers_vendors billing_addresses addresses contacts projects orders delivery_orders bank_transactions ar_transactions); $self->type($::form->{profile}->{type}); } @@ -348,6 +349,7 @@ sub render_inputs { } my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors') + : $self->type eq 'billing_addresses' ? $::locale->text('CSV import: additional billing addresses') : $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses') : $self->type eq 'contacts' ? $::locale->text('CSV import: contacts') : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services') @@ -720,6 +722,7 @@ sub init_worker { return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(@args) : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new(@args) + : $self->{type} eq 'billing_addresses' ? SL::Controller::CsvImport::AdditionalBillingAddress->new(@args) : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new(@args) : $self->{type} eq 'parts' ? SL::Controller::CsvImport::Part->new(@args) : $self->{type} eq 'inventories' ? SL::Controller::CsvImport::Inventory->new(@args) diff --git a/SL/Controller/CsvImport/AdditionalBillingAddress.pm b/SL/Controller/CsvImport/AdditionalBillingAddress.pm new file mode 100644 index 000000000..6d2d8ea80 --- /dev/null +++ b/SL/Controller/CsvImport/AdditionalBillingAddress.pm @@ -0,0 +1,90 @@ +package SL::Controller::CsvImport::AdditionalBillingAddress; + +use strict; + +use SL::Helper::Csv; + +use parent qw(SL::Controller::CsvImport::Base); + +use Rose::Object::MakeMethods::Generic +( + scalar => [ qw(table) ], +); + +sub set_profile_defaults { +}; + +sub init_class { + my ($self) = @_; + $self->class('SL::DB::AdditionalBillingAddress'); +} + +sub _hash_object { + my ($o) = @_; + return join('--', map({ s/[\s,\.\-]//g; $_ } ($o->name, $o->street))); +} + +sub check_objects { + my ($self) = @_; + + $self->controller->track_progress(phase => 'building data', progress => 0); + + my %existing_by_id_name_street = map { (_hash_object($_) => $_) } @{ $self->existing_objects }; + my $methods = $self->controller->headers->{methods}; + + my $i = 0; + my $num_data = scalar @{ $self->controller->data }; + foreach my $entry (@{ $self->controller->data }) { + $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0; + + $self->check_vc($entry, 'customer_id'); + + next if @{ $entry->{errors} }; + + my $object = $entry->{object}; + my $idx = _hash_object($object); + my $existing = $existing_by_id_name_street{$idx}; + + if (!$existing) { + $existing_by_id_name_street{$idx} = $object; + } else { + $entry->{object_to_save} = $existing; + + $existing->$_( $object->$_ ) for @{ $methods }; + + push @{ $entry->{information} }, $::locale->text('Updating existing entry in database'); + } + + } continue { + $i++; + } + + $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' }); +} + +sub setup_displayable_columns { + my ($self) = @_; + + $self->SUPER::setup_displayable_columns; + + $self->add_displayable_columns( + { name => 'default_address', description => $::locale->text('Default address flag') }, + { name => 'name', description => $::locale->text('Name') }, + { name => 'department_1', description => $::locale->text('Department 1') }, + { name => 'department_2', description => $::locale->text('Department 2') }, + { name => 'street', description => $::locale->text('Street') }, + { name => 'zipcode', description => $::locale->text('Zipcode') }, + { name => 'city', description => $::locale->text('City') }, + { name => 'country', description => $::locale->text('Country') }, + { name => 'contact', description => $::locale->text('Contact') }, + { name => 'email', description => $::locale->text('E-mail') }, + { name => 'fax', description => $::locale->text('Fax') }, + { name => 'gln', description => $::locale->text('GLN') }, + { name => 'phone', description => $::locale->text('Phone') }, + { name => 'customer_id', description => $::locale->text('Customer') }, + { name => 'customer', description => $::locale->text('Customer (name)') }, + { name => 'customernumber', description => $::locale->text('Customer Number') }, + ); +} + +1; diff --git a/SL/DB/AdditionalBillingAddress.pm b/SL/DB/AdditionalBillingAddress.pm index 940dce876..c6ffad4da 100644 --- a/SL/DB/AdditionalBillingAddress.pm +++ b/SL/DB/AdditionalBillingAddress.pm @@ -7,6 +7,24 @@ use SL::DB::Manager::AdditionalBillingAddress; __PACKAGE__->meta->initialize; +__PACKAGE__->after_save('_after_save_ensure_only_one_marked_as_default_per_customer'); + +sub _after_save_ensure_only_one_marked_as_default_per_customer { + my ($self) = @_; + + if ($self->id && $self->customer_id && $self->default_address) { + SL::DB::Manager::AdditionalBillingAddress->update_all( + set => { default_address => 0 }, + where => [ + customer_id => $self->customer_id, + '!id' => $self->id, + ], + ); + } + + return 1; +} + sub displayable_id { my $self = shift; my $text = join('; ', grep { $_ } (map({ $self->$_ } qw(name street)), diff --git a/locale/de/all b/locale/de/all index fb0257284..3125d9961 100755 --- a/locale/de/all +++ b/locale/de/all @@ -538,6 +538,7 @@ $self->{texts} = { 'CSV Export successful!' => 'CSV-Export erfolgreich!', 'CSV export' => 'CSV-Export', 'CSV export -- options' => 'CSV-Export -- Optionen', + 'CSV import: additional billing addresses' => 'CSV-Import: zusätzliche Rechnungsadressen', 'CSV import: ar transactions' => 'CSV Import: Debitorenbuchungen', 'CSV import: bank transactions' => 'CSV Import: Bankbewegungen', 'CSV import: contacts' => 'CSV-Import: Ansprechpersonen', @@ -995,6 +996,7 @@ $self->{texts} = { 'Default Transfer with services' => 'Ein- /Auslagern von Dienstleistungen über Standard-Lagerplatz', 'Default Warehouse' => 'Standard-Lager', 'Default Warehouse with ignoring onhand' => 'Standard-Lager für Auslagern ohne Prüfung auf Bestand', + 'Default address flag' => 'Standard-Adresse-Schalter', 'Default article for converting into quotations and orders' => 'Standardartikel für Konvertierung von Pflichtenheften in Angebote und Aufträge', 'Default booking group' => 'Standardbuchungsgruppe', 'Default client' => 'Standardmandant', diff --git a/menus/user/00-erp.yaml b/menus/user/00-erp.yaml index b8d9630fe..52d7b0707 100644 --- a/menus/user/00-erp.yaml +++ b/menus/user/00-erp.yaml @@ -1354,6 +1354,13 @@ params: action: CsvImport/new profile.type: contacts +- parent: system_import_csv + id: system_import_csv_additional_billing_address + name: Additional Billing Addresses + order: 250 + params: + action: CsvImport/new + profile.type: billing_addresses - parent: system_import_csv id: system_import_csv_shipto name: Shipto -- 2.20.1