CsvImport Aufträge: Variable besser benannt und Kommentar berichtigt.
[kivitendo-erp.git] / SL / Controller / CsvImport / Part.pm
index 09430c6..ef7e238 100644 (file)
@@ -39,7 +39,7 @@ sub set_profile_defaults {
                        sellprice_adjustment_type => 'percent',
                        article_number_policy     => 'update_prices',
                        shoparticle_if_missing    => '0',
-                       parts_type                => 'part',
+                       part_type                 => 'part',
                        default_buchungsgruppe    => ($bugru ? $bugru->id : undef),
                        apply_buchungsgruppe      => 'all',
                       );
@@ -97,12 +97,12 @@ sub init_warehouses_by {
 sub init_parts_by {
   my ($self) = @_;
 
-#  my $parts_by = { id         => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
+#  my $parts_by = { id         => { map { ( $_->id => $_ ) } grep { !$_->part_type = 'assembly' } @{ $self->existing_objects } },
 #                   partnumber => { part    => { },
 #                                   service => { } } };
 #
 #  foreach my $part (@{ $self->existing_objects }) {
-#    next if $part->assembly;
+#    next if $part->part_type eq 'assembly';
 #    $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
 #  }
 
@@ -118,7 +118,7 @@ sub init_parts_by {
 sub init_all_pricegroups {
   my ($self) = @_;
 
-  return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
+  return SL::DB::Manager::Pricegroup->get_all_sorted;
 }
 
 sub init_settings {
@@ -126,7 +126,7 @@ sub init_settings {
 
   return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
                                                                     sellprice_places sellprice_adjustment sellprice_adjustment_type
-                                                                    shoparticle_if_missing parts_type default_unit) };
+                                                                    shoparticle_if_missing part_type default_unit) };
 }
 
 sub init_all_cvar_configs {
@@ -156,7 +156,7 @@ sub check_objects {
     $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
 
     $self->check_buchungsgruppe($entry);
-    $self->check_type($entry);
+    $self->check_part_type($entry);
     $self->check_unit($entry);
     $self->check_price_factor($entry);
     $self->check_payment($entry);
@@ -173,7 +173,8 @@ sub check_objects {
   } continue {
     $i++;
   }
-  $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
+
+  $self->add_columns(qw(part_type)) if $self->settings->{part_type} eq 'mixed';
   $self->add_columns(qw(buchungsgruppen_id unit));
   $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment partsgroup warehouse bin));
   $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
