SimpleSystemSetting: Umstellung von »Preisfaktoren«
[kivitendo-erp.git] / SL / Controller / SimpleSystemSetting.pm
1 package SL::Controller::SimpleSystemSetting;
2
3 use strict;
4 use utf8;
5
6 use parent qw(SL::Controller::Base);
7
8 use SL::Helper::Flash;
9 use SL::Locale::String;
10 use SL::DB::Default;
11 use SL::System::Process;
12
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(type config) ],
15   'scalar --get_set_init' => [ qw(defaults object all_objects class manager_class list_attributes list_url supports_reordering) ],
16 );
17
18 __PACKAGE__->run_before('check_type_and_auth');
19 __PACKAGE__->run_before('setup_javascript', only => [ qw(add create edit update delete) ]);
20
21 # Make locales.pl happy: $self->render("simple_system_setting/_default_form")
22
23 my %supported_types = (
24   bank_account => {
25     # Make locales.pl happy: $self->render("simple_system_setting/_bank_account_form")
26     class  => 'BankAccount',
27     titles => {
28       list => t8('Bank accounts'),
29       add  => t8('Add bank account'),
30       edit => t8('Edit bank account'),
31     },
32     list_attributes => [
33       { method => 'name',                                      title => t8('Name'), },
34       { method => 'iban',                                      title => t8('IBAN'), },
35       { method => 'bank',                                      title => t8('Bank'), },
36       { method => 'bank_code',                                 title => t8('Bank code'), },
37       { method => 'bic',                                       title => t8('BIC'), },
38       { method => 'reconciliation_starting_date_as_date',      title => t8('Date'),    align => 'right' },
39       { method => 'reconciliation_starting_balance_as_number', title => t8('Balance'), align => 'right' },
40     ],
41   },
42
43   part_classification => {
44     # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
45     class  => 'PartClassification',
46     titles => {
47       list => t8('Part classifications'),
48       add  => t8('Add part classification'),
49       edit => t8('Edit part classification'),
50     },
51     list_attributes => [
52       { title => t8('Description'),       formatter => sub { t8($_[0]->description) } },
53       { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
54       { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
55       { title => t8('Used for Sale'),     formatter => sub { $_[0]->used_for_sale     ? t8('yes') : t8('no') } },
56       { title => t8('Report separately'), formatter => sub { $_[0]->report_separate   ? t8('yes') : t8('no') } },
57     ],
58   },
59
60   parts_group => {
61     # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
62     class  => 'PartsGroup',
63     titles => {
64       list => t8('Partsgroups'),
65       add  => t8('Add partsgroup'),
66       edit => t8('Edit partsgroup'),
67     },
68     list_attributes => [
69       { method => 'partsgroup', title => t8('Description') },
70       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
71     ],
72   },
73
74   price_factor => {
75     # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
76     class  => 'PriceFactor',
77     titles => {
78       list => t8('Price Factors'),
79       add  => t8('Add Price Factor'),
80       edit => t8('Edit Price Factor'),
81     },
82     list_attributes => [
83       { method => 'description',      title => t8('Description') },
84       { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
85     ],
86   },
87
88   pricegroup => {
89     # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
90     class  => 'Pricegroup',
91     titles => {
92       list => t8('Pricegroups'),
93       add  => t8('Add pricegroup'),
94       edit => t8('Edit pricegroup'),
95     },
96     list_attributes => [
97       { method => 'pricegroup', title => t8('Description') },
98       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
99     ],
100   },
101 );
102
103 my @default_list_attributes = (
104   { method => 'description', title => t8('Description') },
105 );
106
107 #
108 # actions
109 #
110
111 sub action_list {
112   my ($self) = @_;
113
114   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
115 }
116
117 sub action_new {
118   my ($self) = @_;
119
120   $self->object($self->class->new);
121   $self->render_form(title => $self->config->{titles}->{add});
122 }
123
124 sub action_edit {
125   my ($self) = @_;
126
127   $self->render_form(title => $self->config->{titles}->{edit});
128 }
129
130 sub action_create {
131   my ($self) = @_;
132
133   $self->object($self->class->new);
134   $self->create_or_update;
135 }
136
137 sub action_update {
138   my ($self) = @_;
139
140   $self->create_or_update;
141 }
142
143 sub action_delete {
144   my ($self) = @_;
145
146   if ($self->object->can('orphaned') && !$self->object->orphaned) {
147     flash_later('error', t8('The object is in use and cannot be deleted.'));
148
149   } elsif ( eval { $self->object->delete; 1; } ) {
150     flash_later('info',  t8('The object has been deleted.'));
151
152   } else {
153     flash_later('error', t8('The object is in use and cannot be deleted.'));
154   }
155
156   $self->redirect_to($self->list_url);
157 }
158
159 sub action_reorder {
160   my ($self) = @_;
161
162   $self->class->reorder_list(@{ $::form->{object_id} || [] });
163   $self->render(\'', { type => 'json' });
164 }
165
166 #
167 # filters
168 #
169
170 sub check_type_and_auth {
171   my ($self) = @_;
172
173   $self->type($::form->{type});
174   $self->config($supported_types{$self->type}) || die "Unsupported type";
175
176   $::auth->assert($self->config->{auth} || 'config');
177
178   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
179   require $pm;
180
181   my $setup = "setup_" . $self->type;
182   $self->$setup if $self->can($setup);
183
184   1;
185 }
186
187 sub setup_javascript {
188   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
189 }
190
191 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
192 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
193 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
194 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
195 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
196 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
197 sub init_defaults            { SL::DB::Default->get                                          }
198
199 sub init_list_attributes {
200   my ($self) = @_;
201
202   my $method = "list_attributes_" . $self->type;
203
204   return $self->$method if $self->can($method);
205   return $self->config->{list_attributes} // \@default_list_attributes;
206 }
207
208 #
209 # helpers
210 #
211
212 sub create_or_update {
213   my ($self) = @_;
214   my $is_new = !$self->object->id;
215
216   my $params = delete($::form->{object}) || { };
217
218   $self->object->assign_attributes(%{ $params });
219
220   my @errors;
221
222   push @errors, $self->object->validate if $self->object->can('validate');
223
224   if (@errors) {
225     flash('error', @errors);
226     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
227   }
228
229   $self->object->save;
230
231   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
232
233   $self->redirect_to($self->list_url);
234 }
235
236 sub render_form {
237   my ($self, %params) = @_;
238
239   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
240
241   $self->render(
242     'simple_system_setting/form',
243     %params,
244     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
245   );
246 }
247
248 #
249 # type-specific helper functions
250 #
251
252 1;
253
254 __END__
255
256 =encoding utf-8
257
258 =head1 NAME
259
260 SL::Controller::SimpleSystemSettings — a common CRUD controller for
261 various settings in the "System" menu
262
263 =head1 AUTHOR
264
265 Moritz Bunkus <m.bunkus@linet-services.de>
266
267 =cut