Finanzübersicht: Testcase für Spalten Auftragsvorlauf/-eingang
[kivitendo-erp.git] / t / controllers / financial_overview / sales_orders.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 => 49;
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::Controller::FinancialOverview';
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, $ctrl, $currency_id, $customer, $employee, $order, $part, $tax_zone, $unit, @invoices);
39
40 sub init_common_state {
41   $ar_chart       = SL::DB::Manager::Chart->find_by(accno => '1400')                        || croak "No AR chart";
42   $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%') || croak "No accounting group";
43   $currency_id    = SL::DB::Default->get->currency_id;
44   $employee       = SL::DB::Manager::Employee->current                                      || croak "No employee";
45   $tax_zone       = SL::DB::Manager::TaxZone->find_by( description => 'Inland')             || croak "No taxzone";
46   $unit           = SL::DB::Manager::Unit->find_by(name => 'psch')                          || croak "No unit";
47 }
48
49 sub create_sales_order {
50   my %params = @_;
51
52   $params{$_} ||= {} for qw(customer part tax order orderitem);
53
54   # Clean up: remove invoices, orders, parts and customers
55   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(InvoiceItem Invoice OrderItem Order Customer Part);
56
57   $customer     = SL::DB::Customer->new(
58     name        => 'Test Customer',
59     currency_id => $currency_id,
60     taxzone_id  => $tax_zone->id,
61     %{ $params{customer} }
62   )->save;
63
64   $part = SL::DB::Part->new(
65     partnumber         => 'T4254',
66     description        => 'Fourty-two fifty-four',
67     lastcost           => 222.22,
68     sellprice          => 333.33,
69     buchungsgruppen_id => $buchungsgruppe->id,
70     unit               => $unit->name,
71     %{ $params{part} }
72   )->save;
73   $part->load;
74
75   $order                     = SL::DB::Order->new(
76     customer_id              => $customer->id,
77     currency_id              => $currency_id,
78     taxzone_id               => $tax_zone->id,
79     transaction_description  => '<%period_start_date%>',
80     transdate                => DateTime->from_kivitendo('01.03.2014'),
81     orderitems               => [
82       { parts_id             => $part->id,
83         description          => $part->description,
84         lastcost             => $part->lastcost,
85         sellprice            => $part->sellprice,
86         qty                  => 1,
87         unit                 => $unit->name,
88         %{ $params{orderitem} },
89       },
90     ],
91     periodic_invoices_config => $params{periodic_invoices_config} ? {
92       active                 => 1,
93       ar_chart_id            => $ar_chart->id,
94       %{ $params{periodic_invoices_config} },
95     } : undef,
96     %{ $params{order} },
97   );
98
99   $order->calculate_prices_and_taxes;
100
101   ok($order->save(cascade => 1));
102
103   $::form         = Form->new('');
104   $::form->{year} = 2014;
105   $ctrl           = SL::Controller::FinancialOverview->new;
106
107   $ctrl->get_objects;
108   $ctrl->calculate_one_time_data;
109   $ctrl->calculate_periodic_invoices;
110 }
111
112 init_common_state();
113
114 # ----------------------------------------------------------------------
115 # An order without periodic invoices:
116 create_sales_order();
117
118 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "no periodic invoices, data for $_")
119   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
120
121 is_deeply($ctrl->data->{$_}, { months => [ 0, 0, 333.33, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 333.33, 0, 0, 0 ], year => 333.33 }, "no periodic invoices, data for $_")
122   for qw(sales_orders sales_orders_per_inv);
123
124 # ----------------------------------------------------------------------
125 # order_value_periodicity=y, periodicity=q
126 create_sales_order(
127   periodic_invoices_config  => {
128     periodicity             => 'm',
129     order_value_periodicity => 'y',
130     start_date              => DateTime->from_kivitendo('01.05.2014'),
131   });
132
133 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "periodic conf p=m ovp=y, no invoices, data for $_")
134   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
135
136 is_deeply($ctrl->data->{sales_orders},
137           { months => [ 0, 0, 0, 0, 27.7775, 27.7775, 27.7775, 27.7775, 27.7775, 27.7775, 27.7775, 27.7775 ], quarters => [ 0, 55.555, 83.3325, 83.3325 ], year => 222.22 },
138           "periodic conf p=m ovp=y, no invoices, data for sales_orders");
139 is_deeply($ctrl->data->{sales_orders_per_inv},
140           { months => [ 0, 0, 333.33, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 333.33, 0, 0, 0 ], year => 333.33 },
141           "periodic conf p=m ovp=y, no invoices, data for sales_orders_per_inv");
142
143 # ----------------------------------------------------------------------
144 # order_value_periodicity=y, periodicity=q, starting in previous year
145 create_sales_order(
146   order                     => {
147     transdate               => DateTime->from_kivitendo('01.03.2013'),
148   },
149   periodic_invoices_config  => {
150     periodicity             => 'q',
151     order_value_periodicity => 'y',
152     start_date              => DateTime->from_kivitendo('01.05.2013'),
153   });
154
155 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "periodic conf p=q ovp=y, no invoices, starting previous year, data for $_")
156   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
157
158 is_deeply($ctrl->data->{sales_orders},
159           { months => [ 0, 83.3325, 0, 0, 83.3325, 0, 0, 83.3325, 0, 0, 83.3325, 0 ], quarters => [ 83.3325, 83.3325, 83.3325, 83.3325 ], year => 333.33 },
160           "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders");
161 is_deeply($ctrl->data->{sales_orders_per_inv},
162           { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
163           "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders_per_inv");
164
165 # ----------------------------------------------------------------------
166 # order_value_periodicity=y, periodicity=q, starting in previous year, ending middle of year
167 create_sales_order(
168   order                     => {
169     transdate               => DateTime->from_kivitendo('01.03.2013'),
170   },
171   periodic_invoices_config  => {
172     periodicity             => 'q',
173     order_value_periodicity => 'y',
174     start_date              => DateTime->from_kivitendo('01.05.2013'),
175     end_date                => DateTime->from_kivitendo('01.09.2014'),
176     terminated              => 1,
177   });
178
179 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for $_")
180   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
181
182 is_deeply($ctrl->data->{sales_orders},
183           { months => [ 0, 83.3325, 0, 0, 83.3325, 0, 0, 83.3325, 0, 0, 0, 0 ], quarters => [ 83.3325, 83.3325, 83.3325, 0 ], year => 249.9975 },
184           "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders");
185 is_deeply($ctrl->data->{sales_orders_per_inv},
186           { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
187           "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders_per_inv");
188
189 # ----------------------------------------------------------------------
190 # order_value_periodicity=y, periodicity=q, starting and ending before current
191 create_sales_order(
192   order                     => {
193     transdate               => DateTime->from_kivitendo('01.03.2012'),
194   },
195   periodic_invoices_config  => {
196     periodicity             => 'q',
197     order_value_periodicity => 'y',
198     start_date              => DateTime->from_kivitendo('01.05.2012'),
199     end_date                => DateTime->from_kivitendo('01.09.2013'),
200     terminated              => 1,
201   });
202
203 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "periodic conf p=q ovp=y, no invoices, starting and ending before current year, data for $_")
204   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_orders sales_orders_per_inv sales_quotations);
205
206 done_testing();