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