@@ -223,7 +224,7 @@ sub check_buchungsgruppe {
   $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
 
   # 3. Look up name if supplied.
-  if (!$object->buchungsgruppen_id) {
+  if (!$object->buchungsgruppen_id && $entry->{raw_data}->{buchungsgruppe}) {
     my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
     $object->buchungsgruppen_id($bg->id) if $bg;
   }
@@ -272,13 +273,13 @@ sub check_existing {
   }
 
   if ($entry->{part}) {
-    if ($entry->{part}->type ne $object->type ) {
+    if ($entry->{part}->part_type ne $object->part_type ) {
       push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database with different type'));
       return;
     }
-    if ( $entry->{part}->unit ne $object->unit || $entry->{part}->inventory_accno_id != $object->inventory_accno_id ) {
+    if ( $entry->{part}->unit ne $object->unit ) {
       if ( $entry->{part}->onhand != 0 || $self->_part_is_used($entry->{part})) {
-        push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry with different unit or inventory_accno_id'));
+        push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry with different unit'));
         return;
       }
     }
@@ -403,16 +404,21 @@ sub handle_shoparticle {
   $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
 }
 
-sub check_type {
+sub check_part_type {
   my ($self, $entry) = @_;
 
-  my $type = $self->settings->{parts_type};
+  # TODO: assemblies or assortments can't be imported
+
+  # my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
+  # $bg  ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1); # does this case ever occur?
 
-  if ($type eq 'mixed' && $entry->{raw_data}->{type}) {
-    $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
-          : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
-          : $entry->{raw_data}->{type} =~ m/^a/i ? 'assembly'
-          :                                        undef;
+  my $part_type = $self->settings->{part_type};
+  if ($part_type eq 'mixed') {
+    $part_type = $entry->{raw_data}->{part_type} =~ m/^p/i ? 'part'
+               : $entry->{raw_data}->{part_type} =~ m/^s/i ? 'service'
+               : $entry->{raw_data}->{part_type} =~ m/^assem/i ? 'assembly'
+               : $entry->{raw_data}->{part_type} =~ m/^assor/i ? 'assortment'
+               : undef;
   }
 
   # when saving income_accno_id or expense_accno_id use ids from the selected
@@ -421,32 +427,23 @@ sub check_type {
   # not all be set.
   # Only use existing bg
 
-  my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
+  # $entry->{object}->income_accno_id( $bg->income_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
 
-  # if not set there is an error occurred in check_buchungsgruppe()
-  # but if the part exists the new values for accno are ignored
+  # if ($part_type eq 'part' || $part_type eq 'service') {
+  #   $entry->{object}->expense_accno_id( $bg->expense_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
+  # }
 
-  if ( $bg ) {
-    $entry->{object}->income_accno_id( $bg->income_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
-    $self->clone_methods->{income_accno_id} = 1;
+  # if ($part_type eq 'part') {
+  #   $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
+  # }
 
-    if ($type eq 'part' || $type eq 'service') {
-      $entry->{object}->expense_accno_id( $bg->expense_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
-      $self->clone_methods->{expense_accno_id} = 1;
-    }
+  if (none { $_ eq $part_type } qw(part service assembly assortment)) {
+    push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
+    return 0;
   }
 
-  if ($type eq 'part') {
-    if ( $bg ) {
-      $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
-    }
-    else {
-      #use an existent bg
-      $entry->{object}->inventory_accno_id( SL::DB::Manager::Buchungsgruppe->get_first->id );
-    }
-  } elsif ($type eq 'assembly') {
-      $entry->{object}->assembly(1);
-  }
+  $entry->{object}->part_type($part_type);
+
   return 1;
 }
 
@@ -675,7 +672,7 @@ sub init_profile {
   my ($self) = @_;
 
   my $profile = $self->SUPER::init_profile;
-  delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
+  delete @{$profile}{qw(bom makemodel priceupdate stockable type)};
 
   $profile->{"pricegroup_$_"} = '' for 1 .. scalar @{ $_[0]->all_pricegroups };
 
@@ -700,8 +697,7 @@ sub setup_displayable_columns {
   $self->SUPER::setup_displayable_columns;
   $self->add_cvar_columns_to_displayable_columns;
 
-  $self->add_displayable_columns({ name => 'assembly',           description => $::locale->text('assembly')                                             },
-                                 { name => 'bin_id',             description => $::locale->text('Bin (database ID)')                                    },
+  $self->add_displayable_columns({ name => 'bin_id',             description => $::locale->text('Bin (database ID)')                                    },
                                  { name => 'bin',                description => $::locale->text('Bin (name)')                                           },
                                  { name => 'buchungsgruppen_id', description => $::locale->text('Booking group (database ID)')                         },
                                  { name => 'buchungsgruppe',     description => $::locale->text('Booking group (name)')                                },
@@ -712,7 +708,6 @@ sub setup_displayable_columns {
                                  { name => 'gv',                 description => $::locale->text('Business Volume')                                      },
                                  { name => 'has_sernumber',      description => $::locale->text('Has serial number')                                    },
                                  { name => 'image',              description => $::locale->text('Image')                                                },
-                                 { name => 'inventory_accno_id', description => $::locale->text('part')                                                 },
                                  { name => 'lastcost',           description => $::locale->text('Last Cost')                                            },
                                  { name => 'listprice',          description => $::locale->text('List Price')                                           },
                                  { name => 'make_X',             description => $::locale->text('Make (vendor\'s database ID, number or name; with X being a number)') . ' [1]' },