1 package SL::Controller::CsvImport::Base;
5 use List::MoreUtils qw(pairwise);
10 use SL::DB::PaymentTerm;
14 use parent qw(Rose::Object);
16 use Rose::Object::MakeMethods::Generic
18 scalar => [ qw(controller file csv save_with_cascade) ],
19 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ],
25 my $profile = $self->profile;
26 $self->csv(SL::Helper::Csv->new(file => $self->file->file_name,
27 encoding => $self->controller->profile->get('charset'),
28 class => $self->class,
30 ignore_unknown_columns => 1,
32 case_insensitive_header => 1,
33 map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
36 my $old_numberformat = $::myconfig{numberformat};
37 $::myconfig{numberformat} = $self->controller->profile->get('numberformat');
41 $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
43 return if ( !$self->csv->header || $self->csv->errors );
45 my $headers = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
46 $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
47 $headers->{used} = { map { ($_ => 1) } @{ $headers->{headers} } };
48 $self->controller->headers($headers);
49 $self->controller->raw_data_headers({ used => { }, headers => [ ] });
50 $self->controller->info_headers({ used => { }, headers => [ ] });
52 my @objects = $self->csv->get_objects;
53 my @raw_data = @{ $self->csv->get_data };
54 $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
57 if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) {
58 $self->check_std_duplicates();
59 $self->check_duplicates();
61 $self->fix_field_lengths;
63 $::myconfig{numberformat} = $old_numberformat;
67 my ($self, @columns) = @_;
69 my $h = $self->controller->headers;
71 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
72 $h->{used}->{$column} = 1;
73 push @{ $h->{methods} }, $column;
74 push @{ $h->{headers} }, $column;
78 sub add_info_columns {
79 my ($self, @columns) = @_;
81 my $h = $self->controller->info_headers;
83 foreach my $column (grep { !$h->{used}->{ $_->{method} } } map { ref $_ eq 'HASH' ? $_ : { method => $_, header => $_ } } @columns) {
84 $h->{used}->{ $column->{method} } = 1;
85 push @{ $h->{methods} }, $column->{method};
86 push @{ $h->{headers} }, $column->{header};
90 sub add_raw_data_columns {
91 my ($self, @columns) = @_;
93 my $h = $self->controller->raw_data_headers;
95 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
96 $h->{used}->{$column} = 1;
97 push @{ $h->{headers} }, $column;
101 sub add_cvar_raw_data_columns {
104 map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns };
107 sub init_cvar_columns {
110 return [ map { "cvar_" . $_->name } (@{ $self->all_cvar_configs }) ];
113 sub init_all_languages {
116 return SL::DB::Manager::Language->get_all;
119 sub init_payment_terms_by {
122 my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
123 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
129 return { customers => SL::DB::Manager::Customer->get_all,
130 vendors => SL::DB::Manager::Vendor->get_all };
136 my %by_id = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} };
137 my %by_number = ( customers => { map { ( $_->customernumber => $_ ) } @{ $self->all_vc->{customers} } },
138 vendors => { map { ( $_->vendornumber => $_ ) } @{ $self->all_vc->{vendors} } } );
139 my %by_name = ( customers => { map { ( $_->name => $_ ) } @{ $self->all_vc->{customers} } },
140 vendors => { map { ( $_->name => $_ ) } @{ $self->all_vc->{vendors} } } );
142 return { id => \%by_id,
143 number => \%by_number,
144 name => \%by_name, };
148 my ($self, $entry, $id_column) = @_;
150 if ($entry->{object}->$id_column) {
151 $entry->{object}->$id_column(undef) if !$self->vc_by->{id}->{ $entry->{object}->$id_column };
154 if (!$entry->{object}->$id_column) {
155 my $vc = $self->vc_by->{number}->{customers}->{ $entry->{raw_data}->{customernumber} }
156 || $self->vc_by->{number}->{vendors}->{ $entry->{raw_data}->{vendornumber} };
157 $entry->{object}->$id_column($vc->id) if $vc;
160 if (!$entry->{object}->$id_column) {
161 my $vc = $self->vc_by->{name}->{customers}->{ $entry->{raw_data}->{customer} }
162 || $self->vc_by->{name}->{vendors}->{ $entry->{raw_data}->{vendor} };
163 $entry->{object}->$id_column($vc->id) if $vc;
166 if ($entry->{object}->$id_column) {
167 $entry->{info_data}->{vc_name} = $self->vc_by->{id}->{ $entry->{object}->$id_column }->name;
169 push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
174 my ($self, $entry) = @_;
176 return unless $self->can('all_cvar_configs');
178 my %type_to_column = ( text => 'text_value',
179 textfield => 'text_value',
180 select => 'text_value',
181 date => 'timestamp_value_as_date',
182 timestamp => 'timestamp_value_as_date',
183 number => 'number_value_as_number',
184 bool => 'bool_value' );
187 foreach my $config (@{ $self->all_cvar_configs }) {
188 next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
189 my $value = $entry->{raw_data}->{ "cvar_" . $config->name };
190 my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type";
192 push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => '');
195 $entry->{object}->custom_variables(\@cvars);
201 eval "require " . $self->class;
203 my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns });
205 for my $col ($self->class->meta->columns) {
206 next if $unwanted{$col};
208 my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric') ? "$col\_as_number"
209 : $col->isa('Rose::DB::Object::Metadata::Column::Date') ? "$col\_as_date"
210 : $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
213 $profile{$col} = $name;
216 if ($self->can('all_cvar_configs')) {
217 for (@{ $self->all_cvar_configs }) {
218 $profile{ 'cvar_' . $_->name } = '';
222 $self->profile(\%profile);
225 sub add_displayable_columns {
226 my ($self, @columns) = @_;
228 my @cols = @{ $self->controller->displayable_columns || [] };
229 my %ex_col_map = map { $_->{name} => $_ } @cols;
231 foreach my $column (@columns) {
232 if ($ex_col_map{ $column->{name} }) {
233 @{ $ex_col_map{ $column->{name} } }{ keys %{ $column } } = @{ $column }{ keys %{ $column } };
239 $self->controller->displayable_columns([ sort { $a->{name} cmp $b->{name} } @cols ]);
242 sub setup_displayable_columns {
245 $self->add_displayable_columns(map { { name => $_ } } keys %{ $self->profile });
248 sub add_cvar_columns_to_displayable_columns {
251 return unless $self->can('all_cvar_configs');
253 $self->add_displayable_columns(map { { name => 'cvar_' . $_->name,
254 description => $::locale->text('#1 (custom variable)', $_->description) } }
255 @{ $self->all_cvar_configs });
258 sub init_existing_objects {
261 eval "require " . $self->class;
262 $self->existing_objects($self->manager_class->get_all);
269 sub init_manager_class {
272 $self->class =~ m/^SL::DB::(.+)/;
273 $self->manager_class("SL::DB::Manager::" . $1);
279 sub check_duplicates {
282 sub check_std_duplicates {
287 my $all_fields = $self->get_duplicate_check_fields();
289 foreach my $key (keys(%{ $all_fields })) {
290 if ( $self->controller->profile->get('duplicates_'. $key) && (!exists($all_fields->{$key}->{std_check}) || $all_fields->{$key}->{std_check} ) ) {
291 $duplicates->{$key} = {};
295 my @duplicates_keys = keys(%{ $duplicates });
297 if ( !scalar(@duplicates_keys) ) {
301 if ( $self->controller->profile->get('duplicates') eq 'check_db' ) {
302 foreach my $object (@{ $self->existing_objects }) {
303 foreach my $key (@duplicates_keys) {
304 my $value = exists($all_fields->{$key}->{maker}) ? $all_fields->{$key}->{maker}->($object, $self) : $object->$key;
305 $duplicates->{$key}->{$value} = 'db';
310 foreach my $entry (@{ $self->controller->data }) {
311 if ( @{ $entry->{errors} } ) {
315 my $object = $entry->{object};
317 foreach my $key (@duplicates_keys) {
318 my $value = exists($all_fields->{$key}->{maker}) ? $all_fields->{$key}->{maker}->($object, $self) : $object->$key;
320 if ( exists($duplicates->{$key}->{$value}) ) {
321 push(@{ $entry->{errors} }, $duplicates->{$key}->{$value} eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file'));
324 $duplicates->{$key}->{$value} = 'csv';
332 sub get_duplicate_check_fields {
337 my ($self, $entry) = @_;
339 my $object = $entry->{object};
341 # Check whether or not payment ID is valid.
342 if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
343 push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
347 # Map name to ID if given.
348 if (!$object->payment_id && $entry->{raw_data}->{payment}) {
349 my $terms = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
352 push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
356 $object->payment_id($terms->id);
363 my ($self, %params) = @_;
365 my $data = $params{data} || $self->controller->data;
367 foreach my $entry (@{ $data }) {
368 next if @{ $entry->{errors} };
370 my $object = $entry->{object_to_save} || $entry->{object};
372 if ( !$object->save(cascade => !!$self->save_with_cascade()) ) {
373 push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
375 $self->controller->num_imported($self->controller->num_imported + 1);
384 sub fix_field_lengths {
387 my %field_lengths = $self->field_lengths;
388 foreach my $entry (@{ $self->controller->data }) {
389 next unless @{ $entry->{errors} };
390 map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;