3 use SL::Helper::DateTime;
5 no warnings 'redefine';
8 return shift->new(time_zone => $::locale->get_local_time_zone, year => 2014, month => 3, day => 15, hour => 12, minute => 23, second => 34);
12 return shift->now_local->truncate(to => 'day');
17 use Test::More tests => 43;
24 use Support::TestSetup;
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';
36 Support::TestSetup::login();
38 our ($ar_chart, $ctrl, $customer, $order, $part, $unit, @invoices);
41 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(InvoiceItem Invoice OrderItem Order Customer Part);
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";
49 sub create_sales_order {
52 $params{$_} ||= {} for qw(customer part order orderitem);
54 # Clean up: remove invoices, orders, parts and customers
57 $customer = SL::Dev::CustomerVendor::create_customer(
58 name => 'Test Customer',
59 %{ $params{customer} }
62 $part = SL::Dev::Part::create_part(
63 partnumber => 'T4254',
64 description => 'Fourty-two fifty-four',
71 $order = SL::Dev::Record::create_sales_order(
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} ? {
79 ar_chart_id => $ar_chart->id,
80 %{ $params{periodic_invoices_config} },
85 $::form = Support::TestSetup->create_new_form;
86 $::form->{year} = 2014;
87 $ctrl = SL::Controller::FinancialOverview->new;
90 $ctrl->calculate_one_time_data;
91 $ctrl->calculate_periodic_invoices;
96 # ----------------------------------------------------------------------
97 # An order without periodic invoices:
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);
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);
106 # ----------------------------------------------------------------------
107 # order_value_periodicity=y, periodicity=q
109 periodic_invoices_config => {
111 order_value_periodicity => 'y',
112 start_date => DateTime->from_kivitendo('01.05.2014'),
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);
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");
125 # ----------------------------------------------------------------------
126 # order_value_periodicity=y, periodicity=q, starting in previous year
129 transdate => DateTime->from_kivitendo('01.03.2013'),
131 periodic_invoices_config => {
133 order_value_periodicity => 'y',
134 start_date => DateTime->from_kivitendo('01.05.2013'),
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);
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");
147 # ----------------------------------------------------------------------
148 # order_value_periodicity=y, periodicity=q, starting in previous year, ending middle of year
151 transdate => DateTime->from_kivitendo('01.03.2013'),
153 periodic_invoices_config => {
155 order_value_periodicity => 'y',
156 start_date => DateTime->from_kivitendo('01.05.2013'),
157 end_date => DateTime->from_kivitendo('01.09.2014'),
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);
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");
171 # ----------------------------------------------------------------------
172 # order_value_periodicity=y, periodicity=q, starting and ending before current
175 transdate => DateTime->from_kivitendo('01.03.2012'),
177 periodic_invoices_config => {
179 order_value_periodicity => 'y',
180 start_date => DateTime->from_kivitendo('01.05.2012'),
181 end_date => DateTime->from_kivitendo('01.09.2013'),
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);