X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FPriceSource%2FPricegroup.pm;h=6408300b13a4de5d1260cb768c69207ea89a8d00;hb=26a7b7af2734e081e1c32c2248c8ee35c67a4225;hp=4b73ac8ff263360186db2d68a7255b895a734822;hpb=eebe8e90991eacadb6fbd20a648c152017a620c7;p=kivitendo-erp.git diff --git a/SL/PriceSource/Pricegroup.pm b/SL/PriceSource/Pricegroup.pm index 4b73ac8ff..6408300b1 100644 --- a/SL/PriceSource/Pricegroup.pm +++ b/SL/PriceSource/Pricegroup.pm @@ -4,7 +4,10 @@ use strict; use parent qw(SL::PriceSource::Base); use SL::PriceSource::Price; +use SL::DB::Price; use SL::Locale::String; +use List::UtilsBy qw(min_by); +use List::Util qw(first); sub name { 'pricegroup' } @@ -13,12 +16,14 @@ sub description { t8('Pricegroup') } sub available_prices { my ($self, %params) = @_; + return () unless $self->record->is_sales; + my $item = $self->record_item; my $prices = SL::DB::Manager::Price->get_all( query => [ parts_id => $item->parts_id, price => { gt => 0 } ], with_objects => 'pricegroup', - order_by => 'pricegroun.id', + order_by => 'pricegroup.id', ); return () unless @$prices; @@ -28,20 +33,42 @@ sub available_prices { } @$prices; } +sub available_discounts { } + sub price_from_source { my ($self, $source, $spec) = @_; - my $price = SL::DB::Manager::Price->find_by(id => $spec); + my $price = SL::DB::Manager::Price->find_by(pricegroup_id => $spec, parts_id => $self->part->id); + # TODO: if someone deletes the prices entry, this fails. add a fallback return $self->make_price($price); } +sub discount_from_source { } + +sub best_price { + my ($self, %params) = @_; + + return () unless $self->record->is_sales; + + my @prices = $self->available_prices; + my $customer = $self->record->customer; + + return () if !$customer || !$customer->klass; + + my $best_price = first { $_->spec == $customer->klass } @prices; + + return $best_price || (); +} + +sub best_discount { } + sub make_price { my ($self, $price_obj) = @_; SL::PriceSource::Price->new( price => $price_obj->price, - source => 'pricegroup/' . $price_obj->id, + spec => $price_obj->pricegroup->id, description => $price_obj->pricegroup->pricegroup, price_source => $self, )