fixed the errors pointed out by Ivan in the following email:
[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
28  },
29   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 'cutoff_day' ],
30   #'setup' => 'what.setup_fee.value',
31   #'recur' => '\'my $mnow = $sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate) )[0,1,2,3,4,5]; my $mstart = timelocal(0,0,0,1,$mon,$year); my $mend = timelocal(0,0,0,1, $mon == 11 ? 0 : $mon+1, $year+($mon==11)); $sdate = $mstart; ( $part_pkg->freq - 1 ) * \' + what.recur_fee.value + \' / $part_pkg->freq + \' + what.recur_fee.value + \' / $part_pkg->freq * ($mend-$mnow) / ($mend-$mstart) ; \'',
32   'freq' => 'm',
33   'weight' => 20,
34 );
35
36 sub calc_recur {
37   my($self, $cust_pkg, $sdate ) = @_;
38   my $cutoff_day=$self->option('cutoff_day') or 1;
39   my $mnow = $$sdate;
40   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
41   my $mend;
42   my $mstart;
43   if($mday > $cutoff_day){
44     $mend = timelocal(0,0,0,$cutoff_day, $mon == 11 ? 0 : $mon+1, $year+($mon==11));
45     $mstart=  timelocal(0,0,0,$cutoff_day,$mon,$year);  
46
47   }
48   else{
49     $mend = timelocal(0,0,0,$cutoff_day, $mon, $year);
50      if ($mon==0) {$mon=11;$year--;} else {$mon--;}
51     $mstart=  timelocal(0,0,0,$cutoff_day,$mon,$year);  
52   }
53
54    $$sdate = $mstart;
55   my $permonth = $self->option('recur_fee') / $self->freq;
56
57   $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
58 }
59
60 1;