1 package SL::PriceSource;
4 use parent 'SL::DB::Object';
5 use Rose::Object::MakeMethods::Generic (
6 scalar => [ qw(record_item record) ],
7 'array --get_set_init' => [ qw(all_price_sources) ],
10 use List::UtilsBy qw(min_by max_by);
11 use SL::PriceSource::ALL;
12 use SL::PriceSource::Price;
13 use SL::Locale::String;
15 sub init_all_price_sources {
19 $_->new(record_item => $self->record_item, record => $self->record)
20 } SL::PriceSource::ALL->all_enabled_price_sources ]
23 sub price_from_source {
24 my ($self, $source) = @_;
25 my ($source_name, $spec) = split m{/}, $source, 2;
27 my $class = SL::PriceSource::ALL->price_source_class_by_name($source_name);
30 ? $class->new(record_item => $self->record_item, record => $self->record)->price_from_source($source, $spec)
34 sub discount_from_source {
35 my ($self, $source) = @_;
36 my ($source_name, $spec) = split m{/}, $source, 2;
38 my $class = SL::PriceSource::ALL->price_source_class_by_name($source_name);
41 ? $class->new(record_item => $self->record_item, record => $self->record)->discount_from_source($source, $spec)
45 sub available_prices {
46 map { $_->available_prices } $_[0]->all_price_sources;
49 sub available_discounts {
50 return if $_[0]->record_item->part->not_discountable;
51 map { $_->available_discounts } $_[0]->all_price_sources;
55 min_by { $_->price } max_by { $_->priority } grep { $_->price > 0 } grep { $_ } map { $_->best_price } $_[0]->all_price_sources;
59 max_by { $_->discount } max_by { $_->priority } grep { $_->discount } grep { $_ } map { $_->best_discount } $_[0]->all_price_sources;
63 SL::PriceSource::Price->new(
64 description => t8('None (PriceSource)'),
69 SL::PriceSource::Discount->new(
70 description => t8('None (PriceSource Discount)'),
82 SL::PriceSource - mixin for price_sources in record items
86 PriceSource is an interface that allows generic algorithms to be plugged
87 together to calculate available prices for a position in a record.
89 Each algorithm can access details of the record to realize dependencies on
90 part, customer, vendor, date, quantity etc, which was previously not possible.
92 =head1 BACKGROUND AND PHILOSOPHY
94 sql ledger and subsequently Lx-Office had three prices per part: sellprice,
95 listprice and lastcost. When adding an item to a record, the applicable price
96 was copied and after that it was free to be changed.
98 Later on additional things were added. Various types of discount, vendor pricelists
99 and the infamous price groups. The problem was not that those didn't work, the
100 problem was they had to guess too much when to change a price with the
101 available price from the database, and when to leave the user entered price.
103 The result was that the price of an item in a record seemed to change on a
104 whim, and the origin of the price itself being opaque.
106 Unrelated to that, users asked for more ways to store special prices, based on
107 qty (block pricing, bulk discount), based on date (special offers), based on
108 customers (special terms), up to full blown calculation modules.
110 On a third front sales personnel asked for ways to see what price options a
111 position in a quotation has, and wanted information available when prices
112 changed to make better informed choices about sales later in the workflow.
114 Price sources now extend the previous pricing by attaching a source to every
115 price in records. The information it provides are:
121 Where did this price originate?
125 If this price would be calculated today, is it still the same as it was when
126 this record was created?
130 If I want to price an item in this record now, which prices are available?
134 Which one is the "best"?
140 To ensure price source prices are comprehensible and reproducible, some
141 invariants are guaranteed:
147 Price sources will never on their own change a price. They will offer options,
148 and it is up to the user to change a price.
152 If a price is set from a source then the system will try to prevent the user
153 from messing it up. By default this means the price will be read-only.
154 Implementations can choose to make prices editable, but even then deviations
155 from the calculatied price will be marked.
157 A price that is not set from a source will not have any of this.
161 A price should be able to repeat the calculations done to arrive at the price
162 when it was first used. If these calculations are no longer applicable (special
163 offer expired) this should be signalled. If the calculations result in a
164 different price, this should be signalled. If the calculations fail (needed
165 information is no longer present) this must be signalled.
169 The first point creates user security by never changing a price for them
170 without their explicit consent, eliminating all problems originating from
171 trying to be smart. The second and third one ensure that later on the
172 calculation can be repeated so that invalid prices can be caught (because for
173 example the special offer is no longer valid), and so that sales personnel have
174 information about rising or falling prices.
178 Price sources are managed by this package (L<SL::PriceSource>), and all
179 external access should be by using its interface.
181 Each source is an instance of L<SL::PriceSource::Base> and the available
182 implementations are recorded in L<SL::PriceSource::ALL>. Prices and discounts
183 returned by interface methods are instances of L<SL::PriceSource::Price> and
184 L<SL::PriceSource::Discount>.
186 Returned prices and discounts should be checked for entries in C<invalid> and
187 C<missing>, see documentation in their classes.
189 =head1 INTERFACE METHODS
195 C<PARAMS> must contain both C<record> and C<record_item>. C<record_item> does
196 not have to be registered in C<record>.
198 =item C<price_from_source>
200 Attempts to retrieve a formerly calculated price with the same conditions
202 =item C<discount_from_source>
204 Attempts to retrieve a formerly calculated discount with the same conditions
206 =item C<available_prices>
208 Returns all available prices.
210 =item C<available_discounts>
212 Returns all available discounts.
216 Attempts to get the best available price. returns L<empty_price> if no price is
219 =item C<best_discount>
221 Attempts to get the best available discount. returns L<empty_discount> if no
226 A special empty price that does not change the previously entered price and
227 opens the price field to manual changes.
229 =item C<empty_discount>
231 A special empty discount that does not change the previously entered discount
232 and opens the discount field to manual changes.
238 L<SL::PriceSource::Base>,
239 L<SL::PriceSource::Price>,
240 L<SL::PriceSource::Discount>,
241 L<SL::PriceSource::ALL>
243 =head1 BUGS AND CAVEATS
249 The current model of price sources requires a record and a record_item for
250 every price calculation. This means that price structures can never be used
251 when no record is available, such as calculation the worth of assembly rows.
253 A possible solution is to either split price sources into simple and complex
254 ones (where the former do not require records).
256 Another would be to have default values for the input normally taken from
257 records (like qty defaulting to 1).
259 A last one would be to provide an alternative input channel for needed
264 Discount sources were implemented as a copy of the prices with slightly
265 different semantics. Need to do a real design. A requirement is, that a single
266 source can provide both prices and discounts (needed for price_rules).
270 Priorities are implemented ad hoc. The semantics which are chosen by the "best"
271 accessors are unintuitive because they do not guarantee anything. Better
272 terminology might help.
276 It is currently not possible to link a price to the price of the generating
277 record_item (i.e. the price of a delivery order item to the order item it was
278 generated from). This is crucial to enterprises that calculate all their prices
279 in orders, and update those after they made delivery orders.
283 Currently it is only possible to provide additional prices, but not to restrict
284 prices. Potential scenarios include credit limit customers which do not receive
285 benefits from sales, or general ALLOW, DENY order calculation.
289 Composing price sources is disallowed for clarity, but all price sources need
290 to be aware of units and price_factors. This is madness.
294 The current implementation of lastcost is useless. Since it's one of the
295 master_data prices it will always compete with listprice. But in real scenarios
296 the listprice tends to go up, while lastcost stays the same, so lastcost
297 usually wins. Lastcost could be lower priority, but a better design would be
302 Guarantee 1 states that price sources will never change prices on their own.
303 Further testing in the wild has shown that this is desirable within a record,
304 but not when copying items from one record to another within a workflow.
306 Specifically when changing from sales to purchase records prices don't make
307 sense anymore. The guarantees should be updated to reflect this and
308 transposition guidelines should be documented.
310 The previously mentioned linked prices can emulated by allowing price sources
311 to set a new price when changing to a new record in the workflow. The decision
312 about whether a price is eligable to be set can be suggested by the price
313 source implementation but is ultimately up to the surrounding framework, which
314 can make this configurable.
318 Prices were originally planned as a context element rather than a modal popup.
319 It would be great to have this now with better framework.
323 Large records (30 positions or more) in combination with complicated price
324 sources run into n+1 problems. There should be an extra hook that allows price
325 source implementations to make bulk calculations before the actual position loop.
331 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>