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