marei: cleanup scrlayers
[kivitendo-erp.git] / SL / Controller / PaymentTerm.pm
1 package SL::Controller::PaymentTerm;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::PaymentTerm;
8 use SL::DB::Language;
9 use SL::Helper::Flash;
10 use SL::Locale::String qw(t8);
11
12 use Rose::Object::MakeMethods::Generic
13 (
14  scalar => [ qw(payment_term languages) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_payment_term', only => [ qw(         edit        update destroy) ]);
19 __PACKAGE__->run_before('load_languages',    only => [ qw(new list edit create update) ]);
20 __PACKAGE__->run_before('setup',             only => [ qw(new      edit) ]);
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   $self->setup_list_action_bar;
30   $self->render('payment_term/list',
31                 title         => $::locale->text('Payment terms'),
32                 PAYMENT_TERMS => SL::DB::Manager::PaymentTerm->get_all_sorted);
33 }
34
35 sub action_new {
36   my ($self) = @_;
37
38   $self->{payment_term} = SL::DB::PaymentTerm->new(auto_calculation => 1);
39   $self->setup_form_action_bar;
40   $self->render('payment_term/form', title => $::locale->text('Create a new payment term'));
41 }
42
43 sub action_edit {
44   my ($self) = @_;
45
46   $self->setup_form_action_bar;
47   $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
48 }
49
50 sub action_create {
51   my ($self) = @_;
52
53   $self->{payment_term} = SL::DB::PaymentTerm->new;
54   $self->create_or_update;
55 }
56
57 sub action_update {
58   my ($self) = @_;
59   $self->create_or_update;
60 }
61
62 sub action_destroy {
63   my ($self) = @_;
64
65   if (eval { $self->{payment_term}->delete; 1; }) {
66     flash_later('info',  $::locale->text('The payment term has been deleted.'));
67   } else {
68     flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
69   }
70
71   $self->redirect_to(action => 'list');
72 }
73
74 sub action_reorder {
75   my ($self) = @_;
76
77   SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
78
79   $self->render(\'', { type => 'json' });
80 }
81
82 #
83 # filters
84 #
85
86 sub check_auth {
87   $::auth->assert('config');
88 }
89
90 sub setup {
91   $::request->layout->use_javascript("kivi.PaymentTerm.js");
92 }
93
94 #
95 # helpers
96 #
97
98 sub create_or_update {
99   my $self   = shift;
100   my $is_new = !$self->{payment_term}->id;
101   my $params = delete($::form->{payment_term}) || { };
102
103   $self->{payment_term}->assign_attributes(%{ $params });
104   $self->{payment_term}->terms_netto(0) if !$self->{payment_term}->auto_calculation;
105
106   my @errors = $self->{payment_term}->validate;
107
108   if (@errors) {
109     flash('error', @errors);
110     $self->render('payment_term/form', title => $is_new ? $::locale->text('Create a new payment term') : $::locale->text('Edit payment term'));
111     return;
112   }
113
114   $self->{payment_term}->save;
115   foreach my $language (@{ $self->{languages} }) {
116     $self->{payment_term}->save_attribute_translation('description_long', $language, $::form->{"translation_" . $language->id});
117     $self->{payment_term}->save_attribute_translation('description_long_invoice', $language, $::form->{"translation_invoice_" . $language->id});
118   }
119
120   flash_later('info', $is_new ? $::locale->text('The payment term has been created.') : $::locale->text('The payment term has been saved.'));
121   $self->redirect_to(action => 'list');
122 }
123
124 sub load_payment_term {
125   my ($self) = @_;
126   $self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
127 }
128
129 sub load_languages {
130   my ($self) = @_;
131   $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
132 }
133
134 sub setup_list_action_bar {
135   my ($self) = @_;
136
137   for my $bar ($::request->layout->get('actionbar')) {
138     $bar->add(
139       link => [
140         t8('Add'),
141         link => $self->url_for(action => 'new'),
142       ],
143     );
144   }
145 }
146
147 sub setup_form_action_bar {
148   my ($self) = @_;
149
150   my $is_new = !$self->payment_term->id;
151
152   for my $bar ($::request->layout->get('actionbar')) {
153     $bar->add(
154       action => [
155         t8('Save'),
156         submit    => [ '#form', { action => 'PaymentTerm/' . ($is_new ? 'create' : 'update') } ],
157         checks    => [ 'kivi.validate_form' ],
158         accesskey => 'enter',
159       ],
160
161       action => [
162         t8('Delete'),
163         submit   => [ '#form', { action => 'PaymentTerm/destroy' } ],
164         confirm  => t8('Do you really want to delete this object?'),
165         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
166       ],
167
168       'separator',
169
170       link => [
171         t8('Abort'),
172         link => $self->url_for(action => 'list'),
173       ],
174     );
175   }
176   $::request->layout->add_javascripts('kivi.Validator.js');
177 }
178
179 1;