X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FPriceSource%2FPricegroup.pm;fp=SL%2FPriceSource%2FPricegroup.pm;h=3bae2d0d5a525e9f2f55569e4d5264fc5adcb387;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hp=6408300b13a4de5d1260cb768c69207ea89a8d00;hpb=deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44;p=kivitendo-erp.git diff --git a/SL/PriceSource/Pricegroup.pm b/SL/PriceSource/Pricegroup.pm index 6408300b1..3bae2d0d5 100644 --- a/SL/PriceSource/Pricegroup.pm +++ b/SL/PriceSource/Pricegroup.pm @@ -20,10 +20,21 @@ sub available_prices { my $item = $self->record_item; + my $query = [ parts_id => $item->parts_id, price => { gt => 0 } ]; + + # add a pricegroup_filter for obsolete pricegroups, unless part of an + # existing pricegroup where that pricegroup was actually used. + if ( $self->record->id and $item->active_price_source =~ m/^pricegroup/ ) { + my ($pricegroup_id) = $item->active_price_source =~ m/^pricegroup\/(\d+)$/; + push(@{$query}, or => [ 'pricegroup.obsolete' => 0, 'pricegroup_id' => $pricegroup_id ]); + } else { + push(@{$query}, 'pricegroup.obsolete' => 0); + } + my $prices = SL::DB::Manager::Price->get_all( - query => [ parts_id => $item->parts_id, price => { gt => 0 } ], + query => $query, with_objects => 'pricegroup', - order_by => 'pricegroup.id', + sort_by => 'pricegroup.sortkey', ); return () unless @$prices; @@ -40,7 +51,13 @@ sub price_from_source { 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 + if (!$price) { + return SL::PriceSource::Price->new( + price_source => $self, + missing => t8('Could not find an entry for this part in the pricegroup.'), + ); + } + return $self->make_price($price); } @@ -54,9 +71,9 @@ sub best_price { my @prices = $self->available_prices; my $customer = $self->record->customer; - return () if !$customer || !$customer->klass; + return () if !$customer || !$customer->pricegroup_id; - my $best_price = first { $_->spec == $customer->klass } @prices; + my $best_price = first { $_->spec == $customer->pricegroup_id } @prices; return $best_price || (); }