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 => 49;
24 use Support::TestSetup;
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';
36 Support::TestSetup::login();
38 our ($ar_chart, $buchungsgruppe, $ctrl, $currency_id, $customer, $employee, $order, $part, $tax_zone, $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 $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";
53 sub create_sales_order {
56 $params{$_} ||= {} for qw(customer part tax order orderitem);
58 # Clean up: remove invoices, orders, parts and customers
61 $customer = SL::DB::Customer->new(
62 name => 'Test Customer',
63 currency_id => $currency_id,
64 taxzone_id => $tax_zone->id,
65 %{ $params{customer} }
68 $part = SL::DB::Part->new(
69 partnumber => 'T4254',
70 description => 'Fourty-two fifty-four',
73 buchungsgruppen_id => $buchungsgruppe->id,
79 $order = SL::DB::Order->new(
80 customer_id => $customer->id,
81 currency_id => $currency_id,
82 taxzone_id => $tax_zone->id,
83 transaction_description => '<%period_start_date%>',
84 transdate => DateTime->from_kivitendo('01.03.2014'),
86 { parts_id => $part->id,
87 description => $part->description,
88 lastcost => $part->lastcost,
89 sellprice => $part->sellprice,
92 %{ $params{orderitem} },
95 periodic_invoices_config => $params{periodic_invoices_config} ? {
97 ar_chart_id => $ar_chart->id,
98 %{ $params{periodic_invoices_config} },
103 $order->calculate_prices_and_taxes;
105 ok($order->save(cascade => 1));
107 $::form = Form->new('');
108 $::form->{year} = 2014;
109 $ctrl = SL::Controller::FinancialOverview->new;
112 $ctrl->calculate_one_time_data;
113 $ctrl->calculate_periodic_invoices;
118 # ----------------------------------------------------------------------
119 # An order without periodic invoices:
120 create_sales_order();
122 is_deeply($ctrl->data->{$_}, { months => [ (0) x 12 ], quarters => [ 0, 0, 0, 0 ], year => 0 }, "no periodic invoices, data for $_")
123 for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
125 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 $_")
126 for qw(sales_orders sales_orders_per_inv);
128 # ----------------------------------------------------------------------
129 # order_value_periodicity=y, periodicity=q
131 periodic_invoices_config => {
133 order_value_periodicity => 'y',
134 start_date => DateTime->from_kivitendo('01.05.2014'),
137 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 $_")
138 for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
140 is_deeply($ctrl->data->{sales_orders},
141 { 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 },
142 "periodic conf p=m ovp=y, no invoices, data for sales_orders");
143 is_deeply($ctrl->data->{sales_orders_per_inv},
144 { months => [ 0, 0, 333.33, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 333.33, 0, 0, 0 ], year => 333.33 },
145 "periodic conf p=m ovp=y, no invoices, data for sales_orders_per_inv");
147 # ----------------------------------------------------------------------
148 # order_value_periodicity=y, periodicity=q, starting in previous 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'),
159 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 $_")
160 for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
162 is_deeply($ctrl->data->{sales_orders},
163 { 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 },
164 "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders");
165 is_deeply($ctrl->data->{sales_orders_per_inv},
166 { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
167 "periodic conf p=q ovp=y, no invoices, starting previous year, data for sales_orders_per_inv");
169 # ----------------------------------------------------------------------
170 # order_value_periodicity=y, periodicity=q, starting in previous year, ending middle of year
173 transdate => DateTime->from_kivitendo('01.03.2013'),
175 periodic_invoices_config => {
177 order_value_periodicity => 'y',
178 start_date => DateTime->from_kivitendo('01.05.2013'),
179 end_date => DateTime->from_kivitendo('01.09.2014'),
183 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 $_")
184 for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_quotations);
186 is_deeply($ctrl->data->{sales_orders},
187 { 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 },
188 "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders");
189 is_deeply($ctrl->data->{sales_orders_per_inv},
190 { months => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], quarters => [ 0, 0, 0, 0 ], year => 0 },
191 "periodic conf p=q ovp=y, no invoices, starting previous year, ending middle of year, data for sales_orders_per_inv");
193 # ----------------------------------------------------------------------
194 # order_value_periodicity=y, periodicity=q, starting and ending before current
197 transdate => DateTime->from_kivitendo('01.03.2012'),
199 periodic_invoices_config => {
201 order_value_periodicity => 'y',
202 start_date => DateTime->from_kivitendo('01.05.2012'),
203 end_date => DateTime->from_kivitendo('01.09.2013'),
207 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 $_")
208 for qw(purchase_invoices purchase_orders requests_for_quotation sales_invoices sales_orders sales_orders_per_inv sales_quotations);