Merge branch 'bankerweiterung_und_skonto'
[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
11 use Rose::Object::MakeMethods::Generic
12 (
13  scalar => [ qw(payment_term languages) ],
14 );
15
16 __PACKAGE__->run_before('check_auth');
17 __PACKAGE__->run_before('load_payment_term', only => [ qw(         edit        update destroy) ]);
18 __PACKAGE__->run_before('load_languages',    only => [ qw(new list edit create update) ]);
19
20 #
21 # actions
22 #
23
24 sub action_list {
25   my ($self) = @_;
26
27   $self->render('payment_term/list',
28                 title               => $::locale->text('Payment terms'),
29                 PAYMENT_TERMS => SL::DB::Manager::PaymentTerm->get_all_sorted);
30 }
31
32 sub action_new {
33   my ($self) = @_;
34
35   $self->{payment_term} = SL::DB::PaymentTerm->new;
36   $self->render('payment_term/form', title => $::locale->text('Create a new payment term'));
37 }
38
39 sub action_edit {
40   my ($self) = @_;
41   $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
42 }
43
44 sub action_create {
45   my ($self) = @_;
46
47   $self->{payment_term} = SL::DB::PaymentTerm->new;
48   $self->create_or_update;
49 }
50
51 sub action_update {
52   my ($self) = @_;
53   $self->create_or_update;
54 }
55
56 sub action_destroy {
57   my ($self) = @_;
58
59   if (eval { $self->{payment_term}->delete; 1; }) {
60     flash_later('info',  $::locale->text('The payment term has been deleted.'));
61   } else {
62     flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
63   }
64
65   $self->redirect_to(action => 'list');
66 }
67
68 sub action_reorder {
69   my ($self) = @_;
70
71   SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
72
73   $self->render(\'', { type => 'json' });
74 }
75
76 #
77 # filters
78 #
79
80 sub check_auth {
81   $::auth->assert('config');
82 }
83
84 #
85 # helpers
86 #
87
88 sub create_or_update {
89   my $self   = shift;
90   my $is_new = !$self->{payment_term}->id;
91   my $params = delete($::form->{payment_term}) || { };
92
93   $self->{payment_term}->assign_attributes(%{ $params });
94
95   my @errors = $self->{payment_term}->validate;
96
97   if (@errors) {
98     flash('error', @errors);
99     $self->render('payment_term/form', title => $is_new ? $::locale->text('Create a new payment term') : $::locale->text('Edit payment term'));
100     return;
101   }
102
103   $self->{payment_term}->save;
104   foreach my $language (@{ $self->{languages} }) {
105     $self->{payment_term}->save_attribute_translation('description_long', $language, $::form->{"translation_" . $language->id});
106   }
107
108   flash_later('info', $is_new ? $::locale->text('The payment term has been created.') : $::locale->text('The payment term has been saved.'));
109   $self->redirect_to(action => 'list');
110 }
111
112 sub load_payment_term {
113   my ($self) = @_;
114   $self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
115 }
116
117 sub load_languages {
118   my ($self) = @_;
119   $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
120 }
121
122 1;