]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/ReclamationReason.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / Controller / ReclamationReason.pm
1 package SL::Controller::ReclamationReason;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::ReclamationReason;
8 use SL::Helper::Flash;
9 use SL::Locale::String;
10 use List::MoreUtils qw(any);
11 use SL::CVar;
12
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(reclamation_reason) ],
15   'scalar --get_set_init' => [ qw(reclamation_reason) ],
16 );
17
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('load_reclamation_reason', only => [ qw(edit delete) ]);
20
21 sub check_auth {
22   $::auth->assert('config');
23 }
24
25 #
26 # actions
27 #
28
29 sub action_list {
30   my ($self, %params) = @_;
31
32   $self->setup_list_action_bar;
33   $self->render(
34     'reclamation_reason/list',
35     title => $::locale->text('Reclamation Reasons'),
36     RECLAMATION_REASONS => SL::DB::Manager::ReclamationReason->get_all_sorted(),
37   );
38 }
39
40 sub action_sort_list {
41   my ($self, %params) = @_;
42
43   $self->setup_sort_list_action_bar;
44   $self->render(
45     'reclamation_reason/sort_list',
46     title => $::locale->text('reclamation reasons'),
47     RECLAMATION_REASONS => SL::DB::Manager::ReclamationReason->get_all_sorted(),
48   );
49 }
50
51 sub action_new {
52   my ($self) = @_;
53
54   $self->reclamation_reason(SL::DB::ReclamationReason->new());
55   $self->show_form(title => t8('Add reclamation reason'));
56 }
57
58 sub action_edit {
59   my ($self) = @_;
60
61   $self->show_form(
62     title       => t8('Edit reclamation reason'),
63   );
64 }
65
66 sub action_save {
67   my ($self) = @_;
68
69   if ($::form->{id}) {
70     $self->load_reclamation_reason();
71   }
72
73   $self->create_or_save;
74 }
75
76 sub action_delete {
77   my ($self) = @_;
78
79   if (!$self->reclamation_reason->db->with_transaction(sub {
80     $self->reclamation_reason->delete();
81     flash_later('info',  $::locale->text('The reclamation reason has been deleted.'));
82
83     1;
84   })) {
85     flash_later('error', $::locale->text('The reclamation reason is in use and cannot be deleted.'))
86   };
87
88   $self->redirect_to(action => 'list');
89 }
90
91 #
92 # ajax actions
93 #
94
95 sub action_reorder {
96   my ($self) = @_;
97
98   SL::DB::ReclamationReason->reorder_list(@{ $::form->{reclamation_reason_id} || [] });
99
100   $self->render(\'', { type => 'json' });
101 }
102
103 #
104 # action bars
105 #
106
107 sub setup_form_action_bar {
108   my ($self) = @_;
109
110   my $is_new = !$self->reclamation_reason->id;
111
112   for my $bar ($::request->layout->get('actionbar')) {
113     $bar->add(
114       action => [
115         $is_new ? t8('Create') : t8('Save'),
116         submit    => [ '#form', { action => 'ReclamationReason/save' } ],
117         checks    => [ 'kivi.validate_form' ],
118         accesskey => 'enter',
119       ],
120       action => [
121         t8('Delete'),
122         submit   => [ '#form', { action => 'ReclamationReason/delete' } ],
123         confirm  => t8('Do you really want to delete this reclamation reason?'),
124         only_if  => !$is_new,
125       ],
126       link => [
127         t8('Abort'),
128         link => $self->url_for(action => 'list'),
129       ],
130     );
131   }
132   $::request->layout->add_javascripts('kivi.Validator.js');
133 }
134
135 sub setup_list_action_bar {
136   my ($self) = @_;
137
138   for my $bar ($::request->layout->get('actionbar')) {
139     $bar->add(
140       link => [
141         t8('Add'),
142         link => $self->url_for(action => 'new'),
143       ],
144       link => [
145         t8('Sort'),
146         link => $self->url_for(action => 'sort_list'),
147       ],
148     );
149   }
150 }
151
152 sub setup_sort_list_action_bar {
153   my ($self) = @_;
154
155   for my $bar ($::request->layout->get('actionbar')) {
156     $bar->add(
157       link => [
158         t8('Add'),
159         link => $self->url_for(action => 'new'),
160       ],
161       link => [
162         t8('List'),
163         link => $self->url_for(action => 'list'),
164       ],
165     );
166   }
167 }
168
169 #
170 # helpers
171 #
172
173 sub create_or_save {
174   my ($self) = @_;
175
176   unless ($self->reclamation_reason) {
177     $self->reclamation_reason(SL::DB::ReclamationReason->new());
178   }
179
180   my $is_new = !$self->reclamation_reason->id;
181
182   my $params = delete($::form->{reclamation_reason}) || { };
183   delete $params->{id};
184
185   my @errors;
186
187   my $db = $self->reclamation_reason->db;
188   if (!$db->with_transaction(sub {
189
190     # assign attributes and validate
191     $self->reclamation_reason->assign_attributes( %{$params} ) ;
192
193     push(@errors, $self->reclamation_reason->validate); # check data before DB error
194
195     if (@errors) {
196       die @errors . "\n";
197     };
198     $self->reclamation_reason->save;
199     1;
200   })) {
201     die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
202   }
203
204   flash_later('info', $is_new ? t8('The reclamation reason has been created.') : t8('The reclamation reason has been saved.'));
205   $self->redirect_to(action => 'list');
206 }
207
208 sub show_form {
209   my ($self, %params) = @_;
210
211   $self->setup_form_action_bar;
212   $self->render(
213     'reclamation_reason/form',
214     %params,
215   );
216 }
217
218 sub load_reclamation_reason {
219   my ($self) = @_;
220
221   $self->reclamation_reason(SL::DB::ReclamationReason->new(id => $::form->{id})->load);
222 }
223
224 1;
225
226 __END__
227
228 =encoding utf-8
229
230 =head1 NAME
231
232 SL::Controller::ReclamationReason - controller for reclamation_reasons
233
234 =head1 SYNOPSIS
235
236 This is a small controller to add end edit reclamation_reasons.
237
238 =head2 Key Features
239
240 =over 4
241
242 =item *
243
244 Adding, editing, deleting and sorting of reclamation reasons.
245
246 =item *
247
248 Reasons can be valid for purchase, sales or both.
249
250 =back
251
252 =head1 CODE
253
254 =head2 Layout
255
256 =over 4
257
258 =item * C<SL/Controller/ReclamationReason.pm>
259
260 the controller
261
262 =item * C<template/webpages/reclamation_reason/form.html>
263
264 main form
265
266 =item * C<template/webpages/reclamation_reason/list.html>
267
268 list form
269
270 =item * C<template/webpages/reclamation_reason/sort_list.html>
271
272 sorting form
273
274 =back
275
276 =head1 AUTHOR
277
278 Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>
279
280 =cut