1 package SL::PriceSource::Makemodel;
 
   4 use parent qw(SL::PriceSource::Base);
 
   6 use SL::PriceSource::Price;
 
   7 use SL::Locale::String;
 
   9 use List::UtilsBy qw(min_by);
 
  11 sub name { 'makemodel' }
 
  13 sub description { t8('Makemodel Price') }
 
  15 sub available_prices {
 
  16   my ($self, %params) = @_;
 
  18   return () if !$self->part;
 
  19   return () if  $self->record->is_sales;
 
  21   map { $self->make_price_from_makemodel($_) }
 
  22   grep { $_->make == $self->record->vendor_id }
 
  23   $self->part->makemodels;
 
  26 sub available_discounts { }
 
  28 sub price_from_source {
 
  29   my ($self, $source, $spec) = @_;
 
  31   my $makemodel = SL::DB::Manager::MakeModel->find_by(id => $spec);
 
  33   # TODO: if someone deletes the prices entry, this fails. add a fallback
 
  34   return $self->make_price_from_makemodel($makemodel);
 
  39   my ($self, %params) = @_;
 
  41   return () if $self->record->is_sales;
 
  43   min_by { $_->price } $self->available_prices;
 
  49 sub make_price_from_makemodel {
 
  50   my ($self, $makemodel) = @_;
 
  52   return SL::PriceSource::Price->new(
 
  53     price        => $makemodel->lastcost,
 
  54     spec         => $makemodel->id,
 
  55     description  => $makemodel->model,
 
  56     price_source => $self,