fix setup fee discounts w/intro period price plans, RT#21063
[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 NEXT;
6
7 %info = (
8   'disabled' => 1,
9   'fields' => {
10     'free_days' => { 'name' => 'Initial free days',
11                      'default' => 0,
12                    },
13     'delay_setup' => { 'name' => 'Delay setup fee in addition to recurring fee',
14                        'type' => 'checkbox',
15                      },
16     'recur_notify' => { 'name' => 'Number of days before recurring billing'.
17                                   ' commences to notify customer. (0 means'.
18                                   ' no warning)',
19                      'default' => 0,
20                     },
21   },
22   'fieldorder' => [ 'free_days', 'delay_setup', 'recur_notify', ],
23 );
24
25 sub calc_setup {
26   my $self = shift;
27   my( $cust_pkg, $time ) = @_;
28
29   unless ( $self->option('delay_setup', 1) ) {
30     my $d = $cust_pkg->bill || $time;
31     $d += 86400 * $self->option('free_days');
32     $cust_pkg->bill($d);
33   }
34   
35   $self->NEXT::calc_setup(@_);
36 }
37
38 sub calc_remain {
39   my ($self, $cust_pkg, %options) = @_;
40
41   unless ( $self->option('delay_setup', 1) ) {
42     my $last_bill = $cust_pkg->last_bill || 0;
43     my $next_bill = $cust_pkg->getfield('bill') || 0;
44     my $free_days = $self->option('free_days');
45
46     return 0 if    $last_bill + (86400 * $free_days) == $next_bill
47                 && $last_bill == $cust_pkg->setup;
48   }
49
50   return $self->NEXT::calc_remain($cust_pkg, %options);
51 }
52
53 sub can_start_date { ! shift->option('delay_setup', 1) }
54
55 1;