test action
[kivitendo-erp.git] / SL / DB / Pricegroup.pm
1 package SL::DB::Pricegroup;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Pricegroup;
6 use SL::DB::Manager::Pricegroup;
7 use SL::DB::Helper::ActsAsList;
8
9 __PACKAGE__->meta->initialize;
10
11 sub displayable_name {
12   my $self = shift;
13
14   return join ' ', grep $_, $self->id, $self->pricegroup;
15 }
16
17 sub validate {
18   my ($self) = @_;
19   require SL::DB::Customer;
20
21   my @errors;
22
23   if ( $self->obsolete && SL::DB::Manager::Customer->get_all_count(query => [ pricegroup_id => $self->id ]) ) {
24     push @errors, $::locale->text('The pricegroup is being used by customers.');
25   }
26
27   return @errors;
28 }
29
30 sub orphaned {
31   my ($self) = @_;
32   die 'not an accessor' if @_ > 1;
33
34   return 1 unless $self->id;
35
36   my @relations = qw(
37     SL::DB::Customer
38     SL::DB::Price
39   );
40
41   # check if pricegroup is the default pricegroup for any customers and has any
42   # prices assigned.
43
44   for my $class (@relations) {
45     eval "require $class";
46     return 0 if $class->_get_manager_class->get_all_count(query => [ pricegroup_id => $self->id ]);
47   }
48
49   # check if pricegroup was used in any pricesource
50   my @item_relations = qw(
51     SL::DB::OrderItem
52     SL::DB::DeliveryOrderItem
53     SL::DB::InvoiceItem
54   );
55
56   for my $class (@item_relations) {
57     eval "require $class";
58     return 0 if $class->_get_manager_class->get_all_count(query => [ active_price_source => 'pricegroup/' . $self->id ]);
59   }
60
61   return 1;
62 }
63
64 1;