shorter names and rearranged weights for a brighter tommorow^W^Wbetter price plan...
[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   'shortname' => 'Anniversary, with intro price',
18   'fields' =>  {
19     'setup_fee' => { 'name' => 'Setup fee for this package',
20                      'default' => 0,
21                    },
22     'intro_fee' => { 'name' => 'Introductory recurring free for this package',
23                      'default' => 0,
24                    },
25     'intro_duration' => { 'name' => 'Duration of the introductory period, ' .
26                                     'in number of months',
27                           'default' => 0,
28                         },
29     'recur_fee' => { 'name' => 'Recurring fee for this package',
30                      'default' => 0,
31                     },
32     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
33                                    ' of service at cancellation',
34                          'type' => 'checkbox',
35                        },
36   },
37   'fieldorder' => [ 'setup_fee', 'intro_duration', 'intro_fee', 'recur_fee', 'unused_credit' ],
38   'weight' => 14,
39 );
40
41 sub calc_recur {
42   my($self, $cust_pkg, $time ) = @_;
43
44   my ($duration) = ($self->option('intro_duration') =~ /^(\d+)$/);
45   unless ($duration) {
46     die "Invalid intro_duration: " . $self->option('intro_duration');
47   }
48
49   my $setup = &ParseDate('epoch ' . $cust_pkg->getfield('setup'));
50   my $intro_end = &DateCalc($setup, "+${duration} month");
51   my $recur;
52
53   warn $DEBUG_PRE . "\$duration = ${duration}" if $DEBUG;
54   warn $DEBUG_PRE . "\$intro_end = ${intro_end}" if $DEBUG;
55   warn $DEBUG_PRE . "$$time < " . &UnixDate($intro_end, '%s') if $DEBUG;
56
57   if ($$time < &UnixDate($intro_end, '%s')) {
58     $recur = $self->option('intro_fee');
59   } else {
60     $recur = $self->option('recur_fee');
61   }
62
63   $recur;
64
65 }
66
67
68 1;