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);
 
  35 use SL::DB::Department;
 
  43 require "bin/mozilla/common.pl";
 
  45 sub continue { call_sub($main::form->{"nextsub"}); }
 
  48   $::lxdebug->enter_sub;
 
  49   $::auth->assert('datev_export');
 
  51   my $stamm = SL::DATEV->new->get_datev_stamm;
 
  53   setup_datev_export_action_bar();
 
  56   print $::form->parse_html_template('datev/export', $stamm);
 
  58   $::lxdebug->leave_sub;
 
  62   $::lxdebug->enter_sub;
 
  63   $::auth->assert('datev_export');
 
  65   export_bewegungsdaten();
 
  67   $::lxdebug->leave_sub;
 
  70 sub export_bewegungsdaten {
 
  71   $::lxdebug->enter_sub;
 
  72   $::auth->assert('datev_export');
 
  74   setup_datev_export2_action_bar();
 
  77   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
 
  78   $::form->{show_pk_option}  = SL::DATEV->new->check_vcnumbers_are_valid_pk_numbers;
 
  80   # check if we have mismatching number length domains
 
  81   SL::DATEV->new->check_valid_length_of_accounts;
 
  83   print $::form->parse_html_template('datev/export_bewegungsdaten');
 
  85   $::lxdebug->leave_sub;
 
  89   $::lxdebug->enter_sub;
 
  90   $::auth->assert('datev_export');
 
  93     exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
 
  94     format     => $::form->{exportformat} eq 'kne' ? DATEV_FORMAT_KNE :  DATEV_FORMAT_CSV,
 
  97   @data{qw(from to)} = _get_dates(
 
  98     $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
 
  99     $::form->{transdatefrom}, $::form->{transdateto},
 
 101   $data{use_pk} = $::form->{use_pk};
 
 102   $data{locked} = $::form->{locked};
 
 103   $data{imported} = $::form->{imported};
 
 105   my $datev = SL::DATEV->new(%data);
 
 107   $datev->clean_temporary_directories;
 
 108   $datev->save_datev_stamm($::form);
 
 112   if (!$datev->errors) {
 
 113     setup_datev_export3_action_bar(download_token => $datev->download_token);
 
 116     print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
 
 118     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
 
 121   $::lxdebug->leave_sub;
 
 125   $main::lxdebug->enter_sub();
 
 127   my $form     = $main::form;
 
 128   my $locale   = $main::locale;
 
 130   $::auth->assert('datev_export');
 
 132   my $tmp_name = Common->tmpname();
 
 133   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
 
 137   my $datev = SL::DATEV->new(download_token => $form->{download_token});
 
 139   my $path = $datev->export_path;
 
 141     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 144   chdir($path) || die("chdir $path");
 
 146   my @filenames = glob "*";
 
 150     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 153   my $zip = Archive::Zip->new();
 
 154   map { $zip->addFile($_); } @filenames;
 
 155   $zip->writeToFileNamed($tmp_name);
 
 159   open(IN, $tmp_name) || die("open $tmp_name");
 
 160   $::locale->with_raw_io(\*STDOUT, sub {
 
 161     print("Content-Type: application/zip\n");
 
 162     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
 
 171   $main::lxdebug->leave_sub();
 
 175   $::lxdebug->enter_sub;
 
 177   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
 
 178   my ($fromdate, $todate);
 
 180   if ($mode eq "monat") {
 
 181     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
 
 182     # december export is usually in january/february
 
 183     $fromdate = $fromdate->subtract(years => 1) if ($month == 12);
 
 185     $todate   = $fromdate->clone->add(months => 1)->add(days => -1);
 
 186   } elsif ($mode eq "quartal") {
 
 187     die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
 
 188     $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
 
 189     $todate   = $fromdate->clone->add(months => 3)->add(days => -1);
 
 190   } elsif ($mode eq "zeit") {
 
 191     $fromdate = DateTime->from_lxoffice($transdatefrom);
 
 192     $todate   = DateTime->from_lxoffice($transdateto);
 
 193     die 'need from and to time' unless $fromdate && $todate;
 
 195     die 'undefined interval mode';
 
 198   $::lxdebug->leave_sub;
 
 200   return ($fromdate, $todate);
 
 203 sub setup_datev_export_action_bar {
 
 206   for my $bar ($::request->layout->get('actionbar')) {
 
 210         submit    => [ '#form', { action => 'export2' } ],
 
 211         accesskey => 'enter',
 
 217 sub setup_datev_export2_action_bar {
 
 220   for my $bar ($::request->layout->get('actionbar')) {
 
 224         submit    => [ '#form', { action => 'export3' } ],
 
 225         accesskey => 'enter',
 
 229         call => [ 'kivi.history_back' ],
 
 235 sub setup_datev_export3_action_bar {
 
 238   for my $bar ($::request->layout->get('actionbar')) {
 
 242         link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
 
 246         call => [ 'kivi.history_back' ],