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