+sub init_all_languages {
+ my ($self) = @_;
+
+ return SL::DB::Manager::Language->get_all;
+}
+
+sub init_all_bank_accounts {
+ my ($self) = @_;
+
+ return SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
+}
+
+sub init_payment_terms_by {
+ my ($self) = @_;
+
+ my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
+}
+
+sub init_delivery_terms_by {
+ my ($self) = @_;
+
+ my $all_delivery_terms = SL::DB::Manager::DeliveryTerm->get_all;
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_delivery_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_clone_methods {
+ {}
+}
+
+sub force_allow_columns {
+ return ();
+}
+
+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} } } );
+ my %by_gln = ( customers => { map { ( $_->gln => $_ ) } grep $_->gln, @{ $self->all_vc->{customers} } },
+ vendors => { map { ( $_->gln => $_ ) } grep $_->gln, @{ $self->all_vc->{vendors} } } );
+
+ return { id => \%by_id,
+ number => \%by_number,
+ name => \%by_name,
+ gln => \%by_gln };
+}
+
+sub init_vc_counts_by {
+ my ($self) = @_;
+
+ my $vc_counts_by = {};
+
+ $vc_counts_by->{number}->{customers}->{$_->customernumber}++ for @{ $self->all_vc->{customers} };
+ $vc_counts_by->{number}->{vendors}-> {$_->vendornumber}++ for @{ $self->all_vc->{vendors} };
+ $vc_counts_by->{name}-> {customers}->{$_->name}++ for @{ $self->all_vc->{customers} };
+ $vc_counts_by->{name}-> {vendors}-> {$_->name}++ for @{ $self->all_vc->{vendors} };
+ $vc_counts_by->{gln}-> {customers}->{$_->gln}++ for grep $_->gln, @{ $self->all_vc->{customers} };
+ $vc_counts_by->{gln}-> {vendors}-> {$_->gln}++ for grep $_->gln, @{ $self->all_vc->{vendors} };
+
+ return $vc_counts_by;
+}
+
+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 };
+ }
+
+ my $is_ambiguous;
+ if (!$entry->{object}->$id_column) {
+ my $vc;
+ if ($entry->{raw_data}->{customernumber}) {
+ $vc = $self->vc_by->{number}->{customers}->{ $entry->{raw_data}->{customernumber} };
+ if ($vc && $self->vc_counts_by->{number}->{customers}->{ $entry->{raw_data}->{customernumber} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ } elsif ($entry->{raw_data}->{vendornumber}) {
+ $vc = $self->vc_by->{number}->{vendors}->{ $entry->{raw_data}->{vendornumber} };
+ if ($vc && $self->vc_counts_by->{number}->{vendors}->{ $entry->{raw_data}->{vendornumber} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ }
+
+ $entry->{object}->$id_column($vc->id) if $vc;
+ }
+
+ if (!$entry->{object}->$id_column) {
+ my $vc;
+ if ($entry->{raw_data}->{customer}) {
+ $vc = $self->vc_by->{name}->{customers}->{ $entry->{raw_data}->{customer} };
+ if ($vc && $self->vc_counts_by->{name}->{customers}->{ $entry->{raw_data}->{customer} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ } elsif ($entry->{raw_data}->{vendor}) {
+ $vc = $self->vc_by->{name}->{vendors}->{ $entry->{raw_data}->{vendor} };
+ if ($vc && $self->vc_counts_by->{name}->{vendors}->{ $entry->{raw_data}->{vendor} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ }
+
+ $entry->{object}->$id_column($vc->id) if $vc;
+ }
+
+ if (!$entry->{object}->$id_column) {
+ my $vc;
+ if ($entry->{raw_data}->{customer_gln}) {
+ $vc = $self->vc_by->{gln}->{customers}->{ $entry->{raw_data}->{customer_gln} };
+ if ($vc && $self->vc_counts_by->{gln}->{customers}->{ $entry->{raw_data}->{customer_gln} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ } elsif ($entry->{raw_data}->{vendor_gln}) {
+ $vc = $self->vc_by->{gln}->{vendors}->{ $entry->{raw_data}->{vendor_gln} };
+ if ($vc && $self->vc_counts_by->{gln}->{vendors}->{ $entry->{raw_data}->{vendor_gln} } > 1) {
+ $vc = undef;
+ $is_ambiguous = 1;
+ }
+ }
+ $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 {
+ if ($is_ambiguous) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor is ambiguous');
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
+ }
+ }
+}
+