Preisquellen: Fehlerbehandlung in MakeModel
[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 available_discounts { }
27
28 sub price_from_source {
29   my ($self, $source, $spec) = @_;
30
31   my $makemodel = SL::DB::Manager::MakeModel->find_by(id => $spec);
32
33   return SL::PriceSource::Price->new(
34     price_source => $self,
35     missing      => t8('This makemodel price does not exist anymore'),
36   ) if !$makemodel;
37
38   return $self->make_price_from_makemodel($makemodel);
39
40 }
41
42 sub best_price {
43   my ($self, %params) = @_;
44
45   return () if $self->record->is_sales;
46
47   min_by { $_->price } $self->available_prices;
48
49 }
50
51 sub best_discount { }
52
53 sub make_price_from_makemodel {
54   my ($self, $makemodel) = @_;
55
56   return SL::PriceSource::Price->new(
57     price        => $makemodel->lastcost,
58     spec         => $makemodel->id,
59     description  => $makemodel->model,
60     price_source => $self,
61   );
62 }
63
64 1;