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