Zu Kontoauszug Zuordnung verbessern, alte Logik auch entfernen
[kivitendo-erp.git] / SL / DB / PriceRule.pm
index 23e314a..1c43d4a 100644 (file)
@@ -7,7 +7,6 @@ use strict;
 
 use SL::DB::MetaSetup::PriceRule;
 use SL::DB::Manager::PriceRule;
-use Rose::DB::Object::Helpers qw(clone_and_reset);
 use SL::Locale::String qw(t8);
 
 __PACKAGE__->meta->add_relationship(
@@ -44,15 +43,19 @@ sub is_sales {
   : $_[0]->type eq 'vendor'   ? 0 : do { die 'wrong type' };
 }
 
-sub price_or_discount {
+sub price_type {
   my ($self, $value) = @_;
 
   if (@_ > 1) {
-    my $number = $self->price || $self->discount;
-    if ($value) {
+    my $number = $self->price || $self->discount || $self->reduction;
+    if ($value == SL::DB::Manager::PriceRule::PRICE_NEW()) {
+      $self->price($number);
+    } elsif ($value == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
+      $self->reduction($number);
+    } elsif ($value == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
       $self->discount($number);
     } else {
-      $self->price($number);
+      die 'unknown price_or_discount value';
     }
     $self->price_or_discount_state($value);
   }
@@ -61,22 +64,39 @@ sub price_or_discount {
 
 sub price_or_discount_as_number {
   my ($self, @slurp) = @_;
+  my $type = $self->price_type;
+
+  $self->price(undef)     unless $type == SL::DB::Manager::PriceRule::PRICE_NEW();
+  $self->reduction(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA();
+  $self->discount(undef)  unless $type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT();
 
-  $self->price_or_discount ? $self->price(undef)               : $self->discount(undef);
-  $self->price_or_discount ? $self->discount_as_number(@slurp) : $self->price_as_number(@slurp);
+
+  if ($type == SL::DB::Manager::PriceRule::PRICE_NEW()) {
+    return $self->price_as_number(@slurp)
+  } elsif ($type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
+    return $self->reduction_as_number(@slurp);
+  } elsif ($type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
+    return $self->discount_as_number(@slurp)
+  } else {
+    die 'unknown price_or_discount';
+  }
 }
 
 sub init_price_or_discount_state {
-    defined $_[0]->price ? 0
-  : defined $_[0]->discount ? 1 : 0
+    defined $_[0]->price     ? SL::DB::Manager::PriceRule::PRICE_NEW()
+  : defined $_[0]->reduction ? SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()
+  : defined $_[0]->discount  ? SL::DB::Manager::PriceRule::PRICE_DISCOUNT()
+  :                            SL::DB::Manager::PriceRule::PRICE_NEW();
 }
 
 sub validate {
   my ($self) = @_;
 
   my @errors;
-  push @errors, $::locale->text('The name must not be empty.')           if !$self->name;
-  push @errors, $::locale->text('Price or discount must not be zero.')   if !$self->price && !$self->discount;
+  push @errors, $::locale->text('The name must not be empty.')              if !$self->name;
+  push @errors, $::locale->text('Price or discount must not be zero.')      if !$self->price && !$self->discount && !$self->reduction;
+  push @errors, $::locale->text('Price rules must have at least one rule.') if !@{[ $self->items ]};
+  push @errors, $_->validate                                                for $self->items;
 
   return @errors;
 }
@@ -94,7 +114,7 @@ sub clone_and_reset_deep {
 sub full_description {
   my ($self) = @_;
 
-  my $items = join ', ', map { $_->full_description } $self->items;
+  my $items = $self->item_summary;
   my $price = $self->price_or_discount
             ? t8('Discount #1%', $self->discount_as_number)
             : t8('Price #1', $self->price_as_number);
@@ -102,6 +122,10 @@ sub full_description {
   sprintf "%s: %s (%s)", $self->name, $price, $items;
 }
 
+sub item_summary {
+  join ', ', map { $_->full_description } $_[0]->items;
+}
+
 sub in_use {
   my ($self) = @_;
 
@@ -118,5 +142,12 @@ sub in_use {
   || SL::DB::Manager::InvoiceItem->get_all_count(query => [ active_price_source => $price_source_spec ]);
 }
 
+sub priority_as_text {
+  my ($self) = @_;
+
+  return t8('Override') if $self->priority == 4;
+  t8('Normal');
+}
+
 
 1;