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;
 
  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);
 
  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';
 
  38 Support::TestSetup::login();
 
  40 our ($ar_chart, $ctrl, $customer, $order, $part, $unit, @invoices);
 
  43   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(InvoiceItem Invoice OrderItem Order Customer Part);
 
  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";
 
  51 sub make_sales_order {
 
  54   $params{$_} ||= {} for qw(customer part order orderitem);
 
  56   # Clean up: remove invoices, orders, parts and customers
 
  59   $customer     = new_customer(
 
  60     name        => 'Test Customer',
 
  61     %{ $params{customer} }
 
  65     partnumber         => 'T4254',
 
  66     description        => 'Fourty-two fifty-four',
 
  73   $order                     = create_sales_order(
 
  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} ? {
 
  81       ar_chart_id            => $ar_chart->id,
 
  82       %{ $params{periodic_invoices_config} },
 
  87   $::form         = Support::TestSetup->create_new_form;
 
  88   $::form->{year} = 2014;
 
  89   $ctrl           = SL::Controller::FinancialOverview->new;
 
  92   $ctrl->calculate_one_time_data;
 
  93   $ctrl->calculate_periodic_invoices;
 
  98 # ----------------------------------------------------------------------
 
  99 # An order without periodic invoices:
 
 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);
 
 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);
 
 108 # ----------------------------------------------------------------------
 
 109 # order_value_periodicity=y, periodicity=q
 
 111   periodic_invoices_config  => {
 
 113     order_value_periodicity => 'y',
 
 114     start_date              => DateTime->from_kivitendo('01.05.2014'),
 
 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);
 
 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");
 
 127 # ----------------------------------------------------------------------
 
 128 # order_value_periodicity=y, periodicity=q, starting in previous year
 
 131     transdate               => DateTime->from_kivitendo('01.03.2013'),
 
 133   periodic_invoices_config  => {
 
 135     order_value_periodicity => 'y',
 
 136     start_date              => DateTime->from_kivitendo('01.05.2013'),
 
 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);
 
 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");
 
 149 # ----------------------------------------------------------------------
 
 150 # order_value_periodicity=y, periodicity=q, starting in previous year, ending middle of year
 
 153     transdate               => DateTime->from_kivitendo('01.03.2013'),
 
 155   periodic_invoices_config  => {
 
 157     order_value_periodicity => 'y',
 
 158     start_date              => DateTime->from_kivitendo('01.05.2013'),
 
 159     end_date                => DateTime->from_kivitendo('01.09.2014'),
 
 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);
 
 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");
 
 173 # ----------------------------------------------------------------------
 
 174 # order_value_periodicity=y, periodicity=q, starting and ending before current
 
 177     transdate               => DateTime->from_kivitendo('01.03.2012'),
 
 179   periodic_invoices_config  => {
 
 181     order_value_periodicity => 'y',
 
 182     start_date              => DateTime->from_kivitendo('01.05.2012'),
 
 183     end_date                => DateTime->from_kivitendo('01.09.2013'),
 
 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);