0ecc8b002ca53633f82fb479937745b1c5efe74e
[kivitendo-erp.git] / SL / PriceSource / Price.pm
1 package SL::PriceSource::Price;
2
3 use strict;
4
5 use parent 'SL::DB::Object';
6 use Rose::Object::MakeMethods::Generic (
7   scalar => [ qw(price 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   price => '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 ]}, price: @{[ $_[0]->price ]}, 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::Price - container to pass calculated prices around
55
56 =head1 SYNOPSIS
57
58   # in PriceSource::Base implementation
59   $price = SL::PriceSource::Price->new(
60     price        => 10.3,
61     spec         => '3', # something you can easily parse later
62     description  => t8('Fix price 10.3 for customer 3'),
63     price_source => $self,
64   )
65
66   # special empty price in SL::PriceSource, for internal use.
67   SL::PriceSource::Price->new(
68     description => t8('None (PriceSource)'),
69   );
70
71   # price can't be restored
72   SL::PriceSource::Price->new(
73     missing      => t8('Um, sorry, cannot find that one'),
74     price_source => $self,
75   );
76
77   # invalid price
78   SL::PriceSource::Price->new(
79     price        => $original_price,
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 prices that are generated by L<SL::PriceSource::Base>
91 implementations.
92
93 =head1 CONSTRUCTOR FIELDS
94
95 =over 4
96
97 =item C<price>
98
99 The price. A price of 0 is special and is considered undesirable. If passed as
100 part of C<available_prices> it will be filtered out. If returned as
101 C<best_price> or C<price_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 price for a given
107 record_item.
108
109 =item C<description>
110
111 A localized short description of the origins of this price.
112
113 =item C<price_source>
114
115 A ref to the creating algorithm.
116
117 =item C<priority>
118
119 OPTIONAL. Prices may supply a numerical priority. Higher will trump lower, even when
120 supplying higher prices. Defaults to 3 (as in middle of 1-5).
121
122 =item C<missing>
123
124 OPTIONAL. Both indicator and localized message that the price with this spec
125 could not be reproduced and should be changed.
126
127 If price 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 price are no longer valid, and that the price should be changed.
133
134 If price 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