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 SL::Locale::String qw(t8);
12 __PACKAGE__->meta->add_relationship(
14 type => 'one to many',
15 class => 'SL::DB::PriceRuleItem',
16 column_map => { id => 'price_rules_id' },
20 __PACKAGE__->meta->initialize;
22 use Rose::Object::MakeMethods::Generic (
23 'scalar --get_set_init' => [ qw(price_or_discount_state) ],
27 my ($self, %params) = @_;
29 die 'need record' unless $params{record};
30 die 'need record_item' unless $params{record_item};
33 next if $_->match(%params);
42 $_[0]->type eq 'customer' ? 1
43 : $_[0]->type eq 'vendor' ? 0 : do { die 'wrong type' };
47 my ($self, $value) = @_;
50 my $number = $self->price || $self->discount || $self->reduction;
51 if ($value == SL::DB::Manager::PriceRule::PRICE_NEW()) {
52 $self->price($number);
53 } elsif ($value == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
54 $self->reduction($number);
55 } elsif ($value == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
56 $self->discount($number);
58 die 'unknown price_or_discount value';
60 $self->price_or_discount_state($value);
62 $self->price_or_discount_state;
65 sub price_or_discount_as_number {
66 my ($self, @slurp) = @_;
67 my $type = $self->price_type;
69 $self->price(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_NEW();
70 $self->reduction(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA();
71 $self->discount(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT();
74 if ($type == SL::DB::Manager::PriceRule::PRICE_NEW()) {
75 return $self->price_as_number(@slurp)
76 } elsif ($type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
77 return $self->reduction_as_number(@slurp);
78 } elsif ($type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
79 return $self->discount_as_number(@slurp)
81 die 'unknown price_or_discount';
85 sub init_price_or_discount_state {
86 defined $_[0]->price ? SL::DB::Manager::PriceRule::PRICE_NEW()
87 : defined $_[0]->reduction ? SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()
88 : defined $_[0]->discount ? SL::DB::Manager::PriceRule::PRICE_DISCOUNT()
89 : SL::DB::Manager::PriceRule::PRICE_NEW();
96 push @errors, $::locale->text('The name must not be empty.') if !$self->name;
97 push @errors, $::locale->text('Price or discount must not be zero.') if !$self->price && !$self->discount && !$self->reduction;
98 push @errors, $::locale->text('Price rules must have at least one rule.') if !@{[ $self->items ]};
99 push @errors, $_->validate for $self->items;
104 sub clone_and_reset_deep {
107 my $clone = $self->clone_and_reset;
108 $clone->items(map { $_->clone_and_reset } $self->items);
114 sub full_description {
117 my $items = $self->item_summary;
118 my $price = $self->price_or_discount
119 ? t8('Discount #1%', $self->discount_as_number)
120 : t8('Price #1', $self->price_as_number);
122 sprintf "%s: %s (%s)", $self->name, $price, $items;
126 join ', ', map { $_->full_description } $_[0]->items;
132 # is use is in this case used by record_items for their current price source
133 # so, get any of those that might have it
134 require SL::DB::OrderItem;
135 require SL::DB::DeliveryOrderItem;
136 require SL::DB::InvoiceItem;
138 my $price_source_spec = 'price_rules' . '/' . $self->id;
140 SL::DB::Manager::OrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
141 || SL::DB::Manager::DeliveryOrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
142 || SL::DB::Manager::InvoiceItem->get_all_count(query => [ active_price_source => $price_source_spec ]);
145 sub priority_as_text {
148 return t8('Override') if $self->priority == 4;