1 package SL::Controller::PaymentTerm;
5 use parent qw(SL::Controller::Base);
7 use SL::DB::PaymentTerm;
11 use Rose::Object::MakeMethods::Generic
13 scalar => [ qw(payment_term languages) ],
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) ]);
28 $self->render('payment_term/list',
29 title => $::locale->text('Payment terms'),
30 PAYMENT_TERMS => SL::DB::Manager::PaymentTerm->get_all_sorted);
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'));
43 $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
49 $self->{payment_term} = SL::DB::PaymentTerm->new;
50 $self->create_or_update;
55 $self->create_or_update;
61 if (eval { $self->{payment_term}->delete; 1; }) {
62 flash_later('info', $::locale->text('The payment term has been deleted.'));
64 flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
67 $self->redirect_to(action => 'list');
73 SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
75 $self->render(\'', { type => 'json' });
83 $::auth->assert('config');
87 $::request->layout->use_javascript("kivi.PaymentTerm.js");
94 sub create_or_update {
96 my $is_new = !$self->{payment_term}->id;
97 my $params = delete($::form->{payment_term}) || { };
99 $self->{payment_term}->assign_attributes(%{ $params });
100 $self->{payment_term}->terms_netto(0) if !$self->{payment_term}->auto_calculation;
102 my @errors = $self->{payment_term}->validate;
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'));
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});
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');
120 sub load_payment_term {
122 $self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
127 $self->{languages} = SL::DB::Manager::Language->get_all_sorted;