use strict;
-use List::MoreUtils qw(pairwise);
+use List::MoreUtils qw(pairwise firstidx);
use SL::Helper::Csv;
my $profile = $self->profile;
- $self->csv(SL::Helper::Csv->new(file => $self->file->file_name,
+ $self->csv(SL::Helper::Csv->new(file => ('SCALAR' eq ref $self->file)? $self->file: $self->file->file_name,
encoding => $self->controller->profile->get('charset'),
profile => $profile,
ignore_unknown_columns => 1,
$self->controller->raw_data_headers($raw_data_headers);
$self->controller->info_headers($info_headers);
- my @objects = $self->csv->get_objects;
+ my $objects = $self->csv->get_objects;
$self->controller->track_progress(progress => 70);
$self->controller->track_progress(progress => 80);
- $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
+ $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
$self->controller->track_progress(progress => 90);
$::myconfig{numberformat} = $old_numberformat;
}
+sub init_manager_class {
+ my ($self) = @_;
+
+ my @manager_classes;
+ foreach my $class (@{ $self->class }) {
+ $class =~ m/^SL::DB::(.+)/;
+ push @manager_classes, "SL::DB::Manager::" . $1;
+ }
+ $self->manager_class(\@manager_classes);
+}
+
sub add_columns {
my ($self, $row_ident, @columns) = @_;
my $ri = $p->{row_ident};
$ccb->{row_ident}->{$ri} = [ map { "cvar_" . $_->name } (@{ $self->cvar_configs_by->{row_ident}->{$ri} }) ];
}
-
+
return $ccb;
}
sub handle_cvars {
my ($self, $entry, %params) = @_;
+ return if @{ $entry->{errors} };
+
my %type_to_column = ( text => 'text_value',
textfield => 'text_value',
select => 'text_value',
bool => 'bool_value' );
$params{sub_module} ||= '';
+
+ # autovivify all cvars (cvars_by_config will do that for us)
my @cvars;
+ @cvars = @{ $entry->{object}->cvars_by_config } if $entry->{object}->can('cvars_by_config');
+
foreach my $config (@{ $self->cvar_configs_by->{row_ident}->{$entry->{raw_data}->{datatype}} }) {
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, sub_module => $params{sub_module});
+ my $cvar = SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => $params{sub_module});
+
+ # replace autovivified cvar by new one
+ my $idx = firstidx { $_->config_id == $config->id } @cvars;
+ $cvars[$idx] = $cvar if -1 != $idx;
}
$entry->{object}->custom_variables(\@cvars) if @cvars;
eval "require " . $class;
my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $class->meta->primary_key_columns });
+
+ # TODO: exceptions for AccTransaction and Invoice wh
+ if ( $class =~ m/^SL::DB::AccTransaction/ ) {
+ my %unwanted_acc_trans = map { ( $_ => 1 ) } (qw(acc_trans_id trans_id cleared fx_transaction ob_transaction cb_transaction itime mtime chart_link tax_id description gldate memo source transdate), map { $_->name } @{ $class->meta->primary_key_columns });
+ @unwanted{keys %unwanted_acc_trans} = values %unwanted_acc_trans;
+ };
+ if ( $class =~ m/^SL::DB::Invoice/ ) {
+ # remove fields that aren't needed / shouldn't be set for ar transaction
+ my %unwanted_ar = map { ( $_ => 1 ) } (qw(closed currency currency_id datepaid dunning_config_id gldate invnumber_for_credit_note invoice marge_percent marge_total amount netamount paid shippingpoint shipto_id shipvia storno storno_id type cp_id), map { $_->name } @{ $class->meta->primary_key_columns });
+ @unwanted{keys %unwanted_ar} = values %unwanted_ar;
+ };
+
my %prof;
$prof{datatype} = '';
for my $col ($class->meta->columns) {
my %field_lengths_by_ri = $self->field_lengths;
foreach my $entry (@{ $self->controller->data }) {
- next unless @{ $entry->{errors} };
+ next unless defined $entry->{errors} && @{ $entry->{errors} };
my %field_lengths = %{ $field_lengths_by_ri{ $entry->{raw_data}->{datatype} } };
map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
}
sub is_multiplexed { 1 }
1;
-