Merge branch 'patch-19' of https://github.com/gjones2/Freeside
[freeside.git] / FS / FS / part_pkg / flat_introrate.pm
1 package FS::part_pkg::flat_introrate;
2 use base qw( FS::part_pkg::flat );
3
4 use strict;
5 use vars qw( %info );
6
7 %info = (
8   'name' => 'Introductory price for X months, then flat rate,'.
9             'relative to setup date (anniversary billing)',
10   'shortname' => 'Anniversary, with intro price',
11   'inherit_fields' => [ 'flat', 'usage_Mixin', 'global_Mixin' ],
12   'fields' => {
13     'intro_fee' => { 'name' => 'Introductory recurring fee for this package',
14                      'default' => 0,
15                    },
16     'intro_duration' =>
17          { 'name' => 'Duration of the introductory period, in number of months',
18            'default' => 0,
19          },
20   },
21   'fieldorder' => [ qw(intro_duration intro_fee) ],
22   'weight' => 14,
23 );
24
25 sub base_recur {
26   my($self, $cust_pkg, $time ) = @_;
27
28   warn "flat_introrate base_recur requires date!" if !$time;
29   my $now = $time ? $$time : time;
30
31   my ($duration) = ($self->option('intro_duration') =~ /^\s*(\d+)\s*$/);
32   unless (length($duration)) {
33     die "Invalid intro_duration: " . $self->option('intro_duration');
34   }
35   my $intro_end = $self->add_freq($cust_pkg->setup, $duration);
36
37   if ($now < $intro_end) {
38     return $self->option('intro_fee');
39   } else {
40     return $self->option('recur_fee');
41   }
42
43 }
44
45
46 1;