retouch bandwidth countdown
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
2
3 use strict;
4 use vars qw(@ISA %info);
5 #use FS::Record qw(qsearch);
6 use FS::part_pkg;
7
8 @ISA = qw(FS::part_pkg);
9
10 %info = (
11   'name' => 'Flat rate (anniversary billing)',
12   'fields' => {
13     'setup_fee'     => { 'name' => 'Setup fee for this package',
14                          'default' => 0,
15                        },
16     'recur_fee'     => { 'name' => 'Recurring fee for this package',
17                          'default' => 0,
18                        },
19     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
20                                    ' of service at cancellation',
21                          'type' => 'checkbox',
22                        },
23     'externalid' => { 'name'   => 'Optional External ID',
24                       'default' => '',
25                     },
26     'seconds'       => { 'name' => 'Time limit for this package',
27                          'default' => '',
28                        },
29     'upbytes'       => { 'name' => 'Upload limit for this package',
30                          'default' => '',
31                        },
32     'downbytes'     => { 'name' => 'Download limit for this package',
33                          'default' => '',
34                        },
35     'totalbytes'    => { 'name' => 'Transfer limit for this package',
36                          'default' => '',
37                        },
38     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
39                          'default' => '',
40                        },
41     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
42                          'default' => '',
43                        },
44     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
45                          'default' => '',
46                        },
47     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',
48                          'default' => '',
49                        },
50     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',
51                          'default' => '',
52                        },
53   },
54   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 
55                     'seconds', 'upbytes', 'downbytes', 'totalbytes',
56                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
57                     'recharge_downbytes', 'recharge_totalbytes',
58                     'externalid' ],
59   'weight' => 10,
60 );
61
62 sub calc_setup {
63   my($self, $cust_pkg ) = @_;
64   $self->option('setup_fee');
65 }
66
67 sub calc_recur {
68   my($self, $cust_pkg) = @_;
69   $self->reset_usage($cust_pkg);
70   $self->base_recur($cust_pkg);
71 }
72
73 sub base_recur {
74   my($self, $cust_pkg) = @_;
75   $self->option('recur_fee');
76 }
77
78 sub calc_remain {
79   my ($self, $cust_pkg) = @_;
80   my $time = time;  #should be able to pass this in for credit calculation
81   my $next_bill = $cust_pkg->getfield('bill') || 0;
82   my $last_bill = $cust_pkg->last_bill || 0;
83   return 0 if    ! $self->base_recur
84               || ! $self->option('unused_credit', 1)
85               || ! $last_bill
86               || ! $next_bill
87               || $next_bill < $time;
88
89   my %sec = (
90     'h' =>    3600, # 60 * 60
91     'd' =>   86400, # 60 * 60 * 24
92     'w' =>  604800, # 60 * 60 * 24 * 7
93     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
94   );
95
96   $self->freq =~ /^(\d+)([hdwm]?)$/
97     or die 'unparsable frequency: '. $self->freq;
98   my $freq_sec = $1 * $sec{$2||'m'};
99   return 0 unless $freq_sec;
100
101   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
102
103 }
104
105 sub is_free_options {
106   qw( setup_fee recur_fee );
107 }
108
109 sub is_prepaid {
110   0; #no, we're postpaid
111 }
112
113 sub reset_usage {
114   my($self, $cust_pkg) = @_;
115   my %values = map { $_, $self->option($_) } 
116     grep { $self->option($_) } 
117     qw(seconds upbytes downbytes totalbytes);
118   $cust_pkg->set_usage(\%values);
119 }
120
121 1;