1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
4 package SL::DB::PriceRule;
8 use SL::DB::MetaSetup::PriceRule;
9 use SL::DB::Manager::PriceRule;
10 use Rose::DB::Object::Helpers qw(clone_and_reset);
11 use SL::Locale::String qw(t8);
13 __PACKAGE__->meta->add_relationship(
15 type => 'one to many',
16 class => 'SL::DB::PriceRuleItem',
17 column_map => { id => 'price_rules_id' },
21 __PACKAGE__->meta->initialize;
23 use Rose::Object::MakeMethods::Generic (
24 'scalar --get_set_init' => [ qw(price_or_discount_state) ],
28 my ($self, %params) = @_;
30 die 'need record' unless $params{record};
31 die 'need record_item' unless $params{record_item};
34 next if $_->match(%params);
43 $_[0]->type eq 'customer' ? 1
44 : $_[0]->type eq 'vendor' ? 0 : do { die 'wrong type' };
48 my ($self, $value) = @_;
51 my $number = $self->price || $self->discount || $self->reduction;
52 if ($value == SL::DB::Manager::PriceRule::PRICE_NEW()) {
53 $self->price($number);
54 } elsif ($value == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
55 $self->reduction($number);
56 } elsif ($value == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
57 $self->discount($number);
59 die 'unknown price_or_discount value';
61 $self->price_or_discount_state($value);
63 $self->price_or_discount_state;
66 sub price_or_discount_as_number {
67 my ($self, @slurp) = @_;
68 my $type = $self->price_type;
70 $self->price(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_NEW();
71 $self->reduction(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA();
72 $self->discount(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT();
75 if ($type == SL::DB::Manager::PriceRule::PRICE_NEW()) {
76 return $self->price_as_number(@slurp)
77 } elsif ($type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
78 return $self->reduction_as_number(@slurp);
79 } elsif ($type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
80 return $self->discount_as_number(@slurp)
82 die 'unknown price_or_discount';
86 sub init_price_or_discount_state {
87 defined $_[0]->price ? SL::DB::Manager::PriceRule::PRICE_NEW()
88 : defined $_[0]->reduction ? SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()
89 : defined $_[0]->discount ? SL::DB::Manager::PriceRule::PRICE_DISCOUNT()
90 : SL::DB::Manager::PriceRule::PRICE_NEW();
97 push @errors, $::locale->text('The name must not be empty.') if !$self->name;
98 push @errors, $::locale->text('Price or discount must not be zero.') if !$self->price && !$self->discount && !$self->reduction;
99 push @errors, $::locale->text('Price rules must have at least one rule.') if !@{[ $self->items ]};
100 push @errors, $_->validate for $self->items;
105 sub clone_and_reset_deep {
108 my $clone = $self->clone_and_reset;
109 $clone->items(map { $_->clone_and_reset } $self->items);
115 sub full_description {
118 my $items = $self->item_summary;
119 my $price = $self->price_or_discount
120 ? t8('Discount #1%', $self->discount_as_number)
121 : t8('Price #1', $self->price_as_number);
123 sprintf "%s: %s (%s)", $self->name, $price, $items;
127 join ', ', map { $_->full_description } $_[0]->items;
133 # is use is in this case used by record_items for their current price source
134 # so, get any of those that might have it
135 require SL::DB::OrderItem;
136 require SL::DB::DeliveryOrderItem;
137 require SL::DB::InvoiceItem;
139 my $price_source_spec = 'price_rules' . '/' . $self->id;
141 SL::DB::Manager::OrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
142 || SL::DB::Manager::DeliveryOrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
143 || SL::DB::Manager::InvoiceItem->get_all_count(query => [ active_price_source => $price_source_spec ]);
146 sub priority_as_text {
149 return t8('Override') if $self->priority == 4;