import torrus 1.0.9
[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 use Date::Manip qw(DateCalc UnixDate ParseDate);
8
9 @ISA = qw(FS::part_pkg::flat);
10 $me = '[' . __PACKAGE__ . ']';
11 $DEBUG = 0;
12
13 %info = (
14   'name' => 'Introductory price for X months, then flat rate,'.
15             'relative to setup date (anniversary billing)',
16   'shortname' => 'Anniversary, with intro price',
17   'inherit_fields' => [ 'flat', 'usage_Mixin', 'global_Mixin' ],
18   'fields' => {
19     'intro_fee' => { 'name' => 'Introductory recurring fee for this package',
20                      'default' => 0,
21                    },
22     'intro_duration' =>
23          { 'name' => 'Duration of the introductory period, in number of months',
24            'default' => 0,
25          },
26   },
27   'fieldorder' => [ qw(intro_duration intro_fee) ],
28   'weight' => 14,
29 );
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;