more accurate calculation of package start delays, #20686
[freeside.git] / FS / FS / part_pkg.pm
index 0722647..9e3b67e 100644 (file)
@@ -5,7 +5,8 @@ use strict;
 use vars qw( %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
 use Carp qw(carp cluck confess);
 use Scalar::Util qw( blessed );
-use Time::Local qw( timelocal timelocal_nocheck );
+use DateTime;
+use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
 use Tie::IxHash;
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs dbh dbdef );
@@ -367,7 +368,9 @@ and I<options>
 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
 values, the appropriate FS::pkg_svc records will be replaced.  I<hidden_svc>
 can be set to a hashref of svcparts and flag values ('Y' or '') to set the 
-'hidden' field in these records.
+'hidden' field in these records.  I<bulk_skip> can be set to a hashref of
+svcparts and flag values ('Y' or '') to set the 'bulk_skip' field in those
+records.
 
 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
 FS::pkg_svc record will be updated.
@@ -504,10 +507,12 @@ sub replace {
   warn "  replacing pkg_svc records" if $DEBUG;
   my $pkg_svc = $options->{'pkg_svc'};
   my $hidden_svc = $options->{'hidden_svc'} || {};
+  my $bulk_skip  = $options->{'bulk_skip'} || {};
   if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
     foreach my $part_svc ( qsearch('part_svc', {} ) ) {
-      my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
-      my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
+      my $quantity  = $pkg_svc->{$part_svc->svcpart} || 0;
+      my $hidden    = $hidden_svc->{$part_svc->svcpart} || '';
+      my $bulk_skip = $bulk_skip->{$part_svc->svcpart} || '';
       my $primary_svc =
         ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
           && $options->{'primary_svc'} == $part_svc->svcpart
@@ -523,16 +528,19 @@ sub replace {
       my $old_quantity = 0;
       my $old_primary_svc = '';
       my $old_hidden = '';
+      my $old_bulk_skip = '';
       if ( $old_pkg_svc ) {
         $old_quantity = $old_pkg_svc->quantity;
         $old_primary_svc = $old_pkg_svc->primary_svc 
           if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
         $old_hidden = $old_pkg_svc->hidden;
+        $old_bulk_skip = $old_pkg_svc->old_bulk_skip;
       }
    
-      next unless $old_quantity != $quantity || 
-                  $old_primary_svc ne $primary_svc ||
-                  $old_hidden ne $hidden;
+      next unless $old_quantity    != $quantity
+               || $old_primary_svc ne $primary_svc
+               || $old_hidden      ne $hidden
+               || $old_bulk_skip   ne $bulk_skip;
     
       my $new_pkg_svc = new FS::pkg_svc( {
         'pkgsvcnum'   => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
@@ -541,6 +549,7 @@ sub replace {
         'quantity'    => $quantity, 
         'primary_svc' => $primary_svc,
         'hidden'      => $hidden,
+        'bulk_skip'   => $bulk_skip,
       } );
       my $error = $old_pkg_svc
                     ? $new_pkg_svc->replace($old_pkg_svc)
@@ -1081,32 +1090,16 @@ sub can_discount { 0; }
 # whether the plan allows changing the start date
 sub can_start_date { 1; }
   
-# the default start date; takes an FS::cust_main as an argument
-sub default_start_date {
+# the delay start date if present
+sub delay_start_date {
   my $self = shift;
-  my $cust_main = shift;
-  my $conf = FS::Conf->new;
-
-  if ( $self->delay_start ) {
-    my $delay = $self->delay_start;
-    
-    my ($mday,$mon,$year) = (localtime(time))[3,4,5];
-    my $start_date = timelocal(0,0,0,$mday,$mon,$year) + 86400 * $delay;
-    return $start_date;
 
-  } elsif ( $conf->exists('order_pkg-no_start_date') ) {
+  my $delay = $self->delay_start or return '';
 
-    return '';
-
-  } elsif ( $cust_main ) {
-    
-    return $cust_main->next_bill_date;
-  
-  } else {
-    
-    return '';
-
-  }
+  # avoid timelocal silliness  
+  my $dt = DateTime->today(time_zone => 'local');
+  $dt->add(days => $delay);
+  $dt->epoch;
 }
 
 sub can_currency_exchange { 0; }