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);
49 return SL::PriceSource::Discount->new(
50 price_source => $self,
51 missing => t8('The price rule for this price does not exist anymore'),
54 if ($rule->price_type != SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
55 return $self->make_price_from_rule($rule);
57 return SL::PriceSource::Price->new(
58 price_source => $self,
59 invalid => t8('The price rule is not a rule for prices'),
64 sub discount_from_source {
65 my ($self, $source, $spec) = @_;
67 my $rule = SL::DB::Manager::PriceRule->find_by(id => $spec);
69 return SL::PriceSource::Discount->new(
70 price_source => $self,
71 missing => t8('The price rule for this discount does not exist anymore'),
74 if ($rule->price_type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
75 return $self->make_discount_from_rule($rule);
77 return SL::PriceSource::Discount->new(
78 price_source => $self,
79 invalid => t8('The price rule is not a rule for discounts'),
87 my @rules = $self->available_price_rules;
91 my @max_prio = max_by { $_->priority } @rules;
92 my $min_price = min_by { $self->price_for_rule($_) } @max_prio;
94 $self->make_price_from_rule($min_price);
100 my @rules = $self->available_discount_rules;
102 return unless @rules;
104 my @max_prio = max_by { $_->priority } @rules;
105 my $max_discount = max_by { $_->discount } @max_prio;
107 $self->make_discount_from_rule($max_discount);
111 my ($self, $rule) = @_;
112 $rule->price_type != SL::DB::Manager::PriceRule::PRICE_NEW()
113 ? (1 - $rule->reduction / 100) * ($rule->is_sales ? $self->part->sellprice : $self->part->lastcost)
117 sub make_price_from_rule {
118 my ($self, $rule) = @_;
120 SL::PriceSource::Price->new(
121 price => $self->price_for_rule($rule),
123 description => $rule->name,
124 priority => $rule->priority,
125 price_source => $self,
126 (invalid => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
130 sub make_discount_from_rule {
131 my ($self, $rule) = @_;
133 SL::PriceSource::Discount->new(
134 discount => $rule->discount / 100,
136 description => $rule->name,
137 priority => $rule->priority,
138 price_source => $self,
139 (invalid => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,