f0590a165214fd9264eee66d339525e56a1e2546
[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       {                                                        title => t8('Use for ZUGFeRD'), formatter => sub { $_[0]->use_for_zugferd ? t8('yes') : t8('no') } },
39       { method => 'reconciliation_starting_date_as_date',      title => t8('Date'),    align => 'right' },
40       { method => 'reconciliation_starting_balance_as_number', title => t8('Balance'), align => 'right' },
41     ],
42   },
43
44   business => {
45     # Make locales.pl happy: $self->render("simple_system_setting/_business_form")
46     class  => 'Business',
47     titles => {
48       list => t8('Businesses'),
49       add  => t8('Add business'),
50       edit => t8('Edit business'),
51     },
52     list_attributes => [
53       { method => 'description',         title => t8('Description'), },
54       {                                  title => t8('Discount'), formatter => sub { $_[0]->discount_as_percent . ' %' }, align => 'right' },
55       { method => 'customernumberinit',  title => t8('Customernumberinit'), },
56     ],
57   },
58
59   department => {
60     class  => 'Department',
61     titles => {
62       list => t8('Departments'),
63       add  => t8('Add department'),
64       edit => t8('Edit department'),
65     },
66   },
67
68   greeting => {
69     class  => 'Greeting',
70     auth   => 'config',
71     titles => {
72       list => t8('Greetings'),
73       add  => t8('Add greeting'),
74       edit => t8('Edit greeting'),
75     },
76   },
77
78   language => {
79     # Make locales.pl happy: $self->render("simple_system_setting/_language_form")
80     class  => 'Language',
81     titles => {
82       list => t8('Languages'),
83       add  => t8('Add language'),
84       edit => t8('Edit language'),
85     },
86     list_attributes => [
87       { method => 'description',   title => t8('Description'), },
88       { method => 'template_code', title => t8('Template Code'), },
89       { method => 'article_code',  title => t8('Article Code'), },
90       {                            title => t8('Number Format'), formatter => sub { $_[0]->output_numberformat || t8('use program settings') } },
91       {                            title => t8('Date Format'),   formatter => sub { $_[0]->output_dateformat   || t8('use program settings') } },
92       {                            title => t8('Long Dates'),    formatter => sub { $_[0]->output_longdates ? t8('yes') : t8('no') } },
93     ],
94   },
95
96   part_classification => {
97     # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
98     class  => 'PartClassification',
99     titles => {
100       list => t8('Part classifications'),
101       add  => t8('Add part classification'),
102       edit => t8('Edit part classification'),
103     },
104     list_attributes => [
105       { title => t8('Description'),       formatter => sub { t8($_[0]->description) } },
106       { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
107       { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
108       { title => t8('Used for Sale'),     formatter => sub { $_[0]->used_for_sale     ? t8('yes') : t8('no') } },
109       { title => t8('Report separately'), formatter => sub { $_[0]->report_separate   ? t8('yes') : t8('no') } },
110     ],
111   },
112
113   parts_group => {
114     # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
115     class  => 'PartsGroup',
116     titles => {
117       list => t8('Partsgroups'),
118       add  => t8('Add partsgroup'),
119       edit => t8('Edit partsgroup'),
120     },
121     list_attributes => [
122       { method => 'partsgroup', title => t8('Description') },
123       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
124     ],
125   },
126
127   price_factor => {
128     # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
129     class  => 'PriceFactor',
130     titles => {
131       list => t8('Price Factors'),
132       add  => t8('Add Price Factor'),
133       edit => t8('Edit Price Factor'),
134     },
135     list_attributes => [
136       { method => 'description',      title => t8('Description') },
137       { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
138     ],
139   },
140
141   pricegroup => {
142     # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
143     class  => 'Pricegroup',
144     titles => {
145       list => t8('Pricegroups'),
146       add  => t8('Add pricegroup'),
147       edit => t8('Edit pricegroup'),
148     },
149     list_attributes => [
150       { method => 'pricegroup', title => t8('Description') },
151       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
152     ],
153   },
154
155   project_status => {
156     class  => 'ProjectStatus',
157     titles => {
158       list => t8('Project statuses'),
159       add  => t8('Add project status'),
160       edit => t8('Edit project status'),
161     },
162   },
163
164   project_type => {
165     class  => 'ProjectType',
166     titles => {
167       list => t8('Project types'),
168       add  => t8('Add project type'),
169       edit => t8('Edit project type'),
170     },
171   },
172
173   requirement_spec_acceptance_status => {
174     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
175     class  => 'RequirementSpecAcceptanceStatus',
176     titles => {
177       list => t8('Acceptance Statuses'),
178       add  => t8('Add acceptance status'),
179       edit => t8('Edit acceptance status'),
180     },
181     list_attributes => [
182       { method => 'name',        title => t8('Name') },
183       { method => 'description', title => t8('Description') },
184     ],
185   },
186
187   requirement_spec_complexity => {
188     class  => 'RequirementSpecComplexity',
189     titles => {
190       list => t8('Complexities'),
191       add  => t8('Add complexity'),
192       edit => t8('Edit complexity'),
193     },
194   },
195
196   requirement_spec_predefined_text => {
197     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
198     class  => 'RequirementSpecPredefinedText',
199     titles => {
200       list => t8('Pre-defined Texts'),
201       add  => t8('Add pre-defined text'),
202       edit => t8('Edit pre-defined text'),
203     },
204     list_attributes => [
205       { method => 'description', title => t8('Description') },
206       { method => 'title',       title => t8('Title') },
207       {                          title => t8('Content'),                 formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
208       {                          title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
209       {                          title => t8('Useable for sections'),    formatter => sub { $_[0]->useable_for_sections    ? t8('yes') : t8('no') } },
210     ],
211   },
212
213   requirement_spec_risk => {
214     class  => 'RequirementSpecRisk',
215     titles => {
216       list => t8('Risk levels'),
217       add  => t8('Add risk level'),
218       edit => t8('Edit risk level'),
219     },
220   },
221
222   requirement_spec_status => {
223     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
224     class  => 'RequirementSpecStatus',
225     titles => {
226       list => t8('Requirement Spec Statuses'),
227       add  => t8('Add requirement spec status'),
228       edit => t8('Edit requirement spec status'),
229     },
230     list_attributes => [
231       { method => 'name',        title => t8('Name') },
232       { method => 'description', title => t8('Description') },
233     ],
234   },
235
236   requirement_spec_type => {
237     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
238     class  => 'RequirementSpecType',
239     titles => {
240       list => t8('Requirement Spec Types'),
241       add  => t8('Add requirement spec type'),
242       edit => t8('Edit requirement spec type'),
243     },
244     list_attributes => [
245       { method => 'description',                  title => t8('Description') },
246       { method => 'section_number_format',        title => t8('Section number format') },
247       { method => 'function_block_number_format', title => t8('Function block number format') },
248     ],
249   },
250
251 );
252
253 my @default_list_attributes = (
254   { method => 'description', title => t8('Description') },
255 );
256
257 #
258 # actions
259 #
260
261 sub action_list {
262   my ($self) = @_;
263
264   $self->setup_list_action_bar;
265   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
266 }
267
268 sub action_new {
269   my ($self) = @_;
270
271   $self->object($self->class->new);
272   $self->render_form(title => $self->config->{titles}->{add});
273 }
274
275 sub action_edit {
276   my ($self) = @_;
277
278   $self->render_form(title => $self->config->{titles}->{edit});
279 }
280
281 sub action_create {
282   my ($self) = @_;
283
284   $self->object($self->class->new);
285   $self->create_or_update;
286 }
287
288 sub action_update {
289   my ($self) = @_;
290
291   $self->create_or_update;
292 }
293
294 sub action_delete {
295   my ($self) = @_;
296
297   if ($self->object->can('orphaned') && !$self->object->orphaned) {
298     flash_later('error', t8('The object is in use and cannot be deleted.'));
299
300   } elsif ( eval { $self->object->delete; 1; } ) {
301     flash_later('info',  t8('The object has been deleted.'));
302
303   } else {
304     flash_later('error', t8('The object is in use and cannot be deleted.'));
305   }
306
307   $self->redirect_to($self->list_url);
308 }
309
310 sub action_reorder {
311   my ($self) = @_;
312
313   $self->class->reorder_list(@{ $::form->{object_id} || [] });
314   $self->render(\'', { type => 'json' });
315 }
316
317 #
318 # filters
319 #
320
321 sub check_type_and_auth {
322   my ($self) = @_;
323
324   $self->type($::form->{type});
325   $self->config($supported_types{$self->type}) || die "Unsupported type";
326
327   $::auth->assert($self->config->{auth} || 'config');
328
329   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
330   require $pm;
331
332   my $setup = "setup_" . $self->type;
333   $self->$setup if $self->can($setup);
334
335   1;
336 }
337
338 sub setup_javascript {
339   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
340 }
341
342 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
343 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
344 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
345 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
346 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
347 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
348 sub init_defaults            { SL::DB::Default->get                                          }
349
350 sub init_list_attributes {
351   my ($self) = @_;
352
353   my $method = "list_attributes_" . $self->type;
354
355   return $self->$method if $self->can($method);
356   return $self->config->{list_attributes} // \@default_list_attributes;
357 }
358
359 #
360 # helpers
361 #
362
363 sub create_or_update {
364   my ($self) = @_;
365   my $is_new = !$self->object->id;
366
367   my $params = delete($::form->{object}) || { };
368
369   $self->object->assign_attributes(%{ $params });
370
371   my @errors;
372
373   push @errors, $self->object->validate if $self->object->can('validate');
374
375   if (@errors) {
376     flash('error', @errors);
377     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
378   }
379
380   $self->object->save;
381
382   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
383
384   $self->redirect_to($self->list_url);
385 }
386
387 sub render_form {
388   my ($self, %params) = @_;
389
390   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
391
392   $self->setup_render_form_action_bar;
393   $self->render(
394     'simple_system_setting/form',
395     %params,
396     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
397   );
398 }
399
400 #
401 # type-specific helper functions
402 #
403
404 sub setup_requirement_spec_acceptance_status {
405   my ($self) = @_;
406
407   no warnings 'once';
408   $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
409 }
410
411 sub setup_requirement_spec_status {
412   my ($self) = @_;
413
414   no warnings 'once';
415   $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
416 }
417
418 sub setup_language {
419   my ($self) = @_;
420
421   $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
422   $self->{dateformats}   = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
423 }
424
425 #
426 # action bar
427 #
428
429 sub setup_list_action_bar {
430   my ($self, %params) = @_;
431
432   for my $bar ($::request->layout->get('actionbar')) {
433     $bar->add(
434       link => [
435         t8('Add'),
436         link => $self->url_for(action => 'new', type => $self->type),
437       ],
438     );
439   }
440 }
441
442 sub setup_render_form_action_bar {
443   my ($self) = @_;
444
445   my $is_new         = !$self->object->id;
446   my $can_be_deleted = !$is_new
447                     && (!$self->object->can("orphaned")       || $self->object->orphaned)
448                     && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
449
450   for my $bar ($::request->layout->get('actionbar')) {
451     $bar->add(
452       action => [
453         t8('Save'),
454         submit    => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
455         checks    => [ 'kivi.validate_form' ],
456         accesskey => 'enter',
457       ],
458
459       action => [
460         t8('Delete'),
461         submit   => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
462         confirm  => t8('Do you really want to delete this object?'),
463         disabled => $is_new          ? t8('This object has not been saved yet.')
464                   : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
465                   :                    undef,
466       ],
467
468       link => [
469         t8('Abort'),
470         link => $self->list_url,
471       ],
472     );
473   }
474   $::request->layout->add_javascripts('kivi.Validator.js');
475 }
476
477 1;
478
479 __END__
480
481 =encoding utf-8
482
483 =head1 NAME
484
485 SL::Controller::SimpleSystemSettings — a common CRUD controller for
486 various settings in the "System" menu
487
488 =head1 AUTHOR
489
490 Moritz Bunkus <m.bunkus@linet-services.de>
491
492 =cut