1 package SL::DB::PeriodicInvoicesConfig;
 
   5 use SL::DB::MetaSetup::PeriodicInvoicesConfig;
 
   7 __PACKAGE__->meta->initialize;
 
   9 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
 
  10 __PACKAGE__->meta->make_manager_class;
 
  12 our @PERIODICITIES  = qw(m q f b y);
 
  13 our %PERIOD_LENGTHS = ( m => 1, q => 3, f => 4, b => 6, y => 12 );
 
  15 sub get_period_length {
 
  17   return $PERIOD_LENGTHS{ $self->periodicity } || 1;
 
  21   $::lxdebug->message(LXDebug->DEBUG1(), join('', @_));
 
  24 sub handle_automatic_extension {
 
  27   _log_msg("HAE for " . $self->id . "\n");
 
  28   # Don't extend configs that have been terminated. There's nothing to
 
  29   # extend if there's no end date.
 
  30   return if $self->terminated || !$self->end_date;
 
  32   my $today    = DateTime->now_local;
 
  33   my $end_date = $self->end_date;
 
  35   _log_msg("today $today end_date $end_date\n");
 
  37   # The end date has not been reached yet, therefore no extension is
 
  39   return if $today <= $end_date;
 
  41   # The end date has been reached. If no automatic extension has been
 
  42   # set then terminate the config and return.
 
  43   if (!$self->extend_automatically_by) {
 
  44     _log_msg("setting inactive\n");
 
  50   # Add the automatic extension period to the new end date as long as
 
  51   # the new end date is in the past. Then save it and get out.
 
  52   $end_date->add(months => $self->extend_automatically_by) while $today > $end_date;
 
  53   _log_msg("new end date $end_date\n");
 
  55   $self->end_date($end_date);
 
  61 sub get_previous_invoice_date {
 
  65     SELECT MAX(ar.transdate)
 
  66     FROM periodic_invoices
 
  67     LEFT JOIN ar ON (ar.id = periodic_invoices.ar_id)
 
  68     WHERE periodic_invoices.config_id = ?
 
  71   my ($max_transdate) = $self->dbh->selectrow_array($query, undef, $self->id);
 
  73   return undef unless $max_transdate;
 
  74   return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate);