1 package SL::PriceSource::Customer;
 
   4 use parent qw(SL::PriceSource::Base);
 
   7 use SL::PriceSource::Discount;
 
   8 use SL::Locale::String;
 
  10 sub name { 'customer_discount' }
 
  12 sub description { t8('Customer Discount') }
 
  14 sub available_prices { }
 
  16 sub available_discounts {
 
  17   my ($self, %params) = @_;
 
  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;
 
  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,
 
  32 sub price_from_source { }
 
  34 sub discount_from_source {
 
  35   my ($self, $source, $spec) = @_;
 
  37   my $customer = SL::DB::Customer->load_cached($spec);
 
  40     return SL::PriceSource::Discount->new(
 
  41       missing      => t8('Could not load this customer'),
 
  42       price_source => $self,
 
  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'),
 
  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),
 
  66   return SL::PriceSource::Discount->new(
 
  67     discount     => $customer->discount,
 
  68     spec         => $customer->id,
 
  69     description  => t8('Customer Discount'),
 
  70     price_source => $self,