1 #=====================================================================
 
   6 #   Email: p.reetz@linet-services.de
 
   7 #     Web: http://www.lx-office.org
 
  10 # This program is free software; you can redistribute it and/or modify
 
  11 # it under the terms of the GNU General Public License as published by
 
  12 # the Free Software Foundation; either version 2 of the License, or
 
  13 # (at your option) any later version.
 
  15 # This program is distributed in the hope that it will be useful,
 
  16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  18 # GNU General Public License for more details.
 
  19 # You should have received a copy of the GNU General Public License
 
  20 # along with this program; if not, write to the Free Software
 
  21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
  23 #======================================================================
 
  27 #======================================================================
 
  29 use POSIX qw(strftime getcwd);
 
  30 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
 
  33 use SL::DATEV qw(:CONSTANTS);
 
  34 use SL::Locale::String qw(t8);
 
  42 require "bin/mozilla/common.pl";
 
  44 sub continue { call_sub($main::form->{"nextsub"}); }
 
  47   $::lxdebug->enter_sub;
 
  48   $::auth->assert('datev_export');
 
  50   my $stamm = SL::DATEV->new->get_datev_stamm;
 
  52   setup_datev_export_action_bar();
 
  55   print $::form->parse_html_template('datev/export', $stamm);
 
  57   $::lxdebug->leave_sub;
 
  61   $::lxdebug->enter_sub;
 
  62   $::auth->assert('datev_export');
 
  64   if ($::form->{exporttype} == 0) {
 
  65     export_bewegungsdaten();
 
  69   $::lxdebug->leave_sub;
 
  72 sub export_bewegungsdaten {
 
  73   $::lxdebug->enter_sub;
 
  74   $::auth->assert('datev_export');
 
  76   setup_datev_export2_action_bar();
 
  79   print $::form->parse_html_template('datev/export_bewegungsdaten');
 
  81   $::lxdebug->leave_sub;
 
  84 sub export_stammdaten {
 
  85   $::lxdebug->enter_sub;
 
  86   $::auth->assert('datev_export');
 
  88   setup_datev_export2_action_bar();
 
  91   print $::form->parse_html_template('datev/export_stammdaten');
 
  93   $::lxdebug->leave_sub;
 
  97   $::lxdebug->enter_sub;
 
  98   $::auth->assert('datev_export');
 
 101     exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
 
 102     format     => $::form->{kne}        ? DATEV_FORMAT_KNE : DATEV_FORMAT_OBE,
 
 105   if ($::form->{exporttype} == DATEV_ET_STAMM) {
 
 106     $data{accnofrom}  = $::form->{accnofrom},
 
 107     $data{accnoto}    = $::form->{accnoto},
 
 108   } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
 
 109     @data{qw(from to)} = _get_dates(
 
 110       $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
 
 111       $::form->{transdatefrom}, $::form->{transdateto},
 
 114     die 'invalid exporttype';
 
 117   my $datev = SL::DATEV->new(%data);
 
 119   $datev->clean_temporary_directories;
 
 120   $datev->save_datev_stamm($::form);
 
 124   if (!$datev->errors) {
 
 125     setup_datev_export3_action_bar(download_token => $datev->download_token);
 
 128     print $::form->parse_html_template('datev/export3');
 
 130     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
 
 133   $::lxdebug->leave_sub;
 
 137   $main::lxdebug->enter_sub();
 
 139   my $form     = $main::form;
 
 140   my $locale   = $main::locale;
 
 142   $::auth->assert('datev_export');
 
 144   my $tmp_name = Common->tmpname();
 
 145   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
 
 149   my $datev = SL::DATEV->new(download_token => $form->{download_token});
 
 151   my $path = $datev->export_path;
 
 153     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 156   chdir($path) || die("chdir $path");
 
 158   my @filenames = glob "*";
 
 162     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 165   my $zip = Archive::Zip->new();
 
 166   map { $zip->addFile($_); } @filenames;
 
 167   $zip->writeToFileNamed($tmp_name);
 
 171   open(IN, $tmp_name) || die("open $tmp_name");
 
 172   $::locale->with_raw_io(\*STDOUT, sub {
 
 173     print("Content-Type: application/zip\n");
 
 174     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
 
 183   $main::lxdebug->leave_sub();
 
 187   $::lxdebug->enter_sub;
 
 189   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
 
 190   my ($fromdate, $todate);
 
 192   if ($mode eq "monat") {
 
 193     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
 
 194     $todate   = $fromdate->clone->add(months => 1)->add(days => -1);
 
 195   } elsif ($mode eq "quartal") {
 
 196     die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
 
 197     $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
 
 198     $todate   = $fromdate->clone->add(months => 3)->add(days => -1);
 
 199   } elsif ($mode eq "zeit") {
 
 200     $fromdate = DateTime->from_lxoffice($transdatefrom);
 
 201     $todate   = DateTime->from_lxoffice($transdateto);
 
 202     die 'need from and to time' unless $fromdate && $todate;
 
 204     die 'undefined interval mode';
 
 207   $::lxdebug->leave_sub;
 
 209   return ($fromdate, $todate);
 
 212 sub setup_datev_export_action_bar {
 
 215   for my $bar ($::request->layout->get('actionbar')) {
 
 219         submit    => [ '#form', { action => 'export2' } ],
 
 220         accesskey => 'enter',
 
 226 sub setup_datev_export2_action_bar {
 
 229   for my $bar ($::request->layout->get('actionbar')) {
 
 233         submit    => [ '#form', { action => 'export3' } ],
 
 234         accesskey => 'enter',
 
 238         call => [ 'kivi.history_back' ],
 
 244 sub setup_datev_export3_action_bar {
 
 247   for my $bar ($::request->layout->get('actionbar')) {
 
 251         link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
 
 255         call => [ 'kivi.history_back' ],