Introductory rates...just what we always wanted.
[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 $DEBUG_PRE);
5 #use FS::Record qw(qsearch qsearchs);
6 use FS::part_pkg::flat;
7
8 use Date::Manip qw(DateCalc UnixDate ParseDate);
9
10 @ISA = qw(FS::part_pkg::flat);
11 $DEBUG = 0;
12 $DEBUG_PRE = '[' . __PACKAGE__ . ']: ';
13
14 %info = (
15   'name' => 'Introductory price for X months, then flat rate,'.
16             'relative to setup date (anniversary billing)',
17   'fields' =>  {
18     'setup_fee' => { 'name' => 'Setup fee for this package',
19                      'default' => 0,
20                    },
21     'intro_fee' => { 'name' => 'Introductory recurring free for this package',
22                      'default' => 0,
23                    },
24     'intro_duration' => { 'name' => 'Duration of the introductory period, ' .
25                                     'in number of months',
26                           'default' => 0,
27                         },
28     'recur_fee' => { 'name' => 'Recurring fee for this package',
29                      'default' => 0,
30                     },
31     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
32                                    ' of service at cancellation',
33                          'type' => 'checkbox',
34                        },
35   },
36   'fieldorder' => [ 'setup_fee', 'intro_duration', 'intro_fee', 'recur_fee', 'unused_credit' ],
37   'weight' => 150,
38 );
39
40 sub calc_recur {
41   my($self, $cust_pkg, $time ) = @_;
42
43   my ($duration) = ($self->option('intro_duration') =~ /^(\d+)$/);
44   unless ($duration) {
45     die "Invalid intro_duration: " . $self->option('intro_duration');
46   }
47
48   my $setup = &ParseDate('epoch ' . $cust_pkg->getfield('setup'));
49   my $intro_end = &DateCalc($setup, "+${duration} month");
50   my $recur;
51
52   warn $DEBUG_PRE . "\$duration = ${duration}" if $DEBUG;
53   warn $DEBUG_PRE . "\$intro_end = ${intro_end}" if $DEBUG;
54   warn $DEBUG_PRE . "$$time < " . &UnixDate($intro_end, '%s') if $DEBUG;
55
56   if ($$time < &UnixDate($intro_end, '%s')) {
57     $recur = $self->option('intro_fee');
58   } else {
59     $recur = $self->option('recur_fee');
60   }
61
62   $recur;
63
64 }
65
66
67 1;