5a33d5f0cf29fa709c58b795b366d9058c0d0d9d
[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;
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 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || die "No accounting group for 7\%";
20 my $bank            = SL::DB::Manager::Chart->find_by(description => 'Bank')                 || die 'Can\'t find chart "Bank"';
21 my $date            = DateTime->new(year => 2017, month =>  1, day => 1);
22 my $payment_date    = DateTime->new(year => 2017, month =>  1, day => 5);
23
24 my $part1 = SL::Dev::Part::create_part(partnumber => '19', description => 'Part 19%')->save;
25 my $part2 = SL::Dev::Part::create_part(
26   partnumber         => '7',
27   description        => 'Part 7%',
28   buchungsgruppen_id => $buchungsgruppe7->id,
29 )->save;
30
31 my $invoice = SL::Dev::Record::create_sales_invoice(
32   invnumber    => "1 sales invoice",
33   taxincluded  => 0,
34   transdate    => $date,
35   invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
36                     SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
37                   ]
38 );
39 $invoice->pay_invoice(chart_id      => $bank->id,
40                       amount        => $invoice->open_amount,
41                       transdate     => $payment_date->to_kivitendo,
42                       memo          => 'foobar',
43                       source        => 'barfoo',
44                      );
45 my $datev1 = SL::DATEV->new(
46   dbh        => $invoice->db->dbh,
47   trans_id   => $invoice->id,
48 );
49 $datev1->generate_datev_data;
50 my $kne_lines1 = $datev1->generate_datev_lines;
51 cmp_bag $datev1->generate_datev_lines, [
52                                          {
53                                            'belegfeld1'   => '1 sales invoice',
54                                            'buchungstext' => 'Testcustomer',
55                                            'datum'        => '01.01.2017',
56                                            'gegenkonto'   => '8400',
57                                            'konto'        => '1400',
58                                            'umsatz'       => '249.9',
59                                            'waehrung'     => 'EUR'
60                                          },
61                                          {
62                                            'belegfeld1'   => '1 sales invoice',
63                                            'buchungstext' => 'Testcustomer',
64                                            'datum'        => '01.01.2017',
65                                            'gegenkonto'   => '8300',
66                                            'konto'        => '1400',
67                                            'umsatz'       => 535,
68                                            'waehrung'     => 'EUR'
69                                          },
70                                          {
71                                            'belegfeld1'   => '1 sales invoice',
72                                            'buchungstext' => 'Testcustomer',
73                                            'datum'        => '05.01.2017',
74                                            'gegenkonto'   => '1400',
75                                            'konto'        => '1200',
76                                            'umsatz'       => '784.9',
77                                            'waehrung'     => 'EUR'
78                                          },
79                                        ], "trans_id datev check ok";
80
81 my $invoice2 = SL::Dev::Record::create_sales_invoice(
82   invnumber    => "2 sales invoice",
83   taxincluded  => 0,
84   transdate    => $date,
85   invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  6, sellprice => 70),
86                     SL::Dev::Record::create_invoice_item(part => $part2, qty => 20, sellprice => 50),
87                   ]
88 );
89
90 my $credit_note = SL::Dev::Record::create_credit_note(
91   invnumber    => 'Gutschrift 34',
92   taxincluded  => 0,
93   transdate    => $date,
94   invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
95                     SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
96                   ]
97 );
98
99 my $startdate = DateTime->new(year => 2017, month =>  1, day => 1);
100 my $enddate   = DateTime->new(year => 2017, month => 12, day => 31);
101
102 my $datev = SL::DATEV->new(
103   dbh        => $credit_note->db->dbh,
104   from       => $startdate,
105   to         => $enddate
106 );
107 $datev->generate_datev_data(from_to => $datev->fromto);
108 my $datev_lines = $datev->generate_datev_lines;
109 my $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
110 is($umsatzsumme, 3924.50, "umsatzsumme ok");
111
112 done_testing();
113 clear_up();
114
115 sub clear_up {
116   SL::DB::Manager::AccTransaction->delete_all(all => 1);
117   SL::DB::Manager::InvoiceItem->delete_all(   all => 1);
118   SL::DB::Manager::Invoice->delete_all(       all => 1);
119   SL::DB::Manager::Customer->delete_all(      all => 1);
120   SL::DB::Manager::Part->delete_all(          all => 1);
121 };
122
123
124 1;
125