X-Git-Url: http://wagnertech.de/git?p=kivitendo-erp.git;a=blobdiff_plain;f=SL%2FDB%2FPricegroup.pm;fp=SL%2FDB%2FPricegroup.pm;h=a7cfc205398919fde7f3410841029e5ffbdc9c52;hp=b3183005f3238b7a6d071374c0436ce149dcf59f;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hpb=deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44 diff --git a/SL/DB/Pricegroup.pm b/SL/DB/Pricegroup.pm index b3183005f..a7cfc2053 100644 --- a/SL/DB/Pricegroup.pm +++ b/SL/DB/Pricegroup.pm @@ -4,8 +4,10 @@ use strict; use SL::DB::MetaSetup::Pricegroup; use SL::DB::Manager::Pricegroup; +use SL::DB::Helper::ActsAsList; __PACKAGE__->meta->initialize; +__PACKAGE__->before_save('_before_save_remove_customer_pricegroup'); sub displayable_name { my $self = shift; @@ -13,5 +15,69 @@ sub displayable_name { return join ' ', grep $_, $self->id, $self->pricegroup; } +sub _before_save_remove_customer_pricegroup { + my ($self) = @_; + + return 1 unless $::form->{SELF}{remove_customer_pricegroup}; + + my %attributes = (pricegroup_id => undef); + require SL::DB::Customer; + SL::DB::Manager::Customer->update_all( + set => \%attributes, + where => [ + 'pricegroup_id' => $self->id, + ], + ); + + return 1; +} + +sub validate { + my ($self) = @_; + require SL::DB::Customer; + + my @errors; + if (!$::form->{SELF}{remove_customer_pricegroup} && + $self->obsolete && + SL::DB::Manager::Customer->get_all_count(query => [ pricegroup_id => $self->id ]) ) { + push @errors, $::locale->text('The pricegroup is being used by customers.'); + } + + return @errors; +} + +sub orphaned { + my ($self) = @_; + die 'not an accessor' if @_ > 1; + + return 1 unless $self->id; + + my @relations = qw( + SL::DB::Customer + SL::DB::Price + ); + + # check if pricegroup is the default pricegroup for any customers and has any + # prices assigned. + + for my $class (@relations) { + eval "require $class"; + return 0 if $class->_get_manager_class->get_all_count(query => [ pricegroup_id => $self->id ]); + } + + # check if pricegroup was used in any pricesource + my @item_relations = qw( + SL::DB::OrderItem + SL::DB::DeliveryOrderItem + SL::DB::InvoiceItem + ); + + for my $class (@item_relations) { + eval "require $class"; + return 0 if $class->_get_manager_class->get_all_count(query => [ active_price_source => 'pricegroup/' . $self->id ]); + } + + return 1; +} 1;