1c43d4a966ce96acd12006392f92c73324657c19
[kivitendo-erp.git] / SL / DB / PriceRule.pm
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.
3
4 package SL::DB::PriceRule;
5
6 use strict;
7
8 use SL::DB::MetaSetup::PriceRule;
9 use SL::DB::Manager::PriceRule;
10 use SL::Locale::String qw(t8);
11
12 __PACKAGE__->meta->add_relationship(
13   items => {
14     type         => 'one to many',
15     class        => 'SL::DB::PriceRuleItem',
16     column_map   => { id => 'price_rules_id' },
17   },
18 );
19
20 __PACKAGE__->meta->initialize;
21
22 use Rose::Object::MakeMethods::Generic (
23   'scalar --get_set_init' => [ qw(price_or_discount_state) ],
24 );
25
26 sub match {
27   my ($self, %params) = @_;
28
29   die 'need record'      unless $params{record};
30   die 'need record_item' unless $params{record_item};
31
32   for ($self->items) {
33     next if $_->match(%params);
34     # TODO save for error
35     return
36   }
37
38   return 1;
39 }
40
41 sub is_sales {
42     $_[0]->type eq 'customer' ? 1
43   : $_[0]->type eq 'vendor'   ? 0 : do { die 'wrong type' };
44 }
45
46 sub price_type {
47   my ($self, $value) = @_;
48
49   if (@_ > 1) {
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);
57     } else {
58       die 'unknown price_or_discount value';
59     }
60     $self->price_or_discount_state($value);
61   }
62   $self->price_or_discount_state;
63 }
64
65 sub price_or_discount_as_number {
66   my ($self, @slurp) = @_;
67   my $type = $self->price_type;
68
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();
72
73
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)
80   } else {
81     die 'unknown price_or_discount';
82   }
83 }
84
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();
90 }
91
92 sub validate {
93   my ($self) = @_;
94
95   my @errors;
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;
100
101   return @errors;
102 }
103
104 sub clone_and_reset_deep {
105   my ($self) = @_;
106
107   my $clone = $self->clone_and_reset;
108   $clone->items(map { $_->clone_and_reset } $self->items);
109   $clone->name('');
110
111   return $clone;
112 }
113
114 sub full_description {
115   my ($self) = @_;
116
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);
121
122   sprintf "%s: %s (%s)", $self->name, $price, $items;
123 }
124
125 sub item_summary {
126   join ', ', map { $_->full_description } $_[0]->items;
127 }
128
129 sub in_use {
130   my ($self) = @_;
131
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;
137
138   my $price_source_spec = 'price_rules' . '/' . $self->id;
139
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 ]);
143 }
144
145 sub priority_as_text {
146   my ($self) = @_;
147
148   return t8('Override') if $self->priority == 4;
149   t8('Normal');
150 }
151
152
153 1;