1 package SL::PriceSource::Pricegroup;
 
   4 use parent qw(SL::PriceSource::Base);
 
   6 use SL::PriceSource::Price;
 
   7 use SL::Locale::String;
 
   8 use List::UtilsBy qw(min_by);
 
   9 use List::Util qw(first);
 
  11 sub name { 'pricegroup' }
 
  13 sub description { t8('Pricegroup') }
 
  15 sub available_prices {
 
  16   my ($self, %params) = @_;
 
  18   my $item = $self->record_item;
 
  20   my $prices = SL::DB::Manager::Price->get_all(
 
  21     query        => [ parts_id => $item->parts_id, price => { gt => 0 } ],
 
  22     with_objects => 'pricegroup',
 
  23     order_by     => 'pricegroun.id',
 
  26   return () unless @$prices;
 
  29     $self->make_price($_);
 
  33 sub price_from_source {
 
  34   my ($self, $source, $spec) = @_;
 
  36   my $price = SL::DB::Manager::Price->find_by(id => $spec);
 
  38   return $self->make_price($price);
 
  42   my ($self, %params) = @_;
 
  44   my @prices    = $self->availabe_prices;
 
  45   my $customer  = $self->record->customer;
 
  46   my $min_price = min_by { $_->price } @prices;
 
  48   return $min_price if !$customer || !$customer->cv_klass;
 
  50   my $best_price = first { $_->spec == $customer->cv_class } @prices;
 
  52   return $best_price || $min_price;
 
  56   my ($self, $price_obj) = @_;
 
  58   SL::PriceSource::Price->new(
 
  59     price        => $price_obj->price,
 
  60     spec         => $price_obj->id,
 
  61     description  => $price_obj->pricegroup->pricegroup,
 
  62     price_source => $self,