fix some indentation and the default cutoff day
[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   },
27   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 'externalid' ],
28   'weight' => 10,
29 );
30
31 sub calc_setup {
32   my($self, $cust_pkg ) = @_;
33   $self->option('setup_fee');
34 }
35
36 sub calc_recur {
37   my $self = shift;
38   $self->base_recur(@_);
39 }
40
41 sub base_recur {
42   my($self, $cust_pkg) = @_;
43   $self->option('recur_fee');
44 }
45
46 sub calc_remain {
47   my ($self, $cust_pkg) = @_;
48   my $time = time;  #should be able to pass this in for credit calculation
49   my $next_bill = $cust_pkg->getfield('bill') || 0;
50   my $last_bill = $cust_pkg->last_bill || 0;
51   return 0 if    ! $self->base_recur
52               || ! $self->option('unused_credit', 1)
53               || ! $last_bill
54               || ! $next_bill
55               || $next_bill < $time;
56
57   my %sec = (
58     'h' =>    3600, # 60 * 60
59     'd' =>   86400, # 60 * 60 * 24
60     'w' =>  604800, # 60 * 60 * 24 * 7
61     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
62   );
63
64   $self->freq =~ /^(\d+)([hdwm]?)$/
65     or die 'unparsable frequency: '. $self->freq;
66   my $freq_sec = $1 * $sec{$2||'m'};
67   return 0 unless $freq_sec;
68
69   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
70
71 }
72
73 sub is_free_options {
74   qw( setup_fee recur_fee );
75 }
76
77 sub is_prepaid {
78   0; #no, we're postpaid
79 }
80
81 1;