PriceSource: Rabattbehandlung
[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   # TODO: if someone deletes the prices entry, this fails. add a fallback
34   return $self->make_price_from_makemodel($makemodel);
35
36 }
37
38 sub best_price {
39   my ($self, %params) = @_;
40
41   return () if $self->record->is_sales;
42
43   min_by { $_->price } $self->available_prices;
44
45 }
46
47 sub best_discount { }
48
49 sub make_price_from_makemodel {
50   my ($self, $makemodel) = @_;
51
52   return SL::PriceSource::Price->new(
53     price        => $makemodel->lastcost,
54     spec         => $makemodel->id,
55     description  => $makemodel->model,
56     price_source => $self,
57   );
58 }
59
60
61 1;