Projekt

Allgemein

Profil

Herunterladen (1,92 KB) Statistiken
| Zweig: | Markierung: | Revision:
4fd22b56 Sven Schöling
package SL::DB::Pricegroup;

use strict;

use SL::DB::MetaSetup::Pricegroup;
feef731c Moritz Bunkus
use SL::DB::Manager::Pricegroup;
e48eb4dc Geoffrey Richardson
use SL::DB::Helper::ActsAsList;
4fd22b56 Sven Schöling
2d7e4203 Sven Schöling
__PACKAGE__->meta->initialize;
3a21f30f Jan Büren
__PACKAGE__->before_save('_before_save_remove_customer_pricegroup');
2d7e4203 Sven Schöling
818a31fa Sven Schöling
sub displayable_name {
my $self = shift;

return join ' ', grep $_, $self->id, $self->pricegroup;
}

3a21f30f Jan Büren
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;
}

e48eb4dc Geoffrey Richardson
sub validate {
my ($self) = @_;
9ffd6eed Sven Schöling
require SL::DB::Customer;
e48eb4dc Geoffrey Richardson
my @errors;
3a21f30f Jan Büren
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.');
e48eb4dc Geoffrey Richardson
}

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;
}
818a31fa Sven Schöling
4fd22b56 Sven Schöling
1;