1 package SL::PriceSource::Base;
5 use parent qw(SL::DB::Object);
6 use Rose::Object::MakeMethods::Generic (
7 scalar => [ qw(record_item record) ],
10 sub name { die 'name needs to be implemented' }
12 sub description { die 'description needs to be implemented' }
14 sub available_prices { die 'available_prices needs to be implemented' }
16 sub available_discounts { die 'available_discounts needs to be implemented' }
18 sub best_price { die 'best_price needs to be implemented' }
20 sub best_discounts { die 'best_discounts needs to be implemented' }
22 sub price_from_source { die 'price_from_source needs to be implemented:' . "@_" }
24 sub discount_from_source { die 'discount_from_source needs to be implemented:' . "@_" }
27 $_[0]->record_item->part;
31 $_[0]->record->is_sales ? $_[0]->record->customer : $_[0]->record->vendor;
42 SL::PriceSource::Base - this is the base class for price source adapters
46 # working example adapter:
47 package SL::PriceSource::FiveOnEverything;
49 use parent qw(SL::PriceSource::Base);
51 # used as internal identifier
54 # used in frontend to signal where this comes from
55 sub description { t8('Simple') }
57 my $price = SL::PriceSource::Price->new(
59 description => t8('Only today 5$ on everything!'),
60 price_source => $self,
63 # give list of prices that this
64 sub available_prices {
72 sub price_from_source {
78 See L<SL::PriceSource> for information about the mechanism.
80 This is the base class for a price source algorithm. To play well, you'll have
81 to implement a number of interface methods and be aware of a number of corner
84 =head1 AVAILABLE METHODS
92 C<record> can be any one of L<SL::DB::Order>, L<SL::DB::DeliveryOrder>,
93 L<SL::DB::Invoice>, L<SL::DB::PurchaseInvoice>. C<record_item> is of the
94 corresponding position type.
96 You can assume that both are filled with all information available at the time.
97 C<part> and C<customer>/C<vendor> as well as C<is_sales> can be relied upon. You must NOT
98 rely on both being linked together, in particular
100 $self->record_item->record # don't do that
102 is not guaranteed to work.
104 Also these are copies and not the original documents. Do not try to change
105 anything and do not save those.
109 Shortcut to C<< record_item->part >>
111 =item C<customer_vendor>
113 Shortcut to C<< record->is_sales ? record->customer : record->vendor >>
117 =head1 INTERFACE METHODS
123 Must return a unique internal name. Must be entered in
124 L<SL::PriceSource::ALL>.
128 Must return a translated name to be used in the frontend. Will be used to
129 distinguish the origin of different prices.
131 =item C<available_prices>
133 Must return a list of all prices that your algorithm can recommend to the user
134 for the current situation. Each price must have a unique spec that can be used
135 to recreate it later. Try to be brief, no one needs 20 different price
138 =item C<available_discounts>
140 Must return a list of all prices that your algorithm can recommend to the user
141 for the current situation. Each discount must have a unique spec that can be
142 used to recreate it later. Try to be brief, no one needs 20 different discount
147 Must return what you think of as the best matching price in your
148 C<available_prices>. This does not have to be the lowest price, but it will be
149 compared later to other price sources, and the lowest will be set.
151 =item C<best_discount>
153 Must return what you think of as the best matching discount in your
154 C<available_discounts>. This does not have to be the highest discount, but it
155 will be compared later to other price sources, and the highest will be set.
157 =item C<price_from_source SOURCE, SPEC>
159 Must recreate the price or discount from C<SPEC> and return. For reference, the
160 complete C<SOURCE> entry from C<record_item.active_price_source> or
161 C<record_item.active_discount_source> is included.
163 Note that constraints from the rest of the C<record> do not apply anymore. If
164 information needed for the retrieval can be deleted elsewhere, then you must
167 If the price for the same conditions changed, return the new price. It will be
168 presented as an option to the user if the record is still editable.
170 If the price is not valid anymore or not reconstructable, return a price with
171 C<price_source> and C<spec> set to the same values as before but with
172 C<invalid> or C<missing> set.
176 =head1 TRAPS AND CORNER CASES
182 Be aware that all 8 types of record will be passed to your algorithm. If you
183 don't serve some of them, just return empty lists on C<available_prices> and
188 Information in C<record> might be missing. Especially on newly or automatically
189 created records there might be fields not set at all.
193 Records will not be calculated. If you need tax data or position totals, you
194 need to invoke that yourself.
198 Accessor methods might not be present in some of the record types.
202 You do not need to do price factor and row discount calculation. These will be
203 done automatically afterwards. You do have to include customer/vendor discounts
204 if your price interacts with those.
208 The price field in purchase records is still C<sellprice>.
212 C<source> and C<spec> are tainted. If you store data directly in C<spec>, sanitize.
219 L<SL::PriceSource::Price>,
220 L<SL::PriceSource::ALL>
228 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>