Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_pkg / recur_Common.pm
1 package FS::part_pkg::recur_Common;
2 use base qw( FS::part_pkg::flat );
3
4 use strict;
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_setup {
23   # moved from all descendant packages which just had $self->option('setup_fee')
24   my($self, $cust_pkg, $sdate, $details, $param) = @_;
25
26   return 0 if $self->prorate_setup($cust_pkg, $sdate);
27
28   my $charge = $self->option('setup_fee');
29
30   my $discount = 0;
31   if ( $charge > 0 ) {
32       $param->{'setup_charge'} = $charge;
33       $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
34       delete $param->{'setup_charge'};
35   }
36
37   sprintf('%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
38 }
39
40 sub cutoff_day {
41   # prorate/subscription only; we don't support sync_bill_date here
42   my( $self, $cust_pkg ) = @_;
43   my $recur_method = $self->option('recur_method',1) || 'anniversary';
44   my $cust_main = $cust_pkg->cust_main;
45
46   return ( $cust_main->prorate_day )
47     if $cust_main->prorate_day and (    $cust_main->force_prorate_day
48                                      || $recur_method eq 'prorate'
49                                      || $recur_method eq 'subscription'
50                                    );
51
52   return split(/\s*,\s*/, $self->option('cutoff_day', 1) || '1')
53     if $recur_method eq 'prorate'
54     || $recur_method eq 'subscription';
55
56   return ();
57 }
58
59 sub calc_recur_Common {
60   my $self = shift;
61   my($cust_pkg, $sdate, $details, $param ) = @_; #only need $sdate & $param
62
63   my $charges = 0;
64
65   if ( $param->{'increment_next_bill'} ) {
66
67     my $recur_method = $self->option('recur_method', 1) || 'anniversary';
68     my @cutoff_day = $self->cutoff_day($cust_pkg);
69
70     $charges = $self->base_recur($cust_pkg, $sdate, $details, $param);
71     $charges += $param->{'override_charges'} if $param->{'override_charges'};
72
73     if ( $recur_method eq 'prorate' ) {
74
75       $charges = $self->calc_prorate(@_, @cutoff_day);
76       $charges += $param->{'override_charges'} if $param->{'override_charges'};
77
78     } elsif ( $recur_method eq 'subscription' ) {
79
80       my ($day, $mon, $year) = ( localtime($$sdate) )[ 3..5 ];
81
82       if ( $day < $cutoff_day[0] ) {
83         if ( $mon == 0 ) { $mon=11; $year--; }
84         else { $mon--; }
85       }
86
87       $$sdate = timelocal(0, 0, 0, $cutoff_day[0], $mon, $year);
88
89     }#$recur_method
90
91     $charges -= $self->calc_discount( $cust_pkg, $sdate, $details, $param );
92
93   }#increment_next_bill
94
95   return $charges;
96
97 }
98
99 1;