PriceSource: Featureabdeckung
[kivitendo-erp.git] / SL / PriceSource / Makemodel.pm
1 package SL::PriceSource::Makemodel;
2
3 use strict;
4 use parent qw(SL::PriceSource::Base);
5
6 use SL::PriceSource::Price;
7 use SL::Locale::String;
8 use SL::DB::MakeModel;
9 use List::UtilsBy qw(min_by);
10
11 sub name { 'makemodel' }
12
13 sub description { t8('Makemodel Price') }
14
15 sub available_prices {
16   my ($self, %params) = @_;
17
18   return () if !$self->part;
19   return () if  $self->record->is_sales;
20
21   map { $self->make_price_from_makemodel($_) }
22   grep { $_->make == $self->record->vendor_id }
23   $self->part->makemodels;
24 }
25
26 sub price_from_source {
27   my ($self, $source, $spec) = @_;
28
29   my $makemodel = SL::DB::Manager::MakeModel->find_by(id => $spec);
30
31   # TODO: if someone deletes the prices entry, this fails. add a fallback
32   return $self->make_price_from_makemodel($makemodel);
33
34 }
35
36 sub best_price {
37   my ($self, %params) = @_;
38
39   return () if $self->record->is_sales;
40
41   min_by { $_->price } $self->available_prices;
42
43 }
44
45 sub make_price_from_makemodel {
46   my ($self, $makemodel) = @_;
47
48   return SL::PriceSource::Price->new(
49     price        => $makemodel->lastcost,
50     spec         => $makemodel->id,
51     description  => $makemodel->model,
52     price_source => $self,
53   );
54 }
55
56
57 1;