kivitendo/SL/Controller/Buchungsgruppen.pm @ 4a663bf8
f5c454e3 | Niclas Zimmermann | package SL::Controller::Buchungsgruppen;
|
||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
use SL::DB::TaxZone;
|
||||
use SL::Helper::Flash;
|
||||
use SL::Locale::String;
|
||||
use SL::DB::TaxzoneChart;
|
||||
058d70b8 | Geoffrey Richardson | use SL::DB::Default;
|
||
f5c454e3 | Niclas Zimmermann | |||
use Rose::Object::MakeMethods::Generic (
|
||||
scalar => [ qw(config) ],
|
||||
058d70b8 | Geoffrey Richardson | 'scalar --get_set_init' => [ qw(defaults) ],
|
||
f5c454e3 | Niclas Zimmermann | );
|
||
__PACKAGE__->run_before('check_auth');
|
||||
058d70b8 | Geoffrey Richardson | __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
|
||
f5c454e3 | Niclas Zimmermann | |||
#
|
||||
# actions
|
||||
#
|
||||
sub action_list {
|
||||
my ($self) = @_;
|
||||
my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
|
||||
6e603868 | Geoffrey Richardson | my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);
|
||
f5c454e3 | Niclas Zimmermann | |||
my %chartlist = ();
|
||||
foreach my $gruppe (@{ $buchungsgruppen }) {
|
||||
$chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
|
||||
}
|
||||
7da802e8 | Safwan Shakhy | my $title = t8('Booking groups');
|
||
5e8c9df6 | Moritz Bunkus | $self->setup_list_action_bar;
|
||
e1cdb457 | Safwan Shakhy | $::form->{title} = $title;
|
||
f5c454e3 | Niclas Zimmermann | $::form->header;
|
||
$self->render('buchungsgruppen/list',
|
||||
e1cdb457 | Safwan Shakhy | title => $title,
|
||
f5c454e3 | Niclas Zimmermann | BUCHUNGSGRUPPEN => $buchungsgruppen,
|
||
CHARTLIST => \%chartlist,
|
||||
TAXZONES => $taxzones);
|
||||
}
|
||||
sub action_new {
|
||||
my ($self) = @_;
|
||||
$self->config(SL::DB::Buchungsgruppe->new());
|
||||
504fcaf1 | Geoffrey Richardson | $self->show_form(title => t8('Add booking group'));
|
||
f5c454e3 | Niclas Zimmermann | }
|
||
sub show_form {
|
||||
my ($self, %params) = @_;
|
||||
5e8c9df6 | Moritz Bunkus | $self->setup_show_form_action_bar;
|
||
f5c454e3 | Niclas Zimmermann | $self->render('buchungsgruppen/form', %params,
|
||
000588da | Geoffrey Richardson | TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted());
|
||
f5c454e3 | Niclas Zimmermann | }
|
||
sub action_edit {
|
||||
my ($self) = @_;
|
||||
d3801bc9 | Geoffrey Richardson | # Allow editing of the charts of the Buchungsgruppe if it isn't assigned to
|
||
# any parts. This is checked inside the template via the Buchungsgruppen
|
||||
# orphaned method, where an IF-ELSE statement toggles between L.select_tag
|
||||
# and text.
|
||||
6e603868 | Geoffrey Richardson | |||
504fcaf1 | Geoffrey Richardson | $self->show_form(title => t8('Edit booking group'),
|
||
f5c454e3 | Niclas Zimmermann | CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
|
||
}
|
||||
sub action_create {
|
||||
my ($self) = @_;
|
||||
$self->config(SL::DB::Buchungsgruppe->new());
|
||||
$self->create_or_update;
|
||||
}
|
||||
sub action_update {
|
||||
my ($self) = @_;
|
||||
$self->create_or_update;
|
||||
}
|
||||
058d70b8 | Geoffrey Richardson | sub action_delete {
|
||
my ($self) = @_;
|
||||
# allow deletion of unused Buchungsgruppen. Will fail, due to database
|
||||
# constraint, if Buchungsgruppe is connected to a part
|
||||
96670fe8 | Moritz Bunkus | $self->{config}->db->with_transaction(sub {
|
||
my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
|
||||
foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
|
||||
$self->config->delete();
|
||||
flash_later('info', $::locale->text('The booking group has been deleted.'));
|
||||
1;
|
||||
504fcaf1 | Geoffrey Richardson | }) || flash_later('error', $::locale->text('The booking group is in use and cannot be deleted.'));
|
||
058d70b8 | Geoffrey Richardson | |||
$self->redirect_to(action => 'list');
|
||||
}
|
||||
f5c454e3 | Niclas Zimmermann | sub action_reorder {
|
||
my ($self) = @_;
|
||||
SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
|
||||
$self->render(\'', { type => 'json' });
|
||||
}
|
||||
#
|
||||
# filters
|
||||
#
|
||||
sub check_auth {
|
||||
$::auth->assert('config');
|
||||
}
|
||||
sub load_config {
|
||||
my ($self) = @_;
|
||||
$self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
|
||||
}
|
||||
#
|
||||
# helpers
|
||||
#
|
||||
sub create_or_update {
|
||||
my ($self) = @_;
|
||||
my $is_new = !$self->config->id;
|
||||
my $params = delete($::form->{config}) || { };
|
||||
delete $params->{id};
|
||||
a28a585e | Geoffrey Richardson | my @errors;
|
||
f5c454e3 | Niclas Zimmermann | |||
a28a585e | Geoffrey Richardson | my $db = $self->config->db;
|
||
96670fe8 | Moritz Bunkus | if (!$db->with_transaction(sub {
|
||
f5c454e3 | Niclas Zimmermann | |||
a28a585e | Geoffrey Richardson | $self->config->assign_attributes(%{ $params }); # assign description and inventory_accno_id
|
||
@errors = $self->config->validate; # check for description and inventory_accno_id
|
||||
cff9b88d | Moritz Bunkus | return 0 if @errors;
|
||
a28a585e | Geoffrey Richardson | |||
$self->config->save;
|
||||
f5c454e3 | Niclas Zimmermann | |||
a28a585e | Geoffrey Richardson | # Save or update taxzone_charts for new or unused Buchungsgruppen
|
||
if ($is_new or $self->config->orphaned) {
|
||||
my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
|
||||
f5c454e3 | Niclas Zimmermann | |||
a28a585e | Geoffrey Richardson | foreach my $tz (@{ $taxzones }) {
|
||
f5c454e3 | Niclas Zimmermann | |||
a28a585e | Geoffrey Richardson | my $income_accno_id = $::form->{"income_accno_id_" . $tz->id};
|
||
my $expense_accno_id = $::form->{"expense_accno_id_" . $tz->id};
|
||||
my ($income_accno, $expense_accno);
|
||||
$income_accno = SL::DB::Manager::Chart->find_by( id => $income_accno_id ) if $income_accno_id;
|
||||
$expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
|
||||
push(@errors, t8('Tax zone #1 needs a valid income account' , $tz->description)) unless $income_accno;
|
||||
push(@errors, t8('Tax zone #1 needs a valid expense account' , $tz->description)) unless $expense_accno;
|
||||
my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
|
||||
$taxzone_chart->taxzone_id($tz->id);
|
||||
$taxzone_chart->buchungsgruppen_id($self->config->id);
|
||||
$taxzone_chart->income_accno_id($income_accno->id);
|
||||
$taxzone_chart->expense_accno_id($expense_accno->id);
|
||||
$taxzone_chart->save;
|
||||
}
|
||||
f5c454e3 | Niclas Zimmermann | }
|
||
96670fe8 | Moritz Bunkus | |||
1;
|
||||
})) {
|
||||
cff9b88d | Moritz Bunkus | my $error = @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
|
||
$::form->show_generic_error($error);
|
||||
96670fe8 | Moritz Bunkus | # die with rollback of taxzone save if saving of any of the taxzone_charts fails
|
||
# only show the $db->error if we haven't already identified the likely error ourselves
|
||||
}
|
||||
f5c454e3 | Niclas Zimmermann | |||
504fcaf1 | Geoffrey Richardson | flash_later('info', $is_new ? t8('The booking group has been created.') : t8('The booking group has been saved.'));
|
||
f5c454e3 | Niclas Zimmermann | $self->redirect_to(action => 'list');
|
||
}
|
||||
058d70b8 | Geoffrey Richardson | #
|
||
# initializers
|
||||
#
|
||||
sub init_defaults { SL::DB::Default->get }
|
||||
5e8c9df6 | Moritz Bunkus | #
|
||
# helpers
|
||||
#
|
||||
sub setup_show_form_action_bar {
|
||||
my ($self) = @_;
|
||||
my $is_new = !$self->config->id;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Save'),
|
||||
submit => [ '#form', { action => 'Buchungsgruppen/' . ($is_new ? 'create' : 'update') } ],
|
||||
checks => [ 'kivi.validate_form' ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
action => [
|
||||
t8('Delete'),
|
||||
submit => [ '#form', { action => 'Buchungsgruppen/delete' } ],
|
||||
confirm => t8('Do you really want to delete this object?'),
|
||||
disabled => $is_new ? t8('This object has not been saved yet.')
|
||||
: !$self->config->orphaned ? t8('The object is in use and cannot be deleted.')
|
||||
: undef,
|
||||
],
|
||||
link => [
|
||||
t8('Abort'),
|
||||
link => $self->url_for(action => 'list'),
|
||||
],
|
||||
);
|
||||
}
|
||||
bf231cac | Sven Schöling | $::request->layout->add_javascripts('kivi.Validator.js');
|
||
5e8c9df6 | 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'),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
f5c454e3 | Niclas Zimmermann | 1;
|