Merge branch 'test' of ../kivitendo-erp_20220811
[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 note("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 => 'Total falsche Abteilung, niemals zuordnen!'
55   )->save;
56
57   SL::DB::Department->new(
58     description => '2. Total falsche Abteilung, niemals zuordnen!'
59   )->save;
60
61   SL::DB::Department->new(
62     description => '3. Total falsche Abteilung, niemals zuordnen!'
63   )->save;
64
65   SL::DB::Department->new(
66     description => 'annahme stelle. Total falsche Abteilung, niemals zuordnen!'
67   )->save;
68
69
70   SL::DB::Department->new(
71     description => 'Wisavis'
72   )->save;
73
74 SL::Controller::PayPostingImport::parse_and_import();
75
76 # get all gl imported bookings
77 my $gl_bookings = SL::DB::Manager::GLTransaction->get_all(where => [imported => 1] );
78
79 # $i number of real data entries in the array (first two rows are headers)
80 my $i = 2;
81 is(scalar @{ $csv_array } - $i, scalar @{ $gl_bookings }, "Correct number of imported Pay Posting Bookings");
82
83 # check all imported bookings
84 foreach my $booking (@{ $gl_bookings }) {
85   my $current_row = $csv_array->[$i];
86
87   my $accno_credit = $current_row->[1] eq 'S' ? $current_row->[7] : $current_row->[6];
88   my $accno_debit  = $current_row->[1] eq 'S' ? $current_row->[6] : $current_row->[7];
89   my $amount       = $::form->parse_amount({ numberformat => '1000,00' }, $current_row->[0]);
90
91   # gl
92   is ($current_row->[13], $booking->reference, "Buchungstext correct");
93   if ($current_row->[36] eq 'wisavis') {
94     is(ref $booking->department eq 'SL::DB::Department', 1, "Department assigned");
95     is ($current_row->[36], 'wisavis', "Department correctly assigned");                # lowercase
96     is ('Wisavis', $booking->department->description, "Department correctly assigned"); # upper case
97   } else {
98     is ($current_row->[36], '', "No Department correctly assigned");
99
100   }
101   is ($source, $booking->transactions->[0]->source, "Source 0 correctly assigned");
102   is ($source, $booking->transactions->[1]->source, "Source 1 correctly assigned");
103
104   # acc_trans
105   cmp_ok ($amount,      '==',  $booking->transactions->[0]->amount, "Correct amount Soll");
106   cmp_ok ($amount * -1, '==',  $booking->transactions->[1]->amount, "Correct amount Haben");
107   is (ref $booking->transdate, 'DateTime', "Booking has a Transdate");
108   is ($accno_credit, $booking->transactions->[0]->chart->accno, "Sollkonto richtig");
109   is ($accno_debit, $booking->transactions->[1]->chart->accno, "Habenkonto richtig");
110
111   $i++;
112 }
113
114 clear_up();
115
116
117 done_testing();
118
119 1;
120
121 sub clear_up {
122   SL::DB::Manager::AccTransaction->delete_all( all => 1);
123   SL::DB::Manager::GLTransaction->delete_all(  all => 1);
124   foreach my $accno (@charts) {
125     SL::DB::Manager::Chart->delete_all(where => [ accno => $accno ] );
126   }
127 };