WebshopApi: Shop Controller
[kivitendo-erp.git] / t / datev / invoices.t
index 5a33d5f..52c665e 100644 (file)
@@ -6,7 +6,7 @@ use lib 't';
 
 use_ok 'Support::TestSetup';
 use SL::DATEV qw(:CONSTANTS);
-use SL::Dev::ALL;
+use SL::Dev::ALL qw(:ALL);
 use List::Util qw(sum);
 use SL::DB::Buchungsgruppe;
 use SL::DB::Chart;
@@ -16,25 +16,35 @@ Support::TestSetup::login();
 
 clear_up();
 
+my $dbh = SL::DB->client->dbh;
+
 my $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || die "No accounting group for 7\%";
 my $bank            = SL::DB::Manager::Chart->find_by(description => 'Bank')                 || die 'Can\'t find chart "Bank"';
 my $date            = DateTime->new(year => 2017, month =>  1, day => 1);
 my $payment_date    = DateTime->new(year => 2017, month =>  1, day => 5);
+my $gldate          = DateTime->new(year => 2017, month =>  2, day => 9); # simulate bookings for Jan being made in Feb
+my $department      = create_department(description => 'Kostenstelle DATEV-Schnittstelle 2018');
+my $project         = create_project(projectnumber => 2017, description => 'Crowd-Funding September 2017');
 
