Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / SL / Controller / CsvImport / Part.pm
index 1181c73..387bdb5 100644 (file)
@@ -20,7 +20,7 @@ use parent qw(SL::Controller::CsvImport::Base);
 use Rose::Object::MakeMethods::Generic
 (
  scalar                  => [ qw(table makemodel_columns) ],
- 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by packing_types_by partsgroups_by
+ 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by partsgroups_by
                                  translation_columns all_pricegroups) ],
 );
 
@@ -43,13 +43,6 @@ sub init_price_factors_by {
   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
 }
 
-sub init_packing_types_by {
-  my ($self) = @_;
-
-  my $all_packing_types = SL::DB::Manager::PackingType->get_all;
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
-}
-
 sub init_partsgroups_by {
   my ($self) = @_;
 
@@ -90,7 +83,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) };
+                                                                    shoparticle_if_missing parts_type default_unit) };
 }
 
 sub init_all_cvar_configs {
@@ -118,7 +111,6 @@ sub check_objects {
     $self->check_unit($entry);
     $self->check_price_factor($entry);
     $self->check_payment($entry);
-    $self->check_packing_type($entry);
     $self->check_partsgroup($entry);
     $self->handle_pricegroups($entry);
     $self->check_existing($entry) unless @{ $entry->{errors} };
@@ -132,7 +124,7 @@ sub check_objects {
 
   $self->add_columns(qw(type)) if $self->settings->{parts_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 packing_type partsgroup));
+  $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment partsgroup));
   $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
   $self->add_cvar_raw_data_columns;
   map { $self->add_raw_data_columns("pricegroup_${_}") } (1..scalar(@{ $self->all_pricegroups }));
