continue prorate cleanup, #10630
[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 cutoff_day {
23   # prorate/subscription only; we don't support sync_bill_date here
24   my $self = shift;
25   my $cust_pkg = shift;
26   my $recur_method = $self->option('recur_method',1) || 'anniversary';
27   if ( $recur_method eq 'prorate' or $recur_method eq 'subscription' ) {
28     return $self->option('cutoff_day',1) || 1;
29   } else {
30     return 0;
31   }
32 }
33
34 sub calc_recur_Common {
35   my $self = shift;
36   my($cust_pkg, $sdate, $details, $param ) = @_; #only need $sdate & $param
37
38   my $charges = 0;
39
40   if ( $param->{'increment_next_bill'} ) {
41
42     my $recur_method = $self->option('recur_method', 1) || 'anniversary';
43     my $cutoff_day = $self->cutoff_day($cust_pkg);
44
45     $charges = $self->base_recur;
46     $charges += $param->{'override_charges'} if $param->{'override_charges'};
47
48     if ( $recur_method eq 'prorate' ) {
49
50       $charges = $self->calc_prorate(@_, $cutoff_day);
51       $charges += $param->{'override_charges'} if $param->{'override_charges'};
52
53     } elsif ( $recur_method eq 'subscription' ) {
54
55       my ($day, $mon, $year) = ( localtime($$sdate) )[ 3..5 ];
56
57       if ( $day < $cutoff_day ) {
58         if ( $mon == 0 ) { $mon=11; $year--; }
59         else { $mon--; }
60       }
61
62       $$sdate = timelocal(0, 0, 0, $cutoff_day, $mon, $year);
63
64     }#$recur_method
65
66     $charges -= $self->calc_discount( $cust_pkg, $sdate, $details, $param );
67
68   }#increment_next_bill
69
70   return $charges;
71
72 }
73
74 1;