Rechnungsmaske: "Browser-Zurück verhindern" konfigurierbar in Mandantenkonfig
[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 __PACKAGE__->before_save('_before_save_remove_customer_pricegroup');
11
12 sub displayable_name {
13   my $self = shift;
14
15   return join ' ', grep $_, $self->id, $self->pricegroup;
16 }
17
18 sub _before_save_remove_customer_pricegroup {
19   my ($self) = @_;
20
21   return 1 unless $::form->{SELF}{remove_customer_pricegroup};
22
23   my %attributes          = (pricegroup_id => undef);
24   require SL::DB::Customer;
25   SL::DB::Manager::Customer->update_all(
26     set   => \%attributes,
27     where => [
28       'pricegroup_id' => $self->id,
29     ],
30   );
31
32   return 1;
33 }
34
35 sub validate {
36   my ($self) = @_;
37   require SL::DB::Customer;
38
39   my @errors;
40   if (!$::form->{SELF}{remove_customer_pricegroup}                                    &&
41       $self->obsolete                                                                 &&
42       SL::DB::Manager::Customer->get_all_count(query => [ pricegroup_id => $self->id ]) ) {
43       push @errors, $::locale->text('The pricegroup is being used by customers.');
44   }
45
46   return @errors;
47 }
48
49 sub orphaned {
50   my ($self) = @_;
51   die 'not an accessor' if @_ > 1;
52
53   return 1 unless $self->id;
54
55   my @relations = qw(
56     SL::DB::Customer
57     SL::DB::Price
58   );
59
60   # check if pricegroup is the default pricegroup for any customers and has any
61   # prices assigned.
62
63   for my $class (@relations) {
64     eval "require $class";
65     return 0 if $class->_get_manager_class->get_all_count(query => [ pricegroup_id => $self->id ]);
66   }
67
68   # check if pricegroup was used in any pricesource
69   my @item_relations = qw(
70     SL::DB::OrderItem
71     SL::DB::DeliveryOrderItem
72     SL::DB::InvoiceItem
73   );
74
75   for my $class (@item_relations) {
76     eval "require $class";
77     return 0 if $class->_get_manager_class->get_all_count(query => [ active_price_source => 'pricegroup/' . $self->id ]);
78   }
79
80   return 1;
81 }
82
83 1;