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