This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / part_pkg / base_rate.pm
1 package FS::part_pkg::base_rate;
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' => 'Base rate (anniversary billing, Times units ordered)',
12             # XXX it multiplies recurring fee by cust_pkg option "units", how to
13             # express that
14   'shortname' => 'Bulk (manual from "units" option)',
15   'inherit_fields' => [ 'global_Mixin' ],
16   'fields' => {
17     'externalid' => { 'name'    => 'Optional External ID',
18                       'default' => '',
19                     },
20   },
21   'fieldorder' => [ qw( externalid ) ],
22   'weight' => 52,
23 );
24
25 sub price_info {
26     my $self = shift;
27     my $conf = new FS::Conf;
28     my $money_char = $conf->config('money_char') || '$';
29     my $setup = $self->option('setup_fee') || 0;
30     my $recur = $self->option('recur_fee', 1) || 0;
31     my $str = '';
32     $str = $money_char . $setup . ' one-time' if $setup;
33     $str .= ', ' if ($setup && $recur);
34     $str .= $money_char . $recur . ' recurring per unit ' if $recur;
35     $str;
36 }
37
38
39 sub calc_setup {
40   my($self, $cust_pkg, $sdate, $details ) = @_;
41
42   my $i = 0;
43   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
44   while ($i < $count) {
45     push @$details, $self->option( 'additional_info' . $i++ );
46   }
47
48   $self->option('setup_fee');
49 }
50
51 sub calc_recur {
52   my($self, $cust_pkg) = @_;
53   $self->base_recur($cust_pkg);
54 }
55
56 sub base_recur {
57   my($self, $cust_pkg) = @_;
58   my $units = $cust_pkg->option('units') ? $cust_pkg->option('units') : 1 ;
59         # default to 1 if not found
60   sprintf("%.2f", 
61           ($self->option('recur_fee') * $units ) 
62   );
63 }
64
65 sub calc_remain {
66   my ($self, $cust_pkg, %options) = @_;
67   my $time = $options{'time'} || time;
68   my $next_bill = $cust_pkg->getfield('bill') || 0;
69   return 0 if  ! $self->base_recur($cust_pkg)
70               || ! $next_bill
71               || $next_bill < $time;
72
73   my %sec = (
74     'h' =>    3600, # 60 * 60
75     'd' =>   86400, # 60 * 60 * 24
76     'w' =>  604800, # 60 * 60 * 24 * 7
77     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
78   );
79
80   $self->freq =~ /^(\d+)([hdwm]?)$/
81     or die 'unparsable frequency: '. $self->freq;
82   my $freq_sec = $1 * $sec{$2||'m'};
83   return 0 unless $freq_sec;
84
85   sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
86
87 }
88
89 sub is_free_options {
90   qw( setup_fee recur_fee );
91 }
92
93 sub is_prepaid {
94   0; #no, we're postpaid
95 }
96
97 1;