596351b6946fcb7a2cf5c48f308bebd077ae79d2
[kivitendo-erp.git] / SL / Controller / Gdpdu.pm
1 package SL::Controller::Gdpdu;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use DateTime;
8 use SL::GDPDU;
9 use SL::Locale::String qw(t8);
10 use SL::Helper::Flash;
11
12 use SL::DB::AccTransaction;
13
14 use Rose::Object::MakeMethods::Generic (
15   'scalar --get_set_init' => [ qw(from to) ],
16 );
17
18 __PACKAGE__->run_before('check_auth');
19
20 sub action_filter {
21   my ($self) = @_;
22
23   $self->from(DateTime->today->add(years => -1)->add(days => 1)) if !$self->from;
24   $self->to(DateTime->today)                                     if !$self->to;
25
26   $::request->layout->add_javascripts('kivi.Gdpdu.js');
27   $self->render('gdpdu/filter', current_year => DateTime->today->year, title => t8('GDPDU Export'));
28 }
29
30 sub action_export {
31   my ($self) = @_;
32
33   if (!$self->check_inputs) {
34     $self->action_filter;
35     return;
36   }
37
38   my $gdpdu = SL::GDPDU->new(
39     company    => $::instance_conf->get_company,
40     location   => $::instance_conf->get_address,
41     from       => $self->from,
42     to         => $self->to,
43   );
44
45   my $filename = $gdpdu->generate_export;
46
47   $self->send_file($filename, name => t8('gdpdu-#1-#2.zip', $self->from->ymd, $self->to->ymd), unlink => 1);
48 }
49
50 #--- other stuff
51
52 sub check_auth { $::auth->assert('report') }
53
54 sub check_inputs {
55   my ($self) = @_;
56
57   my $error = 0;
58
59   if ($::form->{method} eq 'year') {
60     if ($::form->{year}) {
61       $self->from(DateTime->new(year => $::form->{year}, month => 1,  day => 1));
62       $self->to(  DateTime->new(year => $::form->{year}, month => 12, day => 31));
63     } else {
64       $error = 1;
65       flash('error', t8('No year given for method year'));
66     }
67   } else {
68     if (!$::form->{from}) {
69       my $epoch = DateTime->new(day => 1, month => 1, year => 1900);
70       flash('info', t8('No start date given, setting to #1', $epoch->to_kivitendo));
71       $self->from($epoch);
72     }
73
74     if (!$::form->{to}) {
75       flash('info', t8('No end date given, setting to today'));
76       $self->to(DateTime->today);
77     }
78   }
79
80   !$error;
81 }
82
83 sub available_years {
84   my ($self) = @_;
85
86   my $first_trans = SL::DB::Manager::AccTransaction->get_first(sort_by => 'transdate', limit => 1);
87
88   return [] unless $first_trans;
89   return [ reverse $first_trans->transdate->year .. DateTime->today->year ];
90 }
91
92 sub init_from { DateTime->from_kivitendo($::form->{from}) }
93 sub init_to { DateTime->from_kivitendo($::form->{to}) }
94
95 1;