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
26 my $department = create_department(description => 'Kostenstelle DATEV-Schnittstelle 2018');
27 my $project = create_project(projectnumber => 2017, description => 'Crowd-Funding September 2017');
29 my $part1 = new_part(partnumber => '19', description => 'Part 19%')->save;
32 description => 'Part 7%',
33 buchungsgruppen_id => $buchungsgruppe7->id,
36 my $invoice = create_sales_invoice(
37 invnumber => "1 sales invoice",
40 intnotes => 'booked in February',
43 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
44 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
46 department_id => $department->id,
47 globalproject_id => $project->id,
49 $invoice->pay_invoice(chart_id => $bank->id,
50 amount => $invoice->open_amount,
51 transdate => $payment_date->to_kivitendo,
55 my $datev1 = SL::DATEV->new(
56 dbh => $invoice->db->dbh,
57 trans_id => $invoice->id,
59 $datev1->generate_datev_data;
60 cmp_bag $datev1->generate_datev_lines, [
62 'belegfeld1' => '1 sales invoice',
63 'buchungstext' => 'Testcustomer',
64 'datum' => '01.01.2017',
65 'gegenkonto' => '8400',
67 'kost1' => 'Kostenstelle DATEV-Schnittstelle 2018',
68 'kost2' => 'Crowd-Funding September 2017',
73 'belegfeld1' => '1 sales invoice',
74 'buchungstext' => 'Testcustomer',
75 'datum' => '01.01.2017',
76 'gegenkonto' => '8300',
78 'kost1' => 'Kostenstelle DATEV-Schnittstelle 2018',
79 'kost2' => 'Crowd-Funding September 2017',
84 'belegfeld1' => '1 sales invoice',
85 'buchungstext' => 'Testcustomer',
86 'datum' => '05.01.2017',
87 'gegenkonto' => '1400',
89 'kost1' => 'Kostenstelle DATEV-Schnittstelle 2018',
90 'kost2' => 'Crowd-Funding September 2017',
94 ], "trans_id datev check ok";
96 my $march_9 = DateTime->new(year => 2017, month => 3, day => 9);
97 my $invoice2 = create_sales_invoice(
98 invnumber => "2 sales invoice",
101 intnotes => 'booked in March',
104 invoiceitems => [ create_invoice_item(part => $part1, qty => 6, sellprice => 70),
105 create_invoice_item(part => $part2, qty => 20, sellprice => 50),
109 my $credit_note = create_credit_note(
110 invnumber => 'Gutschrift 34',
113 intnotes => 'booked in February',
116 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
117 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
121 my $startdate = DateTime->new(year => 2017, month => 1, day => 1);
122 my $enddate = DateTime->new(year => 2017, month => 12, day => 31);
124 my $datev = SL::DATEV->new(
129 $datev->generate_datev_data(from_to => $datev->fromto);
130 my $datev_lines = $datev->generate_datev_lines;
131 my $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
132 cmp_ok($::form->round_amount($umsatzsumme,2), '==', 3924.5, "Sum of all bookings ok");
134 note('testing gldatefrom');
135 $datev = SL::DATEV->new(
138 to => DateTime->new(year => 2017, month => 01, day => 31),
141 $::form = Support::TestSetup->create_new_form;
142 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 3, day => 1)->to_kivitendo;
144 $datev->generate_datev_data(from_to => $datev->fromto);
145 $datev_lines = $datev->generate_datev_lines;
146 $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
147 cmp_ok($umsatzsumme, '==', 1569.8, "Sum of bookings made after March 1st (only invoice2) ok");
149 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 5, day => 1)->to_kivitendo;
150 $datev->generate_datev_data(from_to => $datev->fromto);
151 cmp_bag $datev->generate_datev_lines, [], "no bookings for January made after May 1st: ok";
157 SL::DB::Manager::AccTransaction->delete_all(all => 1);
158 SL::DB::Manager::InvoiceItem->delete_all( all => 1);
159 SL::DB::Manager::Invoice->delete_all( all => 1);
160 SL::DB::Manager::Customer->delete_all( all => 1);
161 SL::DB::Manager::Part->delete_all( all => 1);
162 SL::DB::Manager::Project->delete_all( all => 1);
163 SL::DB::Manager::Department->delete_all( all => 1);