1 use Test::More tests => 70;
9 use Support::TestSetup;
12 use List::MoreUtils qw(pairwise);
13 use SL::Controller::CsvImport;
17 use_ok 'SL::Controller::CsvImport::ARTransaction';
19 use SL::DB::Buchungsgruppe;
26 use SL::DB::AccTransaction;
28 my ($customer, $currency_id, $employee, $taxzone, $project, $department);
34 $params{$_} ||= {} for qw(buchungsgruppe customer tax);
37 $employee = SL::DB::Manager::Employee->current || croak "No employee";
38 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
39 $currency_id = $::instance_conf->get_currency_id;
41 $customer = SL::DB::Customer->new(
42 name => 'Test Customer',
43 currency_id => $currency_id,
44 taxzone_id => $taxzone->id,
45 %{ $params{customer} }
48 $project = SL::DB::Project->new(
49 projectnumber => 'P1',
50 description => 'Project X',
51 project_type => SL::DB::Manager::ProjectType->find_by(description => 'Standard'),
52 project_status => SL::DB::Manager::ProjectStatus->find_by(name => 'running'),
55 $department = SL::DB::Department->new(
56 description => 'Department 1',
60 Support::TestSetup::login();
62 reset_state(customer => {id => 960, customernumber => 2});
68 my $controller = SL::Controller::CsvImport->new(
69 type => 'ar_transactions'
71 $controller->load_default_profile;
72 $controller->profile->set(
76 numberformat => $::myconfig{numberformat},
79 my $csv_artransactions_import = SL::Controller::CsvImport::ARTransaction->new(
80 settings => {'ar_column' => 'Rechnung',
81 'transaction_column' => 'AccTransaction',
82 'max_amount_diff' => 0.02
84 controller => $controller,
88 # $csv_artransactions_import->init_vc_by;
89 $csv_artransactions_import->run(test => 0);
91 # don't try and save objects that have errors
92 $csv_artransactions_import->save_objects unless scalar @{$csv_artransactions_import->controller->data->[0]->{errors}};
94 return $csv_artransactions_import->controller->data;
97 ##### manually create an ar transaction from scratch, testing the methods
98 $::myconfig{numberformat} = '1000.00';
99 my $old_locale = $::locale;
100 # set locale to en so we can match errors
101 $::locale = Locale->new('en');
105 my $ar = SL::DB::Invoice->new(
107 invnumber => 'manual invoice',
108 taxzone_id => $taxzone->id,
109 currency_id => $currency_id,
111 customer_id => $customer->id,
112 transdate => DateTime->today,
113 employee_id => SL::DB::Manager::Employee->current->id,
117 my $tax3 = SL::DB::Manager::Tax->find_by(rate => 0.19, taxkey => 3) || die "can't find tax with taxkey 3";
118 my $income_chart = SL::DB::Manager::Chart->find_by(accno => '8400') || die "can't find income chart";
120 $ar->add_ar_amount_row(
122 chart => $income_chart,
126 $ar->recalculate_amounts; # set amount and netamount from transactions
127 is $ar->amount, '10', 'amount of manual invoice is 10';
128 is $ar->netamount, '8.4', 'netamount of manual invoice is 10';
130 $ar->create_ar_row( chart => SL::DB::Manager::Chart->find_by(accno => '1400', link => 'AR') );
131 my $result = $ar->validate_acc_trans(debug => 0);
132 is $result, 1, 'manual $ar validates';
135 is ${ $ar->transactions }[0]->chart->accno, '8400', 'assigned income chart after save ok';
136 is ${ $ar->transactions }[2]->chart->accno, '1400', 'assigned receivable chart after save ok';
137 is scalar @{$ar->transactions}, 3, 'manual invoice has 3 acc_trans entries';
139 $ar->pay_invoice( chart_id => SL::DB::Manager::Chart->find_by(accno => '1200')->id, # bank
140 amount => $ar->open_amount,
141 transdate => DateTime->now->to_kivitendo,
142 payment_type => 'without_skonto', # default if not specified
144 $result = $ar->validate_acc_trans(debug => 0);
145 is $result, 1, 'manual invoice validates after payment';
147 reset_state(customer => {id => 960, customernumber => 2});
149 my ($entries, $entry, $file);
151 # starting test of csv imports
152 # to debug errors in certain tests, run after test_import:
153 # die Dumper($entry->{errors});
156 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
157 datatype,accno,amount,taxkey
158 "Rechnung",960,4,1,"invoice 1",f,1400
159 "AccTransaction",8400,159.48,3
161 $entries = test_import($file);
162 $entry = $entries->[0];
163 $entry->{object}->validate_acc_trans;
165 is $entry->{object}->invnumber, 'invoice 1', 'simple invnumber ok (customer)';
166 is $entry->{object}->customer_id, '960', 'simple customer_id ok (customer)';
167 is scalar @{$entry->{object}->transactions}, 3, 'invoice 1 has 3 acc_trans entries';
168 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), 159.48, 'invoice 1 ar amount is 159.48';
169 is $entry->{object}->direct_debit, '0', 'no direct debit';
170 is $entry->{object}->taxincluded, '0', 'taxincluded is false';
171 is $entry->{object}->amount, '189.78', 'ar amount tax not included is 189.78';
172 is $entry->{object}->netamount, '159.48', 'ar netamount tax not included is 159.48';
174 ##### test for duplicate invnumber
176 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
177 datatype,accno,amount,taxkey
178 "Rechnung",960,4,1,"invoice 1",f,1400
179 "AccTransaction",8400,159.48,3
181 $entries = test_import($file);
182 $entry = $entries->[0];
183 $entry->{object}->validate_acc_trans;
184 is $entry->{errors}->[0], 'Error: invnumber already exists', 'detects verify_amount differences';
186 ##### test for no invnumber given
188 datatype,customer_id,taxzone_id,currency_id,taxincluded,archart
189 datatype,accno,amount,taxkey
190 "Rechnung",960,4,1,f,1400
191 "AccTransaction",8400,159.48,3
193 $entries = test_import($file);
194 $entry = $entries->[0];
195 $entry->{object}->validate_acc_trans;
196 is $entry->{object}->invnumber =~ /^\d+$/, 1, 'invnumber assigned automatically';
198 ##### basic test without amounts in Rechnung, only specified in AccTransaction
200 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
201 datatype,accno,amount,taxkey
202 "Rechnung",960,4,1,"invoice 1 no amounts",f,1400
203 "AccTransaction",8400,159.48,3
205 $entries = test_import($file);
206 $entry = $entries->[0];
207 $entry->{object}->validate_acc_trans;
209 is $entry->{object}->invnumber, 'invoice 1 no amounts', 'simple invnumber ok (customer)';
210 is $entry->{object}->customer_id, '960', 'simple customer_id ok (customer)';
211 is scalar @{$entry->{object}->transactions}, 3, 'invoice 1 has 3 acc_trans entries';
212 is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'not taxincluded ar amount';
213 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '159.48', 'not taxincluded acc_trans netamount';
214 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), 159.48, 'invoice 1 ar amount is 159.48';
216 ##### basic test: credit_note
218 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
219 datatype,accno,amount,taxkey
220 "Rechnung",960,4,1,"credit note",f,1400
221 "AccTransaction",8400,-159.48,3
223 $entries = test_import($file);
224 $entry = $entries->[0];
225 $entry->{object}->validate_acc_trans;
227 is $entry->{object}->invnumber, 'credit note', 'simple credit note ok';
228 is scalar @{$entry->{object}->transactions}, 3, 'credit note has 3 acc_trans entries';
229 is $::form->round_amount($entry->{object}->amount, 2), '-189.78', 'taxincluded ar amount';
230 is $::form->round_amount($entry->{object}->netamount, 2), '-159.48', 'taxincluded ar net amount';
231 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), -159.48, 'credit note ar amount is -159.48';
232 is $entry->{object}->amount, '-189.78', 'credit note amount tax not included is 189.78';
233 is $entry->{object}->netamount, '-159.48', 'credit note netamount tax not included is 159.48';
235 #### verify_amount differs: max_amount_diff = 0.02, 189.80 is ok, 189.81 is not
237 datatype,customer_id,verify_amount,verify_netamount,taxzone_id,currency_id,invnumber,taxincluded,archart
238 datatype,accno,amount,taxkey
239 "Rechnung",960,189.81,159.48,4,1,"invoice amounts differing",f,1400
240 "AccTransaction",8400,159.48,3
242 $entries = test_import($file);
243 $entry = $entries->[0];
244 is $entry->{errors}->[0], 'Amounts differ too much', 'detects verify_amount differences';
248 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,direct_debit,archart
249 datatype,accno,amount,taxkey
250 "Rechnung",960,4,1,"invoice with direct debit",f,t,1400
251 "AccTransaction",8400,159.48,3
254 $entries = test_import($file);
255 $entry = $entries->[0];
256 $entry->{object}->validate_acc_trans;
257 is $entry->{object}->direct_debit, '1', 'direct debit';
261 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
262 datatype,accno,amount,taxkey
263 "Rechnung",960,4,1,"invoice 1 tax included no amounts",t,1400
264 "AccTransaction",8400,189.78,3
267 $entries = test_import($file);
268 $entry = $entries->[0];
269 $entry->{object}->validate_acc_trans(debug => 0);
270 is $entry->{object}->taxincluded, '1', 'taxincluded is true';
271 is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'taxincluded ar amount';
272 is $::form->round_amount($entry->{object}->netamount, 2), '159.48', 'taxincluded ar net amount';
273 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '159.48', 'taxincluded acc_trans netamount';
275 #### multiple tax included
277 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
278 datatype,accno,amount,taxkey
279 "Rechnung",960,4,1,"invoice multiple tax included",t,1400
280 "AccTransaction",8400,94.89,3
281 "AccTransaction",8400,94.89,3
284 $entries = test_import($file);
285 $entry = $entries->[0];
286 $entry->{object}->validate_acc_trans;
287 is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'taxincluded ar amount';
288 is $::form->round_amount($entry->{object}->netamount, 2), '159.48', 'taxincluded ar netamount';
289 is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '79.74', 'taxincluded amount';
290 is $::form->round_amount($entry->{object}->transactions->[1]->amount, 2), '15.15', 'taxincluded tax';
292 # different receivables chart
294 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
295 datatype,accno,amount,taxkey
296 "Rechnung",960,4,1,"invoice mit archart 1448",f,1448
297 "AccTransaction",8400,159.48,3
299 $entries = test_import($file);
300 $entry = $entries->[0];
301 $entry->{object}->validate_acc_trans;
302 is $entry->{object}->transactions->[2]->chart->accno, '1448', 'archart set to 1448';
306 datatype,taxzone_id,currency_id,invnumber,taxincluded,archart
307 datatype,accno,amount,taxkey
308 "Rechnung",4,1,"invoice missing customer",f,1400
309 "AccTransaction",8400,159.48,3
311 $entries = test_import($file);
312 $entry = $entries->[0];
313 is $entry->{errors}->[0], 'Error: Customer/vendor missing', 'detects missing customer or vendor';
316 ##### customer by name
318 datatype,customer,taxzone_id,currency_id,invnumber,taxincluded,archart
319 datatype,accno,amount,taxkey
320 "Rechnung","Test Customer",4,1,"invoice customer name",f,1400
321 "AccTransaction",8400,159.48,3
323 $entries = test_import($file);
324 $entry = $entries->[0];
325 $entry->{object}->validate_acc_trans;
326 is $entry->{object}->customer->name, "Test Customer", 'detects customer by name';
328 ##### detect missing chart
330 datatype,taxzone_id,currency_id,invnumber,customer,archart
331 datatype,amount,taxkey
332 "Rechnung",4,1,"invoice missing chart","Test Customer",1400
335 $entries = test_import($file);
336 $entry = $entries->[1];
337 is $entry->{errors}->[0], 'Error: chart missing', 'detects missing chart (chart_id or accno)';
339 ##### detect illegal chart by accno
341 datatype,taxzone_id,currency_id,invnumber,customer,archart
342 datatype,accno,amount,taxkey
343 "Rechnung",4,1,"invoice illegal chart accno","Test Customer",1400
344 "AccTransaction",9999,4,3
346 $entries = test_import($file);
347 $entry = $entries->[1];
348 is $entry->{errors}->[0], 'Error: invalid chart (accno)', 'detects invalid chart (chart_id or accno)';
350 # ##### detect illegal archart
352 datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
353 datatype,accno,amount,taxkey
354 "Rechnung",4,1,"invoice illegal archart","Test Customer",f,11400
355 "AccTransaction",8400,159.48,3
357 $entries = test_import($file);
358 $entry = $entries->[0];
359 is $entry->{errors}->[0], "Error: can't find ar chart with accno 11400", 'detects illegal receivables chart (archart)';
361 ##### detect chart by id
363 datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
364 datatype,amount,chart_id,taxkey
365 "Rechnung",4,1,"invoice chart_id","Test Customer",f,1400
366 "AccTransaction",159.48,184,3
368 $entries = test_import($file);
369 $entry = $entries->[1]; # acc_trans entry is at entry array pos 1
370 $entries->[0]->{object}->validate_acc_trans;
371 is $entry->{object}->chart->id, "184", 'detects chart by id';
373 ##### detect chart by accno
375 datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
376 datatype,amount,accno,taxkey
377 "Rechnung",4,1,"invoice by chart accno","Test Customer",f,1400
378 "AccTransaction",159.48,8400,3
380 $entries = test_import($file);
381 $entry = $entries->[1];
382 $entries->[0]->{object}->validate_acc_trans;
383 is $entry->{object}->chart->accno, "8400", 'detects chart by accno';
385 ##### detect chart isn't an ar_chart
387 datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
388 datatype,amount,accno,taxkey
389 "Rechnung",4,1,"invoice by chart accno","Test Customer",f,1400
390 "AccTransaction",159.48,1400,3
392 $entries = test_import($file);
393 $entry = $entries->[1];
394 $entries->[0]->{object}->validate_acc_trans;
395 is $entry->{errors}->[0], 'Error: chart isn\'t an ar_amount chart', 'detects valid chart that is not an ar_amount chart';
399 datatype,taxzone_id,currency_id,invnumber,customer,archart
400 datatype,amount,accno
401 "Rechnung",4,1,"invoice missing taxkey chart accno","Test Customer",1400
402 "AccTransaction",159.48,8400
404 $entries = test_import($file);
405 $entry = $entries->[1];
406 is $entry->{errors}->[0], 'Error: taxkey missing', 'detects missing taxkey (DATEV Steuerschlüssel)';
410 datatype,taxzone_id,currency_id,invnumber,customer,archart
411 datatype,amount,accno,taxkey
412 "Rechnung",4,1,"invoice illegal taxkey","Test Customer",1400
413 "AccTransaction",4,8400,123
415 $entries = test_import($file);
416 $entry = $entries->[1];
417 is $entry->{errors}->[0], 'Error: invalid taxkey', 'detects invalid taxkey (DATEV Steuerschlüssel)';
421 datatype,customer_id,taxzone_id,currency_id,invnumber,archart
422 datatype,accno,amount,taxkey
423 "Rechnung",960,4,1,"invoice by taxkey",1400
424 "AccTransaction",8400,4,3
427 $entries = test_import($file);
428 $entry = $entries->[1];
429 is $entry->{object}->taxkey, 3, 'detects taxkey';
433 datatype,customer_id,taxzone_id,currency_id,invnumber,archart,taxincluded
434 datatype,accno,amount,taxkey,projectnumber
435 "Rechnung",960,4,1,"invoice with acc_trans project",1400,f
436 "AccTransaction",8400,159.48,3,P1
439 $entries = test_import($file);
440 $entry = $entries->[1];
441 # die Dumper($entries->[0]->{errors}) if scalar @{$entries->[0]->{errors}};
442 is $entry->{object}->project->projectnumber, 'P1', 'detects acc_trans project';
446 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
447 datatype,accno,amount,taxkey,projectnumber
448 "Rechnung",960,4,1,"invoice various",t,1400,21.04.2016,30.04.2016,P1,Department 1
449 "AccTransaction",8400,119,3,P1
450 "AccTransaction",8300,107,2,P1
451 "AccTransaction",8200,100,0,P1
454 $entries = test_import($file);
455 $entry = $entries->[0];
456 $entry->{object}->validate_acc_trans;
457 is $entry->{object}->duedate->to_kivitendo, '30.04.2016', 'duedate';
458 is $entry->{object}->transdate->to_kivitendo, '21.04.2016', 'transdate';
459 is $entry->{object}->globalproject->description, 'Project X', 'project';
460 is $entry->{object}->department->description, 'Department 1', 'department';
461 # 8300 is third entry after 8400 and tax for 8400
462 is $::form->round_amount($entry->{object}->transactions->[2]->amount), '100', '8300 net amount: 100';
463 is $::form->round_amount($entry->{object}->transactions->[2]->taxkey), '2', '8300 has taxkey 2';
464 is $::form->round_amount($entry->{object}->transactions->[2]->project_id), $project->id, 'AccTrans project';
468 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
469 datatype,accno,amount,taxkey,projectnumber
470 "Rechnung",960,4,1,"invoice various 1",t,1400,21.04.2016,30.04.2016,P1,Department 1
471 "AccTransaction",8400,119,3,P1
472 "AccTransaction",8300,107,2,P1
473 "AccTransaction",8200,100,0,P1
474 "Rechnung",960,4,1,"invoice various 2",t,1400,21.04.2016,30.04.2016,P1,Department 1
475 "AccTransaction",8400,119,3,P1
476 "AccTransaction",8300,107,2,P1
477 "AccTransaction",8200,100,0,P1
480 $entries = test_import($file);
481 $entry = $entries->[0];
482 $entry->{object}->validate_acc_trans;
483 is $entry->{object}->duedate->to_kivitendo, '30.04.2016', 'duedate';
484 is $entry->{info_data}->{amount}, '326', "First invoice amount displayed in info data";
485 is $entries->[4]->{info_data}->{amount}, '326', "Second invoice amount displayed in info data";
487 # multiple entries, taxincluded = f
489 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
490 datatype,accno,amount,taxkey
491 "Rechnung",960,4,1,"invoice 4 acc_trans",f,1400
492 "AccTransaction",8400,39.87,3
493 "AccTransaction",8400,39.87,3
494 "AccTransaction",8400,39.87,3
495 "AccTransaction",8400,39.87,3
496 "Rechnung",960,4,1,"invoice 4 acc_trans 2",f,1400
497 "AccTransaction",8400,39.87,3
498 "AccTransaction",8400,39.87,3
499 "AccTransaction",8400,39.87,3
500 "AccTransaction",8400,39.87,3
501 "Rechnung",960,4,1,"invoice 4 acc_trans 3",f,1400
502 "AccTransaction",8400,39.87,3
503 "AccTransaction",8400,39.87,3
504 "AccTransaction",8400,39.87,3
505 "AccTransaction",8400,39.87,3
506 "Rechnung",960,4,1,"invoice 4 acc_trans 4",f,1448
507 "AccTransaction",8400,39.87,3
508 "AccTransaction",8400,39.87,3
509 "AccTransaction",8400,39.87,3
510 "AccTransaction",8400,39.87,3
512 $entries = test_import($file);
515 foreach my $entry ( @$entries ) {
516 next unless $entry->{object}->isa('SL::DB::Invoice');
518 is scalar @{$entry->{object}->transactions}, 9, "invoice $i: 'invoice 4 acc_trans' has 9 acc_trans entries";
519 $entry->{object}->validate_acc_trans;
522 ##### missing acc_trans
524 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
525 datatype,accno,amount,taxkey,projectnumber
526 "Rechnung",960,4,1,"invoice acc_trans missing",t,1400,21.04.2016,30.04.2016,P1,Department 1
527 "Rechnung",960,4,1,"invoice various a",t,1400,21.04.2016,30.04.2016,P1,Department 1
528 "AccTransaction",8400,119,3,P1
529 "AccTransaction",8300,107,2,P1
532 $entries = test_import($file);
533 $entry = $entries->[0];
534 is $entry->{errors}->[0], "Error: ar transaction doesn't validate", 'detects invalid ar, maybe acc_trans entry missing';
536 my $number_of_imported_invoices = SL::DB::Manager::Invoice->get_all_count;
537 is $number_of_imported_invoices, 19, 'All invoices saved';
539 #### taxkey differs from active_taxkey
541 datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
542 datatype,accno,amount,taxkey
543 "Rechnung",960,4,1,"invoice 1 tax included no amounts",t,1400
544 "AccTransaction",8400,189.78,2
547 $entries = test_import($file);
548 $entry = $entries->[0];
549 $entry->{object}->validate_acc_trans(debug => 0);
551 clear_up(); # remove all data at end of tests
556 SL::DB::Manager::AccTransaction->delete_all(all => 1);
557 SL::DB::Manager::Invoice->delete_all (all => 1);
558 SL::DB::Manager::Customer->delete_all (all => 1);
559 SL::DB::Manager::Project->delete_all (all => 1);
560 SL::DB::Manager::Department->delete_all (all => 1);
568 # set emacs to perl mode