fix flat_introrate without an introductory period, RT#18124
[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::part_pkg::flat;
6
7 @ISA = qw(FS::part_pkg::flat);
8 $me = '[' . __PACKAGE__ . ']';
9 $DEBUG = 0;
10
11 %info = (
12   'name' => 'Introductory price for X months, then flat rate,'.
13             'relative to setup date (anniversary billing)',
14   'shortname' => 'Anniversary, with intro price',
15   'inherit_fields' => [ 'flat', 'usage_Mixin', 'global_Mixin' ],
16   'fields' => {
17     'intro_fee' => { 'name' => 'Introductory recurring fee for this package',
18                      'default' => 0,
19                    },
20     'intro_duration' =>
21          { 'name' => 'Duration of the introductory period, in number of months',
22            'default' => 0,
23          },
24   },
25   'fieldorder' => [ qw(intro_duration intro_fee) ],
26   'weight' => 14,
27 );
28
29 sub base_recur {
30   my($self, $cust_pkg, $time ) = @_;
31
32   warn "flat_introrate base_recur requires date!" if !$time;
33   my $now = $time ? $$time : time;
34
35   my ($duration) = ($self->option('intro_duration') =~ /^\s*(\d+)\s*$/);
36   unless (length($duration)) {
37     die "Invalid intro_duration: " . $self->option('intro_duration');
38   }
39   my $intro_end = $self->add_freq($cust_pkg->setup, $duration);
40
41   if ($now < $intro_end) {
42     return $self->option('intro_fee');
43   } else {
44     return $self->option('recur_fee');
45   }
46
47 }
48
49
50 1;