@@ -206,7 +198,12 @@ sub check_existing {
 
   if ($self->settings->{article_number_policy} eq 'update_prices') {
     if ($entry->{part}) {
-      map { $entry->{part}->$_( $object->$_ ) } qw(sellprice listprice lastcost min_sellprice prices);
+      map { $entry->{part}->$_( $object->$_ ) } qw(sellprice listprice lastcost);
+
+      # merge prices
+      my %prices_by_pricegroup_id = map { $_->pricegroup->id => $_ } $entry->{part}->prices, $object->prices;
+      $entry->{part}->prices(grep { $_ } map { $prices_by_pricegroup_id{$_->id} } @{ $self->all_pricegroups });
+
       push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
       $entry->{object_to_save} = $entry->{part};
     }
@@ -289,32 +286,6 @@ sub check_price_factor {
   return 1;
 }
 
-sub check_packing_type {
-  my ($self, $entry) = @_;
-
-  my $object = $entry->{object};
-
-  # Check whether or not packing type ID is valid.
-  if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
-    push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
-    return 0;
-  }
-
-  # Map name to ID if given.
-  if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
-    my $type = $self->packing_types_by->{description}->{ $entry->{raw_data}->{packing_type} };
-
-    if (!$type) {
-      push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
-      return 0;
-    }
-
-    $object->packing_type_id($type->id);
-  }
-
-  return 1;
-}
-
 sub check_partsgroup {
   my ($self, $entry) = @_;
 
@@ -346,6 +317,8 @@ sub check_unit {
 
   my $object = $entry->{object};
 
+  $object->unit($self->settings->{default_unit}) unless $object->unit;
+
   # Check whether or unit is valid.
   if (!$self->units_by->{name}->{ $object->unit }) {
     push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
@@ -446,43 +419,39 @@ sub setup_displayable_columns {
   $self->SUPER::setup_displayable_columns;
   $self->add_cvar_columns_to_displayable_columns;
 
-  $self->add_displayable_columns({ name => 'bin',                description => $::locale->text('Bin')                           },
-                                 { name => 'binding_max_qty',    description => $::locale->text('Binding Max Qty')               },
-                                 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)')  },
-                                 { name => 'buchungsgruppe',     description => $::locale->text('Buchungsgruppe (name)')         },
-                                 { name => 'description',        description => $::locale->text('Description')                   },
-                                 { name => 'drawing',            description => $::locale->text('Drawing')                       },
-                                 { name => 'ean',                description => $::locale->text('EAN')                           },
-                                 { name => 'formel',             description => $::locale->text('Formula')                       },
-                                 { name => 'gv',                 description => $::locale->text('Business Volume')               },
-                                 { name => 'has_sernumber',      description => $::locale->text('Has serial number')             },
-                                 { name => 'image',              description => $::locale->text('Image')                         },
-                                 { name => 'lastcost',           description => $::locale->text('Last Cost')                     },
-                                 { name => 'listprice',          description => $::locale->text('List Price')                    },
-                                 { name => 'make_X',             description => $::locale->text('Make (with X being a number)')  },
-                                 { name => 'microfiche',         description => $::locale->text('Microfiche')                    },
-                                 { name => 'min_sellprice',      description => $::locale->text('Minimum Sell Price')            },
-                                 { name => 'model_X',            description => $::locale->text('Model (with X being a number)') },
-                                 { name => 'not_discountable',   description => $::locale->text('Not Discountable')              },
-                                 { name => 'notes',              description => $::locale->text('Notes')                         },
-                                 { name => 'obsolete',           description => $::locale->text('Obsolete')                      },
-                                 { name => 'onhand',             description => $::locale->text('On Hand')                       },
-                                 { name => 'packing_type_id',    description => $::locale->text('Packing type (database ID)')    },
-                                 { name => 'packing_type',       description => $::locale->text('Packing type (name)')           },
-                                 { name => 'partnumber',         description => $::locale->text('Part Number')                   },
-                                 { name => 'partsgroup_id',      description => $::locale->text('Partsgroup (database ID)')      },
-                                 { name => 'partsgroup',         description => $::locale->text('Partsgroup (name)')             },
-                                 { name => 'payment_id',         description => $::locale->text('Payment terms (database ID)')   },
-                                 { name => 'payment',            description => $::locale->text('Payment terms (name)')          },
-                                 { name => 'price_factor_id',    description => $::locale->text('Price factor (database ID)')    },
-                                 { name => 'price_factor',       description => $::locale->text('Price factor (name)')           },
-                                 { name => 'rop',                description => $::locale->text('ROP')                           },
-                                 { name => 'sellprice',          description => $::locale->text('Sellprice')                     },
-                                 { name => 'shop',               description => $::locale->text('Shopartikel')                   },
-                                 { name => 'type',               description => $::locale->text('Article type (see below)')      },
-                                 { name => 'unit',               description => $::locale->text('Unit')                          },
-                                 { name => 've',                 description => $::locale->text('Verrechnungseinheit')           },
-                                 { name => 'weight',             description => $::locale->text('Weight')                        },
+  $self->add_displayable_columns({ name => 'bin',                description => $::locale->text('Bin')                                                  },
+                                 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)')                         },
+                                 { name => 'buchungsgruppe',     description => $::locale->text('Buchungsgruppe (name)')                                },
+                                 { name => 'description',        description => $::locale->text('Description')                                          },
+                                 { name => 'drawing',            description => $::locale->text('Drawing')                                              },
+                                 { name => 'ean',                description => $::locale->text('EAN')                                                  },
+                                 { name => 'formel',             description => $::locale->text('Formula')                                              },
+                                 { name => 'gv',                 description => $::locale->text('Business Volume')                                      },
+                                 { name => 'has_sernumber',      description => $::locale->text('Has serial number')                                    },
+                                 { name => 'image',              description => $::locale->text('Image')                                                },
+                                 { name => 'lastcost',           description => $::locale->text('Last Cost')                                            },
+                                 { name => 'listprice',          description => $::locale->text('List Price')                                           },
+                                 { name => 'make_X',             description => $::locale->text('Make (with X being a number)')                         },
+                                 { name => 'microfiche',         description => $::locale->text('Microfiche')                                           },
+                                 { name => 'model_X',            description => $::locale->text('Model (with X being a number)')                        },
+                                 { name => 'not_discountable',   description => $::locale->text('Not Discountable')                                     },
+                                 { name => 'notes',              description => $::locale->text('Notes')                                                },
+                                 { name => 'obsolete',           description => $::locale->text('Obsolete')                                             },
+                                 { name => 'onhand',             description => $::locale->text('On Hand')                                              },
+                                 { name => 'partnumber',         description => $::locale->text('Part Number')                                          },
+                                 { name => 'partsgroup_id',      description => $::locale->text('Partsgroup (database ID)')                             },
+                                 { name => 'partsgroup',         description => $::locale->text('Partsgroup (name)')                                    },
+                                 { name => 'payment_id',         description => $::locale->text('Payment terms (database ID)')                          },
+                                 { name => 'payment',            description => $::locale->text('Payment terms (name)')                                 },
+                                 { name => 'price_factor_id',    description => $::locale->text('Price factor (database ID)')                           },
+                                 { name => 'price_factor',       description => $::locale->text('Price factor (name)')                                  },
+                                 { name => 'rop',                description => $::locale->text('ROP')                                                  },
+                                 { name => 'sellprice',          description => $::locale->text('Sellprice')                                            },
+                                 { name => 'shop',               description => $::locale->text('Shopartikel')                                          },
+                                 { name => 'type',               description => $::locale->text('Article type (see below)')                             },
+                                 { name => 'unit',               description => $::locale->text('Unit (if missing or empty default unit will be used)') },
+                                 { name => 've',                 description => $::locale->text('Verrechnungseinheit')                                  },
+                                 { name => 'weight',             description => $::locale->text('Weight')                                               },
                                 );
 
   foreach my $language (@{ $self->all_languages }) {