52d35ee926c49064aa5f4a02964d6e66b05aece5
[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 if     $self->part->not_discountable;
20   return unless $self->record->is_sales;
21   return unless $self->record->customer;
22   return unless $self->record->customer->discount != 0;
23
24   SL::PriceSource::Discount->new(
25     discount     => $self->record->customer->discount,
26     spec         => $self->record->customer->id,
27     description  => t8('Customer Discount'),
28     price_source => $self,
29   );
30 }
31
32 sub price_from_source {
33   my ($self, $source, $spec) = @_;
34
35   my $customer = SL::DB::Customer->load_cached($spec);
36
37   if (!$customer) {
38     return SL::PriceSource::Discount->new(
39       missing      => t8('Could not load this customer'),
40       price_source => $self,
41     )
42   }
43
44   if (!$self->record->customer) {
45     return SL::PriceSource::Discount->new(
46       discount     => $customer->discount,
47       spec         => $customer->id,
48       description  => t8('Customer Discount'),
49       price_source => $self,
50       invalid      => t8('This discount is only valid in sales documents'),
51     )
52   }
53
54   if ($customer->id != $self->record->customer->id) {
55     return SL::PriceSource::Discount->new(
56       discount     => $customer->discount,
57       spec         => $customer->id,
58       description  => t8('Customer Discount'),
59       price_source => $self,
60       invalid      => t8('This discount is only valid for customer #1', $customer->full_description),
61     )
62   }
63
64   return SL::PriceSource::Discount->new(
65     discount     => $customer->discount,
66     spec         => $customer->id,
67     description  => t8('Customer Discount'),
68     price_source => $self,
69   );
70 }
71
72 sub best_price { }
73
74 sub best_discount {
75   &available_discounts;
76 }
77
78 1;
79