94b7d9947612775452bca36c6832bb07f9ebb59d
[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, $sdate, $details ) = @_;
64
65   my $i = 0;
66   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
67   while ($i < $count) {
68     push @$details, $self->option( 'additional_info' . $i++ );
69   }
70
71   $self->option('setup_fee');
72 }
73
74 sub calc_recur {
75   my($self, $cust_pkg) = @_;
76   $self->reset_usage($cust_pkg);
77   $self->base_recur($cust_pkg);
78 }
79
80 sub base_recur {
81   my($self, $cust_pkg) = @_;
82   $self->option('recur_fee');
83 }
84
85 sub calc_remain {
86   my ($self, $cust_pkg) = @_;
87   my $time = time;  #should be able to pass this in for credit calculation
88   my $next_bill = $cust_pkg->getfield('bill') || 0;
89   my $last_bill = $cust_pkg->last_bill || 0;
90   return 0 if    ! $self->base_recur
91               || ! $self->option('unused_credit', 1)
92               || ! $last_bill
93               || ! $next_bill
94               || $next_bill < $time;
95
96   my %sec = (
97     'h' =>    3600, # 60 * 60
98     'd' =>   86400, # 60 * 60 * 24
99     'w' =>  604800, # 60 * 60 * 24 * 7
100     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
101   );
102
103   $self->freq =~ /^(\d+)([hdwm]?)$/
104     or die 'unparsable frequency: '. $self->freq;
105   my $freq_sec = $1 * $sec{$2||'m'};
106   return 0 unless $freq_sec;
107
108   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
109
110 }
111
112 sub is_free_options {
113   qw( setup_fee recur_fee );
114 }
115
116 sub is_prepaid {
117   0; #no, we're postpaid
118 }
119
120 sub reset_usage {
121   my($self, $cust_pkg) = @_;
122   my %values = map { $_, $self->option($_) } 
123     grep { $self->option($_, 'hush') } 
124     qw(seconds upbytes downbytes totalbytes);
125   $cust_pkg->set_usage(\%values);
126 }
127
128 1;