Rechnungsmaske: "Drucken und Buchen" und Browser-Zurück entschärfen
[kivitendo-erp.git] / SL / Controller / CsvImport / Order.pm
index 0d54944..503b4d1 100644 (file)
@@ -19,14 +19,14 @@ use SL::DB::Project;
 use SL::DB::Shipto;
 use SL::DB::TaxZone;
 use SL::DB::Unit;
-use SL::TransNumber;
+use SL::PriceSource;
 
 use parent qw(SL::Controller::CsvImport::BaseMulti);
 
 
 use Rose::Object::MakeMethods::Generic
 (
- 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by ct_shiptos_by price_factors_by pricegroups_by units_by) ],
+ 'scalar --get_set_init' => [ qw(settings languages_by all_parts parts_by part_counts_by contacts_by ct_shiptos_by price_factors_by pricegroups_by units_by) ],
 );
 
 
@@ -162,6 +162,7 @@ sub setup_displayable_columns {
                                  { name => 'cusordnumber',    description => $::locale->text('Customer Order Number')      },
                                  { name => 'description',     description => $::locale->text('Description')                },
                                  { name => 'discount',        description => $::locale->text('Discount')                   },
+                                 { name => 'ean',             description => $::locale->text('EAN')                        },
                                  { name => 'lastcost',        description => $::locale->text('Lastcost')                   },
                                  { name => 'longdescription', description => $::locale->text('Long Description')           },
                                  { name => 'marge_percent',   description => $::locale->text('Margepercent')               },
@@ -193,11 +194,27 @@ sub init_languages_by {
   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
 }
 
+sub init_all_parts {
+  my ($self) = @_;
+
+  return SL::DB::Manager::Part->get_all;
+}
+
 sub init_parts_by {
   my ($self) = @_;
 
-  my $all_parts = SL::DB::Manager::Part->get_all;
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_parts } } ) } qw(id partnumber ean description) };
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_parts } } ) } qw(id partnumber ean description) };
+}
+
+sub init_part_counts_by {
+  my ($self) = @_;
+
+  my $part_counts_by;
+
+  $part_counts_by->{ean}->        {$_->ean}++         for @{ $self->all_parts };
+  $part_counts_by->{description}->{$_->description}++ for @{ $self->all_parts };
+
+  return $part_counts_by;
 }
 
 sub init_contacts_by {
@@ -254,6 +271,7 @@ sub check_objects {
 
   my $i = 0;
   my $num_data = scalar @{ $self->controller->data };
+  my $order_entry;
   foreach my $entry (@{ $self->controller->data }) {
     $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
 
@@ -261,9 +279,13 @@ sub check_objects {
 
     if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
       $self->handle_order($entry);
+      $order_entry = $entry;
     } elsif ($entry->{raw_data}->{datatype} eq $self->_item_column && $entry->{object}->can('part')) {
-      $self->handle_item($entry);
+      $self->handle_item($entry, $order_entry);
+    } else {
+      $order_entry = undef;
     }
+
     $self->handle_cvars($entry, sub_module => 'orderitems');
 
   } continue {
@@ -327,6 +349,7 @@ sub handle_order {
     foreach (qw(payment_id delivery_term_id language_id taxzone_id currency_id)) {
       $object->$_($vc_obj->$_) unless $object->$_;
     }
+    $object->intnotes($vc_obj->notes) unless $object->intnotes;
   }
 
   $self->handle_salesman($entry);
@@ -365,15 +388,17 @@ sub check_language {
 }
 
 sub handle_item {
-  my ($self, $entry) = @_;
+  my ($self, $entry, $order_entry) = @_;
+
+  return unless $order_entry;
 
   my $object = $entry->{object};
+
   return unless $self->check_part($entry);
 
   my $part_obj = SL::DB::Part->new(id => $object->parts_id)->load;
 
   $self->handle_unit($entry);
-  $self->handle_sellprice($entry);
 
   # copy from part if not given
   $object->description($part_obj->description) unless $object->description;
@@ -381,12 +406,14 @@ sub handle_item {
   $object->lastcost($part_obj->lastcost)       unless defined $object->lastcost;
 
   # set to 0 if not given
-  $object->discount(0) unless $object->discount;
   $object->ship(0)     unless $object->ship;
 
   $self->check_project($entry, global => 0);
   $self->check_price_factor($entry);
   $self->check_pricegroup($entry);
+
+  $self->handle_sellprice($entry, $order_entry);
+  $self->handle_discount($entry, $order_entry);
 }
 
 sub handle_unit {
@@ -416,18 +443,62 @@ sub handle_unit {
 }
 
 sub handle_sellprice {
-  my ($self, $entry) = @_;
+  my ($self, $entry, $record_entry) = @_;
 
-  my $object = $entry->{object};
+  my $item   = $entry->{object};
+  my $record = $record_entry->{object};
+
+  return if !$record->customervendor;
+
+  # If sellprice is given, set price source to pricegroup if given or to none.
+  if (exists $entry->{raw_data}->{sellprice}) {
+    my $price_source      = SL::PriceSource->new(record_item => $item, record => $record);
+    my $price_source_spec = $item->pricegroup_id ? 'pricegroup' . '/' . $item->pricegroup_id : '';
+    my $price             = $price_source->price_from_source($price_source_spec);
+    $item->active_price_source($price->source);
 
-  # Set sellprice from part if not given. Convert with respect to unit.
-  if (!defined $object->sellprice) {
-    my $sellprice = $object->part->sellprice;
+  } else {
+    # Set sellprice the best price of price source
+    my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+    my $price        = $price_source->best_price;
+    if ($price) {
+      $item->sellprice($price->price);
+      $item->active_price_source($price->source);
+    } else {
+      $item->sellprice(0);
+      $item->active_price_source($price_source->price_from_source('')->source);
+    }
+  }
+}
+
+sub handle_discount {
+  my ($self, $entry, $record_entry) = @_;
+
+  my $item   = $entry->{object};
+  my $record = $record_entry->{object};
 
-    if ($object->unit ne $object->part->unit) {
-      $sellprice = $object->unit_obj->convert_to($sellprice, $object->part->unit_obj);
+  return if !$record->customervendor;
+
+  # If discount is given, set discount source to none.
+  if (exists $entry->{raw_data}->{discount}) {
+    $item->discount($item->discount/100.0)      if $item->discount;
+    $item->discount(0)                      unless $item->discount;
+
+    my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+    my $discount     = $price_source->price_from_source('');
+    $item->active_discount_source($discount->source);
+
+  } else {
+    # Set discount the best discount of price source
+    my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+    my $discount     = $price_source->best_discount;
+    if ($discount) {
+      $item->discount($discount->discount);
+      $item->active_discount_source($discount->source);
+    } else {
+      $item->discount(0);
+      $item->active_discount_source($price_source->discount_from_source('')->source);
     }
-    $object->sellprice($sellprice);
   }
 }
 
@@ -435,6 +506,7 @@ sub check_part {
   my ($self, $entry) = @_;
 
   my $object = $entry->{object};
+  my $is_ambiguous;
 
   # Check whether or not part ID is valid.
   if ($object->parts_id && !$self->parts_by->{id}->{ $object->parts_id }) {
@@ -461,13 +533,36 @@ sub check_part {
       return 0;
     }
 
-    $object->parts_id($part->id);
+    if ($self->part_counts_by->{description}->{ $entry->{raw_data}->{description} } > 1) {
+      $is_ambiguous = 1;
+    } else {
+      $object->parts_id($part->id);
+    }
+  }
+
+  # Map ean to ID if given.
+  if (!$object->parts_id && $entry->{raw_data}->{ean}) {
+    my $part = $self->parts_by->{ean}->{ $entry->{raw_data}->{ean} };
+    if (!$part) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid part');
+      return 0;
+    }
+
+    if ($self->part_counts_by->{ean}->{ $entry->{raw_data}->{ean} } > 1) {
+      $is_ambiguous = 1;
+    } else {
+      $object->parts_id($part->id);
+    }
   }
 
   if ($object->parts_id) {
     $entry->{info_data}->{partnumber} = $self->parts_by->{id}->{ $object->parts_id }->partnumber;
   } else {
-    push @{ $entry->{errors} }, $::locale->text('Error: Part not found');
+    if ($is_ambiguous) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Part is ambiguous');
+    } else {
+      push @{ $entry->{errors} }, $::locale->text('Error: Part not found');
+    }
     return 0;
   }