3 use Test::Deep qw(cmp_bag);
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;
15 Support::TestSetup::login();
19 my $dbh = SL::DB->client->dbh;
21 my $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || die "No accounting group for 7\%";
22 my $bank = SL::DB::Manager::Chart->find_by(description => 'Bank') || die 'Can\'t find chart "Bank"';
23 my $date = DateTime->new(year => 2017, month => 1, day => 1);
24 my $payment_date = DateTime->new(year => 2017, month => 1, day => 5);
25 my $gldate = DateTime->new(year => 2017, month => 2, day => 9); # simulate bookings for Jan being made in Feb
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(
35 invnumber => "1 sales invoice",
38 intnotes => 'booked in February',
41 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
42 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
45 $invoice->pay_invoice(chart_id => $bank->id,
46 amount => $invoice->open_amount,
47 transdate => $payment_date->to_kivitendo,
51 my $datev1 = SL::DATEV->new(
52 dbh => $invoice->db->dbh,
53 trans_id => $invoice->id,
55 $datev1->generate_datev_data;
56 my $kne_lines1 = $datev1->generate_datev_lines;
57 cmp_bag $datev1->generate_datev_lines, [
59 'belegfeld1' => '1 sales invoice',
60 'buchungstext' => 'Testcustomer',
61 'datum' => '01.01.2017',
62 'gegenkonto' => '8400',
68 'belegfeld1' => '1 sales invoice',
69 'buchungstext' => 'Testcustomer',
70 'datum' => '01.01.2017',
71 'gegenkonto' => '8300',
77 'belegfeld1' => '1 sales invoice',
78 'buchungstext' => 'Testcustomer',
79 'datum' => '05.01.2017',
80 'gegenkonto' => '1400',
85 ], "trans_id datev check ok";
87 my $march_9 = DateTime->new(year => 2017, month => 3, day => 9);
88 my $invoice2 = create_sales_invoice(
89 invnumber => "2 sales invoice",
92 intnotes => 'booked in March',
95 invoiceitems => [ create_invoice_item(part => $part1, qty => 6, sellprice => 70),
96 create_invoice_item(part => $part2, qty => 20, sellprice => 50),
100 my $credit_note = create_credit_note(
101 invnumber => 'Gutschrift 34',
104 intnotes => 'booked in February',
107 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
108 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
112 my $startdate = DateTime->new(year => 2017, month => 1, day => 1);
113 my $enddate = DateTime->new(year => 2017, month => 12, day => 31);
115 my $datev = SL::DATEV->new(
120 $datev->generate_datev_data(from_to => $datev->fromto);
121 my $datev_lines = $datev->generate_datev_lines;
122 my $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
123 cmp_ok($::form->round_amount($umsatzsumme,2), '==', 3924.5, "Sum of all bookings ok");
125 note('testing gldatefrom');
126 $datev = SL::DATEV->new(
129 to => DateTime->new(year => 2017, month => 01, day => 31),
132 $::form = Support::TestSetup->create_new_form;
133 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 3, day => 1)->to_kivitendo;
135 $datev->generate_datev_data(from_to => $datev->fromto);
136 $datev_lines = $datev->generate_datev_lines;
137 $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
138 cmp_ok($umsatzsumme, '==', 1569.8, "Sum of bookings made after March 1st (only invoice2) ok");
140 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 5, day => 1)->to_kivitendo;
141 $datev->generate_datev_data(from_to => $datev->fromto);
142 cmp_bag $datev->generate_datev_lines, [], "no bookings for January made after May 1st: ok";
148 SL::DB::Manager::AccTransaction->delete_all(all => 1);
149 SL::DB::Manager::InvoiceItem->delete_all( all => 1);
150 SL::DB::Manager::Invoice->delete_all( all => 1);
151 SL::DB::Manager::Customer->delete_all( all => 1);
152 SL::DB::Manager::Part->delete_all( all => 1);