add a reset feature to manual recharges (#1858)
[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     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
78                                     'package recharge',
79                           'type' => 'checkbox',
80                         },
81
82     #it would be better if this had to be turned on, its confusing
83     'externalid' => { 'name'   => 'Optional External ID',
84                       'default' => '',
85                     },
86   },
87   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 'cutoff_day',
88                     'seconds', 'upbyte', 'downbytes', 'totalbytes',
89                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
90                     'recharge_downbytes', 'recharge_totalbytes',
91                     'usage_rollover', 'recharge_reset', 'externalid', ],
92   'freq' => 'm',
93   'weight' => 20,
94 );
95
96 sub calc_recur {
97   my($self, $cust_pkg, $sdate ) = @_;
98   my $cutoff_day = $self->option('cutoff_day', 1) || 1;
99   my $mnow = $$sdate;
100   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
101   my $mend;
102   my $mstart;
103   
104   if ( $mday >= $cutoff_day ) {
105     $mend =
106       timelocal(0,0,0,$cutoff_day, $mon == 11 ? 0 : $mon+1, $year+($mon==11));
107     $mstart =
108       timelocal(0,0,0,$cutoff_day,$mon,$year);  
109
110   } else {
111     $mend = timelocal(0,0,0,$cutoff_day, $mon, $year);
112     if ($mon==0) {$mon=11;$year--;} else {$mon--;}
113     $mstart=  timelocal(0,0,0,$cutoff_day,$mon,$year);  
114   }
115
116   $$sdate = $mstart;
117   my $permonth = $self->option('recur_fee') / $self->freq;
118
119   $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
120 }
121
122 1;