1 package SL::Controller::CsvImport::BaseMulti;
5 use List::MoreUtils qw(pairwise firstidx);
9 use parent qw(SL::Controller::CsvImport::Base);
11 use Rose::Object::MakeMethods::Generic
13 'scalar --get_set_init' => [ qw(cvar_configs_by cvar_columns_by) ],
17 my ($self, %params) = @_;
19 $self->test_run($params{test});
21 $self->controller->track_progress(phase => 'parsing csv', progress => 0);
23 my $profile = $self->profile;
25 $self->csv(SL::Helper::Csv->new(file => ('SCALAR' eq ref $self->file)? $self->file: $self->file->file_name,
26 encoding => $self->controller->profile->get('charset'),
28 ignore_unknown_columns => 1,
30 case_insensitive_header => 1,
31 map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
34 $self->controller->track_progress(progress => 10);
36 my $old_numberformat = $::myconfig{numberformat};
37 $::myconfig{numberformat} = $self->controller->profile->get('numberformat');
41 $self->controller->track_progress(progress => 50);
43 $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
45 return if ( !$self->csv->header || $self->csv->errors );
49 foreach my $header (@{ $self->csv->header }) {
51 my $profile = $self->csv->profile->[$i]->{profile};
52 my $row_ident = $self->csv->profile->[$i]->{row_ident};
54 my $h = { headers => [ grep { $profile->{$_} } @{ $header } ] };
55 $h->{methods} = [ map { $profile->{$_} } @{ $h->{headers} } ];
56 $h->{used} = { map { ($_ => 1) } @{ $h->{headers} } };
58 $headers->{$row_ident} = $h;
62 $self->controller->headers($headers);
66 foreach my $p (@{ $self->csv->profile }) {
67 $raw_data_headers->{ $p->{row_ident} } = { used => { }, headers => [ ] };
68 $info_headers->{ $p->{row_ident} } = { used => { }, headers => [ ] };
70 $self->controller->raw_data_headers($raw_data_headers);
71 $self->controller->info_headers($info_headers);
73 my $objects = $self->csv->get_objects;
75 $self->controller->track_progress(progress => 70);
77 my @raw_data = @{ $self->csv->get_data };
79 $self->controller->track_progress(progress => 80);
81 $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
83 $self->controller->track_progress(progress => 90);
86 if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) {
87 $self->check_std_duplicates();
88 $self->check_duplicates();
90 $self->fix_field_lengths;
92 $self->controller->track_progress(progress => 100);
94 $::myconfig{numberformat} = $old_numberformat;
97 sub init_manager_class {
101 foreach my $class (@{ $self->class }) {
102 $class =~ m/^SL::DB::(.+)/;
103 push @manager_classes, "SL::DB::Manager::" . $1;
105 $self->manager_class(\@manager_classes);
109 my ($self, $row_ident, @columns) = @_;
111 my $h = $self->controller->headers->{$row_ident};
113 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
114 $h->{used}->{$column} = 1;
115 push @{ $h->{methods} }, $column;
116 push @{ $h->{headers} }, $column;
120 sub add_info_columns {
121 my ($self, $row_ident, @columns) = @_;
123 my $h = $self->controller->info_headers->{$row_ident};
125 foreach my $column (grep { !$h->{used}->{ $_->{method} } } map { ref $_ eq 'HASH' ? $_ : { method => $_, header => $_ } } @columns) {
126 $h->{used}->{ $column->{method} } = 1;
127 push @{ $h->{methods} }, $column->{method};
128 push @{ $h->{headers} }, $column->{header};
132 sub add_raw_data_columns {
133 my ($self, $row_ident, @columns) = @_;
135 my $h = $self->controller->raw_data_headers->{$row_ident};
137 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
138 $h->{used}->{$column} = 1;
139 push @{ $h->{headers} }, $column;
143 sub add_cvar_raw_data_columns {
146 foreach my $data (@{ $self->controller->data }) {
147 my $ri = $data->{raw_data}->{datatype};
148 map { $self->add_raw_data_columns($ri, $_) if exists $data->{raw_data}->{$_} } @{ $self->cvar_columns_by->{row_ident}->{$ri} };
152 sub init_cvar_configs_by {
153 # Must be overridden by derived specialized importer classes.
157 sub init_cvar_columns_by {
161 foreach my $p (@{ $self->profile }) {
162 my $ri = $p->{row_ident};
163 $ccb->{row_ident}->{$ri} = [ map { "cvar_" . $_->name } (@{ $self->cvar_configs_by->{row_ident}->{$ri} }) ];
170 my ($self, $entry, %params) = @_;
172 return if @{ $entry->{errors} };
173 return unless $entry->{object}->can('cvars_by_config');
175 my %type_to_column = ( text => 'text_value',
176 textfield => 'text_value',
177 htmlfield => 'text_value',
178 select => 'text_value',
179 date => 'timestamp_value_as_date',
180 timestamp => 'timestamp_value_as_date',
181 number => 'number_value_as_number',
182 bool => 'bool_value' );
184 $params{sub_module} ||= '';
186 # autovivify all cvars (cvars_by_config will do that for us)
188 @cvars = @{ $entry->{object}->cvars_by_config };
190 foreach my $config (@{ $self->cvar_configs_by->{row_ident}->{$entry->{raw_data}->{datatype}} }) {
191 next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
192 my $value = $entry->{raw_data}->{ "cvar_" . $config->name };
193 my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type";
195 my $cvar = SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => $params{sub_module});
197 # replace autovivified cvar by new one
198 my $idx = firstidx { $_->config_id == $config->id } @cvars;
199 $cvars[$idx] = $cvar if -1 != $idx;
202 $entry->{object}->custom_variables(\@cvars) if @cvars;
209 foreach my $class (@{ $self->class }) {
210 eval "require " . $class;
212 my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $class->meta->primary_key_columns });
214 # TODO: exceptions for AccTransaction and Invoice wh
215 if ( $class =~ m/^SL::DB::AccTransaction/ ) {
216 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 });
217 @unwanted{keys %unwanted_acc_trans} = values %unwanted_acc_trans;
219 if ( $class =~ m/^SL::DB::Invoice/ ) {
220 # remove fields that aren't needed / shouldn't be set for ar transaction
221 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 });
222 @unwanted{keys %unwanted_ar} = values %unwanted_ar;
226 $prof{datatype} = '';
227 for my $col ($class->meta->columns) {
228 next if $unwanted{$col};
230 my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric') ? "$col\_as_number"
231 : $col->isa('Rose::DB::Object::Metadata::Column::Date') ? "$col\_as_date"
232 : $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
238 $prof{ 'cvar_' . $_->name } = '' for @{ $self->cvar_configs_by->{class}->{$class} };
240 $class =~ m/^SL::DB::(.+)/;
241 push @profile, {'profile' => \%prof, 'class' => $class, 'row_ident' => $::locale->text($1)};
247 sub add_displayable_columns {
248 my ($self, $row_ident, @columns) = @_;
250 my $dis_cols = $self->controller->displayable_columns || {};
252 my @cols = @{ $dis_cols->{$row_ident} || [] };
253 my %ex_col_map = map { $_->{name} => $_ } @cols;
255 foreach my $column (@columns) {
256 if ($ex_col_map{ $column->{name} }) {
257 @{ $ex_col_map{ $column->{name} } }{ keys %{ $column } } = @{ $column }{ keys %{ $column } };
263 my $by_name_datatype_first = sub { 'datatype' eq $a->{name} ? -1 :
264 'datatype' eq $b->{name} ? 1 :
265 $a->{name} cmp $b->{name} };
266 $dis_cols->{$row_ident} = [ sort $by_name_datatype_first @cols ];
268 $self->controller->displayable_columns($dis_cols);
271 sub setup_displayable_columns {
274 foreach my $p (@{ $self->profile }) {
275 $self->add_displayable_columns($p->{row_ident}, map { { name => $_ } } keys %{ $p->{profile} });
279 sub add_cvar_columns_to_displayable_columns {
280 my ($self, $row_ident) = @_;
282 $self->add_displayable_columns($row_ident,
283 map { { name => 'cvar_' . $_->name,
284 description => $::locale->text('#1 (custom variable)', $_->description) } }
285 @{ $self->cvar_configs_by->{row_ident}->{$row_ident} });
291 my %field_lengths_by_ri = ();
293 foreach my $p (@{ $self->profile }) {
294 my %field_lengths = map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{ $p->{class}->meta->columns };
295 $field_lengths_by_ri{ $p->{row_ident} } = \%field_lengths;
298 return %field_lengths_by_ri;
301 sub fix_field_lengths {
304 my %field_lengths_by_ri = $self->field_lengths;
305 foreach my $entry (@{ $self->controller->data }) {
306 next unless defined $entry->{errors} && @{ $entry->{errors} };
307 my %field_lengths = %{ $field_lengths_by_ri{ $entry->{raw_data}->{datatype} } };
308 map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
314 sub is_multiplexed { 1 }