]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/PayPostingImport.pm
restart apache2 in postinst
[mfinanz.git] / SL / Controller / PayPostingImport.pm
index 76a5a9978254617eaf61d5cbf92635651385e77d..733dc5d28adad8b03ef98e13728f5c9739559bed 100644 (file)
@@ -17,7 +17,15 @@ sub action_upload_pay_postings {
   my ($self, %params) = @_;
 
   $self->setup_pay_posting_action_bar;
-  $self->render('pay_posting_import/form', title => $::locale->text('Import Pay Postings'));
+
+  # new closedto
+  my $today = DateTime->now();
+  $today->subtract(months => 1);
+
+  my $dt = DateTime->last_day_of_month(year  => $today->year, month => $today->month);
+
+  my $new_closedto = $dt->to_kivitendo();
+  $self->render('pay_posting_import/form', title => $::locale->text('Import Pay Postings'), closedto => $new_closedto);
 }
 
 sub action_import_datev_pay_postings {
@@ -26,28 +34,31 @@ sub action_import_datev_pay_postings {
   die t8("missing file for action import") unless ($::form->{file});
 
   my $filename= $::form->{ATTACHMENTS}{file}{filename};
-
   # check name and first fields of CSV data
   die t8("Wrong file name, expects name like: DTVF_*_LOHNBUCHUNG*.csv") unless $filename =~ /^DTVF_.*_LOHNBUCHUNGEN_LUG.*\.csv$/;
-  die t8("not a valid DTVF file, expected first field in A1 'DTVF'")   unless ($::form->{file} =~ m/^"DTVF";/);
+  die t8("not a valid DTVF file, expected first field in A1 'DTVF'")    unless ($::form->{file} =~ m/^('|")?DTVF/);
   die t8("not a valid DTVF file, expected field header start with 'Umsatz; (..) ;Konto;Gegenkonto'")
     unless ($::form->{file} =~ m/Umsatz;S\/H;;;;;Konto;Gegenkonto.*;;Belegdatum;Belegfeld 1;Belegfeld 2;;Buchungstext/);
 
   # check if file is already imported
-  my $acc_trans_doc = SL::DB::Manager::AccTransaction->get_first(source => $filename);
-  die t8("Already imported") if ref $acc_trans_doc eq 'SL::DB::AccTransaction';
+  my $acc_trans_doc = SL::DB::Manager::AccTransaction->get_first(query => [ source => $filename ]);
+  die t8("Already imported: ") . $acc_trans_doc->source if ref $acc_trans_doc eq 'SL::DB::AccTransaction';
 
-  if (parse_and_import($::form->{file}, $filename)) {
+  if (parse_and_import($self)) {
     flash_later('info', t8("All pay postings successfully imported."));
   }
-  # $self->redirect_to("gl.pl?action=search", source => $filename);
+  if ($::form->{set_closedto} && _set_closedto($self)) {
+    flash_later('info', t8("Books closed until:") . ' ' . $::form->{closedto});
+  }
+  $self->setup_pay_posting_action_bar;
+  $self->render('pay_posting_import/form', title => $::locale->text('Imported Pay Postings'));
 }
 
 sub parse_and_import {
-  my $doc      = shift;
+  my $self     = shift;
 
   my $csv = Text::CSV_XS->new ({ binary => 0, auto_diag => 1, sep_char => ";" });
-  open my $fh, "<:encoding(cp1252)", \$doc;
+  open (my $fh, "<:encoding(cp1252)", \$::form->{file}) or die "cannot open $::form->{file} $!";
   # Read/parse CSV
   # Umsatz S/H Konto Gegenkonto (ohne BU-Schlüssel) Belegdatum Belegfeld 1 Belegfeld 2 Buchungstext
   my $year = substr($csv->getline($fh)->[12], 0, 4);
@@ -81,7 +92,9 @@ sub parse_and_import {
 
       # optional KOST1 - KOST2 ?
       $department_name = $row->[36];
-      $department    = SL::DB::Manager::Department->get_first(description => { like =>  $department_name . '%' });
+      if ($department_name) {
+        $department    = SL::DB::Manager::Department->get_first(where => [ description => { ilike =>  $department_name . '%' } ]);
+      }
 
       my $amount = $::form->parse_amount({ numberformat => '1000,00' }, $row->[0]);
 
@@ -103,7 +116,7 @@ sub parse_and_import {
           source => $::form->{ATTACHMENTS}{file}{filename},
       )->post;
 
-      # push @rows, $current_transaction->id;
+      push @{ $self->{gl_trans} }, $current_transaction;
 
       if ($::instance_conf->get_doc_storage) {
         my $file = SL::File->save(object_id   => $current_transaction->id,
@@ -112,7 +125,7 @@ sub parse_and_import {
                        source      => 'uploaded',
                        file_type   => 'attachment',
                        file_name   => $::form->{ATTACHMENTS}{file}{filename},
-                       file_contents   => $doc
+                       file_contents   => $::form->{file},
                       );
       }
     }
@@ -122,6 +135,19 @@ sub parse_and_import {
   }) or do { die t8("Cannot add Booking, reason: #1 DB: #2 ", $@, SL::DB->client->error) };
 }
 
+
+sub _set_closedto {
+  my $self     = shift;
+  die "no date:" . $::form->{closedto} unless $::form->{closedto};
+
+  my $defaults   = SL::DB::Default->get;
+
+  $defaults->closedto(DateTime->from_kivitendo($::form->{closedto}));
+  $defaults->save || die "Cannot save closedto!";
+
+  return 1;
+}
+
 sub check_auth {
   $::auth->assert('general_ledger');
 }