import torrus 1.0.9
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
2
3 use strict;
4 use vars qw( @ISA %info
5              %usage_recharge_fields @usage_recharge_fieldorder
6            );
7 use Tie::IxHash;
8 use List::Util qw(min); # max);
9 #use FS::Record qw(qsearch);
10 use FS::UI::bytecount;
11 use FS::Conf;
12 use FS::part_pkg;
13
14 @ISA = qw(FS::part_pkg 
15           FS::part_pkg::prorate_Mixin
16           FS::part_pkg::discount_Mixin);
17
18 tie my %temporalities, 'Tie::IxHash',
19   'upcoming'  => "Upcoming (future)",
20   'preceding' => "Preceding (past)",
21 ;
22
23 tie my %contract_years, 'Tie::IxHash', (
24   '' => '(none)',
25   map { $_*12 => $_ } (1..5),
26 );
27
28 %info = (
29   'name' => 'Flat rate (anniversary billing)',
30   'shortname' => 'Anniversary',
31   'inherit_fields' => [ 'usage_Mixin', 'global_Mixin' ],
32   'fields' => {
33     #false laziness w/voip_cdr.pm
34     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
35                              'type' => 'select',
36                              'select_options' => \%temporalities,
37                            },
38
39     #used in cust_pkg.pm so could add to any price plan
40     'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
41                        },
42     'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
43                        },
44     'contract_end_months'=> { 
45                         'name' => 'Auto-add a contract end date this number of years out',
46                         'type' => 'select',
47                         'select_options' => \%contract_years,
48                       },
49     #used in cust_pkg.pm so could add to any price plan where it made sense
50     'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
51                          'type' => 'checkbox',
52                        },
53     'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
54                                     'with the customer\'s other packages',
55                           'type' => 'checkbox',
56                         },
57     'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
58                         'type' => 'checkbox',
59                       },
60     'unsuspend_adjust_bill' => 
61                         { 'name' => 'Adjust next bill date forward when '.
62                                     'unsuspending',
63                           'type' => 'checkbox',
64                         },
65
66     'externalid' => { 'name'   => 'Optional External ID',
67                       'default' => '',
68                     },
69   },
70   'fieldorder' => [ qw( recur_temporality 
71                         expire_months adjourn_months
72                         contract_end_months
73                         start_1st sync_bill_date
74                         suspend_bill unsuspend_adjust_bill
75                         externalid ),
76                   ],
77   'weight' => 10,
78 );
79
80 sub calc_setup {
81   my($self, $cust_pkg, $sdate, $details ) = @_;
82
83   my $i = 0;
84   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
85   while ($i < $count) {
86     push @$details, $self->option( 'additional_info' . $i++ );
87   }
88
89   my $quantity = $cust_pkg->quantity || 1;
90
91   sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
92 }
93
94 sub unit_setup {
95   my($self, $cust_pkg, $sdate, $details ) = @_;
96
97   $self->option('setup_fee') || 0;
98 }
99
100 sub calc_recur {
101   my $self = shift;
102   my($cust_pkg, $sdate, $details, $param ) = @_;
103
104   #my $last_bill = $cust_pkg->last_bill;
105   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
106
107   return 0
108     if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
109
110   my $charge = $self->base_recur($cust_pkg);
111   if ( $self->option('sync_bill_date',1) ) {
112     my $next_bill = $cust_pkg->cust_main->next_bill_date;
113     if ( defined($next_bill) ) {
114       my $cutoff_day = (localtime($next_bill))[3];
115       $charge = $self->calc_prorate(@_, $cutoff_day);
116     }
117   }
118   elsif ( $param->{freq_override} ) {
119     # XXX not sure if this should be mutually exclusive with sync_bill_date.
120     # Given the very specific problem that freq_override is meant to 'solve',
121     # it probably should.
122     $charge *= $param->{freq_override} if $param->{freq_override};
123   }
124
125   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
126   return sprintf('%.2f', $charge - $discount);
127 }
128
129 sub base_recur {
130   my($self, $cust_pkg) = @_;
131   $self->option('recur_fee', 1) || 0;
132 }
133
134 sub base_recur_permonth {
135   my($self, $cust_pkg) = @_;
136
137   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
138
139   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
140 }
141
142 sub calc_remain {
143   my ($self, $cust_pkg, %options) = @_;
144
145   my $time;
146   if ($options{'time'}) {
147     $time = $options{'time'};
148   } else {
149     $time = time;
150   }
151
152   my $next_bill = $cust_pkg->getfield('bill') || 0;
153
154   return 0 if    ! $self->base_recur($cust_pkg)
155               || ! $next_bill
156               || $next_bill < $time;
157
158   my %sec = (
159     'h' =>    3600, # 60 * 60
160     'd' =>   86400, # 60 * 60 * 24
161     'w' =>  604800, # 60 * 60 * 24 * 7
162     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
163   );
164
165   $self->freq =~ /^(\d+)([hdwm]?)$/
166     or die 'unparsable frequency: '. $self->freq;
167   my $freq_sec = $1 * $sec{$2||'m'};
168   return 0 unless $freq_sec;
169
170   sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
171
172 }
173
174 sub is_free_options {
175   qw( setup_fee recur_fee );
176 }
177
178 sub is_prepaid { 0; } #no, we're postpaid
179
180 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
181 sub can_discount {
182   my $self = shift;
183   $self->freq =~ /^\d+$/ && $self->freq > 0;
184 }
185
186 sub usage_valuehash {
187   my $self = shift;
188   map { $_, $self->option($_) }
189     grep { $self->option($_, 'hush') } 
190     qw(seconds upbytes downbytes totalbytes);
191 }
192
193 sub reset_usage {
194   my($self, $cust_pkg, %opt) = @_;
195   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
196   my %values = $self->usage_valuehash;
197   if ($self->option('usage_rollover', 1)) {
198     $cust_pkg->recharge(\%values);
199   }else{
200     $cust_pkg->set_usage(\%values, %opt);
201   }
202 }
203
204 1;