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