fix cancellation errors with updated flat_introrate, RT#5865
[freeside.git] / FS / FS / part_pkg / flat_delayed.pm
1 package FS::part_pkg::flat_delayed;
2
3 use strict;
4 use vars qw(@ISA %info);
5 #use FS::Record qw(qsearch qsearchs);
6 use FS::part_pkg::flat;
7
8 @ISA = qw(FS::part_pkg::flat);
9
10 %info = (
11   'name' => 'Free (or setup fee) for X days, then flat rate'.
12             ' (anniversary billing)',
13   'shortname' => 'Anniversary, with intro period',
14   'fields' =>  {
15     'setup_fee' => { 'name' => 'Setup fee for this package',
16                      'default' => 0,
17                    },
18     'free_days' => { 'name' => 'Initial free days',
19                      'default' => 0,
20                    },
21     'recur_fee' => { 'name' => 'Recurring fee for this package',
22                      'default' => 0,
23                     },
24     'recur_notify' => { 'name' => 'Number of days before recurring billing'.
25                                   ' commences to notify customer. (0 means'.
26                                   ' no warning)',
27                      'default' => 0,
28                     },
29     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
30                                    ' of service at cancellation',
31                          'type' => 'checkbox',
32                        },
33   },
34   'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee', 'recur_notify',
35                     'unused_credit'
36                   ],
37   #'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',
38   #'recur' => 'what.recur_fee.value',
39   'weight' => 12,
40 );
41
42 sub calc_setup {
43   my($self, $cust_pkg, $time ) = @_;
44
45   my $d = $cust_pkg->bill || $time;
46   $d += 86400 * $self->option('free_days');
47   $cust_pkg->bill($d);
48   
49   $self->option('setup_fee');
50 }
51
52 sub calc_remain {
53   my ($self, $cust_pkg, %options) = @_;
54   my $next_bill = $cust_pkg->getfield('bill') || 0;
55   my $last_bill = $cust_pkg->last_bill || 0;
56   my $free_days = $self->option('free_days');
57
58   return 0 if    $last_bill + (86400 * $free_days) == $next_bill
59               && $last_bill == $cust_pkg->setup;
60
61   return 0 if    ! $self->base_recur($cust_pkg)
62               || ! $self->option('unused_credit', 1)
63               || ! $last_bill
64               || ! $next_bill;
65
66   return $self->SUPER::calc_remain($cust_pkg, %options);
67 }
68
69 1;