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