]> wagnertech.de Git - mfinanz.git/blob - t/datev/invoices.t
Testfall datev erweitert: datev_data kost1 und kost2
[mfinanz.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 my $department      = create_department(description => 'Kostenstelle DATEV-Schnittstelle 2018');
27 my $project         = create_project(projectnumber => 2017, description => 'Crowd-Funding September 2017');
28
29 my $part1 = new_part(partnumber => '19', description => 'Part 19%')->save;
30 my $part2 = new_part(
31   partnumber         => '7',
32   description        => 'Part 7%',
33   buchungsgruppen_id => $buchungsgruppe7->id,
34 )->save;
35
36 my $invoice = create_sales_invoice(
37   invnumber    => "1 sales invoice",
38   itime        => $gldate,
39   gldate       => $gldate,
40   intnotes     => 'booked in February',
41   taxincluded  => 0,
42   transdate    => $date,
43   invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
44                     create_invoice_item(part => $part2, qty => 10, sellprice => 50),
45                   ],
46   department_id    => $department->id,
47   globalproject_id => $project->id,
48 );
49 $invoice->pay_invoice(chart_id      => $bank->id,
50                       amount        => $invoice->open_amount,
51                       transdate     => $payment_date->to_kivitendo,
52                       memo          => 'foobar',
53                       source        => 'barfoo',
54                      );
55 my $datev1 = SL::DATEV->new(
56   dbh        => $invoice->db->dbh,
57   trans_id   => $invoice->id,
58 );
59 $datev1->generate_datev_data;
60 cmp_bag $datev1->generate_datev_lines, [
61                                          {
62                                            'belegfeld1'   => '1 sales invoice',
63                                            'buchungstext' => 'Testcustomer',
64                                            'datum'        => '01.01.2017',
65                                            'gegenkonto'   => '8400',
66                                            'konto'        => '1400',
67                                            'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
68                                            'kost2'        => 'Crowd-Funding September 2017',
69                                            'umsatz'       => '249.9',
70                                            'waehrung'     => 'EUR'
71                                          },
72                                          {
73                                            'belegfeld1'   => '1 sales invoice',
74                                            'buchungstext' => 'Testcustomer',
75                                            'datum'        => '01.01.2017',
76                                            'gegenkonto'   => '8300',
77                                            'konto'        => '1400',
78                                            'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
79                                            'kost2'        => 'Crowd-Funding September 2017',
80                                            'umsatz'       => 535,
81                                            'waehrung'     => 'EUR'
82                                          },
83                                          {
84                                            'belegfeld1'   => '1 sales invoice',
85                                            'buchungstext' => 'Testcustomer',
86                                            'datum'        => '05.01.2017',
87                                            'gegenkonto'   => '1400',
88                                            'konto'        => '1200',
89                                            'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
90                                            'kost2'        => 'Crowd-Funding September 2017',
91                                            'umsatz'       => '784.9',
92                                            'waehrung'     => 'EUR'
93                                          },
94                                        ], "trans_id datev check ok";
95
96 my $march_9 = DateTime->new(year => 2017, month =>  3, day => 9);
97 my $invoice2 = create_sales_invoice(
98   invnumber    => "2 sales invoice",
99   itime        => $march_9,
100   gldate       => $march_9,
101   intnotes     => 'booked in March',
102   taxincluded  => 0,
103   transdate    => $date,
104   invoiceitems => [ create_invoice_item(part => $part1, qty =>  6, sellprice => 70),
105                     create_invoice_item(part => $part2, qty => 20, sellprice => 50),
106                   ]
107 );
108
109 my $credit_note = create_credit_note(
110   invnumber    => 'Gutschrift 34',
111   itime        => $gldate,
112   gldate       => $gldate,
113   intnotes     => 'booked in February',
114   taxincluded  => 0,
115   transdate    => $date,
116   invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
117                     create_invoice_item(part => $part2, qty => 10, sellprice => 50),
118                   ]
119 );
120
121 my $startdate = DateTime->new(year => 2017, month =>  1, day =>  1);
122 my $enddate   = DateTime->new(year => 2017, month => 12, day => 31);
123
124 my $datev = SL::DATEV->new(
125   dbh        => $dbh,
126   from       => $startdate,
127   to         => $enddate,
128 );
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");
133
134 note('testing gldatefrom');
135 $datev = SL::DATEV->new(
136   dbh        => $dbh,
137   from       => $startdate,
138   to         => DateTime->new(year => 2017, month => 01, day => 31),
139 );
140
141 $::form               = Support::TestSetup->create_new_form;
142 $::form->{gldatefrom} = DateTime->new(year => 2017, month => 3, day => 1)->to_kivitendo;
143
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");
148
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";
152
153 done_testing();
154 clear_up();
155
156 sub clear_up {
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);
164 };
165
166 1;