ae4592bd97666618541fe8b9a52955f49e855ec4
[kivitendo-erp.git] / SL / PriceSource / Discount.pm
1 package SL::PriceSource::Discount;
2
3 use strict;
4
5 use parent 'SL::DB::Object';
6 use Rose::Object::MakeMethods::Generic (
7   scalar => [ qw(discount description spec price_source invalid missing) ],
8   'scalar --get_set_init' => [ qw(priority) ],
9 );
10
11 require SL::DB::Helper::Attr;
12 SL::DB::Helper::Attr::make(__PACKAGE__,
13   discount => 'numeric(15,5)',
14 );
15
16 sub source {
17   $_[0]->price_source
18   ? $_[0]->price_source->name . '/' . $_[0]->spec
19   : '';
20 }
21
22 sub full_description {
23   my ($self) = @_;
24
25   $self->price_source
26     ? $self->price_source->description . ': ' . $self->description
27     : $self->description
28 }
29
30 sub source_description {
31   my ($self) = @_;
32
33   $self->price_source
34     ? $self->price_source->description
35     : $self->description
36 }
37
38 sub to_str {
39   "source: @{[ $_[0]->source ]}, discount: @{[ $_[0]->discount ]}, description: @{[ $_[0]->description ]}"
40 }
41
42 sub init_priority {
43   3
44 }
45
46 1;
47
48 __END__
49
50 =encoding utf-8
51
52 =head1 NAME
53
54 SL::PriceSource::Discount - container to pass calculated discounts around
55
56 =head1 SYNOPSIS
57
58   # in PriceSource::Base implementation
59   $price = SL::PriceSource::Discount->new(
60     discount     => 10,
61     spec         => 'summersale2014', # something you can easily parse later
62     description  => t8('10% discount during summer sale 2014'),
63     price_source => $self,
64   )
65
66   # special empty discount in SL::PriceSource, for internal use.
67   SL::PriceSource::Discount->new(
68     description => t8('None (PriceSource)'),
69   );
70
71   # price can't be restored
72   SL::PriceSource::Discount->new(
73     missing      => t8('Um, sorry, cannot find that one'),
74     price_source => $self,
75   );
76
77   # invalid discount
78   SL::PriceSource::Discount->new(
79     discount     => $original_discount,
80     spec         => $original_spec,
81     description  => $original_description,
82     invalid      => t8('Offer expired #1 weeks ago', $dt->delta_weeks),
83     price_source => $self,
84   );
85
86 =head1 DESCRIPTION
87
88 See L<SL::PriceSource> for information about the mechanism.
89
90 This is a container for discounts that are generated by L<SL::PriceSource::Base>
91 implementations.
92
93 =head1 CONSTRUCTOR FIELDS
94
95 =over 4
96
97 =item C<discount>
98
99 The discount in percent. A discount of 0 will be ignored. If passed as
100 part of C<available_prices> it will be filtered out. If returned as
101 C<best_discount> or C<discount_from_source> it will trigger a warning.
102
103 =item C<spec>
104
105 A unique string that can later be understood by the creating implementation.
106 Can be empty if the implementation only supports one discount for a given
107 record_item.
108
109 =item C<description>
110
111 A localized short description of the origins of this discount.
112
113 =item C<price_source>
114
115 A ref to the creating algorithm.
116
117 =item C<priority>
118
119 OPTIONAL. Discounts may supply a numerical priority. Higher will trump over lower, even when
120 supplying lower discounts. Defaults to 3 (as in middle of 1-5).
121
122 =item C<missing>
123
124 OPTIONAL. Both indicator and localized message that the discount with this spec
125 could not be reproduced and should be changed.
126
127 If discount is missing, you do not need to supply anything except C<source>.
128
129 =item C<invalid>
130
131 OPTIONAL. Both indicator and localized message that the conditions for this
132 discount are no longer valid, and that the discount should be changed.
133
134 If discount is missing, you do not need to supply anything except C<source>.
135
136 =back
137
138 =head1 SEE ALSO
139
140 L<SL::PriceSource>,
141 L<SL::PriceSource::Base>,
142 L<SL::PriceSource::ALL>
143
144 =head1 BUGS
145
146 None yet. :)
147
148 =head1 AUTHOR
149
150 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
151
152 =cut