X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/kivitendo-erp.git/blobdiff_plain/c653b98f17446bf3dab478e5361b1bc6df822e61..89b2668811eac6023ad58322e2f9970ddb6a27c9:/SL/PriceSource/Customer.pm diff --git a/SL/PriceSource/Customer.pm b/SL/PriceSource/Customer.pm new file mode 100644 index 000000000..6bf06bc30 --- /dev/null +++ b/SL/PriceSource/Customer.pm @@ -0,0 +1,78 @@ +package SL::PriceSource::Customer; + +use strict; +use parent qw(SL::PriceSource::Base); + +use SL::DB::Customer; +use SL::PriceSource::Discount; +use SL::Locale::String; + +sub name { 'customer_discount' } + +sub description { t8('Customer Discount') } + +sub available_prices { } + +sub available_discounts { + my ($self, %params) = @_; + + return unless $self->record->is_sales; + return unless $self->record->customer; + return unless $self->record->customer->discount != 0; + + SL::PriceSource::Discount->new( + discount => $self->record->customer->discount, + spec => $self->record->customer->id, + description => t8('Customer Discount'), + price_source => $self, + ); +} + +sub price_from_source { + my ($self, $source, $spec) = @_; + + my $customer = SL::DB::Customer->load_cached($spec); + + if (!$customer) { + return SL::PriceSource::Discount->new( + missing => t8('Could not load this customer'), + price_source => $self, + ) + } + + if (!$self->record->customer) { + return SL::PriceSource::Discount->new( + discount => $customer->discount, + spec => $customer->id, + description => t8('Customer Discount'), + price_source => $self, + invalid => t8('This discount is only valid in sales documents'), + ) + } + + if ($customer->id != $self->record->customer->id) { + return SL::PriceSource::Discount->new( + discount => $customer->discount, + spec => $customer->id, + description => t8('Customer Discount'), + price_source => $self, + invalid => t8('This discount is only valid for customer #1', $customer->full_description), + ) + } + + return SL::PriceSource::Discount->new( + discount => $customer->discount, + spec => $customer->id, + description => t8('Customer Discount'), + price_source => $self, + ); +} + +sub best_price { } + +sub best_discount { + &available_discounts; +} + +1; +