fix RT per-transaction recipient squelching, RT#25260
[freeside.git] / FS / FS / part_pkg.pm
index c9b2411..332bb62 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
 use Carp qw(carp cluck confess);
 use Scalar::Util qw( blessed );
-use Time::Local qw( timelocal_nocheck );
+use Time::Local qw( timelocal timelocal_nocheck );
 use Tie::IxHash;
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs dbh dbdef );
@@ -106,6 +106,8 @@ inherits from FS::Record.  The following fields are currently supported:
 =item fcc_voip_class - Which column of FCC form 477 part II.B this package 
 belongs in.
 
+=item delay_start - Number of days to delay package start, by default
+
 =back
 
 =head1 METHODS
@@ -577,6 +579,7 @@ sub check {
        )
     || $self->ut_numbern('fcc_ds0s')
     || $self->ut_numbern('fcc_voip_class')
+    || $self->ut_numbern('delay_start')
     || $self->SUPER::check
   ;
   return $error if $error;
@@ -866,10 +869,23 @@ sub is_free {
   }
 }
 
+# whether the plan allows discounts to be applied to this package
 sub can_discount { 0; }
 
+# whether the plan allows changing the start date
 sub can_start_date { 1; }
 
+# the delay start date if present
+sub delay_start_date {
+  my $self = shift;
+
+  my $delay = $self->delay_start or return '';
+    
+  my ($mday,$mon,$year) = (localtime(time))[3,4,5];
+  timelocal(0,0,0,$mday,$mon,$year) + 86400 * $delay;
+
+}
+
 sub freqs_href {
   # moved to FS::Misc to make this accessible to other packages
   # at initialization
@@ -927,6 +943,9 @@ sub add_freq {
   if ( $freq =~ /^\d+$/ ) {
     $mon += $freq;
     until ( $mon < 12 ) { $mon -= 12; $year++; }
+
+    $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
+
   } elsif ( $freq =~ /^(\d+)w$/ ) {
     my $weeks = $1;
     $mday += $weeks * 7;
@@ -1318,6 +1337,29 @@ sub recur_cost_permonth {
   sprintf('%.2f', $self->recur_cost / $self->freq );
 }
 
+=item cust_bill_pkg_recur CUST_PKG
+
+Actual recurring charge for the specified customer package from customer's most
+recent invoice
+
+=cut
+
+sub cust_bill_pkg_recur {
+  my($self, $cust_pkg) = @_;
+  my $cust_bill_pkg = qsearchs({
+    'table'     => 'cust_bill_pkg',
+    'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
+    'hashref'   => { 'pkgnum' => $cust_pkg->pkgnum,
+                     'recur'  => { op=>'>', value=>'0' },
+                   },
+    'order_by'  => 'ORDER BY cust_bill._date     DESC,
+                             cust_bill_pkg.sdate DESC
+                     LIMIT 1
+                   ',
+  }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
+  $cust_bill_pkg->recur;
+}
+
 =item format OPTION DATA
 
 Returns data formatted according to the function 'format' described
@@ -1516,7 +1558,7 @@ sub _upgrade_data { # class method
     );
     if ( $bad_upgrade ) {
       my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
-                  ' AND  history_date >  '.($bad_upgrade->_date - 600);
+                  ' AND  history_date >  '.($bad_upgrade->_date - 3600);
       my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
         qsearch({
           'select'    => '*',