in grouped payment search, only show tabs when there's more than one group, #71364
[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 use FS::Log;
8
9 # mostly false laziness with FS::part_pkg::global_Mixin::validate_moneyn,
10 # except for blank string handling...
11 sub validate_money {
12   my ($option, $valref) = @_;
13   if ( $$valref eq '' ) {
14     $$valref = '0';
15   } elsif ( $$valref =~ /^\s*(\d*)(\.\d{1})\s*$/ ) {
16     #handle one decimal place without barfing out
17     $$valref = ( ($1||''). ($2.'0') ) || 0;
18   } elsif ( $$valref =~ /^\s*(\d*)(\.\d{2})?\s*$/ ) {
19     $$valref = ( ($1||''). ($2||'') ) || 0;
20   } else {
21     return "Illegal (money) $option: ". $$valref;
22   }
23   return '';
24 }
25
26 sub validate_number {
27   my ($option, $valref) = @_;
28   $$valref = 0 unless $$valref;
29   return "Invalid $option"
30     unless ($$valref) = ($$valref =~ /^\s*(\d+)\s*$/);
31   return '';
32 }
33
34 %info = (
35   'name' => 'Introductory price for X months, then flat rate,'.
36             'relative to setup date (anniversary billing)',
37   'shortname' => 'Anniversary, with intro price',
38   'inherit_fields' => [ 'flat', 'usage_Mixin', 'global_Mixin' ],
39   'fields' => {
40     'intro_fee' => { 'name' => 'Introductory recurring fee for this package',
41                      'default' => 0,
42                      'validate' => \&validate_money,
43                    },
44     'intro_duration' =>
45          { 'name' => 'Duration of the introductory period, in number of months',
46            'default' => 0,
47            'validate' => \&validate_number,
48          },
49   },
50   'fieldorder' => [ qw(intro_duration intro_fee) ],
51   'weight' => 14,
52 );
53
54 sub base_recur {
55   my($self, $cust_pkg, $time ) = @_;
56
57   warn "flat_introrate base_recur requires date!" if !$time;
58   my $now = $time ? $$time : time;
59
60   my ($duration) = ($self->option('intro_duration') =~ /^\s*(\d+)\s*$/);
61   unless (length($duration)) {
62     my $log = FS::Log->new('FS::part_pkg');
63     $log->warning("Invalid intro_duration '".$self->option('intro_duration')."' on pkgpart ".$self->pkgpart
64                 .", defaulting to 0, check package definition");
65     $duration = 0;
66   }
67   my $intro_end = $self->add_freq($cust_pkg->setup, $duration);
68
69   if ($now < $intro_end) {
70     return $self->option('intro_fee');
71   } else {
72     return $self->option('recur_fee');
73   }
74
75 }
76
77
78 1;