DATEV-Export: Feldprüfung als Warnungen ausgeben
[kivitendo-erp.git] / t / datev / datev_format_2018.t
1 use strict;
2 use Test::More;
3 use Test::Deep qw(cmp_bag);
4
5 use lib 't';
6
7 use_ok 'Support::TestSetup';
8 use SL::DATEV qw(:CONSTANTS);
9 use SL::Dev::ALL qw(:ALL);
10 use List::Util qw(sum);
11 use SL::DB::Buchungsgruppe;
12 use SL::DB::Chart;
13 use DateTime;
14 use Data::Dumper;
15 use utf8;
16
17 Support::TestSetup::login();
18
19 my $dbh = SL::DB->client->dbh;
20
21 clear_up();
22 my $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || die "No accounting group for 7\%";
23 my $date            = DateTime->new(year => 2017, month =>  7, day => 19);
24 my $department      = create_department(description => 'Kästchenweiße heiße Preise');
25 my $project         = create_project(projectnumber => 2017, description => '299');
26
27 my $part1 = new_part(partnumber => '19', description => 'Part 19%')->save;
28 my $part2 = new_part(
29   partnumber         => '7',
30   description        => 'Part 7%',
31   buchungsgruppen_id => $buchungsgruppe7->id,
32 )->save;
33
34 my $invoice = create_sales_invoice(
35   invnumber    => "ݗݘݰݶ",
36   itime        => $date,
37   gldate       => $date,
38   taxincluded  => 0,
39   transdate    => $date,
40   invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 550),
41                     create_invoice_item(part => $part2, qty => 10, sellprice => 50),
42                   ],
43   department_id    => $department->id,
44   globalproject_id => $project->id,
45 );
46
47 # lets make a boom
48 # generate_datev_* doesnt care about encoding but
49 # csv_buchungsexport does! all arabic will be deleted
50 # and no string will be left as invnumber
51
52 my $datev1 = SL::DATEV->new(
53   dbh        => $dbh,
54   trans_id   => $invoice->id,
55 );
56
57 my $startdate = DateTime->new(year => 2017, month =>  1, day =>  1);
58 my $enddate   = DateTime->new(year => 2017, month =>  12, day => 31);
59 my $today     = DateTime->new(year => 2017, month =>  3, day => 17);
60
61
62 $datev1->from($startdate);
63 $datev1->to($enddate);
64
65 $datev1->generate_datev_data;
66 $datev1->generate_datev_lines;
67
68 # check conversion to csv
69 $datev1->from($startdate);
70 $datev1->to($enddate);
71 $datev1->csv_buchungsexport();
72 my @warnings = $datev1->warnings;
73 is(@warnings[0]->[0]->{untranslated},
74   'Wrong field value \'#1\' for field \'#2\' for the transaction with amount \'#3\'', 'wrong_encoding');
75
76
77 # redefine invnumber, we have mixed encodings, should still generate a warning
78 $invoice->invnumber('ݗݘݰݶmuh');
79 $invoice->save();
80
81 my $datev3 = SL::DATEV->new(
82   dbh        => $dbh,
83   trans_id   => $invoice->id,
84 );
85
86 $datev3->from($startdate);
87 $datev3->to($enddate);
88 $datev3->generate_datev_data;
89 $datev3->generate_datev_lines;
90 $datev3->csv_buchungsexport;
91 @warnings = [];
92 @warnings = $datev3->warnings;
93 is(@warnings[0]->[0]->{untranslated},
94   'Wrong field value \'#1\' for field \'#2\' for the transaction with amount \'#3\'', 'mixed_wrong_encoding');
95
96
97
98 # create one haben buchung with GLTransaction today
99
100 my $expense_chart = SL::DB::Manager::Chart->find_by(accno => '4660'); # Reisekosten
101 my $cash_chart    = SL::DB::Manager::Chart->find_by(accno => '1000'); # Kasse
102 my $tax_chart     = SL::DB::Manager::Chart->find_by(accno => '1576'); # Vorsteuer
103 my $tax_9         = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19) || die "No tax";
104
105 my @acc_trans;
106 push(@acc_trans, SL::DB::AccTransaction->new(
107                                       chart_id   => $expense_chart->id,
108                                       chart_link => $expense_chart->link,
109                                       amount     => -84.03,
110                                       transdate  => $today,
111                                       source     => '',
112                                       taxkey     => 9,
113                                       tax_id     => $tax_9->id,
114                                       project_id => $project->id,
115 ));
116 push(@acc_trans, SL::DB::AccTransaction->new(
117                                       chart_id   => $tax_chart->id,
118                                       chart_link => $tax_chart->link,
119                                       amount     => -15.97,
120                                       transdate  => $today,
121                                       source     => '',
122                                       taxkey     => 9,
123                                       tax_id     => $tax_9->id,
124                                       project_id => $project->id,
125 ));
126 push(@acc_trans, SL::DB::AccTransaction->new(
127                                       chart_id   => $cash_chart->id,
128                                       chart_link => $cash_chart->link,
129                                       amount     => 100,
130                                       transdate  => $today,
131                                       source     => '',
132                                       taxkey     => 0,
133                                       tax_id     => 0,
134 ));
135
136 my $gl_transaction = SL::DB::GLTransaction->new(
137   reference      => "Reisekosten März 2018",
138   description    => "Reisekonsten März 2018 / Ma Schmidt",
139   transdate      => $today,
140   gldate         => $today,
141   employee_id    => SL::DB::Manager::Employee->current->id,
142   taxincluded    => 1,
143   type           => undef,
144   ob_transaction => 0,
145   cb_transaction => 0,
146   storno         => 0,
147   storno_id      => undef,
148   transactions   => \@acc_trans,
149 )->save;
150 my $datev2 = SL::DATEV->new(
151   dbh        => $dbh,
152   trans_id   => $gl_transaction->id,
153 );
154
155 $datev2->from($startdate);
156 $datev2->to($enddate);
157 $datev2->generate_datev_data;
158 $datev2->generate_datev_lines;
159
160 my @data_csv = splice @{ $datev2->csv_buchungsexport() }, 2, 5;
161 @data_csv    = sort { $a->[0] <=> $b->[0] } @data_csv;
162
163
164 my $cp1252_posting_text   = SL::Iconv::convert("UTF-8", "CP1252", 'Reisekosten März 2018');
165 cmp_bag($data_csv[0], [ 100, 'H', 'EUR', undef, undef, undef, '4660', '1000', 9, '1703', 'Reisekosten ',
166                      undef, undef, $cp1252_posting_text, undef, undef, undef, undef, undef, undef, undef, undef,
167                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
168                      undef, undef, '', undef, undef, undef, undef, undef, undef, undef, undef,
169                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
170                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
171                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
172                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
173                      undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
174                      undef, undef, undef, undef, undef ]
175        );
176
177 done_testing();
178 clear_up();
179
180
181 sub clear_up {
182   SL::DB::Manager::AccTransaction->delete_all( all => 1);
183   SL::DB::Manager::InvoiceItem->delete_all(    all => 1);
184   SL::DB::Manager::Invoice->delete_all(        all => 1);
185   SL::DB::Manager::Customer->delete_all(       all => 1);
186   SL::DB::Manager::Part->delete_all(           all => 1);
187   SL::DB::Manager::Project->delete_all(        all => 1);
188   SL::DB::Manager::Department->delete_all(     all => 1);
189   SL::DATEV->clean_temporary_directories;
190 };
191
192 1;