update flat_introrate plan to better fit current codebase RT#4912
[freeside.git] / FS / FS / part_pkg / flat_introrate.pm
1 package FS::part_pkg::flat_introrate;
2
3 use strict;
4 use vars qw(@ISA %info $DEBUG $me);
5 #use FS::Record qw(qsearch qsearchs);
6 use FS::part_pkg::flat;
7 use Data::Dumper;
8
9 use Date::Manip qw(DateCalc UnixDate ParseDate);
10
11 @ISA = qw(FS::part_pkg::flat);
12 $me = '[' . __PACKAGE__ . ']';
13 $DEBUG = 0;
14
15 (%info) = (%FS::part_pkg::flat::info);
16 $info{name} = 'Introductory price for X months, then flat rate,'.
17               'relative to setup date (anniversary billing)';
18 $info{fields} = { %{$info{fields}} };
19 $info{fields}{intro_fee} =
20   { 'name'    => 'Introductory recurring free for this package',
21     'default' => 0,
22   };
23 $info{fields}{intro_duration} =
24   { 'name'    => 'Duration of the introductory period, in number of months',
25     'default' => 0,
26   };
27 $info{fieldorder} = [ @{ $info{fieldorder} } ];
28 splice @{$info{fieldorder}}, 1, 0, qw( intro_duration intro_fee );
29 $info{weight} = 150;
30
31 sub base_recur {
32   my($self, $cust_pkg, $time ) = @_;
33
34   my $now = $time ? $$time : time;
35
36   my ($duration) = ($self->option('intro_duration') =~ /^(\d+)$/);
37   unless ($duration) {
38     die "Invalid intro_duration: " . $self->option('intro_duration');
39   }
40
41   my $setup = &ParseDate('epoch ' . $cust_pkg->getfield('setup'));
42   my $intro_end = &DateCalc($setup, "+${duration} month");
43   my $recur;
44
45   warn "$me: \$duration = ${duration}" if $DEBUG;
46   warn "$me: \$intro_end = ${intro_end}" if $DEBUG;
47   warn "$me: $now < " . &UnixDate($intro_end, '%s') if $DEBUG;
48
49   if ($now < &UnixDate($intro_end, '%s')) {
50     $recur = $self->option('intro_fee');
51   } else {
52     $recur = $self->option('recur_fee');
53   }
54
55   $recur;
56
57 }
58
59
60 1;