small fix to make prorate behave on the 1st as it did before
[freeside.git] / FS / FS / part_pkg / prorate.pm
1 package FS::part_pkg::prorate;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Time::Local qw(timelocal);
6 #use FS::Record qw(qsearch qsearchs);
7 use FS::part_pkg::flat;
8
9 @ISA = qw(FS::part_pkg::flat);
10
11 %info = (
12   'name' => 'First partial month pro-rated, then flat-rate (selectable billing day)',
13   'fields' =>  {
14     'setup_fee' => { 'name' => 'Setup fee for this package',
15                      'default' => 0,
16                    },
17     'recur_fee' => { 'name' => 'Recurring fee for this package',
18                      'default' => 0,
19                     },
20     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
21                                    ' of service at cancellation',
22                          'type' => 'checkbox',
23                        },
24     'cutoff_day' => { 'name' => 'billing day',
25                          'default' => 1,
26                                             },
27     #it would be better if this had to be turned on, its confusing
28     'externalid' => { 'name'   => 'Optional External ID',
29                       'default' => '',
30                     },
31  },
32   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 'cutoff_day',
33                     'externalid', ],
34   'freq' => 'm',
35   'weight' => 20,
36 );
37
38 sub calc_recur {
39   my($self, $cust_pkg, $sdate ) = @_;
40   my $cutoff_day = $self->option('cutoff_day', 1) || 1;
41   my $mnow = $$sdate;
42   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
43   my $mend;
44   my $mstart;
45   
46   if ( $mday >= $cutoff_day ) {
47     $mend =
48       timelocal(0,0,0,$cutoff_day, $mon == 11 ? 0 : $mon+1, $year+($mon==11));
49     $mstart =
50       timelocal(0,0,0,$cutoff_day,$mon,$year);  
51
52   } else {
53     $mend = timelocal(0,0,0,$cutoff_day, $mon, $year);
54     if ($mon==0) {$mon=11;$year--;} else {$mon--;}
55     $mstart=  timelocal(0,0,0,$cutoff_day,$mon,$year);  
56   }
57
58   $$sdate = $mstart;
59   my $permonth = $self->option('recur_fee') / $self->freq;
60
61   $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
62 }
63
64 1;