Artikel-Klassifizierung: "Preis separat ausweisen"
[kivitendo-erp.git] / SL / Controller / PartClassification.pm
1 package SL::Controller::PartClassification;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::PartClassification;
8 use SL::Helper::Flash;
9
10 use Rose::Object::MakeMethods::Generic
11 (
12  scalar => [ qw(part_classification) ],
13 );
14
15 __PACKAGE__->run_before('check_auth');
16 __PACKAGE__->run_before('load_part_classification', only => [ qw(edit update destroy) ]);
17
18 #
19 # This Controller is responsible for creating,editing or deleting
20 # Part Classifications.
21 #
22 # The use of Part Classifications is described in SL::DB::PartClassification
23 #
24 #
25
26 # List all available part classifications
27 #
28
29 sub action_list {
30   my ($self) = @_;
31
32   $self->render('part_classification/list',
33                 title                => $::locale->text('Parts Classifications'),
34                 PART_CLASSIFICATIONS => SL::DB::Manager::PartClassification->get_all_sorted);
35 }
36
37 # A Form for a new creatable part classifications is generated
38 #
39 sub action_new {
40   my ($self) = @_;
41
42   $self->{part_classification} = SL::DB::PartClassification->new;
43   $self->render('part_classification/form', title => $::locale->text('Create a new parts classification'));
44 }
45
46 # Edit an existing part classifications
47 #
48 sub action_edit {
49   my ($self) = @_;
50   $self->render('part_classification/form', title => $::locale->text('Edit parts classification'));
51 }
52
53 # A new part classification is saved
54 #
55 sub action_create {
56   my ($self) = @_;
57
58   $self->{part_classification} = SL::DB::PartClassification->new;
59   $self->create_or_update;
60 }
61
62 # An existing part classification is saved
63 #
64 sub action_update {
65   my ($self) = @_;
66   $self->create_or_update;
67 }
68
69 # An existing part classification is deleted
70 #
71 # The basic classifications cannot be deleted, also classifications which are in use
72 #
73 sub action_destroy {
74   my ($self) = @_;
75
76   if ( $self->{part_classification}->id < 5 ) {
77     flash_later('error', $::locale->text('The basic parts classification cannot be deleted.'));
78   }
79   elsif (eval { $self->{part_classification}->delete; 1; }) {
80     flash_later('info',  $::locale->text('The parts classification has been deleted.'));
81   } else {
82     flash_later('error', $::locale->text('The parts classification is in use and cannot be deleted.'));
83   }
84
85   $self->redirect_to(action => 'list');
86 }
87 # reordering the lines
88 #
89 sub action_reorder {
90   my ($self) = @_;
91
92   SL::DB::PartClassification->reorder_list(@{ $::form->{part_classification_id} || [] });
93
94   $self->render(\'', { type => 'json' });
95 }
96
97 #
98 # filters
99 #
100
101 # check authentication, only "config" is allowed
102 #
103 sub check_auth {
104   $::auth->assert('config');
105 }
106
107 #
108 # helpers
109 #
110
111 # submethod for update the database
112 #
113 sub create_or_update {
114   my $self   = shift;
115   my $is_new = !$self->{part_classification}->id;
116
117   $::form->{part_classification}->{used_for_purchase} = 0 if ! $::form->{part_classification}->{used_for_purchase};
118   $::form->{part_classification}->{used_for_sale}     = 0 if ! $::form->{part_classification}->{used_for_sale};
119   $::form->{part_classification}->{report_separate}   = 0 if ! $::form->{part_classification}->{report_separate};
120
121   my $params = delete($::form->{part_classification}) || { };
122
123   $self->{part_classification}->assign_attributes(%{ $params });
124
125   my @errors = $self->{part_classification}->validate;
126
127   if (@errors) {
128     flash('error', @errors);
129     $self->render('part_classification/form', title => $is_new ? $::locale->text('Create a new parts classification') : $::locale->text('Edit parts classification'));
130     return;
131   }
132
133   $self->{part_classification}->save;
134
135   flash_later('info', $is_new ? $::locale->text('The parts classification has been created.') : $::locale->text('The parts classification has been saved.'));
136   $self->redirect_to(action => 'list');
137 }
138
139 # submethod for loading one item from the database
140 #
141 sub load_part_classification {
142   my ($self) = @_;
143   $self->{part_classification} = SL::DB::PartClassification->new(id => $::form->{id})->load;
144 }
145
146 1;
147
148
149
150 __END__
151
152 =encoding utf-8
153
154 =head1 NAME
155
156 SL::Controller::PartClassification
157
158 =head1 SYNOPSIS
159
160 This Controller is responsible for creating,editing or deleting
161 Part Classifications.
162
163 =head1 DESCRIPTION
164
165 The use of Part Classifications is described in L<SL::DB::PartClassification>
166
167 =head1 METHODS
168
169 =head2 action_create
170
171  $self->action_create();
172
173 A new part classification is saved
174
175
176
177 =head2 action_destroy
178
179  $self->action_destroy();
180
181 An existing part classification is deleted
182
183 The basic classifications cannot be deleted, also classifications which are in use
184
185
186
187 =head2 action_edit
188
189  $self->action_edit();
190
191 Edit an existing part classifications
192
193
194
195 =head2 action_list
196
197  $self->action_list();
198
199 List all available part classifications
200
201
202
203 =head2 action_new
204
205  $self->action_new();
206
207 A Form for a new creatable part classifications is generated
208
209
210
211 =head2 action_reorder
212
213  $self->action_reorder();
214
215 reordering the lines
216
217
218
219 =head2 action_update
220
221  $self->action_update();
222
223 An existing part classification is saved
224
225
226 =head1 AUTHOR
227
228 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
229
230 =cut