CreatePeriodicInvoices-Job: for anstelle von map für reine Nebeneffekt-Schleifen
[kivitendo-erp.git] / SL / PriceSource / Customer.pm
1 package SL::PriceSource::Customer;
2
3 use strict;
4 use parent qw(SL::PriceSource::Base);
5
6 use SL::DB::Customer;
7 use SL::PriceSource::Discount;
8 use SL::Locale::String;
9
10 sub name { 'customer_discount' }
11
12 sub description { t8('Customer Discount') }
13
14 sub available_prices { }
15
16 sub available_discounts {
17   my ($self, %params) = @_;
18
19   return unless $self->record->is_sales;
20   return unless $self->record->customer;
21   return unless $self->record->customer->discount != 0;
22
23   SL::PriceSource::Discount->new(
24     discount     => $self->record->customer->discount,
25     spec         => $self->record->customer->id,
26     description  => t8('Customer Discount'),
27     price_source => $self,
28   );
29 }
30
31 sub price_from_source {
32   my ($self, $source, $spec) = @_;
33
34   my $customer = SL::DB::Customer->load_cached($spec);
35
36   if (!$customer) {
37     return SL::PriceSource::Discount->new(
38       missing      => t8('Could not load this customer'),
39       price_source => $self,
40     )
41   }
42
43   if (!$self->record->customer) {
44     return SL::PriceSource::Discount->new(
45       discount     => $customer->discount,
46       spec         => $customer->id,
47       description  => t8('Customer Discount'),
48       price_source => $self,
49       invalid      => t8('This discount is only valid in sales documents'),
50     )
51   }
52
53   if ($customer->id != $self->record->customer->id) {
54     return SL::PriceSource::Discount->new(
55       discount     => $customer->discount,
56       spec         => $customer->id,
57       description  => t8('Customer Discount'),
58       price_source => $self,
59       invalid      => t8('This discount is only valid for customer #1', $customer->full_description),
60     )
61   }
62
63   return SL::PriceSource::Discount->new(
64     discount     => $customer->discount,
65     spec         => $customer->id,
66     description  => t8('Customer Discount'),
67     price_source => $self,
68   );
69 }
70
71 sub best_price { }
72
73 sub best_discount {
74   &available_discounts;
75 }
76
77 1;
78