delay setup checkbox, RT#13783
authorivan <ivan>
Wed, 10 Aug 2011 23:43:49 +0000 (23:43 +0000)
committerivan <ivan>
Wed, 10 Aug 2011 23:43:49 +0000 (23:43 +0000)
FS/FS/cust_pkg.pm
FS/FS/part_pkg/delayed_Mixin.pm [new file with mode: 0644]
FS/FS/part_pkg/flat_delayed.pm
FS/FS/part_pkg/prorate_delayed.pm

index 08e8b2b..7405617 100644 (file)
@@ -8,7 +8,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 Time::Local qw( timelocal timelocal_nocheck );
 use MIME::Entity;
 use FS::UID qw( getotaker dbh );
 use FS::Misc qw( send_email );
@@ -266,7 +266,9 @@ sub insert {
   my $error = $self->check_pkgpart;
   return $error if $error;
 
-  if ( $self->part_pkg->option('start_1st', 1) && !$self->start_date ) {
+  my $part_pkg = $self->part_pkg;
+
+  if ( $part_pkg->option('start_1st', 1) && !$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++; }
@@ -274,13 +276,21 @@ sub insert {
   }
 
   foreach my $action ( qw(expire adjourn contract_end) ) {
-    my $months = $self->part_pkg->option("${action}_months",1);
+    my $months = $part_pkg->option("${action}_months",1);
     if($months and !$self->$action) {
       my $start = $self->start_date || $self->setup || time;
-      $self->$action( $self->part_pkg->add_freq($start, $months) );
+      $self->$action( $part_pkg->add_freq($start, $months) );
     }
   }
 
+  my $free_days = $part_pkg->option('free_days');
+  if ( $free_days && $part_pkg->option('delay_setup') ) { #&& !$self->start_date
+    my ($mday,$mon,$year) = (localtime(time) )[3,4,5];
+    #my $start_date = ($self->start_date || timelocal(0,0,0,$mday,$mon,$year)) + 86400 * $free_days;
+    my $start_date = timelocal(0,0,0,$mday,$mon,$year) + 86400 * $free_days;
+    $self->start_date($start_date);
+  }
+
   $self->order_date(time);
 
   local $SIG{HUP} = 'IGNORE';
diff --git a/FS/FS/part_pkg/delayed_Mixin.pm b/FS/FS/part_pkg/delayed_Mixin.pm
new file mode 100644 (file)
index 0000000..027cfff
--- /dev/null
@@ -0,0 +1,52 @@
+package FS::part_pkg::delayed_Mixin;
+use base qw( FS::part_pkg );
+
+use strict;
+use vars qw(%info);
+
+%info = (
+  'disabled' => 1,
+  'fields' => {
+    'free_days' => { 'name' => 'Initial free days',
+                     'default' => 0,
+                   },
+    'delay_setup' => { 'name' => 'Delay setup fee in addition to recurring fee',
+                       'type' => 'checkbox',
+                     },
+    'recur_notify' => { 'name' => 'Number of days before recurring billing'.
+                                  ' commences to notify customer. (0 means'.
+                                  ' no warning)',
+                     'default' => 0,
+                    },
+  },
+  'fieldorder' => [ 'free_days', 'delay_setup', 'recur_notify', ],
+);
+
+sub calc_setup {
+  my($self, $cust_pkg, $time ) = @_;
+
+  unless ( $self->option('delay_setup') ) {
+    my $d = $cust_pkg->bill || $time;
+    $d += 86400 * $self->option('free_days');
+    $cust_pkg->bill($d);
+  }
+  
+  $self->option('setup_fee');
+}
+
+sub calc_remain {
+  my ($self, $cust_pkg, %options) = @_;
+
+  unless ( $self->option('delay_setup') ) {
+    my $last_bill = $cust_pkg->last_bill || 0;
+    my $next_bill = $cust_pkg->getfield('bill') || 0;
+    my $free_days = $self->option('free_days');
+
+    return 0 if    $last_bill + (86400 * $free_days) == $next_bill
+                && $last_bill == $cust_pkg->setup;
+  }
+
+  return $self->SUPER::calc_remain($cust_pkg, %options);
+}
+
+1;
index b4be72b..f50557d 100644 (file)
@@ -1,54 +1,22 @@
 package FS::part_pkg::flat_delayed;
+use base qw(FS::part_pkg::delayed_Mixin FS::part_pkg::flat );
 
 use strict;
-use vars qw(@ISA %info);
-#use FS::Record qw(qsearch qsearchs);
-use FS::part_pkg::flat;
-
-@ISA = qw(FS::part_pkg::flat);
+use vars qw(%info);
 
 %info = (
   'name' => 'Free (or setup fee) for X days, then flat rate'.
             ' (anniversary billing)',
   'shortname' => 'Anniversary, with intro period',
-  'inherit_fields' => [ 'global_Mixin' ],
+  'inherit_fields' => [qw( global_Mixin delayed_Mixin )],
   'fields' =>  {
-    'free_days' => { 'name' => 'Initial free days',
-                     'default' => 0,
-                   },
-    'recur_notify' => { 'name' => 'Number of days before recurring billing'.
-                                  ' commences to notify customer. (0 means'.
-                                  ' no warning)',
-                     'default' => 0,
-                    },
+    #shouldn't this be inherited from somewhere?
+    'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
+                        'type' => 'checkbox',
+                      },
       },
-  'fieldorder' => [ 'free_days', 'recur_notify',
-                  ],
-  #'setup' => '\'my $d = $cust_pkg->bill || $time; $d += 86400 * \' + what.free_days.value + \'; $cust_pkg->bill($d); $cust_pkg_mod_flag=1; \' + what.setup_fee.value',
-  #'recur' => 'what.recur_fee.value',
+  'fieldorder' => [ 'suspend_bill', ],
   'weight' => 12,
 );
 
-sub calc_setup {
-  my($self, $cust_pkg, $time ) = @_;
-
-  my $d = $cust_pkg->bill || $time;
-  $d += 86400 * $self->option('free_days');
-  $cust_pkg->bill($d);
-  
-  $self->option('setup_fee');
-}
-
-sub calc_remain {
-  my ($self, $cust_pkg, %options) = @_;
-  my $next_bill = $cust_pkg->getfield('bill') || 0;
-  my $last_bill = $cust_pkg->last_bill || 0;
-  my $free_days = $self->option('free_days');
-
-  return 0 if    $last_bill + (86400 * $free_days) == $next_bill
-              && $last_bill == $cust_pkg->setup;
-
-  return $self->SUPER::calc_remain($cust_pkg, %options);
-}
-
 1;
index 8ea64a8..f0a4f94 100644 (file)
@@ -1,56 +1,22 @@
 package FS::part_pkg::prorate_delayed;
+use base qw( FS::part_pkg::delayed_Mixin FS::part_pkg::prorate );
 
 use strict;
-use vars qw(@ISA %info);
-#use FS::Record qw(qsearch qsearchs);
-use FS::part_pkg;
-
-@ISA = qw(FS::part_pkg::prorate);
+use vars qw(%info);
 
 %info = (
   'name' => 'Free (or setup fee) for X days, then prorate, then flat-rate ' .
          '(1st of month billing)',
   'shortname' => 'Prorate (Nth of month billing), with intro period', #??
-  'inherit_fields' => [ 'global_Mixin' ],
+  'inherit_fields' => [qw( global_Mixin delayed_Mixin )],
   'fields' =>  {
-    'free_days' => { 'name' => 'Initial free days',
-                  'default' => 0,
-                    },
-    'recur_notify' => { 'name' => 'Number of days before recurring billing'.
-                                  ' commences to notify customer. (0 means'.
-                                  ' no warning)',
-                     'default' => 0,
-                    },
+    #shouldn't this be inherited from somewhere?
     'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
                         'type' => 'checkbox',
                       },
   },
-  'fieldorder' => [ 'free_days', 'recur_notify', 'suspend_bill', ],
-  #'setup' => '\'my $d = $cust_pkg->bill || $time; $d += 86400 * \' + what.free_days.value + \'; $cust_pkg->bill($d); $cust_pkg_mod_flag=1; \' + what.setup_fee.value',
-  #'recur' => 'what.recur_fee.value',
+  'fieldorder' => [ 'suspend_bill', ],
   'weight' => 22,
 );
 
-sub calc_setup {
-  my($self, $cust_pkg, $time ) = @_;
-
-  my $d = $cust_pkg->bill || $time;
-  $d += 86400 * $self->option('free_days');
-  $cust_pkg->bill($d);
-  
-  $self->option('setup_fee');
-}
-
-sub calc_remain {
-  my ($self, $cust_pkg, %options) = @_;
-  my $last_bill = $cust_pkg->last_bill || 0;
-  my $next_bill = $cust_pkg->getfield('bill') || 0;
-  my $free_days = $self->option('free_days');
-
-  return 0 if    $last_bill + (86400 * $free_days) == $next_bill
-              && $last_bill == $cust_pkg->setup;
-
-  return $self->SUPER::calc_remain($cust_pkg, %options);
-}
-
 1;