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