1 package SL::PriceSource::PriceRules;
4 use parent qw(SL::PriceSource::Base);
6 use SL::PriceSource::Price;
7 use SL::PriceSource::Discount;
8 use SL::Locale::String;
10 use List::UtilsBy qw(min_by max_by);
12 sub name { 'price_rules' }
14 sub description { t8('Price Rule') }
17 my ($self, %params) = @_;
19 $self->{available} ||= SL::DB::Manager::PriceRule->get_all_matching(record => $self->record, record_item => $self->record_item);
22 sub available_price_rules {
23 my $rules = $_[0]->available_rules;
24 grep { $_->price_type != SL::DB::Manager::PriceRule::PRICE_DISCOUNT() } @$rules
27 sub available_discount_rules {
28 my $rules = $_[0]->available_rules;
29 grep { $_->price_type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT() } @$rules
32 sub available_prices {
33 my ($self, %params) = @_;
35 map { $self->make_price_from_rule($_) } $self->available_price_rules;
38 sub available_discounts {
39 my ($self, %params) = @_;
41 map { $self->make_discount_from_rule($_) } $self->available_discount_rules;
44 sub price_from_source {
45 my ($self, $source, $spec) = @_;
47 my $rule = SL::DB::Manager::PriceRule->find_by(id => $spec);
48 if ($rule->price_type != SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
49 return $self->make_price_from_rule($rule);
55 sub discount_from_source {
56 my ($self, $source, $spec) = @_;
58 my $rule = SL::DB::Manager::PriceRule->find_by(id => $spec);
59 if ($rule->price_type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
60 return $self->make_discount_from_rule($rule);
69 my @rules = $self->available_price_rules;
73 my @max_prio = max_by { $_->priority } @rules;
74 my $min_price = min_by { $self->price_for_rule($_) } @max_prio;
76 $self->make_price_from_rule($min_price);
82 my @rules = $self->available_discount_rules;
86 my @max_prio = max_by { $_->priority } @rules;
87 my $max_discount = max_by { $_->discount } @max_prio;
89 $self->make_discount_from_rule($max_discount);
93 my ($self, $rule) = @_;
94 $rule->price_type != SL::DB::Manager::PriceRule::PRICE_NEW()
95 ? (1 - $rule->reduction / 100) * ($rule->is_sales ? $self->part->sellprice : $self->part->lastcost)
99 sub make_price_from_rule {
100 my ($self, $rule) = @_;
102 SL::PriceSource::Price->new(
103 price => $self->price_for_rule($rule),
105 description => $rule->name,
106 priority => $rule->priority,
107 price_source => $self,
108 (invalid => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
112 sub make_discount_from_rule {
113 my ($self, $rule) = @_;
115 SL::PriceSource::Discount->new(
116 discount => $rule->discount / 100,
118 description => $rule->name,
119 priority => $rule->priority,
120 price_source => $self,
121 (invalid => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,