4c613eeaf4eb6cb29b4798fbaa3abc708a6634e2
[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 => 43;
18
19 use lib 't';
20 use strict;
21 use utf8;
22
23 use Carp;
24 use Support::TestSetup;
25 use SL::Dev::Record qw(create_sales_order create_order_item);
26 use SL::Dev::CustomerVendor qw(new_customer);
27 use SL::Dev::Part qw(new_part);
28
29 use_ok 'SL::BackgroundJob::CreatePeriodicInvoices';
30 use_ok 'SL::Controller::FinancialOverview';
31 use_ok 'SL::DB::Chart';
32 use_ok 'SL::DB::Customer';
33 use_ok 'SL::DB::Default';
34 use_ok 'SL::DB::Invoice';
35 use_ok 'SL::DB::Order';
36 use_ok 'SL::DB::Part';
37
38 Support::TestSetup::login();
39
40 our ($ar_chart, $ctrl, $customer, $order, $part, $unit, @invoices);
41
42 sub clear_up {
43   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(InvoiceItem Invoice OrderItem Order Customer Part);
44 };
45
46 sub init_common_state {
47   $ar_chart       = SL::DB::Manager::Chart->find_by(accno => '1400') || croak "No AR chart";
48   $unit           = SL::DB::Manager::Unit->find_by(name => 'psch')   || croak "No unit";
49 }
50
51 sub make_sales_order {
52   my %params = @_;
53
54   $params{$_} ||= {} for qw(customer part order orderitem);
55
56   # Clean up: remove invoices, orders, parts and customers
57   clear_up();
58
59   $customer     = new_customer(
60     name        => 'Test Customer',
61     %{ $params{customer} }
62   )->save;
63
64   $part = new_part(
65     partnumber         => 'T4254',
66     description        => 'Fourty-two fifty-four',
67     lastcost           => 222.22,
68     sellprice          => 333.33,
69     %{ $params{part} }
70   )->save;
71   $part->load;
72
73   $order                     = create_sales_order(
74     save                     => 1,
75     customer                 => $customer,
76     transaction_description  => '<%period_start_date%>',
77     transdate                => DateTime->from_kivitendo('01.03.2014'),
78     orderitems => [ create_order_item(part => $part, qty =>  1, %{ $params{orderitem} }) ],
79     periodic_invoices_config => $params{periodic_invoices_config} ? {
80       active                 => 1,
81       ar_chart_id            => $ar_chart->id,
82       %{ $params{periodic_invoices_config} },
83     } : undef,
84     %{ $params{order} },
85   );
86
87   $::form         = Support::TestSetup->create_new_form;
88   $::form->{year} = 2014;
89   $ctrl           = SL::Controller::FinancialOverview->new;
90
91   $ctrl->get_objects;
92   $ctrl->calculate_one_time_data;
93   $ctrl->calculate_periodic_invoices;
94 }
95
96 init_common_state();
97
98 # ----------------------------------------------------------------------
99 # An order without periodic invoices:
100 make_sales_order();
101
102 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "no periodic invoices, data for $_")
103   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
104
105 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 $_")
106   for qw(sales_orders sales_orders_per_inv);
107
108 # ----------------------------------------------------------------------
109 # order_value_periodicity=y, periodicity=q
110 make_sales_order(
111   periodic_invoices_config  => {
112     periodicity             => 'm',
113     order_value_periodicity => 'y',
114     start_date              => DateTime->from_kivitendo('01.05.2014'),
115   });
116
117 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 $_")
118   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
119
120 is_deeply($ctrl->data->{sales_orders},
121           { 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 },
122           "periodic conf p=m ovp=y, no invoices, data for sales_orders");
123 is_deeply($ctrl->data->{sales_orders_per_inv},
124           { months => [ 0, 0, 333.33, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 333.33, 0, 0, 0 ], year => 333.33 },
125           "periodic conf p=m ovp=y, no invoices, data for sales_orders_per_inv");
126
127 # ----------------------------------------------------------------------
128 # order_value_periodicity=y, periodicity=q, starting in previous year
129 make_sales_order(
130   order                     => {
131     transdate               => DateTime->from_kivitendo('01.03.2013'),
132   },
133   periodic_invoices_config  => {
134     periodicity             => 'q',
135     order_value_periodicity => 'y',
136     start_date              => DateTime->from_kivitendo('01.05.2013'),
137   });
138
139 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 $_")
140   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
141
142 is_deeply($ctrl->data->{sales_orders},
143           { 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 },
144           "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders");
145 is_deeply($ctrl->data->{sales_orders_per_inv},
146           { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
147           "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders_per_inv");
148
149 # ----------------------------------------------------------------------
150 # order_value_periodicity=y, periodicity=q, starting in previous year, ending middle of year
151 make_sales_order(
152   order                     => {
153     transdate               => DateTime->from_kivitendo('01.03.2013'),
154   },
155   periodic_invoices_config  => {
156     periodicity             => 'q',
157     order_value_periodicity => 'y',
158     start_date              => DateTime->from_kivitendo('01.05.2013'),
159     end_date                => DateTime->from_kivitendo('01.09.2014'),
160     terminated              => 1,
161   });
162
163 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 $_")
164   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
165
166 is_deeply($ctrl->data->{sales_orders},
167           { 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 },
168           "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders");
169 is_deeply($ctrl->data->{sales_orders_per_inv},
170           { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
171           "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders_per_inv");
172
173 # ----------------------------------------------------------------------
174 # order_value_periodicity=y, periodicity=q, starting and ending before current
175 make_sales_order(
176   order                     => {
177     transdate               => DateTime->from_kivitendo('01.03.2012'),
178   },
179   periodic_invoices_config  => {
180     periodicity             => 'q',
181     order_value_periodicity => 'y',
182     start_date              => DateTime->from_kivitendo('01.05.2012'),
183     end_date                => DateTime->from_kivitendo('01.09.2013'),
184     terminated              => 1,
185   });
186
187 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 $_")
188   for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_orders sales_orders_per_inv sales_quotations);
189
190 clear_up();
191
192 done_testing();