a package that starts on the 1st and expires after N months, RT#7738
authorivan <ivan>
Tue, 9 Mar 2010 02:43:41 +0000 (02:43 +0000)
committerivan <ivan>
Tue, 9 Mar 2010 02:43:41 +0000 (02:43 +0000)
FS/FS/cust_pkg.pm
FS/FS/part_pkg/flat.pm

index 8415d62..e553aa3 100644 (file)
@@ -6,6 +6,7 @@ use Carp qw(cluck);
 use Scalar::Util qw( blessed );
 use List::Util qw(max);
 use Tie::IxHash;
+use Time::Local qw( timelocal_nocheck );
 use MIME::Entity;
 use FS::UID qw( getotaker dbh );
 use FS::Misc qw( send_email );
@@ -529,6 +530,26 @@ sub check {
 
   }
 
+  if ( $self->part_pkg->option('start_1st') && !$self->start_date ) {
+    my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
+    $mon += 1 unless $mday == 1;
+    until ( $mon < 12 ) { $mon -= 12; $year++; }
+    $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
+  }
+
+  my $expire_months = $self->part_pkg->option('expire_months');
+  if ( $expire_months && !$self->expire ) {
+    my $start = $self->start_date || $self->setup || time;
+
+    #false laziness w/part_pkg::add_freq
+    my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($start) )[0,1,2,3,4,5];
+    $mon += $expire_months;
+    until ( $mon < 12 ) { $mon -= 12; $year++; }
+
+    #$self->expire( timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year) );
+    $self->expire( timelocal_nocheck(0,0,0,$mday,$mon,$year) );
+  }
+
   $self->otaker(getotaker) unless $self->otaker;
   $self->otaker =~ /^(\w{1,32})$/ or return "Illegal otaker";
   $self->otaker($1);
index f9aaebe..be17fd8 100644 (file)
@@ -104,19 +104,30 @@ tie my %temporalities, 'Tie::IxHash',
                              'type' => 'select',
                              'select_options' => \%temporalities,
                            },
-
-    %usage_fields,
-    %usage_recharge_fields,
-
     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
                                    ' of service at cancellation',
                          'type' => 'checkbox',
                        },
+
+    #used in cust_pkg.pm so could add to any price plan
+    'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
+                       },
+    #used in cust_pkg.pm so could add to any price plan where it made sense
+    'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
+                         'type' => 'checkbox',
+                       },
+
+    %usage_fields,
+    %usage_recharge_fields,
+
     'externalid' => { 'name'   => 'Optional External ID',
                       'default' => '',
                     },
   },
-  'fieldorder' => [ qw( setup_fee recur_fee recur_temporality unused_credit ),
+  'fieldorder' => [ qw( setup_fee recur_fee
+                        recur_temporality unused_credit
+                        expire_months start_1st
+                      ),
                     @usage_fieldorder, @usage_recharge_fieldorder,
                     qw( externalid ),
                   ],