datev tests: Warnungen
[kivitendo-erp.git] / t / datev / invoices.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
15 Support::TestSetup::login();
16
17 clear_up();
18
19 my $dbh = SL::DB->client->dbh;
20
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
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    => "1 sales invoice",
36   itime        => $gldate,
37   gldate       => $gldate,
38   intnotes     => 'booked in February',
39   taxincluded  => 0,
40   transdate    => $date,
41   invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
42                     create_invoice_item(part => $part2, qty => 10, sellprice => 50),
43                   ]
44 );
45 $invoice->pay_invoice(chart_id      => $bank->id,
46                       amount        => $invoice->open_amount,
47                       transdate     => $payment_date->to_kivitendo,
48                       memo          => 'foobar',
49                       source        => 'barfoo',
50                      );
51 my $datev1 = SL::DATEV->new(
52   dbh        => $invoice->db->dbh,
53   trans_id   => $invoice->id,
54 );
55 $datev1->generate_datev_data;
56 my $kne_lines1 = $datev1->generate_datev_lines;
57 cmp_bag $datev1->generate_datev_lines, [
58                                          {
59                                            'belegfeld1'   => '1 sales invoice',
60                                            'buchungstext' => 'Testcustomer',
61                                            'datum'        => '01.01.2017',
62                                            'gegenkonto'   => '8400',
63                                            'konto'        => '1400',
64                                            'umsatz'       => '249.9',
65                                            'waehrung'     => 'EUR'
66                                          },
67                                          {
68                                            'belegfeld1'   => '1 sales invoice',
69                                            'buchungstext' => 'Testcustomer',
70                                            'datum'        => '01.01.2017',
71                                            'gegenkonto'   => '8300',
72                                            'konto'        => '1400',
73                                            'umsatz'       => 535,
74                                            'waehrung'     => 'EUR'
75                                          },
76                                          {
77                                            'belegfeld1'   => '1 sales invoice',
78                                            'buchungstext' => 'Testcustomer',
79                                            'datum'        => '05.01.2017',
80                                            'gegenkonto'   => '1400',
81                                            'konto'        => '1200',
82                                            'umsatz'       => '784.9',
83                                            'waehrung'     => 'EUR'
84                                          },
85                                        ], "trans_id datev check ok";
86
87 my $march_9 = DateTime->new(year => 2017, month =>  3, day => 9);
88 my $invoice2 = create_sales_invoice(
89   invnumber    => "2 sales invoice",
90   itime        => $march_9,
91   gldate       => $march_9,
92   intnotes     => 'booked in March',
93   taxincluded  => 0,
94   transdate    => $date,
95   invoiceitems => [ create_invoice_item(part => $part1, qty =>  6, sellprice => 70),
96                     create_invoice_item(part => $part2, qty => 20, sellprice => 50),
97                   ]
98 );
99
100 my $credit_note = create_credit_note(
101   invnumber    => 'Gutschrift 34',
102   itime        => $gldate,
103   gldate       => $gldate,
104   intnotes     => 'booked in February',
105   taxincluded  => 0,
106   transdate    => $date,
107   invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
108                     create_invoice_item(part => $part2, qty => 10, sellprice => 50),
109                   ]
110 );
111
112 my $startdate = DateTime->new(year => 2017, month =>  1, day =>  1);
113 my $enddate   = DateTime->new(year => 2017, month => 12, day => 31);
114
115 my $datev = SL::DATEV->new(
116   dbh        => $dbh,
117   from       => $startdate,
118   to         => $enddate,
119 );
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");
124
125 note('testing gldatefrom');
126 $datev = SL::DATEV->new(
127   dbh        => $dbh,
128   from       => $startdate,
129   to         => DateTime->new(year => 2017, month => 01, day => 31),
130 );
131
132 $::form               = Support::TestSetup->create_new_form;
133 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 3, day => 1)->to_kivitendo;
134
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");
139
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";
143
144 done_testing();
145 clear_up();
146
147 sub clear_up {
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);
153 };
154
155 1;