1 package SL::Controller::GoBD;
 
   5 use parent qw(SL::Controller::Base);
 
   9 use SL::Locale::String qw(t8);
 
  10 use SL::Helper::Flash;
 
  12 use SL::DB::AccTransaction;
 
  14 use Rose::Object::MakeMethods::Generic (
 
  15   'scalar --get_set_init' => [ qw(from to) ],
 
  18 __PACKAGE__->run_before('check_auth');
 
  23   $self->from(DateTime->today->add(years => -1)->add(days => 1)) if !$self->from;
 
  24   $self->to(DateTime->today)                                     if !$self->to;
 
  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'));
 
  34   if (!$self->check_inputs) {
 
  40   my $gobd = SL::GoBD->new(
 
  41     company    => $::instance_conf->get_company,
 
  42     location   => $::instance_conf->get_address,
 
  48     $filename = $gobd->generate_export;
 
  51     flash('error', t8('The export failed because of malformed transactions. Please fix those before exporting.'));
 
  52     flash('error', $_) for @$errors;
 
  58   $self->send_file($filename, name => t8('gobd-#1-#2.zip', $self->from->ymd, $self->to->ymd), unlink => 1);
 
  63 sub check_auth { $::auth->assert('report') }
 
  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));
 
  76       flash('error', t8('No year given for method year'));
 
  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));
 
  86       flash('info', t8('No end date given, setting to today'));
 
  87       $self->to(DateTime->today);
 
  97   my $first_trans = SL::DB::Manager::AccTransaction->get_first(sort_by => 'transdate', limit => 1);
 
  99   return [] unless $first_trans;
 
 100   return [ reverse $first_trans->transdate->year .. DateTime->today->year ];
 
 103 sub init_from { DateTime->from_kivitendo($::form->{from}) }
 
 104 sub init_to { DateTime->from_kivitendo($::form->{to}) }
 
 106 sub setup_filter_action_bar {
 
 109   for my $bar ($::request->layout->get('actionbar')) {
 
 113         submit    => [ '#filter_form', { action => 'GoBD/export' } ],
 
 114         accesskey => 'enter',