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