epic-s6ts
[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 discount_from_source { }
43
44 sub best_price {
45   my ($self, %params) = @_;
46
47   return () if $self->record->is_sales;
48
49   min_by { $_->price } $self->available_prices;
50
51 }
52
53 sub best_discount { }
54
55 sub make_price_from_makemodel {
56   my ($self, $makemodel) = @_;
57
58   return SL::PriceSource::Price->new(
59     price        => $makemodel->lastcost,
60     spec         => $makemodel->id,
61     description  => $makemodel->model,
62     price_source => $self,
63   );
64 }
65
66 1;