GDPdU Export - erste Version
[kivitendo-erp.git] / SL / Controller / Gdpdu.pm
1 package SL::Controller::Gdpdu;
2
3 # TODO:
4 #  - depending exclusive checkboses via javascript
5
6 use strict;
7
8 use parent qw(SL::Controller::Base);
9
10 use SL::GDPDU;
11 use SL::Locale::String qw(t8);
12 use SL::Helper::Flash;
13
14 use Rose::Object::MakeMethods::Generic (
15   'scalar --get_set_init' => [ qw(from to tables) ],
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   $self->render('gdpdu/filter', title => t8('GDPDU Export'));
27 }
28
29 sub action_export {
30   my ($self) = @_;
31
32   if (!$self->check_inputs) {
33     $self->action_filter;
34     return;
35   }
36
37   my $gdpdu = SL::GDPDU->new(
38     company    => $::instance_conf->get_company,
39     location   => $::instance_conf->get_address,
40     from       => $self->from,
41     to         => $self->to,
42     tables     => $self->tables,
43     all_tables => !@{ $self->tables } && $::form->{all_tables},
44   );
45
46   my $filename = $gdpdu->generate_export;
47
48   $self->send_file($filename, name => t8('gdpdu-#1-#2.zip', $self->from->ymd, $self->to->ymd), unlink => 1);
49 }
50
51 #--- other stuff
52
53 sub check_auth { $::auth->assert('report') }
54
55 sub check_inputs {
56   my ($self) = @_;
57
58   my $error = 0;
59
60   if ($::form->{tables}) {
61     $self->tables([ keys %{ $::form->{tables} } ]);
62     # theese three get inferred
63     push @{ $self->tables }, 'invoice'              if $::form->{tables}{ar} || $::form->{tables}{ap};
64     push @{ $self->tables }, 'orderitems'           if $::form->{tables}{oe};
65     push @{ $self->tables }, 'delivery_order_items' if $::form->{tables}{delivery_orders};
66   }
67
68   if (!@{ $self->tables } && !$::form->{all_tables}) {
69     flash('error', t8('No, I really do need checked tables to export.'));
70     $error = 1;
71   }
72
73   if (!$::form->{from}) {
74     my $epoch = DateTime->new(day => 1, month => 1, year => 1900);
75     flash('info', t8('No start date given, setting to #1', $epoch->to_kivitendo));
76     $self->from($epoch);
77   }
78
79   if (!$::form->{to}) {
80     flash('info', t8('No end date given, setting to today'));
81     $self->to(DateTime->today);
82   }
83
84   !$error;
85 }
86
87 sub init_from { DateTime->from_kivitendo($::form->{from}) }
88 sub init_to { DateTime->from_kivitendo($::form->{to}) }
89 sub init_tables { [ ] }
90
91 1;