PriceSource: Erste Version
[kivitendo-erp.git] / SL / PriceSource.pm
1 package SL::PriceSource;
2
3 use strict;
4 use parent 'SL::DB::Object';
5 use Rose::Object::MakeMethods::Generic (
6   scalar => [ qw(record_item) ],
7 );
8
9 use List::UtilsBy qw(min_by);
10 use SL::PriceSource::ALL;
11 use SL::PriceSource::Price;
12 use SL::Locale::String;
13
14 sub all_price_sources {
15   my ($self) = @_;
16
17   return map {
18     $_->new(record_item => $self->record_item)
19   } SL::PriceSource::ALL->all_price_sources
20 }
21
22 sub price_from_source {
23   my ($self, $source) = @_;
24   my ($source_name, $spec) = split m{/}, $source, 2;
25
26   my $class = SL::PriceSource::ALL->price_source_class_by_name($source_name);
27
28   return $class
29     ? $class->new(record_item => $self->record_item)->price_from_source($source, $spec)
30     : empty_price();
31 }
32
33 sub available_prices {
34   map { $_->available_prices } $_[0]->all_price_sources;
35 }
36
37 sub best_price {
38   min_by { $_->price } map { $_->best_price } $_[0]->all_price_sources;
39 }
40
41 sub empty_price {
42   SL::PriceSource::Price->new(
43     source      => '',
44     description => t8('None (PriceSource)'),
45   );
46 }
47
48 1;
49
50 __END__
51
52 =encoding utf-8
53
54 =head1 NAME
55
56 SL::PriceSource - mixin for price_sources in record items
57
58 =head1 SYNOPSIS
59
60   # in record item class
61
62   use SL::PriceSource;
63
64   # later on:
65
66   $record_item->all_price_sources
67   $record_item->price_source      # get
68   $record_item->price_source($c)  # set
69
70   $record_item->update_price_source # set price to calculated
71
72 =head1 DESCRIPTION
73
74 This mixin provides a way to use price_source objects from within a record item.
75 Record items in this contest mean OrderItems, InvoiceItems and
76 DeliveryOrderItems.
77
78 =head1 FUNCTIONS
79
80 price_sources
81
82 returns a list of price_source objects which are created with the current record
83 item.
84
85 active_price_source
86
87 returns the object representing the currently chosen price_source method or
88 undef if custom price is chosen. Note that this must not necessarily be the
89 active price, if something affecting the price_source has changed, the price
90 calculated can differ from the price in the record. It is the responsibility of
91 the implementing code to decide what to do in this case.
92
93 =head1 BUGS
94
95 None yet. :)
96
97 =head1 AUTHOR
98
99 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
100
101 =cut