7 use_ok 'Support::TestSetup';
 
   9 use SL::Controller::PayPostingImport;
 
  14 use Text::CSV_XS qw (csv);
 
  16 Support::TestSetup::login();
 
  18 my $dbh = SL::DB->client->dbh;
 
  19 my @charts = qw(379000 136900 372000 372500 373000 374000 377000 494700);
 
  20 local $::locale = Locale->new('en');
 
  24 # datev naming convention and expected filename entry in $::form
 
  25 $::form->{ATTACHMENTS}{file}{filename} = 'DTVF_44979_43392_LOHNBUCHUNGEN_LUG_202106_20210623_0946';
 
  26 $::form->{file}                        = read_file('t/pay_posting_import/datev.csv');
 
  27 my $source                             = $::form->{ATTACHMENTS}{file}{filename};
 
  29 # get data as aoa datev encodes always CP1252
 
  30 my $csv_array = csv (in        => "t/pay_posting_import/datev.csv",
 
  32                      auto_diag => 1, sep_char => ";", encoding=> "cp1252");
 
  34 # probably no correct charts in test db
 
  36   SL::Controller::PayPostingImport::parse_and_import();
 
  37  } qr/No such Chart 379000/, "Importing Pay Postings without correct charts";
 
  40 foreach my $accno (@charts) {
 
  43     description    => 'Löhne mit Gestöhne',
 
  48     datevautomatik => 'f',
 
  52 # and add department (KOST1 description)
 
  53   SL::DB::Department->new(
 
  54     description => 'Wisavis'
 
  57 SL::Controller::PayPostingImport::parse_and_import();
 
  59 # get all gl imported bookings
 
  60 my $gl_bookings = SL::DB::Manager::GLTransaction->get_all(where => [imported => 1] );
 
  62 # $i number of real data entries in the array (first two rows are headers)
 
  64 is(scalar @{ $csv_array } - $i, scalar @{ $gl_bookings }, "Correct number of imported Pay Posting Bookings");
 
  66 # check all imported bookings
 
  67 foreach my $booking (@{ $gl_bookings }) {
 
  68   my $current_row = $csv_array->[$i];
 
  70   my $accno_credit = $current_row->[1] eq 'S' ? $current_row->[7] : $current_row->[6];
 
  71   my $accno_debit  = $current_row->[1] eq 'S' ? $current_row->[6] : $current_row->[7];
 
  72   my $amount       = $::form->parse_amount({ numberformat => '1000,00' }, $current_row->[0]);
 
  75   is ($current_row->[13], $booking->reference, "Buchungstext correct");
 
  76   is ("Wisavis", $booking->department->description, "Department correctly assigned");
 
  77   is ($source, $booking->transactions->[0]->source, "Source 0 correctly assigned");
 
  78   is ($source, $booking->transactions->[1]->source, "Source 1 correctly assigned");
 
  81   cmp_ok ($amount,      '==',  $booking->transactions->[0]->amount, "Correct amount Soll");
 
  82   cmp_ok ($amount * -1, '==',  $booking->transactions->[1]->amount, "Correct amount Haben");
 
  83   is (ref $booking->transdate, 'DateTime', "Booking has a Transdate");
 
  84   is ($accno_credit, $booking->transactions->[0]->chart->accno, "Sollkonto richtig");
 
  85   is ($accno_debit, $booking->transactions->[1]->chart->accno, "Habenkonto richtig");
 
  98   SL::DB::Manager::AccTransaction->delete_all( all => 1);
 
  99   SL::DB::Manager::GLTransaction->delete_all(  all => 1);
 
 100   foreach my $accno (@charts) {
 
 101     SL::DB::Manager::Chart->delete_all(where => [ accno => $accno ] );