-my $part1 = SL::Dev::Part::create_part(partnumber => '19', description => 'Part 19%')->save;
-my $part2 = SL::Dev::Part::create_part(
+my $part1 = new_part(partnumber => '19', description => 'Part 19%')->save;
+my $part2 = new_part(
   partnumber         => '7',
   description        => 'Part 7%',
   buchungsgruppen_id => $buchungsgruppe7->id,
 )->save;
 
-my $invoice = SL::Dev::Record::create_sales_invoice(
-  invnumber    => "1 sales invoice",
+my $invoice = create_sales_invoice(
+  invnumber    => "Þ sales ¥& invöice",
+  itime        => $gldate,
+  gldate       => $gldate,
+  intnotes     => 'booked in February',
   taxincluded  => 0,
   transdate    => $date,
-  invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
-                    SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
-                  ]
+  invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
+                    create_invoice_item(part => $part2, qty => 10, sellprice => 50),
+                  ],
+  department_id    => $department->id,
+  globalproject_id => $project->id,
 );
 $invoice->pay_invoice(chart_id      => $bank->id,
                       amount        => $invoice->open_amount,
@@ -47,67 +57,147 @@ my $datev1 = SL::DATEV->new(
   trans_id   => $invoice->id,
 );
 $datev1->generate_datev_data;
-my $kne_lines1 = $datev1->generate_datev_lines;
 cmp_bag $datev1->generate_datev_lines, [
                                          {
-                                           'belegfeld1'   => '1 sales invoice',
+                                           'belegfeld1'   => "\x{de} sales \x{a5}& inv\x{f6}ice",
                                            'buchungstext' => 'Testcustomer',
                                            'datum'        => '01.01.2017',
                                            'gegenkonto'   => '8400',
                                            'konto'        => '1400',
+                                           'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
+                                           'kost2'        => 'Crowd-Funding September 2017',
                                            'umsatz'       => '249.9',
-                                           'waehrung'     => 'EUR'
+                                           'waehrung'     => 'EUR',
+                                           'soll_haben_kennzeichen' => 'S',
                                          },
                                          {
-                                           'belegfeld1'   => '1 sales invoice',
+                                           'belegfeld1'   => "\x{de} sales \x{a5}& inv\x{f6}ice",
                                            'buchungstext' => 'Testcustomer',
                                            'datum'        => '01.01.2017',
                                            'gegenkonto'   => '8300',
                                            'konto'        => '1400',
+                                           'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
+                                           'kost2'        => 'Crowd-Funding September 2017',
                                            'umsatz'       => 535,
-                                           'waehrung'     => 'EUR'
+                                           'waehrung'     => 'EUR',
+                                           'soll_haben_kennzeichen' => 'S',
                                          },
                                          {
-                                           'belegfeld1'   => '1 sales invoice',
+                                           'belegfeld1'   => "\x{de} sales \x{a5}& inv\x{f6}ice",
                                            'buchungstext' => 'Testcustomer',
                                            'datum'        => '05.01.2017',
                                            'gegenkonto'   => '1400',
                                            'konto'        => '1200',
+                                           'kost1'        => 'Kostenstelle DATEV-Schnittstelle 2018',
+                                           'kost2'        => 'Crowd-Funding September 2017',
                                            'umsatz'       => '784.9',
-                                           'waehrung'     => 'EUR'
+                                           'waehrung'     => 'EUR',
+                                           'soll_haben_kennzeichen' => 'S',
                                          },
                                        ], "trans_id datev check ok";
 
-my $invoice2 = SL::Dev::Record::create_sales_invoice(
+my $startdate = DateTime->new(year => 2017, month =>  1, day =>  1);
+my $enddate   = DateTime->new(year => 2017, month => 12, day => 31);
+
+# check conversion to csv
+$datev1->from($startdate);
+$datev1->to($enddate);
+
+# splice away the header, because sort won't do
+# we need sort, because pay_invoice is not acc_trans_id order safe
+my @data_csv = splice @{ $datev1->csv_buchungsexport() }, 2, 5;
+@data_csv    = sort { $a->[0] <=> $b->[0] } @data_csv;
+
+my $cp1252_belegfeld1   = SL::Iconv::convert("UTF-8", "CP1252", 'Þ sales ¥& i');
+my $cp1252_buchungstext = SL::Iconv::convert("UTF-8", "CP1252", 'Þ sales ¥& invöice');
+
+cmp_bag($data_csv[1], [ 535, 'S', 'EUR', undef, undef, undef, '1400', '8300', undef, '0101', $cp1252_belegfeld1,
+                     undef, undef, $cp1252_buchungstext, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, 'Crowd-Fu', 'Kostenst', undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef ]
+       );
+
+cmp_bag($data_csv[0], [ '249,9', 'S', 'EUR', undef, undef, undef, '1400', '8400', undef, '0101', $cp1252_belegfeld1,
+                     undef, undef, $cp1252_buchungstext, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, 'Crowd-Fu', 'Kostenst', undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef ]
+       );
+cmp_bag($data_csv[2], [ '784,9', 'S', 'EUR', undef, undef, undef, '1200', '1400', undef, '0501', $cp1252_belegfeld1,
+                     undef, undef, $cp1252_buchungstext, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, 'Crowd-Fu', 'Kostenst', undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
+                     undef, undef, undef, undef, undef ]
+        );
+my $march_9 = DateTime->new(year => 2017, month =>  3, day => 9);
+my $invoice2 = create_sales_invoice(
   invnumber    => "2 sales invoice",
+  itime        => $march_9,
+  gldate       => $march_9,
+  intnotes     => 'booked in March',
   taxincluded  => 0,
   transdate    => $date,
-  invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  6, sellprice => 70),
-                    SL::Dev::Record::create_invoice_item(part => $part2, qty => 20, sellprice => 50),
+  invoiceitems => [ create_invoice_item(part => $part1, qty =>  6, sellprice => 70),
+                    create_invoice_item(part => $part2, qty => 20, sellprice => 50),
                   ]
 );
 
-my $credit_note = SL::Dev::Record::create_credit_note(
+my $credit_note = create_credit_note(
   invnumber    => 'Gutschrift 34',
+  itime        => $gldate,
+  gldate       => $gldate,
+  intnotes     => 'booked in February',
   taxincluded  => 0,
   transdate    => $date,
-  invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
-                    SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
+  invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
+                    create_invoice_item(part => $part2, qty => 10, sellprice => 50),
                   ]
 );
 
-my $startdate = DateTime->new(year => 2017, month =>  1, day => 1);
-my $enddate   = DateTime->new(year => 2017, month => 12, day => 31);
-
 my $datev = SL::DATEV->new(
-  dbh        => $credit_note->db->dbh,
+  dbh        => $dbh,
   from       => $startdate,
-  to         => $enddate
+  to         => $enddate,
 );
 $datev->generate_datev_data(from_to => $datev->fromto);
 my $datev_lines = $datev->generate_datev_lines;
 my $umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
-is($umsatzsumme, 3924.50, "umsatzsumme ok");
+cmp_ok($::form->round_amount($umsatzsumme,2), '==', 3924.5, "Sum of all bookings ok");
+
+note('testing gldatefrom');
+$datev = SL::DATEV->new(
+  dbh        => $dbh,
+  from       => $startdate,
+  to         => DateTime->new(year => 2017, month => 01, day => 31),
+);
+
+$::form               = Support::TestSetup->create_new_form;
+$::form->{gldatefrom} = DateTime->new(year => 2017, month => 3, day => 1)->to_kivitendo;
+
+$datev->generate_datev_data(from_to => $datev->fromto);
+$datev_lines = $datev->generate_datev_lines;
+$umsatzsumme = sum map { $_->{umsatz} } @{ $datev_lines };
+cmp_ok($umsatzsumme, '==', 1569.8, "Sum of bookings made after March 1st (only invoice2) ok");
+
+$::form->{gldatefrom} = DateTime->new(year => 2017, month => 5, day => 1)->to_kivitendo;
+$datev->generate_datev_data(from_to => $datev->fromto);
+cmp_bag $datev->generate_datev_lines, [], "no bookings for January made after May 1st: ok";
 
 done_testing();
 clear_up();
@@ -118,8 +208,9 @@ sub clear_up {
   SL::DB::Manager::Invoice->delete_all(       all => 1);
   SL::DB::Manager::Customer->delete_all(      all => 1);
   SL::DB::Manager::Part->delete_all(          all => 1);
+  SL::DB::Manager::Project->delete_all(       all => 1);
+  SL::DB::Manager::Department->delete_all(    all => 1);
+  SL::DATEV->clean_temporary_directories;
 };
 
-
 1;
-