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