3 use Test::Deep qw(cmp_deeply);
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;
17 Support::TestSetup::login();
19 my $dbh = SL::DB->client->dbh;
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 my $customer = new_customer(name => 'Test customer', ustid => 'DE12345678')->save();
27 my $part1 = new_part(partnumber => '19', description => 'Part 19%')->save;
30 description => 'Part 7%',
31 buchungsgruppen_id => $buchungsgruppe7->id,
34 my $invoice = create_sales_invoice(
40 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 550),
41 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
43 department_id => $department->id,
44 globalproject_id => $project->id,
45 customer_id => $customer->id,
49 # generate_datev_* doesnt care about encoding but
50 # csv_buchungsexport does! all arabic will be deleted
51 # and no string will be left as invnumber
53 my $datev1 = SL::DATEV->new(
55 trans_id => $invoice->id,
58 my $startdate = DateTime->new(year => 2017, month => 1, day => 1);
59 my $enddate = DateTime->new(year => 2017, month => 12, day => 31);
60 my $today = DateTime->new(year => 2017, month => 3, day => 17);
63 $datev1->from($startdate);
64 $datev1->to($enddate);
66 $datev1->generate_datev_data;
67 $datev1->generate_datev_lines;
69 # check conversion to csv
70 $datev1->from($startdate);
71 $datev1->to($enddate);
72 my ($datev_csv, $die_message);
74 $datev_csv = SL::DATEV::CSV->new(datev_lines => $datev1->generate_datev_lines,
77 locked => $datev1->locked,
79 my $lines_aref = $datev_csv->lines; # dies only if we assign (do stuff with the data)
84 ok($die_message =~ m/Falscher Feldwert 'ݗݘݰݶ' für Feld 'belegfeld1' bei der Transaktion mit dem Umsatz von/, 'wrong_encoding');
87 $invoice->invnumber('ݗݘݰݶmuh');
90 my $datev3 = SL::DATEV->new(
92 trans_id => $invoice->id,
95 $datev3->from($startdate);
96 $datev3->to($enddate);
97 $datev3->generate_datev_data;
98 $datev3->generate_datev_lines;
99 my ($datev_csv2, $die_message2);
101 $datev_csv2 = SL::DATEV::CSV->new(datev_lines => $datev3->generate_datev_lines,
104 locked => $datev3->locked,
106 my $lines_aref = $datev_csv2->lines; # dies only if we assign (do stuff with the data)
113 # redefine invnumber, we have mixed encodings, should still fail
114 ok($die_message2 =~ m/Falscher Feldwert 'ݗݘݰݶmuh' für Feld 'belegfeld1' bei der Transaktion mit dem Umsatz von/, 'mixed_wrong_encoding');
116 # check with good number
117 $invoice->invnumber('meine muh');
120 my $datev4 = SL::DATEV->new(
122 trans_id => $invoice->id,
125 $datev4->from($startdate);
126 $datev4->to($enddate);
127 $datev4->generate_datev_data;
128 $datev4->generate_datev_lines;
129 my ($datev_csv4, $die_message3, $lines_aref);
131 $datev_csv4 = SL::DATEV::CSV->new(datev_lines => $datev4->generate_datev_lines,
134 locked => $datev4->locked,
136 $lines_aref = $datev_csv4->lines; # dies only if we assign (do stuff with the data)
142 ok(!($die_message3), 'no die message');
143 ok(scalar @{ $datev_csv4->warnings } == 0, 'no warnings');
145 my @sorted = sort { $a->[0] cmp $b->[0] } @{ $lines_aref };
146 cmp_deeply $sorted[0], [ '1963,5', 'S', 'EUR', '', '', '',
147 '1400', '8400', '', '1907', 'meine muh',
148 '', '', '', '', '', '', '', '', '',
149 '', '', '', '', '', '', '', '', '',
150 '', '', '', '', '', '', '', "K\x{e4}stchen",
151 '299', '','DE12345678', '', '', '',
152 '', '', '', '', '', '', '', '', '',
153 '', '', '', '', '', '', '', '', '',
154 '', '', '', '', '', '', '', '', '',
155 '', '', '', '', '', '', '', '', '',
156 '', '', '', '', '', '', '', '', '',
157 '', '', '', '', '', '', '', '', '',
158 '', '', '', '', '', '', '', '', '',
159 '', '', '', '', '', '', '', '', '',
162 cmp_deeply $sorted[1], [ '535', 'S', 'EUR', '', '', '',
163 '1400', '8300', '', '1907','meine muh',
164 '', '', '', '', '', '', '', '', '',
165 '', '', '', '', '', '', '', '', '',
166 '', '', '', '', '', '', '', "K\x{e4}stchen",
167 '299', '','DE12345678', '', '', '',
168 '', '', '', '', '', '', '', '', '',
169 '', '', '', '', '', '', '', '', '',
170 '', '', '', '', '', '', '', '', '',
171 '', '', '', '', '', '', '', '', '',
172 '', '', '', '', '', '', '', '', '',
173 '', '', '', '', '', '', '', '', '',
174 '', '', '', '', '', '', '', '', '',
175 '', '', '', '', '', '', '', '', '',
178 # create one haben buchung with GLTransaction today
180 my $expense_chart = SL::DB::Manager::Chart->find_by(accno => '4660'); # Reisekosten
181 my $cash_chart = SL::DB::Manager::Chart->find_by(accno => '1000'); # Kasse
182 my $tax_chart = SL::DB::Manager::Chart->find_by(accno => '1576'); # Vorsteuer
183 my $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19) || die "No tax";
186 push(@acc_trans, SL::DB::AccTransaction->new(
187 chart_id => $expense_chart->id,
188 chart_link => $expense_chart->link,
193 tax_id => $tax_9->id,
194 project_id => $project->id,
196 push(@acc_trans, SL::DB::AccTransaction->new(
197 chart_id => $tax_chart->id,
198 chart_link => $tax_chart->link,
203 tax_id => $tax_9->id,
204 project_id => $project->id,
206 push(@acc_trans, SL::DB::AccTransaction->new(
207 chart_id => $cash_chart->id,
208 chart_link => $cash_chart->link,
216 my $gl_transaction = SL::DB::GLTransaction->new(
217 reference => "Reise März 2018",
218 description => "Reisekonsten März 2018 / Ma Schmidt",
221 employee_id => SL::DB::Manager::Employee->current->id,
228 transactions => \@acc_trans,
230 my $datev2 = SL::DATEV->new(
232 trans_id => $gl_transaction->id,
235 $datev2->from($startdate);
236 $datev2->to($enddate);
237 $datev2->generate_datev_data;
238 $datev2->generate_datev_lines;
240 my $datev_csv3 = SL::DATEV::CSV->new(datev_lines => $datev2->generate_datev_lines,
243 locked => $datev2->locked,
246 my @data_csv = sort { $a->[0] cmp $b->[0] } @{ $datev_csv3->lines };
247 cmp_deeply($data_csv[0], [ 100, 'H', 'EUR', '', '', '', '4660', '1000', 9, '1703', 'Reise März 2',
248 '', '', '', '', '', '', '', '', '', '', '',
249 '', '', '', '', '', '', '', '', '', '', '', '', '',
250 '', '', '', '', '', '', '', '', '', '', '',
251 '', '', '', '', '', '', '', '', '', '', '', '', '',
252 '', '', '', '', '', '', '', '', '', '', '', '', '',
253 '', '', '', '', '', '', '', '', '', '', '', '', '',
254 '', '', '', '', '', '', '', '', '', '', '', '', '',
255 '', '', '', '', '', '', '', '', '', '', '', '', '',
259 # TODO warnings are not yet tested
260 # currently most of the valid_checks are senseless because of
261 # the strict input_checks before. Maybe something like encoding mismatch of invnumber,
262 # can be altered to just a warning (not a mandantory field!)
269 SL::DB::Manager::AccTransaction->delete_all( all => 1);
270 SL::DB::Manager::GLTransaction->delete_all( all => 1);
271 SL::DB::Manager::InvoiceItem->delete_all( all => 1);
272 SL::DB::Manager::Invoice->delete_all( all => 1);
273 SL::DB::Manager::Customer->delete_all( all => 1);
274 SL::DB::Manager::Part->delete_all( all => 1);
275 SL::DB::Manager::Project->delete_all( all => 1);
276 SL::DB::Manager::Department->delete_all( all => 1);
277 SL::DATEV->clean_temporary_directories;