Merge branch 'master' of github.com:kivitendo/kivitendo-erp
authorSven Schöling <s.schoeling@linet-services.de>
Thu, 9 Apr 2015 08:23:03 +0000 (10:23 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Thu, 9 Apr 2015 08:23:03 +0000 (10:23 +0200)
SL/DB/PriceRule.pm
SL/PriceSource.pm
SL/PriceSource/Discount.pm
SL/PriceSource/Price.pm
SL/PriceSource/PriceRules.pm

index 3deac75..e33b3a3 100644 (file)
@@ -48,7 +48,7 @@ sub price_type {
   my ($self, $value) = @_;
 
   if (@_ > 1) {
-    my $number = $self->price || $self->discount;
+    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()) {
index ac419a6..89dffba 100644 (file)
@@ -40,11 +40,11 @@ sub available_discounts {
 }
 
 sub best_price {
-  min_by { $_->price } grep { $_->price > 0 } grep { $_ } map { $_->best_price } $_[0]->all_price_sources;
+  min_by { $_->price } max_by { $_->priority } grep { $_->price > 0 } grep { $_ } map { $_->best_price } $_[0]->all_price_sources;
 }
 
 sub best_discount {
-  max_by { $_->discount } grep { $_->discount } grep { $_ } map { $_->best_discount } $_[0]->all_price_sources;
+  max_by { $_->discount } max_by { $_->priority } grep { $_->discount } grep { $_ } map { $_->best_discount } $_[0]->all_price_sources;
 }
 
 sub empty_price {
index 6c95f90..a7475ea 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use parent 'SL::DB::Object';
 use Rose::Object::MakeMethods::Generic (
   scalar => [ qw(discount description spec price_source invalid missing) ],
+  'scalar --get_set_init' => [ qw(priority) ],
 );
 
 require SL::DB::Helper::Attr;
@@ -38,6 +39,10 @@ sub to_str {
   "source: @{[ $_[0]->source ]}, discount: @{[ $_[0]->discount ]}, description: @{[ $_[0]->description ]}"
 }
 
+sub init_priority {
+  3
+}
+
 1;
 
 __END__
@@ -46,32 +51,32 @@ __END__
 
 =head1 NAME
 
-SL::PriceSource::Price - contrainer to pass calculated prices around
+SL::PriceSource::Discount - contrainer to pass calculated discounts around
 
 =head1 SYNOPSIS
 
   # in PriceSource::Base implementation
-  $price = SL::PriceSource::Price->new(
+  $price = SL::PriceSource::Discount->new(
     discount     => 10,
     spec         => 'summersale2014', # something you can easily parse later
     description  => t8('10% discount during summer sale 2014'),
     price_source => $self,
   )
 
-  # special empty price in SL::PriceSource, for internal use.
-  SL::PriceSource::Price->new(
+  # special empty discount in SL::PriceSource, for internal use.
+  SL::PriceSource::Discount->new(
     description => t8('None (PriceSource)'),
   );
 
   # price can't be restored
-  SL::PriceSource::Price->new(
+  SL::PriceSource::Discount->new(
     missing      => t8('Um, sorry, cannot find that one'),
     price_source => $self,
   );
 
-  # invalid price
-  SL::PriceSource::Price->new(
-    price        => $original_price,
+  # invalid discount
+  SL::PriceSource::Dicount->new(
+    discount     => $original_discount,
     spec         => $original_spec,
     description  => $original_description,
     invalid      => t8('Offer expired #1 weeks ago', $dt->delta_weeks),
@@ -82,7 +87,7 @@ SL::PriceSource::Price - contrainer to pass calculated prices around
 
 See L<SL::PriceSource> for information about the mechanism.
 
-This is a container for prices that are generated by L<SL::PriceSource::Base>
+This is a container for discounts that are generated by L<SL::PriceSource::Base>
 implementations.
 
 =head1 CONSTRUCTOR FIELDS
@@ -93,35 +98,40 @@ implementations.
 
 The discount in percent. A discount of 0 will be ignored. If passed as
 part of C<available_prices> it will be filtered out. If returned as
-C<best_price> or C<price_from_source> it will trigger a warning.
+C<best_discount> or C<discount_from_source> it will trigger a warning.
 
 =item C<spec>
 
 A unique string that can later be understood by the creating implementation.
-Can be empty if the implementation only supports one price for a given
+Can be empty if the implementation only supports one discount for a given
 record_item.
 
 =item C<description>
 
-A localized short description of the origins of this price.
+A localized short description of the origins of this discount.
 
 =item C<price_source>
 
 A ref to the creating algorithm.
 
+=item C<priority>
+
+OPTIONAL. Discounts may supply a numerical priority. Higher will trump over lower, even when
+supplying lower discounts. Defaults to 3 (as in middle of 1-5).
+
 =item C<missing>
 
-OPTIONAL. Both indicator and localized message that the price with this spec
+OPTIONAL. Both indicator and localized message that the discount with this spec
 could not be reproduced and should be changed.
 
-If price is missing, you do not need to supply anything except C<source>.
+If discount is missing, you do not need to supply anything except C<source>.
 
 =item C<invalid>
 
 OPTIONAL. Both indicator and localized message that the conditions for this
-price are no longer valid, and that the price should be changed.
+discount are no longer valid, and that the discount should be changed.
 
-If price is missing, you do not need to supply anything except C<source>.
+If discount is missing, you do not need to supply anything except C<source>.
 
 =back
 
index 71bd1ff..b17fb36 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use parent 'SL::DB::Object';
 use Rose::Object::MakeMethods::Generic (
   scalar => [ qw(price description spec price_source invalid missing) ],
+  'scalar --get_set_init' => [ qw(priority) ],
 );
 
 require SL::DB::Helper::Attr;
@@ -38,6 +39,10 @@ sub to_str {
   "source: @{[ $_[0]->source ]}, price: @{[ $_[0]->price ]}, description: @{[ $_[0]->description ]}"
 }
 
+sub init_priority {
+  3
+}
+
 1;
 
 __END__
@@ -109,6 +114,11 @@ A localized short description of the origins of this price.
 
 A ref to the creating algorithm.
 
+=item C<priority>
+
+OPTIONAL. Prices may supply a numerical priority. Higher will trump over lower, even when
+supplying higher prices. Defaults to 3 (as in middle of 1-5).
+
 =item C<missing>
 
 OPTIONAL. Both indicator and localized message that the price with this spec
index 140706d..e1d1c12 100644 (file)
@@ -92,6 +92,7 @@ sub make_price_from_rule {
     price        => $self->price_for_rule($rule),
     spec         => $rule->id,
     description  => $rule->name,
+    priority     => $rule->priority,
     price_source => $self,
     (invalid      => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
   )
@@ -104,6 +105,7 @@ sub make_discount_from_rule {
     discount     => $rule->discount / 100,
     spec         => $rule->id,
     description  => $rule->name,
+    priority     => $rule->priority,
     price_source => $self,
     (invalid      => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
   )