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;
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('Pirce rules must have at least one rule.') if !@{[ $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;