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