1 package SL::Controller::PaymentTerm;
5 use parent qw(SL::Controller::Base);
7 use SL::DB::PaymentTerm;
10 use SL::Locale::String qw(t8);
12 use Rose::Object::MakeMethods::Generic
14 scalar => [ qw(payment_term languages) ],
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) ]);
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);
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'));
46 $self->setup_form_action_bar;
47 $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
53 $self->{payment_term} = SL::DB::PaymentTerm->new;
54 $self->create_or_update;
59 $self->create_or_update;
65 if (eval { $self->{payment_term}->delete; 1; }) {
66 flash_later('info', $::locale->text('The payment term has been deleted.'));
68 flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
71 $self->redirect_to(action => 'list');
77 SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
79 $self->render(\'', { type => 'json' });
87 $::auth->assert('config');
91 $::request->layout->use_javascript("kivi.PaymentTerm.js");
98 sub create_or_update {
100 my $is_new = !$self->{payment_term}->id;
101 my $params = delete($::form->{payment_term}) || { };
103 $self->{payment_term}->assign_attributes(%{ $params });
104 $self->{payment_term}->terms_netto(0) if !$self->{payment_term}->auto_calculation;
106 my @errors = $self->{payment_term}->validate;
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'));
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});
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');
124 sub load_payment_term {
126 $self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
131 $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
134 sub setup_list_action_bar {
137 for my $bar ($::request->layout->get('actionbar')) {
141 link => $self->url_for(action => 'new'),
147 sub setup_form_action_bar {
150 my $is_new = !$self->payment_term->id;
152 for my $bar ($::request->layout->get('actionbar')) {
156 submit => [ '#form', { action => 'PaymentTerm/' . ($is_new ? 'create' : 'update') } ],
157 checks => [ 'kivi.validate_form' ],
158 accesskey => 'enter',
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,
172 link => $self->url_for(action => 'list'),
176 $::request->layout->add_javascripts('kivi.Validator.js');