182d8b26f3f13df8278fb13e88a2e4dad74c8520
[kivitendo-erp.git] / SL / Controller / GoBD.pm
1 package SL::Controller::GoBD;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use DateTime;
8 use SL::GoBD;
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.GoBD.js');
27   $self->setup_filter_action_bar;
28   $self->render('gobd/filter', current_year => DateTime->today->year, title => t8('GoBD Export'));
29 }
30
31 sub action_export {
32   my ($self) = @_;
33
34   if (!$self->check_inputs) {
35     $self->action_filter;
36     return;
37   }
38
39   my $filename;
40   my $gobd = SL::GoBD->new(
41     company    => $::instance_conf->get_company,
42     location   => $::instance_conf->get_address,
43     from       => $self->from,
44     to         => $self->to,
45   );
46
47   eval {
48     $filename = $gobd->generate_export;
49   } or do {
50     my $errors = $@;
51     flash('error', t8('The export failed because of malformed transactions. Please fix those before exporting.'));
52     flash('error', $_) for @$errors;
53
54     $self->action_filter;
55     return;
56   };
57
58   $self->send_file($filename, name => t8('gobd-#1-#2.zip', $self->from->ymd, $self->to->ymd), unlink => 1);
59 }
60
61 #--- other stuff
62
63 sub check_auth { $::auth->assert('report') }
64
65 sub check_inputs {
66   my ($self) = @_;
67
68   my $error = 0;
69
70   if ($::form->{method} eq 'year') {
71     if ($::form->{year}) {
72       $self->from(DateTime->new(year => $::form->{year}, month => 1,  day => 1));
73       $self->to(  DateTime->new(year => $::form->{year}, month => 12, day => 31));
74     } else {
75       $error = 1;
76       flash('error', t8('No year given for method year'));
77     }
78   } else {
79     if (!$::form->{from}) {
80       my $epoch = DateTime->new(day => 1, month => 1, year => 1900);
81       flash('info', t8('No start date given, setting to #1', $epoch->to_kivitendo));
82       $self->from($epoch);
83     }
84
85     if (!$::form->{to}) {
86       flash('info', t8('No end date given, setting to today'));
87       $self->to(DateTime->today);
88     }
89   }
90
91   !$error;
92 }
93
94 sub available_years {
95   my ($self) = @_;
96
97   my $first_trans = SL::DB::Manager::AccTransaction->get_first(sort_by => 'transdate', limit => 1);
98
99   return [] unless $first_trans;
100   return [ reverse $first_trans->transdate->year .. DateTime->today->year ];
101 }
102
103 sub init_from { DateTime->from_kivitendo($::form->{from}) }
104 sub init_to { DateTime->from_kivitendo($::form->{to}) }
105
106 sub setup_filter_action_bar {
107   my ($self) = @_;
108
109   for my $bar ($::request->layout->get('actionbar')) {
110     $bar->add(
111       action => [
112         t8('Export'),
113         submit    => [ '#filter_form', { action => 'GoBD/export' } ],
114         accesskey => 'enter',
115       ],
116     );
117   }
118 }
119
120 1;