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