allow assignment of auto recharge values AND rollover
[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 (1 - 28)',
25                          'default' => 1,
26                                             },
27     'seconds'       => { 'name' => 'Time limit for this package',
28                          'default' => '',
29                        },
30     'upbytes'       => { 'name' => 'Upload limit for this package',
31                          'default' => '',
32                          'format' => \&FS::UI::bytecount::display_bytecount,
33                          'parse' => \&FS::UI::bytecount::parse_bytecount,
34                        },
35     'downbytes'     => { 'name' => 'Download limit for this package',
36                          'default' => '',
37                          'format' => \&FS::UI::bytecount::display_bytecount,
38                          'parse' => \&FS::UI::bytecount::parse_bytecount,
39                        },
40     'totalbytes'    => { 'name' => 'Transfer limit for this package',
41                          'default' => '',
42                          'format' => \&FS::UI::bytecount::display_bytecount,
43                          'parse' => \&FS::UI::bytecount::parse_bytecount,
44                        },
45     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
46                          'default' => '',
47                        },
48     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
49                          'default' => '',
50                        },
51     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
52                          'default' => '',
53                          'format' => \&FS::UI::bytecount::display_bytecount,
54                          'parse' => \&FS::UI::bytecount::parse_bytecount,
55                        },
56     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',                         'default' => '',
57                          'format' => \&FS::UI::bytecount::display_bytecount,
58                          'parse' => \&FS::UI::bytecount::parse_bytecount,
59                        },
60     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',                         'default' => '',
61                          'format' => \&FS::UI::bytecount::display_bytecount,
62                          'parse' => \&FS::UI::bytecount::parse_bytecount,
63                        },
64     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
65                                     'over into current period',
66                           'type' => 'checkbox',
67                         },
68     #it would be better if this had to be turned on, its confusing
69     'externalid' => { 'name'   => 'Optional External ID',
70                       'default' => '',
71                     },
72  },
73   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 'cutoff_day',
74                     'seconds', 'upbyte', 'downbytes', 'totalbytes',
75                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
76                     'recharge_downbytes', 'recharge_totalbytes',
77                     'usage_rollover', 'externalid', ],
78   'freq' => 'm',
79   'weight' => 20,
80 );
81
82 sub calc_recur {
83   my($self, $cust_pkg, $sdate ) = @_;
84   my $cutoff_day = $self->option('cutoff_day', 1) || 1;
85   my $mnow = $$sdate;
86   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
87   my $mend;
88   my $mstart;
89   
90   if ( $mday >= $cutoff_day ) {
91     $mend =
92       timelocal(0,0,0,$cutoff_day, $mon == 11 ? 0 : $mon+1, $year+($mon==11));
93     $mstart =
94       timelocal(0,0,0,$cutoff_day,$mon,$year);  
95
96   } else {
97     $mend = timelocal(0,0,0,$cutoff_day, $mon, $year);
98     if ($mon==0) {$mon=11;$year--;} else {$mon--;}
99     $mstart=  timelocal(0,0,0,$cutoff_day,$mon,$year);  
100   }
101
102   $$sdate = $mstart;
103   my $permonth = $self->option('recur_fee') / $self->freq;
104
105   $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
106 }
107
108 1;