PriceRule: Korrektes Matching von Nulls
[kivitendo-erp.git] / SL / PriceSource.pm
index 470182c..e7a5906 100644 (file)
@@ -6,7 +6,7 @@ use Rose::Object::MakeMethods::Generic (
   scalar => [ qw(record_item record) ],
 );
 
-use List::UtilsBy qw(min_by);
+use List::UtilsBy qw(min_by max_by);
 use SL::PriceSource::ALL;
 use SL::PriceSource::Price;
 use SL::Locale::String;
@@ -14,9 +14,9 @@ use SL::Locale::String;
 sub all_price_sources {
   my ($self) = @_;
 
-  return map {
+  map {
     $_->new(record_item => $self->record_item, record => $self->record)
-  } SL::PriceSource::ALL->all_price_sources
+  } SL::PriceSource::ALL->all_enabled_price_sources
 }
 
 sub price_from_source {
@@ -26,7 +26,7 @@ sub price_from_source {
   my $class = SL::PriceSource::ALL->price_source_class_by_name($source_name);
 
   return $class
-    ? $class->new(record_item => $self->record_item)->price_from_source($source, $spec)
+    ? $class->new(record_item => $self->record_item, record => $self->record)->price_from_source($source, $spec)
     : empty_price();
 }
 
@@ -34,8 +34,16 @@ sub available_prices {
   map { $_->available_prices } $_[0]->all_price_sources;
 }
 
+sub available_discounts {
+  map { $_->available_discounts } $_[0]->all_price_sources;
+}
+
 sub best_price {
-  min_by { $_->price } map { $_->best_price } $_[0]->all_price_sources;
+  min_by { $_->price } 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;
 }
 
 sub empty_price {
@@ -59,7 +67,7 @@ SL::PriceSource - mixin for price_sources in record items
 PriceSource is an interface that allows generic algorithms to be plugged
 together to calculate available prices for a position in a record.
 
-Each algorithm can access details of the record to realize dependancies on
+Each algorithm can access details of the record to realize dependencies on
 part, customer, vendor, date, quantity etc, which was previously not possible.
 
 =head1 BACKGROUND AND PHILOSOPY
@@ -111,7 +119,7 @@ trying to be smart. The second and third one ensure that later on the
 calculation can be repeated so that invalid prices can be caught (because for
 example the special offer is no longer valid), and so that sales personnel have
 information about rising or falling prices. The fourth point ensures that
-insular calculation processes can be developed independant of the core code.
+insular calculation processes can be developed independent of the core code.
 
 =head1 INTERFACE METHODS