1 package SL::PriceSource::Pricegroup;
4 use parent qw(SL::PriceSource::Base);
6 use SL::PriceSource::Price;
8 use SL::Locale::String;
9 use List::UtilsBy qw(min_by);
10 use List::Util qw(first);
12 sub name { 'pricegroup' }
14 sub description { t8('Pricegroup') }
16 sub available_prices {
17 my ($self, %params) = @_;
19 return () unless $self->record->is_sales;
21 my $item = $self->record_item;
23 my $prices = SL::DB::Manager::Price->get_all(
24 query => [ parts_id => $item->parts_id, price => { gt => 0 } ],
25 with_objects => 'pricegroup',
26 order_by => 'pricegroun.id',
29 return () unless @$prices;
32 $self->make_price($_);
36 sub price_from_source {
37 my ($self, $source, $spec) = @_;
39 my $price = SL::DB::Manager::Price->find_by(pricegroup_id => $spec, parts_id => $self->part->id);
41 # TODO: if someone deletes the prices entry, this fails. add a fallback
42 return $self->make_price($price);
46 my ($self, %params) = @_;
48 return () unless $self->record->is_sales;
50 my @prices = $self->available_prices;
51 my $customer = $self->record->customer;
53 return () if !$customer || !$customer->klass;
55 my $best_price = first { $_->spec == $customer->klass } @prices;
57 return $best_price || ();
61 my ($self, $price_obj) = @_;
63 SL::PriceSource::Price->new(
64 price => $price_obj->price,
65 spec => $price_obj->pricegroup->id,
66 description => $price_obj->pricegroup->pricegroup,
67 price_source => $self,