kivitendo/SL/Controller/GoBD.pm @ 4b1666b7
e2b04039 | Sven Schöling | package SL::Controller::GoBD;
|
||
11e50931 | Sven Schöling | |||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
db11732b | Sven Schöling | use DateTime;
|
||
e2b04039 | Sven Schöling | use SL::GoBD;
|
||
11e50931 | Sven Schöling | use SL::Locale::String qw(t8);
|
||
use SL::Helper::Flash;
|
||||
db11732b | Sven Schöling | use SL::DB::AccTransaction;
|
||
11e50931 | Sven Schöling | use Rose::Object::MakeMethods::Generic (
|
||
00b6dc22 | Sven Schöling | 'scalar --get_set_init' => [ qw(from to) ],
|
||
11e50931 | Sven Schöling | );
|
||
__PACKAGE__->run_before('check_auth');
|
||||
sub action_filter {
|
||||
my ($self) = @_;
|
||||
$self->from(DateTime->today->add(years => -1)->add(days => 1)) if !$self->from;
|
||||
$self->to(DateTime->today) if !$self->to;
|
||||
e2b04039 | Sven Schöling | $::request->layout->add_javascripts('kivi.GoBD.js');
|
||
6da55ef4 | Moritz Bunkus | $self->setup_filter_action_bar;
|
||
e2b04039 | Sven Schöling | $self->render('gobd/filter', current_year => DateTime->today->year, title => t8('GoBD Export'));
|
||
11e50931 | Sven Schöling | }
|
||
sub action_export {
|
||||
my ($self) = @_;
|
||||
if (!$self->check_inputs) {
|
||||
$self->action_filter;
|
||||
return;
|
||||
}
|
||||
b019bd39 | Sven Schöling | my $filename;
|
||
e2b04039 | Sven Schöling | my $gobd = SL::GoBD->new(
|
||
11e50931 | Sven Schöling | company => $::instance_conf->get_company,
|
||
location => $::instance_conf->get_address,
|
||||
from => $self->from,
|
||||
to => $self->to,
|
||||
);
|
||||
b019bd39 | Sven Schöling | eval {
|
||
$filename = $gobd->generate_export;
|
||||
} or do {
|
||||
my $errors = $@;
|
||||
flash('error', t8('The export failed because of malformed transactions. Please fix those before exporting.'));
|
||||
ebb5daab | Sven Schöling | flash('error', $_) for @$errors;
|
||
b019bd39 | Sven Schöling | |||
$self->action_filter;
|
||||
return;
|
||||
};
|
||||
11e50931 | Sven Schöling | |||
e2b04039 | Sven Schöling | $self->send_file($filename, name => t8('gobd-#1-#2.zip', $self->from->ymd, $self->to->ymd), unlink => 1);
|
||
11e50931 | Sven Schöling | }
|
||
#--- other stuff
|
||||
sub check_auth { $::auth->assert('report') }
|
||||
sub check_inputs {
|
||||
my ($self) = @_;
|
||||
my $error = 0;
|
||||
db11732b | Sven Schöling | if ($::form->{method} eq 'year') {
|
||
if ($::form->{year}) {
|
||||
$self->from(DateTime->new(year => $::form->{year}, month => 1, day => 1));
|
||||
$self->to( DateTime->new(year => $::form->{year}, month => 12, day => 31));
|
||||
} else {
|
||||
$error = 1;
|
||||
flash('error', t8('No year given for method year'));
|
||||
}
|
||||
} else {
|
||||
if (!$::form->{from}) {
|
||||
my $epoch = DateTime->new(day => 1, month => 1, year => 1900);
|
||||
flash('info', t8('No start date given, setting to #1', $epoch->to_kivitendo));
|
||||
$self->from($epoch);
|
||||
}
|
||||
if (!$::form->{to}) {
|
||||
flash('info', t8('No end date given, setting to today'));
|
||||
$self->to(DateTime->today);
|
||||
}
|
||||
11e50931 | Sven Schöling | }
|
||
!$error;
|
||||
}
|
||||
db11732b | Sven Schöling | sub available_years {
|
||
my ($self) = @_;
|
||||
my $first_trans = SL::DB::Manager::AccTransaction->get_first(sort_by => 'transdate', limit => 1);
|
||||
return [] unless $first_trans;
|
||||
return [ reverse $first_trans->transdate->year .. DateTime->today->year ];
|
||||
}
|
||||
11e50931 | Sven Schöling | sub init_from { DateTime->from_kivitendo($::form->{from}) }
|
||
sub init_to { DateTime->from_kivitendo($::form->{to}) }
|
||||
6da55ef4 | Moritz Bunkus | sub setup_filter_action_bar {
|
||
my ($self) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Export'),
|
||||
submit => [ '#filter_form', { action => 'GoBD/export' } ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
11e50931 | Sven Schöling | 1;
|