RT#40641: unprovisioning preserved cancelled services
[freeside.git] / FS / FS / part_pkg / delayed_Mixin.pm
1 package FS::part_pkg::delayed_Mixin;
2
3 use strict;
4 use vars qw(%info);
5 use Time::Local qw(timelocal);
6 use NEXT;
7
8 %info = (
9   'disabled' => 1,
10   'fields' => {
11     'free_days' => { 'name' => 'Initial free days',
12                      'default' => 0,
13                    },
14     'delay_setup' => { 'name' => 'Delay setup fee in addition to recurring fee',
15                        'type' => 'checkbox',
16                      },
17     'recur_notify' => { 'name' => 'Number of days before recurring billing'.
18                                   ' commences to notify customer. (0 means'.
19                                   ' no warning)',
20                      'default' => 0,
21                     },
22   },
23   'fieldorder' => [ 'free_days', 'delay_setup', 'recur_notify', ],
24 );
25
26 sub calc_setup {
27   my $self = shift;
28   my( $cust_pkg, $time ) = @_;
29
30   unless ( $self->option('delay_setup', 1) ) {
31     my $d = $cust_pkg->bill || $time;
32     $d += 86400 * $self->option('free_days');
33     $cust_pkg->bill($d);
34   }
35   
36   $self->NEXT::calc_setup(@_);
37 }
38
39 sub calc_remain {
40   my ($self, $cust_pkg, %options) = @_;
41
42   unless ( $self->option('delay_setup', 1) ) {
43     my $last_bill = $cust_pkg->last_bill || 0;
44     my $next_bill = $cust_pkg->getfield('bill') || 0;
45     my $free_days = $self->option('free_days');
46
47     return 0 if    $last_bill + (86400 * $free_days) == $next_bill
48                 && $last_bill == $cust_pkg->setup;
49   }
50
51   return $self->NEXT::calc_remain($cust_pkg, %options);
52 }
53
54 sub can_start_date {
55   my $self = shift;
56   ! $self->option('delay_setup', 1) && $self->NEXT::can_start_date(@_);
57 }
58
59 sub default_start_date {
60   my $self = shift;
61   if ( $self->option('delay_setup') and $self->option('free_days') ) {
62     my $delay = $self->option('free_days');
63
64     my ($mday, $mon, $year) = (localtime(time))[3,4,5];
65     return timelocal(0,0,0,$mday,$mon,$year) + 86400 * $self->option('free_days');
66   }
67   return $self->NEXT::default_start_date(@_);
68 }
69
70 1;