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   if ($::form->{exporttype} == 0) {
 
  66     export_bewegungsdaten();
 
  70   $::lxdebug->leave_sub;
 
  73 sub export_bewegungsdaten {
 
  74   $::lxdebug->enter_sub;
 
  75   $::auth->assert('datev_export');
 
  77   setup_datev_export2_action_bar();
 
  80   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
 
  81   $::form->{show_pk_option}  = SL::DATEV->new->check_vcnumbers_are_valid_pk_numbers;
 
  83   # check if we have mismatching number length domains
 
  84   SL::DATEV->new->check_valid_length_of_accounts;
 
  86   print $::form->parse_html_template('datev/export_bewegungsdaten');
 
  88   $::lxdebug->leave_sub;
 
  91 sub export_stammdaten {
 
  92   $::lxdebug->enter_sub;
 
  93   $::auth->assert('datev_export');
 
  95   setup_datev_export2_action_bar();
 
  98   print $::form->parse_html_template('datev/export_stammdaten');
 
 100   $::lxdebug->leave_sub;
 
 104   $::lxdebug->enter_sub;
 
 105   $::auth->assert('datev_export');
 
 108     exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
 
 109     format     => $::form->{exportformat} eq 'kne' ? DATEV_FORMAT_KNE :  DATEV_FORMAT_CSV,
 
 112   if ($::form->{exporttype} == DATEV_ET_STAMM) {
 
 113     $data{accnofrom}  = $::form->{accnofrom},
 
 114     $data{accnoto}    = $::form->{accnoto},
 
 115   } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
 
 116     @data{qw(from to)} = _get_dates(
 
 117       $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
 
 118       $::form->{transdatefrom}, $::form->{transdateto},
 
 120     $data{use_pk} = $::form->{use_pk};
 
 121     $data{locked} = $::form->{locked};
 
 123     die 'invalid exporttype';
 
 126   my $datev = SL::DATEV->new(%data);
 
 128   $datev->clean_temporary_directories;
 
 129   $datev->save_datev_stamm($::form);
 
 133   if (!$datev->errors) {
 
 134     setup_datev_export3_action_bar(download_token => $datev->download_token);
 
 137     print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
 
 139     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
 
 142   $::lxdebug->leave_sub;
 
 146   $main::lxdebug->enter_sub();
 
 148   my $form     = $main::form;
 
 149   my $locale   = $main::locale;
 
 151   $::auth->assert('datev_export');
 
 153   my $tmp_name = Common->tmpname();
 
 154   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
 
 158   my $datev = SL::DATEV->new(download_token => $form->{download_token});
 
 160   my $path = $datev->export_path;
 
 162     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 165   chdir($path) || die("chdir $path");
 
 167   my @filenames = glob "*";
 
 171     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
 
 174   my $zip = Archive::Zip->new();
 
 175   map { $zip->addFile($_); } @filenames;
 
 176   $zip->writeToFileNamed($tmp_name);
 
 180   open(IN, $tmp_name) || die("open $tmp_name");
 
 181   $::locale->with_raw_io(\*STDOUT, sub {
 
 182     print("Content-Type: application/zip\n");
 
 183     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
 
 192   $main::lxdebug->leave_sub();
 
 196   $::lxdebug->enter_sub;
 
 198   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
 
 199   my ($fromdate, $todate);
 
 201   if ($mode eq "monat") {
 
 202     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
 
 203     # december export is usually in january/february
 
 204     $fromdate = $fromdate->subtract(years => 1) if ($month == 12);
 
 206     $todate   = $fromdate->clone->add(months => 1)->add(days => -1);
 
 207   } elsif ($mode eq "quartal") {
 
 208     die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
 
 209     $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
 
 210     $todate   = $fromdate->clone->add(months => 3)->add(days => -1);
 
 211   } elsif ($mode eq "zeit") {
 
 212     $fromdate = DateTime->from_lxoffice($transdatefrom);
 
 213     $todate   = DateTime->from_lxoffice($transdateto);
 
 214     die 'need from and to time' unless $fromdate && $todate;
 
 216     die 'undefined interval mode';
 
 219   $::lxdebug->leave_sub;
 
 221   return ($fromdate, $todate);
 
 224 sub setup_datev_export_action_bar {
 
 227   for my $bar ($::request->layout->get('actionbar')) {
 
 231         submit    => [ '#form', { action => 'export2' } ],
 
 232         accesskey => 'enter',
 
 238 sub setup_datev_export2_action_bar {
 
 241   for my $bar ($::request->layout->get('actionbar')) {
 
 245         submit    => [ '#form', { action => 'export3' } ],
 
 246         accesskey => 'enter',
 
 250         call => [ 'kivi.history_back' ],
 
 256 sub setup_datev_export3_action_bar {
 
 259   for my $bar ($::request->layout->get('actionbar')) {
 
 263         link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
 
 267         call => [ 'kivi.history_back' ],