+sub add_cvar_raw_data_columns {
+ my ($self) = @_;
+
+ map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns };
+}
+
+sub init_cvar_columns {
+ my ($self) = @_;
+
+ return [ map { "cvar_" . $_->name } (@{ $self->all_cvar_configs }) ];
+}
+
+sub init_all_languages {
+ my ($self) = @_;
+
+ return SL::DB::Manager::Language->get_all;
+}
+
+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_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) = @_;
+
+ return unless $self->can('all_cvar_configs');
+
+ my %type_to_column = ( text => 'text_value',
+ textfield => 'text_value',
+ select => 'text_value',
+ date => 'timestamp_value_as_date',
+ timestamp => 'timestamp_value_as_date',
+ number => 'number_value_as_number',
+ bool => 'bool_value' );
+
+ my @cvars;
+ foreach my $config (@{ $self->all_cvar_configs }) {
+ next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
+ my $value = $entry->{raw_data}->{ "cvar_" . $config->name };
+ my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type";
+
+ push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value);
+ }
+
+ $entry->{object}->custom_variables(\@cvars);
+}
+