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) ]);
27 $self->render('payment_term/list',
28 title => $::locale->text('Payment terms'),
29 PAYMENT_TERMS => SL::DB::Manager::PaymentTerm->get_all_sorted);
35 $self->{payment_term} = SL::DB::PaymentTerm->new;
36 $self->render('payment_term/form', title => $::locale->text('Create a new payment term'));
41 $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
47 $self->{payment_term} = SL::DB::PaymentTerm->new;
48 $self->create_or_update;
53 $self->create_or_update;
59 if (eval { $self->{payment_term}->delete; 1; }) {
60 flash_later('info', $::locale->text('The payment term has been deleted.'));
62 flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
65 $self->redirect_to(action => 'list');
71 SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
73 $self->render(\'', { type => 'json' });
81 $::auth->assert('config');
88 sub create_or_update {
90 my $is_new = !$self->{payment_term}->id;
91 my $params = delete($::form->{payment_term}) || { };
93 $self->{payment_term}->assign_attributes(%{ $params });
95 my @errors = $self->{payment_term}->validate;
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'));
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});
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');
112 sub load_payment_term {
114 $self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
119 $self->{languages} = SL::DB::Manager::Language->get_all_sorted;