PayPostingImport: Testfall gegen DATEV-CSV Struktur
[kivitendo-erp.git] / t / pay_posting_import / datev_import.t
1 use strict;
2 use Test::More;
3 use Test::Exception;
4
5 use lib 't';
6
7 use_ok 'Support::TestSetup';
8
9 use SL::Controller::PayPostingImport;
10
11 use utf8;
12 use Data::Dumper;
13 use File::Slurp;
14 use Text::CSV_XS qw (csv);
15
16 Support::TestSetup::login();
17
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');
21 diag("init csv");
22 clear_up();
23
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};
28
29 # get data as aoa datev encodes always CP1252
30 my $csv_array = csv (in        => "t/pay_posting_import/datev.csv",
31                      binary    => 0,
32                      auto_diag => 1, sep_char => ";", encoding=> "cp1252");
33
34 # probably no correct charts in test db
35 throws_ok{
36   SL::Controller::PayPostingImport::parse_and_import();
37  } qr/No such Chart 379000/, "Importing Pay Postings without correct charts";
38
39 # create charts
40 foreach my $accno (@charts) {
41   SL::DB::Chart->new(
42     accno          => $accno,
43     description    => 'Löhne mit Gestöhne',
44     charttype      => 'A',
45     category       => 'Q',
46     link           => '',
47     taxkey_id      => '0',
48     datevautomatik => 'f',
49   )->save;
50 }
51
52 # and add department (KOST1 description)
53   SL::DB::Department->new(
54     description => 'Wisavis'
55   )->save;
56
57 SL::Controller::PayPostingImport::parse_and_import();
58
59 # get all gl imported bookings
60 my $gl_bookings = SL::DB::Manager::GLTransaction->get_all(where => [imported => 1] );
61
62 # $i number of real data entries in the array (first two rows are headers)
63 my $i = 2;
64 is(scalar @{ $csv_array } - $i, scalar @{ $gl_bookings }, "Correct number of imported Pay Posting Bookings");
65
66 # check all imported bookings
67 foreach my $booking (@{ $gl_bookings }) {
68   my $current_row = $csv_array->[$i];
69
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]);
73
74   # gl
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");
79
80   # acc_trans
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");
86
87   $i++;
88 }
89
90 clear_up();
91
92
93 done_testing();
94
95 1;
96
97 sub clear_up {
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 ] );
102   }
103 };