1 package SL::Controller::CsvImport::Part;
7 use SL::DB::Buchungsgruppe;
8 use SL::DB::PartsGroup;
9 use SL::DB::PaymentTerm;
10 use SL::DB::PriceFactor;
13 use parent qw(SL::Controller::CsvImport::Base);
15 use Rose::Object::MakeMethods::Generic
17 scalar => [ qw(table) ],
18 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by payment_terms_by packing_types_by partsgroups_by) ],
23 $self->class('SL::DB::Part');
29 my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
30 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
33 sub init_price_factors_by {
36 my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
37 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
40 sub init_payment_terms_by {
43 my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
44 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
47 sub init_packing_types_by {
50 my $all_packing_types = SL::DB::Manager::PackingType->get_all;
51 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
54 sub init_partsgroups_by {
57 my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
58 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
64 my $all_units = SL::DB::Manager::Unit->get_all;
65 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
71 my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
72 partnumber => { part => { },
75 foreach my $part (@{ $self->existing_objects }) {
76 next if $part->assembly;
77 $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
86 return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
87 sellprice_places sellprice_adjustment sellprice_adjustment_type
88 shoparticle_if_missing parts_type) };
94 return unless @{ $self->controller->data };
96 $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
97 $self->add_columns(qw(buchungsgruppen_id unit));
98 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{$_} } qw (price_factor payment packing_type partsgroup));
100 foreach my $entry (@{ $self->controller->data }) {
101 my $object = $entry->{object};
102 my $raw_data = $entry->{raw_data};
104 next unless $self->check_buchungsgruppe($entry);
105 next unless $self->check_type($entry);
106 next unless $self->check_unit($entry);
107 next unless $self->check_price_factor($entry);
108 next unless $self->check_payment($entry);
109 next unless $self->check_packing_type($entry);
110 next unless $self->check_partsgroup($entry);
111 $self->check_existing($entry);
112 $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
113 $self->handle_shoparticle($entry);
114 $self->set_various_fields($entry);
118 sub check_duplicates {
119 my ($self, %params) = @_;
123 my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
124 my $name_maker = sub { return $normalizer->($_[0]->shiptoname) . '--' . $normalizer->($_[0]->shiptostreet) };
127 if ('check_db' eq $self->controller->profile->get('duplicates')) {
128 foreach my $type (qw(customers vendors)) {
129 foreach my $vc (@{ $self->all_vc->{$type} }) {
130 $by_id_and_name{ $vc->id } = { map { ( $name_maker->($_) => 'db' ) } @{ $vc->shipto } };
135 foreach my $entry (@{ $self->controller->data }) {
136 next if @{ $entry->{errors} };
138 my $name = $name_maker->($entry->{object});
140 $by_id_and_name{ $entry->{vc}->id } ||= { };
141 if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) {
142 $by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv';
145 push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
150 sub check_buchungsgruppe {
151 my ($self, $entry) = @_;
153 my $object = $entry->{object};
155 # Check Buchungsgruppe
157 # Store and verify default ID.
158 my $default_id = $self->settings->{default_buchungsgruppe};
159 $default_id = undef unless $self->bg_by->{id}->{ $default_id };
161 # 1. Use default ID if enforced.
162 $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
164 # 2. Use supplied ID if valid
165 $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
167 # 3. Look up name if supplied.
168 if (!$object->buchungsgruppen_id) {
169 my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
170 $object->buchungsgruppen_id($bg->id) if $bg;
173 # 4. Use default ID if not valid.
174 $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
176 return 1 if $object->buchungsgruppen_id;
178 push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
183 my ($self, $entry) = @_;
185 my $object = $entry->{object};
187 my $entry->{part} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
189 if ($self->settings->{article_number_policy} eq 'update_prices') {
190 if ($entry->{part}) {
191 map { $object->$_( $entry->{part}->$_ ) } qw(sellprice listprice lastcost);
192 $entry->{priceupdate} = 1;
196 $object->partnumber('####') if $entry->{part};
201 my ($self, $entry) = @_;
203 foreach my $column (qw(sellprice listprice lastcost)) {
204 next unless $self->controller->headers->{used}->{ $column };
206 my $adjustment = $self->settings->{sellprice_adjustment};
207 my $value = $entry->{object}->$column;
209 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
210 $entry->{object}->$column($::form->round_amount($self->settings->{sellprice_places}, $value));
214 sub handle_shoparticle {
215 my ($self, $entry) = @_;
217 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
221 my ($self, $entry) = @_;
223 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
224 die "Program logic error" if !$bg;
226 my $type = $self->settings->{parts_type};
227 if ($type eq 'mixed') {
228 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
229 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
233 $entry->{object}->income_accno_id( $bg->income_accno_id_0 );
234 $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
236 if ($type eq 'part') {
237 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
239 } elsif ($type ne 'service') {
240 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
247 sub check_price_factor {
248 my ($self, $entry) = @_;
250 my $object = $entry->{object};
252 # Check whether or not price factor ID is valid.
253 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
254 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
258 # Map name to ID if given.
259 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
260 my $id = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
263 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
267 $object->price_factor_id($id);
274 my ($self, $entry) = @_;
276 my $object = $entry->{object};
278 # Check whether or not payment ID is valid.
279 if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
280 push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
284 # Map name to ID if given.
285 if (!$object->payment_id && $entry->{raw_data}->{payment}) {
286 my $id = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
289 push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
293 $object->payment_id($id);
299 sub check_packing_type {
300 my ($self, $entry) = @_;
302 my $object = $entry->{object};
304 # Check whether or not packing type ID is valid.
305 if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
306 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
310 # Map name to ID if given.
311 if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
312 my $id = $self->packing_type_by->{description}->{ $entry->{raw_data}->{packing_type} };
315 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
319 $object->packing_type_id($id);
325 sub check_partsgroup {
326 my ($self, $entry) = @_;
328 my $object = $entry->{object};
330 # Check whether or not part group ID is valid.
331 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
332 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
336 # Map name to ID if given.
337 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
338 my $id = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
341 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
345 $object->partsgroup_id($id);
352 my ($self, $entry) = @_;
354 my $object = $entry->{object};
356 # Check whether or unit is valid.
357 if (!$self->units_by->{name}->{ $object->unit }) {
358 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
365 sub set_various_fields {
366 my ($self, $entry) = @_;
368 $entry->{object}->priceupdate(DateTime->now_local);
374 my $profile = $self->SUPER::init_profile;
375 delete $profile->{type};
377 $::lxdebug->dump(0, "prof", $profile);
384 # priceupdate aus Profil raus
385 # CVARs ins Profil rein