kivitendo/SL/Controller/PaymentTerm.pm @ 3eab069a
dbbf8923 | Moritz Bunkus | package SL::Controller::PaymentTerm;
|
||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
use SL::DB::PaymentTerm;
|
||||
use SL::DB::Language;
|
||||
use SL::Helper::Flash;
|
||||
032e3094 | Moritz Bunkus | use SL::Locale::String qw(t8);
|
||
dbbf8923 | Moritz Bunkus | |||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
scalar => [ qw(payment_term languages) ],
|
||||
);
|
||||
2f6e2e40 | Moritz Bunkus | __PACKAGE__->run_before('check_auth');
|
||
f631da56 | Moritz Bunkus | __PACKAGE__->run_before('load_payment_term', only => [ qw( edit update destroy) ]);
|
||
dbbf8923 | Moritz Bunkus | __PACKAGE__->run_before('load_languages', only => [ qw(new list edit create update) ]);
|
||
5bc87ade | Moritz Bunkus | __PACKAGE__->run_before('setup', only => [ qw(new edit) ]);
|
||
dbbf8923 | Moritz Bunkus | |||
#
|
||||
# actions
|
||||
#
|
||||
sub action_list {
|
||||
my ($self) = @_;
|
||||
032e3094 | Moritz Bunkus | $self->setup_list_action_bar;
|
||
dbbf8923 | Moritz Bunkus | $self->render('payment_term/list',
|
||
5357aec0 | Moritz Bunkus | title => $::locale->text('Payment terms'),
|
||
dbbf8923 | Moritz Bunkus | PAYMENT_TERMS => SL::DB::Manager::PaymentTerm->get_all_sorted);
|
||
}
|
||||
sub action_new {
|
||||
my ($self) = @_;
|
||||
5bc87ade | Moritz Bunkus | $self->{payment_term} = SL::DB::PaymentTerm->new(auto_calculation => 1);
|
||
032e3094 | Moritz Bunkus | $self->setup_form_action_bar;
|
||
dbbf8923 | Moritz Bunkus | $self->render('payment_term/form', title => $::locale->text('Create a new payment term'));
|
||
}
|
||||
sub action_edit {
|
||||
my ($self) = @_;
|
||||
5bc87ade | Moritz Bunkus | |||
032e3094 | Moritz Bunkus | $self->setup_form_action_bar;
|
||
dbbf8923 | Moritz Bunkus | $self->render('payment_term/form', title => $::locale->text('Edit payment term'));
|
||
}
|
||||
sub action_create {
|
||||
my ($self) = @_;
|
||||
$self->{payment_term} = SL::DB::PaymentTerm->new;
|
||||
$self->create_or_update;
|
||||
}
|
||||
sub action_update {
|
||||
my ($self) = @_;
|
||||
$self->create_or_update;
|
||||
}
|
||||
sub action_destroy {
|
||||
my ($self) = @_;
|
||||
if (eval { $self->{payment_term}->delete; 1; }) {
|
||||
flash_later('info', $::locale->text('The payment term has been deleted.'));
|
||||
} else {
|
||||
flash_later('error', $::locale->text('The payment term is in use and cannot be deleted.'));
|
||||
}
|
||||
$self->redirect_to(action => 'list');
|
||||
}
|
||||
662b757f | Moritz Bunkus | sub action_reorder {
|
||
dbbf8923 | Moritz Bunkus | my ($self) = @_;
|
||
117fefac | Moritz Bunkus | SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
|
||
662b757f | Moritz Bunkus | |||
7647d46a | Moritz Bunkus | $self->render(\'', { type => 'json' });
|
||
dbbf8923 | Moritz Bunkus | }
|
||
2f6e2e40 | Moritz Bunkus | #
|
||
# filters
|
||||
#
|
||||
sub check_auth {
|
||||
$::auth->assert('config');
|
||||
}
|
||||
5bc87ade | Moritz Bunkus | sub setup {
|
||
$::request->layout->use_javascript("kivi.PaymentTerm.js");
|
||||
}
|
||||
dbbf8923 | Moritz Bunkus | #
|
||
# helpers
|
||||
#
|
||||
sub create_or_update {
|
||||
my $self = shift;
|
||||
my $is_new = !$self->{payment_term}->id;
|
||||
my $params = delete($::form->{payment_term}) || { };
|
||||
$self->{payment_term}->assign_attributes(%{ $params });
|
||||
5bc87ade | Moritz Bunkus | $self->{payment_term}->terms_netto(0) if !$self->{payment_term}->auto_calculation;
|
||
dbbf8923 | Moritz Bunkus | |||
my @errors = $self->{payment_term}->validate;
|
||||
if (@errors) {
|
||||
flash('error', @errors);
|
||||
$self->render('payment_term/form', title => $is_new ? $::locale->text('Create a new payment term') : $::locale->text('Edit payment term'));
|
||||
return;
|
||||
}
|
||||
$self->{payment_term}->save;
|
||||
foreach my $language (@{ $self->{languages} }) {
|
||||
$self->{payment_term}->save_attribute_translation('description_long', $language, $::form->{"translation_" . $language->id});
|
||||
de009a3f | Moritz Bunkus | $self->{payment_term}->save_attribute_translation('description_long_invoice', $language, $::form->{"translation_invoice_" . $language->id});
|
||
dbbf8923 | Moritz Bunkus | }
|
||
flash_later('info', $is_new ? $::locale->text('The payment term has been created.') : $::locale->text('The payment term has been saved.'));
|
||||
$self->redirect_to(action => 'list');
|
||||
}
|
||||
sub load_payment_term {
|
||||
my ($self) = @_;
|
||||
$self->{payment_term} = SL::DB::PaymentTerm->new(id => $::form->{id})->load;
|
||||
}
|
||||
sub load_languages {
|
||||
my ($self) = @_;
|
||||
$self->{languages} = SL::DB::Manager::Language->get_all_sorted;
|
||||
}
|
||||
032e3094 | Moritz Bunkus | sub setup_list_action_bar {
|
||
my ($self) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
link => [
|
||||
t8('Add'),
|
||||
link => $self->url_for(action => 'new'),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
sub setup_form_action_bar {
|
||||
my ($self) = @_;
|
||||
my $is_new = !$self->payment_term->id;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Save'),
|
||||
submit => [ '#form', { action => 'PaymentTerm/' . ($is_new ? 'create' : 'update') } ],
|
||||
checks => [ 'kivi.validate_form' ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
action => [
|
||||
t8('Delete'),
|
||||
submit => [ '#form', { action => 'PaymentTerm/destroy' } ],
|
||||
confirm => t8('Do you really want to delete this object?'),
|
||||
disabled => $is_new ? t8('This object has not been saved yet.') : undef,
|
||||
],
|
||||
'separator',
|
||||
link => [
|
||||
t8('Abort'),
|
||||
link => $self->url_for(action => 'list'),
|
||||
],
|
||||
);
|
||||
}
|
||||
bf231cac | Sven Schöling | $::request->layout->add_javascripts('kivi.Validator.js');
|
||
032e3094 | Moritz Bunkus | }
|
||
dbbf8923 | Moritz Bunkus | 1;
|