1 package SL::PriceSource;
 
   4 use parent 'SL::DB::Object';
 
   5 use Rose::Object::MakeMethods::Generic (
 
   6   scalar => [ qw(record_item record) ],
 
   9 use List::UtilsBy qw(min_by);
 
  10 use SL::PriceSource::ALL;
 
  11 use SL::PriceSource::Price;
 
  12 use SL::Locale::String;
 
  14 sub all_price_sources {
 
  18     $_->new(record_item => $self->record_item, record => $self->record)
 
  19   } SL::PriceSource::ALL->all_enabled_price_sources
 
  22 sub price_from_source {
 
  23   my ($self, $source) = @_;
 
  24   my ($source_name, $spec) = split m{/}, $source, 2;
 
  26   my $class = SL::PriceSource::ALL->price_source_class_by_name($source_name);
 
  29     ? $class->new(record_item => $self->record_item)->price_from_source($source, $spec)
 
  33 sub available_prices {
 
  34   map { $_->available_prices } $_[0]->all_price_sources;
 
  38   min_by { $_->price } grep { $_->price > 0 } map { $_->best_price } $_[0]->all_price_sources;
 
  42   SL::PriceSource::Price->new(
 
  43     description => t8('None (PriceSource)'),
 
  55 SL::PriceSource - mixin for price_sources in record items
 
  59 PriceSource is an interface that allows generic algorithms to be plugged
 
  60 together to calculate available prices for a position in a record.
 
  62 Each algorithm can access details of the record to realize dependencies on
 
  63 part, customer, vendor, date, quantity etc, which was previously not possible.
 
  65 =head1 BACKGROUND AND PHILOSOPY
 
  67 sql ledger and subsequently Lx-Office had three prices per part: sellprice,
 
  68 listprice and lastcost. At the moment a part is loaded into a record, the
 
  69 applicable price is copied and after that free to be changed.
 
  71 Later on additional things joined. Various types of discount, vendor pricelists
 
  72 and the infamous price groups. The problem is not that those didn't work, the
 
  73 problem is, that they had to guess to much when to change a price with the
 
  74 available price from database, and when to leave the user entered price.
 
  76 Unrelated to that, users asked for more ways to store special prices, based on
 
  77 qty (block pricing, bulk discount), based on date (special offers), based on
 
  78 customers (special terms), up to full blown calculation modules.
 
  80 On a third front sales personnel asked for ways to see what price options a
 
  81 position in a quotation has, and wanted information available when a price
 
  84 Price sources put that together by making some compromises:
 
  90 Only change the price on creation of a position or when asked to.
 
  94 Either set the price from a price source and let it be read only, or use a free
 
  99 Save the origin of each price with the record so that the calculation can be
 
 104 Make price calculation flexible and pluggable.
 
 108 The first point creates user security by never changing a price for them
 
 109 without their explicit consent, eliminating all problems originating from
 
 110 trying to be smart. The second and third one ensure that later on the
 
 111 calculation can be repeated so that invalid prices can be caught (because for
 
 112 example the special offer is no longer valid), and so that sales personnel have
 
 113 information about rising or falling prices. The fourth point ensures that
 
 114 insular calculation processes can be developed independent of the core code.
 
 116 =head1 INTERFACE METHODS
 
 122 C<PARAMS> must contain both C<record> and C<record_item>. C<record_item> does
 
 123 not have to be registered in C<record>.
 
 125 =item C<price_from_source>
 
 127 Attempts to retrieve a formerly calculated price with the same conditions
 
 129 =item C<available_prices>
 
 131 Returns all available prices.
 
 135 Attempts to get the best available price. returns L<empty_price> if no price is found.
 
 139 A special empty price, that does not change the previously entered price, and
 
 140 opens the price field to manual changes.
 
 146 L<SL::PriceSource::Base>,
 
 147 L<SL::PriceSource::Price>,
 
 148 L<SL::PriceSource::ALL>
 
 156 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>