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