fix prorates & recurring fees with recur_Common-using packages, RT#11993
[freeside.git] / FS / FS / part_pkg / recur_Common.pm
1 package FS::part_pkg::recur_Common;
2
3 use strict;
4 use base qw( FS::part_pkg::flat );
5 use vars qw( %info %recur_method );
6 use Tie::IxHash;
7 use Time::Local;
8
9 %info = ( 'disabled' => 1 ); #recur_Common not a usable price plan directly
10
11 tie %recur_method, 'Tie::IxHash',
12   'anniversary'  => 'Charge the recurring fee at the frequency specified above',
13   'prorate'      => 'Charge a prorated fee the first time (selectable billing date)',
14   'subscription' => 'Charge the full fee for the first partial period (selectable billing date)',
15 ;
16
17 sub base_recur {
18   my $self = shift;
19   $self->option('recur_fee', 1) || 0;
20 }
21
22 sub calc_recur_Common {
23   my $self = shift;
24   my($cust_pkg, $sdate, $details, $param ) = @_; #only need $sdate & $param
25
26   my $charges = 0;
27
28   if ( $param->{'increment_next_bill'} ) {
29
30     my $recur_method = $self->option('recur_method', 1) || 'anniversary';
31     
32     $charges = $self->base_recur;
33     $charges += $param->{'override_charges'} if $param->{'override_charges'};
34
35     if ( $recur_method eq 'prorate' ) {
36
37       my $cutoff_day = $self->option('cutoff_day') || 1;
38       $charges = $self->calc_prorate(@_, $cutoff_day);
39       $charges += $param->{'override_charges'} if $param->{'override_charges'};
40
41     } elsif ( $recur_method eq 'anniversary' and 
42             $self->option('sync_bill_date',1) ) {
43
44       my $next_bill = $cust_pkg->cust_main->next_bill_date;
45       if ( defined($next_bill) ) {
46         my $cutoff_day = (localtime($next_bill))[3];
47         $charges = $self->calc_prorate(@_, $cutoff_day);
48         $charges += $param->{'override_charges'} if $param->{'override_charges'};
49       }
50
51     } elsif ( $recur_method eq 'subscription' ) {
52
53       my $cutoff_day = $self->option('cutoff_day', 1) || 1;
54       my ($day, $mon, $year) = ( localtime($$sdate) )[ 3..5 ];
55
56       if ( $day < $cutoff_day ) {
57         if ( $mon == 0 ) { $mon=11; $year--; }
58         else { $mon--; }
59       }
60
61       $$sdate = timelocal(0, 0, 0, $cutoff_day, $mon, $year);
62
63     }#$recur_method
64
65     $charges -= $self->calc_discount( $cust_pkg, $sdate, $details, $param );
66
67   }#increment_next_bill
68
69   return $charges;
70
71 }
72
73 1;