Merge branch 'f-factur-x-und-xrechnung'
[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 );
272
273 my @default_list_attributes = (
274   { method => 'description', title => t8('Description') },
275 );
276
277 #
278 # actions
279 #
280
281 sub action_list {
282   my ($self) = @_;
283
284   $self->setup_list_action_bar;
285   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
286 }
287
288 sub action_new {
289   my ($self) = @_;
290
291   $self->object($self->class->new);
292   $self->render_form(title => $self->config->{titles}->{add});
293 }
294
295 sub action_edit {
296   my ($self) = @_;
297
298   $self->render_form(title => $self->config->{titles}->{edit});
299 }
300
301 sub action_create {
302   my ($self) = @_;
303
304   $self->object($self->class->new);
305   $self->create_or_update;
306 }
307
308 sub action_update {
309   my ($self) = @_;
310
311   $self->create_or_update;
312 }
313
314 sub action_delete {
315   my ($self) = @_;
316
317   if ($self->object->can('orphaned') && !$self->object->orphaned) {
318     flash_later('error', t8('The object is in use and cannot be deleted.'));
319
320   } elsif ( eval { $self->object->delete; 1; } ) {
321     flash_later('info',  t8('The object has been deleted.'));
322
323   } else {
324     flash_later('error', t8('The object is in use and cannot be deleted.'));
325   }
326
327   $self->redirect_to($self->list_url);
328 }
329
330 sub action_reorder {
331   my ($self) = @_;
332
333   $self->class->reorder_list(@{ $::form->{object_id} || [] });
334   $self->render(\'', { type => 'json' });
335 }
336
337 #
338 # filters
339 #
340
341 sub check_type_and_auth {
342   my ($self) = @_;
343
344   $self->type($::form->{type});
345   $self->config($supported_types{$self->type}) || die "Unsupported type";
346
347   $::auth->assert($self->config->{auth} || 'config');
348
349   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
350   require $pm;
351
352   my $setup = "setup_" . $self->type;
353   $self->$setup if $self->can($setup);
354
355   1;
356 }
357
358 sub setup_javascript {
359   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
360 }
361
362 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
363 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
364 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
365 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
366 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
367 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
368 sub init_defaults            { SL::DB::Default->get                                          }
369
370 sub init_list_attributes {
371   my ($self) = @_;
372
373   my $method = "list_attributes_" . $self->type;
374
375   return $self->$method if $self->can($method);
376   return $self->config->{list_attributes} // \@default_list_attributes;
377 }
378
379 #
380 # helpers
381 #
382
383 sub create_or_update {
384   my ($self) = @_;
385   my $is_new = !$self->object->id;
386
387   my $params = delete($::form->{object}) || { };
388
389   $self->object->assign_attributes(%{ $params });
390
391   my @errors;
392
393   push @errors, $self->object->validate if $self->object->can('validate');
394
395   if (@errors) {
396     flash('error', @errors);
397     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
398   }
399
400   $self->object->save;
401
402   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
403
404   $self->redirect_to($self->list_url);
405 }
406
407 sub render_form {
408   my ($self, %params) = @_;
409
410   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
411
412   $self->setup_render_form_action_bar;
413   $self->render(
414     'simple_system_setting/form',
415     %params,
416     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
417   );
418 }
419
420 #
421 # type-specific helper functions
422 #
423
424 sub setup_requirement_spec_acceptance_status {
425   my ($self) = @_;
426
427   no warnings 'once';
428   $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
429 }
430
431 sub setup_requirement_spec_status {
432   my ($self) = @_;
433
434   no warnings 'once';
435   $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
436 }
437
438 sub setup_language {
439   my ($self) = @_;
440
441   $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
442   $self->{dateformats}   = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
443 }
444
445 #
446 # action bar
447 #
448
449 sub setup_list_action_bar {
450   my ($self, %params) = @_;
451
452   for my $bar ($::request->layout->get('actionbar')) {
453     $bar->add(
454       link => [
455         t8('Add'),
456         link => $self->url_for(action => 'new', type => $self->type),
457       ],
458     );
459   }
460 }
461
462 sub setup_render_form_action_bar {
463   my ($self) = @_;
464
465   my $is_new         = !$self->object->id;
466   my $can_be_deleted = !$is_new
467                     && (!$self->object->can("orphaned")       || $self->object->orphaned)
468                     && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
469
470   for my $bar ($::request->layout->get('actionbar')) {
471     $bar->add(
472       action => [
473         t8('Save'),
474         submit    => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
475         checks    => [ 'kivi.validate_form' ],
476         accesskey => 'enter',
477       ],
478
479       action => [
480         t8('Delete'),
481         submit   => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
482         confirm  => t8('Do you really want to delete this object?'),
483         disabled => $is_new          ? t8('This object has not been saved yet.')
484                   : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
485                   :                    undef,
486       ],
487
488       link => [
489         t8('Abort'),
490         link => $self->list_url,
491       ],
492     );
493   }
494   $::request->layout->add_javascripts('kivi.Validator.js');
495 }
496
497 1;
498
499 __END__
500
501 =encoding utf-8
502
503 =head1 NAME
504
505 SL::Controller::SimpleSystemSettings — a common CRUD controller for
506 various settings in the "System" menu
507
508 =head1 AUTHOR
509
510 Moritz Bunkus <m.bunkus@linet-services.de>
511
512 =cut