Wiederkehrende Rechnungen: Testcase für Periodizitäten
[kivitendo-erp.git] / t / background_job / create_periodic_invoices.t
1 package DateTime;
2
3 use SL::Helper::DateTime;
4
5 no warnings 'redefine';
6
7 sub now_local {
8   return shift->new(time_zone => $::locale->get_local_time_zone, year => 2014, month => 3, day => 15, hour => 12, minute => 23, second => 34);
9 }
10
11 sub today_local {
12   return shift->now_local->truncate(to => 'day');
13 }
14
15 package main;
16
17 use Test::More tests => 80;
18
19 use lib 't';
20 use strict;
21 use utf8;
22
23 use Carp;
24 use Support::TestSetup;
25
26 use_ok 'SL::BackgroundJob::CreatePeriodicInvoices';
27 use_ok 'SL::DB::Chart';
28 use_ok 'SL::DB::Customer';
29 use_ok 'SL::DB::Default';
30 use_ok 'SL::DB::Invoice';
31 use_ok 'SL::DB::Order';
32 use_ok 'SL::DB::Part';
33 use_ok 'SL::DB::TaxZone';
34
35 Support::TestSetup::login();
36
37 our ($ar_chart, $buchungsgruppe, $currency_id, $customer, $employee, $order, $part, $tax_zone, $unit, @invoices);
38
39 sub init_common_state {
40   $ar_chart       = SL::DB::Manager::Chart->find_by(accno => '1400')                        || croak "No AR chart";
41   $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%') || croak "No accounting group";
42   $currency_id    = SL::DB::Default->get->currency_id;
43   $employee       = SL::DB::Manager::Employee->current                                      || croak "No employee";
44   $tax_zone       = SL::DB::Manager::TaxZone->find_by( description => 'Inland')             || croak "No taxzone";
45   $unit           = SL::DB::Manager::Unit->find_by(name => 'psch')                          || croak "No unit";
46 }
47
48 sub create_invoices {
49   my %params = @_;
50
51   $params{$_} ||= {} for qw(customer part tax order orderitem periodic_invoices_config);
52
53   # Clean up: remove invoices, orders, parts and customers
54   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(InvoiceItem Invoice OrderItem Order Customer Part);
55
56   $customer     = SL::DB::Customer->new(
57     name        => 'Test Customer',
58     currency_id => $currency_id,
59     taxzone_id  => $tax_zone->id,
60     %{ $params{customer} }
61   )->save;
62
63   $part = SL::DB::Part->new(
64     partnumber         => 'T4254',
65     description        => 'Fourty-two fifty-four',
66     lastcost           => 222.22,
67     sellprice          => 333.33,
68     buchungsgruppen_id => $buchungsgruppe->id,
69     unit               => $unit->name,
70     %{ $params{part} }
71   )->save;
72   $part->load;
73
74   $order                     = SL::DB::Order->new(
75     customer_id              => $customer->id,
76     currency_id              => $currency_id,
77     taxzone_id               => $tax_zone->id,
78     transaction_description  => '<%period_start_date%>',
79     orderitems               => [
80       { parts_id             => $part->id,
81         description          => $part->description,
82         lastcost             => $part->lastcost,
83         sellprice            => $part->sellprice,
84         qty                  => 1,
85         unit                 => $unit->name,
86         %{ $params{orderitem} },
87       },
88     ],
89     periodic_invoices_config => {
90       active                 => 1,
91       ar_chart_id            => $ar_chart->id,
92       %{ $params{periodic_invoices_config} },
93     },
94     %{ $params{order} },
95   );
96
97   $order->calculate_prices_and_taxes;
98
99   ok($order->save(cascade => 1));
100
101   SL::BackgroundJob::CreatePeriodicInvoices->new->run(SL::DB::BackgroundJob->new);
102
103   @invoices = @{ SL::DB::Manager::Invoice->get_all(sort_by => [ qw(id) ]) };
104 }
105
106 sub are_invoices {
107   my ($description, @exp_date_netamount_pairs) = @_;
108
109   is scalar(@invoices), scalar(@exp_date_netamount_pairs), "${description} number of invoices " . scalar(@exp_date_netamount_pairs);
110
111   my @actual_date_netamount_pairs = map { [ $_->transaction_description, $_->netamount * 1 ] } @invoices;
112   is_deeply \@actual_date_netamount_pairs, \@exp_date_netamount_pairs, "${description} date/netamount of created invoices";
113 }
114
115 init_common_state();
116
117 # order_value_periodicity=y
118 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => 'y', start_date => DateTime->from_kivitendo('01.01.2013') });
119 are_invoices 'p=m ovp=y',[ '01.01.2013', 27.78 ], [ '01.02.2013', 27.78 ], [ '01.03.2013', 27.78 ], [ '01.04.2013', 27.78 ],
120                          [ '01.05.2013', 27.78 ], [ '01.06.2013', 27.78 ], [ '01.07.2013', 27.78 ], [ '01.08.2013', 27.78 ],
121                          [ '01.09.2013', 27.78 ], [ '01.10.2013', 27.78 ], [ '01.11.2013', 27.78 ], [ '01.12.2013', 27.75 ],
122                          [ '01.01.2014', 27.78 ], [ '01.02.2014', 27.78 ], [ '01.03.2014', 27.78 ];
123
124 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => 'y', start_date => DateTime->from_kivitendo('01.01.2013') });
125 are_invoices 'p=q ovp=y',[ '01.01.2013', 83.33 ], [ '01.04.2013', 83.33 ], [ '01.07.2013', 83.33 ], [ '01.10.2013', 83.34 ], [ '01.01.2014', 83.33 ];
126
127 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => 'y', start_date => DateTime->from_kivitendo('01.01.2013') });
128 are_invoices 'p=b ovp=y',[ '01.01.2013', 166.67 ], [ '01.07.2013', 166.66 ], [ '01.01.2014', 166.67 ];
129
130 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => 'y', start_date => DateTime->from_kivitendo('01.01.2013') });
131 are_invoices 'p=y ovp=y',[ '01.01.2013', 333.33 ], [ '01.01.2014', 333.33 ];
132
133 # order_value_periodicity=b
134 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => 'b', start_date => DateTime->from_kivitendo('01.01.2013') });
135 are_invoices 'p=m ovp=b',[ '01.01.2013', 55.56 ], [ '01.02.2013', 55.56 ], [ '01.03.2013', 55.56 ], [ '01.04.2013', 55.56 ],
136                          [ '01.05.2013', 55.56 ], [ '01.06.2013', 55.53 ], [ '01.07.2013', 55.56 ], [ '01.08.2013', 55.56 ],
137                          [ '01.09.2013', 55.56 ], [ '01.10.2013', 55.56 ], [ '01.11.2013', 55.56 ], [ '01.12.2013', 55.53 ],
138                          [ '01.01.2014', 55.56 ], [ '01.02.2014', 55.56 ], [ '01.03.2014', 55.56 ];
139
140 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => 'b', start_date => DateTime->from_kivitendo('01.01.2013') });
141 are_invoices 'p=q ovp=b',[ '01.01.2013', 166.67 ], [ '01.04.2013', 166.66 ], [ '01.07.2013', 166.67 ], [ '01.10.2013', 166.66 ], [ '01.01.2014', 166.67 ];
142
143 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => 'b', start_date => DateTime->from_kivitendo('01.01.2013') });
144 are_invoices 'p=b ovp=b',[ '01.01.2013', 333.33 ], [ '01.07.2013', 333.33 ], [ '01.01.2014', 333.33 ];
145
146 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => 'b', start_date => DateTime->from_kivitendo('01.01.2013') });
147 are_invoices 'p=y ovp=b',[ '01.01.2013', 666.66 ], [ '01.01.2014', 666.66 ];
148
149 # order_value_periodicity=q
150 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => 'q', start_date => DateTime->from_kivitendo('01.01.2013') });
151 are_invoices 'p=m ovp=q',[ '01.01.2013', 111.11 ], [ '01.02.2013', 111.11 ], [ '01.03.2013', 111.11 ], [ '01.04.2013', 111.11 ],
152                          [ '01.05.2013', 111.11 ], [ '01.06.2013', 111.11 ], [ '01.07.2013', 111.11 ], [ '01.08.2013', 111.11 ],
153                          [ '01.09.2013', 111.11 ], [ '01.10.2013', 111.11 ], [ '01.11.2013', 111.11 ], [ '01.12.2013', 111.11 ],
154                          [ '01.01.2014', 111.11 ], [ '01.02.2014', 111.11 ], [ '01.03.2014', 111.11 ];
155
156 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => 'q', start_date => DateTime->from_kivitendo('01.01.2013') });
157 are_invoices 'p=q ovp=q',[ '01.01.2013', 333.33 ], [ '01.04.2013', 333.33 ], [ '01.07.2013', 333.33 ], [ '01.10.2013', 333.33 ], [ '01.01.2014', 333.33 ];
158
159 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => 'q', start_date => DateTime->from_kivitendo('01.01.2013') });
160 are_invoices 'p=b ovp=q',[ '01.01.2013', 666.66 ], [ '01.07.2013', 666.66 ], [ '01.01.2014', 666.66 ];
161
162 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => 'q', start_date => DateTime->from_kivitendo('01.01.2013') });
163 are_invoices 'p=y ovp=q',[ '01.01.2013', 1333.32 ], [ '01.01.2014', 1333.32 ];
164
165 # order_value_periodicity=m
166 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => 'm', start_date => DateTime->from_kivitendo('01.01.2013') });
167 are_invoices 'p=m ovp=m',[ '01.01.2013', 333.33 ], [ '01.02.2013', 333.33 ], [ '01.03.2013', 333.33 ], [ '01.04.2013', 333.33 ],
168                          [ '01.05.2013', 333.33 ], [ '01.06.2013', 333.33 ], [ '01.07.2013', 333.33 ], [ '01.08.2013', 333.33 ],
169                          [ '01.09.2013', 333.33 ], [ '01.10.2013', 333.33 ], [ '01.11.2013', 333.33 ], [ '01.12.2013', 333.33 ],
170                          [ '01.01.2014', 333.33 ], [ '01.02.2014', 333.33 ], [ '01.03.2014', 333.33 ];
171
172 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => 'm', start_date => DateTime->from_kivitendo('01.01.2013') });
173 are_invoices 'p=q ovp=m',[ '01.01.2013', 999.99 ], [ '01.04.2013', 999.99 ], [ '01.07.2013', 999.99 ], [ '01.10.2013', 999.99 ], [ '01.01.2014', 999.99 ];
174
175 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => 'm', start_date => DateTime->from_kivitendo('01.01.2013') });
176 are_invoices 'p=b ovp=m',[ '01.01.2013', 1999.98 ], [ '01.07.2013', 1999.98 ], [ '01.01.2014', 1999.98 ];
177
178 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => 'm', start_date => DateTime->from_kivitendo('01.01.2013') });
179 are_invoices 'p=y ovp=m',[ '01.01.2013', 3999.96 ], [ '01.01.2014', 3999.96 ];
180
181 # order_value_periodicity=2
182 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => '2', start_date => DateTime->from_kivitendo('01.01.2012') });
183 are_invoices 'p=m ovp=2',[ '01.01.2012', 13.89 ], [ '01.02.2012', 13.89 ], [ '01.03.2012', 13.89 ], [ '01.04.2012', 13.89 ],
184                          [ '01.05.2012', 13.89 ], [ '01.06.2012', 13.89 ], [ '01.07.2012', 13.89 ], [ '01.08.2012', 13.89 ],
185                          [ '01.09.2012', 13.89 ], [ '01.10.2012', 13.89 ], [ '01.11.2012', 13.89 ], [ '01.12.2012', 13.89 ],
186                          [ '01.01.2013', 13.89 ], [ '01.02.2013', 13.89 ], [ '01.03.2013', 13.89 ], [ '01.04.2013', 13.89 ],
187                          [ '01.05.2013', 13.89 ], [ '01.06.2013', 13.89 ], [ '01.07.2013', 13.89 ], [ '01.08.2013', 13.89 ],
188                          [ '01.09.2013', 13.89 ], [ '01.10.2013', 13.89 ], [ '01.11.2013', 13.89 ], [ '01.12.2013', 13.86 ],
189                          [ '01.01.2014', 13.89 ], [ '01.02.2014', 13.89 ], [ '01.03.2014', 13.89 ];
190
191 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => '2', start_date => DateTime->from_kivitendo('01.01.2012') });
192 are_invoices 'p=q ovp=2',[ '01.01.2012', 41.67 ], [ '01.04.2012', 41.67 ], [ '01.07.2012', 41.67 ], [ '01.10.2012', 41.67 ],
193                          [ '01.01.2013', 41.67 ], [ '01.04.2013', 41.67 ], [ '01.07.2013', 41.67 ], [ '01.10.2013', 41.64 ],
194                          [ '01.01.2014', 41.67 ];
195
196 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => '2', start_date => DateTime->from_kivitendo('01.01.2012') });
197 are_invoices 'p=b ovp=2',[ '01.01.2012', 83.33 ], [ '01.07.2012', 83.33 ], [ '01.01.2013', 83.33 ], [ '01.07.2013', 83.34 ], [ '01.01.2014', 83.33 ];
198
199 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => '2', start_date => DateTime->from_kivitendo('01.01.2012') });
200 are_invoices 'p=y ovp=2',[ '01.01.2012', 166.67 ], [ '01.01.2013', 166.66 ], [ '01.01.2014', 166.67 ];
201
202 # order_value_periodicity=5
203 create_invoices(periodic_invoices_config => { periodicity => 'm', order_value_periodicity => '5', start_date => DateTime->from_kivitendo('01.01.2009') });
204 are_invoices 'p=m ovp=5',[ '01.01.2009',  5.56 ], [ '01.02.2009',  5.56 ], [ '01.03.2009',  5.56 ], [ '01.04.2009',  5.56 ],
205                          [ '01.05.2009',  5.56 ], [ '01.06.2009',  5.56 ], [ '01.07.2009',  5.56 ], [ '01.08.2009',  5.56 ],
206                          [ '01.09.2009',  5.56 ], [ '01.10.2009',  5.56 ], [ '01.11.2009',  5.56 ], [ '01.12.2009',  5.56 ],
207                          [ '01.01.2010',  5.56 ], [ '01.02.2010',  5.56 ], [ '01.03.2010',  5.56 ], [ '01.04.2010',  5.56 ],
208                          [ '01.05.2010',  5.56 ], [ '01.06.2010',  5.56 ], [ '01.07.2010',  5.56 ], [ '01.08.2010',  5.56 ],
209                          [ '01.09.2010',  5.56 ], [ '01.10.2010',  5.56 ], [ '01.11.2010',  5.56 ], [ '01.12.2010',  5.56 ],
210                          [ '01.01.2011',  5.56 ], [ '01.02.2011',  5.56 ], [ '01.03.2011',  5.56 ], [ '01.04.2011',  5.56 ],
211                          [ '01.05.2011',  5.56 ], [ '01.06.2011',  5.56 ], [ '01.07.2011',  5.56 ], [ '01.08.2011',  5.56 ],
212                          [ '01.09.2011',  5.56 ], [ '01.10.2011',  5.56 ], [ '01.11.2011',  5.56 ], [ '01.12.2011',  5.56 ],
213                          [ '01.01.2012',  5.56 ], [ '01.02.2012',  5.56 ], [ '01.03.2012',  5.56 ], [ '01.04.2012',  5.56 ],
214                          [ '01.05.2012',  5.56 ], [ '01.06.2012',  5.56 ], [ '01.07.2012',  5.56 ], [ '01.08.2012',  5.56 ],
215                          [ '01.09.2012',  5.56 ], [ '01.10.2012',  5.56 ], [ '01.11.2012',  5.56 ], [ '01.12.2012',  5.56 ],
216                          [ '01.01.2013',  5.56 ], [ '01.02.2013',  5.56 ], [ '01.03.2013',  5.56 ], [ '01.04.2013',  5.56 ],
217                          [ '01.05.2013',  5.56 ], [ '01.06.2013',  5.56 ], [ '01.07.2013',  5.56 ], [ '01.08.2013',  5.56 ],
218                          [ '01.09.2013',  5.56 ], [ '01.10.2013',  5.56 ], [ '01.11.2013',  5.56 ], [ '01.12.2013',  5.29 ],
219                          [ '01.01.2014',  5.56 ], [ '01.02.2014',  5.56 ], [ '01.03.2014',  5.56 ];
220
221 create_invoices(periodic_invoices_config => { periodicity => 'q', order_value_periodicity => '5', start_date => DateTime->from_kivitendo('01.01.2009') });
222 are_invoices 'p=q ovp=5',[ '01.01.2009', 16.67 ], [ '01.04.2009', 16.67 ], [ '01.07.2009', 16.67 ], [ '01.10.2009', 16.67 ],
223                          [ '01.01.2010', 16.67 ], [ '01.04.2010', 16.67 ], [ '01.07.2010', 16.67 ], [ '01.10.2010', 16.67 ],
224                          [ '01.01.2011', 16.67 ], [ '01.04.2011', 16.67 ], [ '01.07.2011', 16.67 ], [ '01.10.2011', 16.67 ],
225                          [ '01.01.2012', 16.67 ], [ '01.04.2012', 16.67 ], [ '01.07.2012', 16.67 ], [ '01.10.2012', 16.67 ],
226                          [ '01.01.2013', 16.67 ], [ '01.04.2013', 16.67 ], [ '01.07.2013', 16.67 ], [ '01.10.2013', 16.60 ],
227                          [ '01.01.2014', 16.67 ];
228
229 create_invoices(periodic_invoices_config => { periodicity => 'b', order_value_periodicity => '5', start_date => DateTime->from_kivitendo('01.01.2009') });
230 are_invoices 'p=b ovp=5',[ '01.01.2009', 33.33 ], [ '01.07.2009', 33.33 ],
231                          [ '01.01.2010', 33.33 ], [ '01.07.2010', 33.33 ],
232                          [ '01.01.2011', 33.33 ], [ '01.07.2011', 33.33 ],
233                          [ '01.01.2012', 33.33 ], [ '01.07.2012', 33.33 ],
234                          [ '01.01.2013', 33.33 ], [ '01.07.2013', 33.36 ],
235                          [ '01.01.2014', 33.33 ];
236
237 create_invoices(periodic_invoices_config => { periodicity => 'y', order_value_periodicity => '5', start_date => DateTime->from_kivitendo('01.01.2009') });
238 are_invoices 'p=y ovp=5',[ '01.01.2009', 66.67 ], [ '01.01.2010', 66.67 ], [ '01.01.2011', 66.67 ], [ '01.01.2012', 66.67 ], [ '01.01.2013', 66.65 ], [ '01.01.2014', 66.67 ];
239
240 done_testing();