SimpleSystemSetting: Umstellung von »Bankkonten«
[kivitendo-erp.git] / SL / Controller / PartsGroup.pm
1 package SL::Controller::PartsGroup;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Helper::Flash;
8 use SL::Locale::String;
9 use SL::DB::Default;
10 use SL::DB::Manager::PartsGroup;
11
12 use Rose::Object::MakeMethods::Generic (
13   scalar                  => [ qw(partsgroup) ],
14   'scalar --get_set_init' => [ qw(all_partsgroups) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_partsgroup', only => [ qw(edit update delete) ]);
19
20 #
21 # actions
22 #
23
24 sub action_list {
25   my ($self) = @_;
26
27   $self->render('partsgroup/list',
28                 title   => t8('Partsgroups'),
29                );
30 }
31
32 sub action_new {
33   my ($self) = @_;
34
35   $self->partsgroup( SL::DB::PartsGroup->new );
36   $self->render('partsgroup/form',
37                  title => t8('Add partsgroup'),
38                );
39 }
40
41 sub action_edit {
42   my ($self) = @_;
43
44   $self->render('partsgroup/form',
45                  title   => t8('Edit partsgroup'),
46                 );
47 }
48
49 sub action_create {
50   my ($self) = @_;
51
52   $self->partsgroup( SL::DB::PartsGroup->new );
53   $self->create_or_update;
54 }
55
56 sub action_update {
57   my ($self) = @_;
58   $self->create_or_update;
59 }
60
61 sub action_delete {
62   my ($self) = @_;
63
64   if ( !$self->partsgroup->orphaned ) {
65     flash_later('error', $::locale->text('The partsgroup has been used and cannot be deleted.'));
66   } elsif ( eval { $self->partsgroup->delete; 1; } ) {
67     flash_later('info',  $::locale->text('The partsgroup has been deleted.'));
68   } else {
69     flash_later('error', $::locale->text('The partsgroup has been used and cannot be deleted.'));
70   };
71   $self->redirect_to(action => 'list');
72 }
73
74 sub action_reorder {
75   my ($self) = @_;
76
77   SL::DB::PartsGroup->reorder_list(@{ $::form->{partsgroup_id} || [] });
78   $self->render(\'', { type => 'json' });
79 }
80
81 #
82 # filters
83 #
84
85 sub check_auth {
86   $::auth->assert('config');
87 }
88
89 sub load_partsgroup {
90   my ($self) = @_;
91
92   $self->partsgroup( SL::DB::PartsGroup->new(id => $::form->{id})->load );
93 }
94
95 sub init_all_partsgroups { SL::DB::Manager::PartsGroup->get_all_sorted }
96
97 #
98 # helpers
99 #
100
101 sub create_or_update {
102   my ($self) = @_;
103   my $is_new = !$self->partsgroup->id;
104
105   my $params = delete($::form->{partsgroup}) || { };
106
107   $self->partsgroup->assign_attributes(%{ $params });
108
109   my @errors = $self->partsgroup->validate;
110
111   if (@errors) {
112     flash('error', @errors);
113     $self->render('partsgroup/form',
114                    title => $is_new ? t8('Add partsgroup') : t8('Edit partsgroup'),
115                  );
116     return;
117   }
118
119   $self->partsgroup->save;
120
121   flash_later('info', $is_new ? t8('The partsgroup has been created.') : t8('The partsgroup has been saved.'));
122   $self->redirect_to(action => 'list');
123 }
124
125 1;
126
127 __END__
128
129 =encoding utf-8
130
131 =head1 NAME
132
133 SL::Controller::PartsGroup - CRUD controller for partsgroups
134
135 =head1 SYNOPSIS
136
137 A new controller to create / edit / delete partsgroups.
138
139 Partsgroups can only be deleted if they haven't been used anywhere.
140
141 =head1 OBSOLETE PARTSGROUPS
142
143 A partsgroup can be deleted if it hasn't been used anywhere / is orphaned.
144
145 A partsgroup can be set to obsolete, which means new items can't be assigned
146 that partsgroup, but old items with that partsgroup can keep it. And you can
147 also still filter for these obsolete partsgroups in reports.
148
149 =head1 ISSUES
150
151 Unlike the old version (pe.pl/PE.pm), there is no way to filter/search the
152 partsgroups in the overview page, it always shows the complete (ordered) list,
153 ordered by sortkey.
154
155 =head1 AUTHOR
156
157 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
158
159 =cut