Bug 1960 - Datenverlust beim CSV-Warenimport bei Preisen
[kivitendo-erp.git] / SL / Controller / CsvImport / Part.pm
index 45f4794..d707aaa 100644 (file)
@@ -10,6 +10,8 @@ use SL::DB::CustomVariableConfig;
 use SL::DB::PartsGroup;
 use SL::DB::PaymentTerm;
 use SL::DB::PriceFactor;
+use SL::DB::Pricegroup;
+use SL::DB::Price;
 use SL::DB::Translation;
 use SL::DB::Unit;
 
@@ -17,9 +19,9 @@ use parent qw(SL::Controller::CsvImport::Base);
 
 use Rose::Object::MakeMethods::Generic
 (
- scalar                  => [ qw(table) ],
- 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by packing_types_by partsgroups_by
-                                 translation_columns) ],
+ scalar                  => [ qw(table makemodel_columns) ],
+ 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by partsgroups_by
+                                 translation_columns all_pricegroups) ],
 );
 
 sub init_class {
@@ -41,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) = @_;
 
@@ -77,12 +72,18 @@ sub init_parts_by {
   return $parts_by;
 }
 
+sub init_all_pricegroups {
+  my ($self) = @_;
+
+  return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
+}
+
 sub init_settings {
   my ($self) = @_;
 
   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 {
@@ -102,31 +103,33 @@ sub check_objects {
 
   return unless @{ $self->controller->data };
 
+  $self->makemodel_columns({});
+
   foreach my $entry (@{ $self->controller->data }) {
-    my $object   = $entry->{object};
-    my $raw_data = $entry->{raw_data};
-
-    next unless $self->check_buchungsgruppe($entry);
-    next unless $self->check_type($entry);
-    next unless $self->check_unit($entry);
-    next unless $self->check_price_factor($entry);
-    next unless $self->check_payment($entry);
-    next unless $self->check_packing_type($entry);
-    next unless $self->check_partsgroup($entry);
-    $self->check_existing($entry);
+    $self->check_buchungsgruppe($entry);
+    $self->check_type($entry);
+    $self->check_unit($entry);
+    $self->check_price_factor($entry);
+    $self->check_payment($entry);
+    $self->check_partsgroup($entry);
+    $self->handle_pricegroups($entry);
+    $self->check_existing($entry) unless @{ $entry->{errors} };
     $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
     $self->handle_shoparticle($entry);
     $self->handle_translations($entry);
     $self->handle_cvars($entry);
+    $self->handle_makemodel($entry);
     $self->set_various_fields($entry);
   }
 
   $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 }));
   map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
+  map { $self->add_raw_data_columns("make_${_}", "model_${_}") } sort { $a <=> $b } keys %{ $self->makemodel_columns };
 }
 
 sub check_duplicates {
@@ -195,7 +198,12 @@ sub check_existing {
 
   if ($self->settings->{article_number_policy} eq 'update_prices') {
     if ($entry->{part}) {
-      map { $object->$_( $entry->{part}->$_ ) } qw(sellprice listprice lastcost);
+      map { $entry->{part}->$_( $object->$_ ) if defined $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};
     }
@@ -229,7 +237,7 @@ sub check_type {
   my ($self, $entry) = @_;
 
   my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
-  die "Program logic error" if !$bg;
+  $bg  ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1, income_accno_id_0 => 1, expense_accno_id_0 => 1);
 
   my $type = $self->settings->{parts_type};
   if ($type eq 'mixed') {
@@ -278,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) = @_;
 
@@ -335,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');
@@ -360,6 +344,48 @@ sub handle_translations {
   $entry->{object}->translations(\@translations);
 }
 
+sub handle_pricegroups {
+  my ($self, $entry) = @_;
+
+  my @prices;
+  my $idx = 0;
+  foreach my $pricegroup (@{ $self->all_pricegroups }) {
+    $idx++;
+    my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"};
+    next if $sellprice eq '';
+
+    push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id,
+                                     price         => $::form->parse_amount(\%::myconfig, $sellprice));
+  }
+
+  $entry->{object}->prices(\@prices);
+}
+
+sub handle_makemodel {
+  my ($self, $entry) = @_;
+
+  my @makemodels;
+  foreach my $idx (map { substr $_, 5 } grep { m/^make_\d+$/ && $entry->{raw_data}->{$_} } keys %{ $entry->{raw_data} }) {
+    my $vendor = $entry->{raw_data}->{"make_${idx}"};
+    $vendor    = $self->vc_by->{id}->               { $vendor }
+              || $self->vc_by->{number}->{vendors}->{ $vendor }
+              || $self->vc_by->{name}->  {vendors}->{ $vendor };
+
+    if (ref($vendor) ne 'SL::DB::Vendor') {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid vendor in column make_#1', $idx);
+
+    } else {
+      push @makemodels, SL::DB::MakeModel->new(make  => $vendor->id,
+                                               model => $entry->{raw_data}->{"model_${idx}"});
+      $self->makemodel_columns->{$idx}    = 1;
+      $entry->{raw_data}->{"make_${idx}"} = $vendor->name;
+    }
+  }
+
+  $entry->{object}->makemodels(\@makemodels);
+  $entry->{object}->makemodel(scalar(@makemodels) ? 1 : 0);
+}
+
 sub set_various_fields {
   my ($self, $entry) = @_;
 
@@ -393,40 +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 => 'microfiche',         description => $::locale->text('Microfiche')                   },
-                                 { name => 'min_sellprice',      description => $::locale->text('Minimum Sell Price')           },
-                                 { 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 => '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 }) {
@@ -435,10 +460,13 @@ sub setup_displayable_columns {
                                    { name        => 'notes_' . $language->article_code,
                                      description => $::locale->text('Notes (translation for #1)', $language->description) });
   }
-}
 
-# TODO:
-# Preisgruppen
-# Preisaktualisierung
+  my $idx = 0;
+  foreach my $pricegroup (@{ $self->all_pricegroups }) {
+    $idx++;
+    $self->add_displayable_columns({ name        => 'pricegroup_' . $idx,
+                                     description => $::locale->text("Sellprice for price group '#1'", $pricegroup->pricegroup) });
+  }
+}
 
 1;