1 package FS::part_pkg::flat;
2 use base qw( FS::part_pkg::prorate_Mixin
3 FS::part_pkg::discount_Mixin
8 use vars qw( %info %usage_recharge_fields @usage_recharge_fieldorder );
9 use FS::Record qw( qsearch );
10 use FS::cust_credit_source_bill_pkg;
12 use List::Util qw( min );
13 use FS::UI::bytecount;
16 tie my %temporalities, 'Tie::IxHash',
17 'upcoming' => "Upcoming (future)",
18 'preceding' => "Preceding (past)",
21 tie my %contract_years, 'Tie::IxHash', (
23 map { $_*12 => $_ } (1..5),
27 'name' => 'Flat rate (anniversary billing)',
28 'shortname' => 'Anniversary',
29 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
31 #false laziness w/voip_cdr.pm
32 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
34 'select_options' => \%temporalities,
37 #used in cust_pkg.pm so could add to any price plan
38 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
40 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
42 'contract_end_months'=> {
43 'name' => 'Auto-add a contract end date this number of years out',
45 'select_options' => \%contract_years,
47 #used in cust_pkg.pm so could add to any price plan where it made sense
48 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
51 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
52 'with the customer\'s other packages',
55 'prorate_defer_bill' => {
56 'name' => 'When synchronizing, defer the bill until '.
57 'the customer\'s next bill date',
60 'prorate_round_day' => {
61 'name' => 'When synchronizing, round the prorated '.
62 'period to the nearest full day',
65 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
67 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
70 'unsuspend_adjust_bill' =>
71 { 'name' => 'Adjust next bill date forward when '.
75 'bill_recur_on_cancel' => {
76 'name' => 'Bill the last period on cancellation',
79 'bill_suspend_as_cancel' => {
80 'name' => 'Bill immediately upon suspension', #desc?
83 'externalid' => { 'name' => 'Optional External ID',
87 'fieldorder' => [ qw( recur_temporality
88 expire_months adjourn_months
91 sync_bill_date prorate_defer_bill prorate_round_day
92 suspend_bill unsuspend_adjust_bill
94 bill_suspend_as_cancel
103 my $conf = new FS::Conf;
104 my $money_char = $conf->config('money_char') || '$';
105 my $setup = $opt{cust_pkg} ? $self->base_setup( $opt{cust_pkg} )
106 : ($self->option('setup_fee') || 0);
107 my $recur = $opt{cust_pkg} ? $self->base_recur( $opt{cust_pkg} )
108 : ($self->option('recur_fee', 1) || 0);
109 $recur += $self->usageprice_recur( $opt{cust_pkg} ) if $opt{cust_pkg};
111 $str = $money_char . $setup . ($recur ? ' setup' : ' one-time') if $setup;
112 $str .= ', ' if ($setup && $recur);
113 $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
118 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
120 return 0 if $self->prorate_setup($cust_pkg, $sdate);
123 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
124 while ($i < $count) {
125 push @$details, $self->option( 'additional_info' . $i++ );
128 my $charge = $self->base_setup($cust_pkg, $sdate, $details);
132 $param->{'setup_charge'} = $charge;
133 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
134 delete $param->{'setup_charge'};
137 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
141 my($self, $cust_pkg, $sdate, $details ) = @_;
143 $self->option('setup_fee') || 0;
148 my($cust_pkg, $sdate, $details, $param ) = @_;
150 #my $last_bill = $cust_pkg->last_bill;
151 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
154 if $self->recur_temporality eq 'preceding' && !$last_bill;
156 my $charge = $self->base_recur($cust_pkg, $sdate);
157 # always treat cutoff_day as a list
158 if ( my @cutoff_day = $self->cutoff_day($cust_pkg) ) {
159 $charge = $self->calc_prorate(@_, @cutoff_day);
161 elsif ( $param->{freq_override} ) {
162 # XXX not sure if this should be mutually exclusive with sync_bill_date.
163 # Given the very specific problem that freq_override is meant to 'solve',
164 # it probably should.
165 $charge *= $param->{freq_override} if $param->{freq_override};
168 $charge += $self->usageprice_recur($cust_pkg, $sdate);
169 $cust_pkg->apply_usageprice(); #$sdate for prorating?
171 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
173 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
178 my $cust_pkg = shift;
179 if ( $self->option('sync_bill_date',1) ) {
180 my $next_bill = $cust_pkg->cust_main->next_bill_date;
181 if ( defined($next_bill) ) {
182 return (localtime($next_bill))[3];
189 my($self, $cust_pkg, $sdate) = @_;
190 $self->option('recur_fee', 1) || 0;
193 sub base_recur_permonth {
194 my($self, $cust_pkg) = @_;
196 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
198 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
201 sub usageprice_recur {
202 my($self, $cust_pkg, $sdate) = @_;
205 $recur += $_->price foreach $cust_pkg->cust_pkg_usageprice;
207 sprintf('%.2f', $recur);
212 my $conf = new FS::Conf;
213 if ( $self->recur_temporality eq 'preceding'
214 and $self->option('bill_recur_on_cancel', 1) ) {
215 # run another recurring cycle
216 return $self->calc_recur(@_);
218 elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option?
219 and $self->can('calc_usage') ) {
220 # bill for outstanding usage
221 return $self->calc_usage(@_);
227 my ($self, $cust_pkg, %options) = @_;
230 if ($options{'time'}) {
231 $time = $options{'time'};
236 my $next_bill = $cust_pkg->getfield('bill') || 0;
238 return 0 if ! $self->base_recur($cust_pkg, \$time)
240 || $next_bill < $time;
242 # Use actual charge for this period, not base_recur (for discounts).
243 # Use sdate < $time and edate >= $time because when billing on
244 # cancellation, edate = $time.
246 foreach my $cust_bill_pkg (
247 qsearch('cust_bill_pkg', {
248 pkgnum => $cust_pkg->pkgnum,
249 sdate => {op => '<' , value => $time},
250 edate => {op => '>=', value => $time},
251 recur => {op => '>' , value => 0},
255 # hack to deal with the weird behavior of edate on package cancellation
256 my $edate = $cust_bill_pkg->edate;
257 if ( $self->recur_temporality eq 'preceding' ) {
258 $edate = $self->add_freq($cust_bill_pkg->sdate);
261 my $amount = ($cust_bill_pkg->recur - $cust_bill_pkg->usage) *
262 ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
265 push @{ $options{'cust_credit_source_bill_pkg'} },
266 new FS::cust_credit_source_bill_pkg {
267 'billpkgnum' => $cust_bill_pkg->billpkgnum,
268 'amount' => sprintf('%.2f', $amount),
269 'currency' => $cust_bill_pkg->cust_bill->currency,
271 if $options{'cust_credit_source_bill_pkg'};
275 sprintf('%.2f', $credit);
279 sub is_free_options {
280 qw( setup_fee recur_fee );
283 sub is_prepaid { 0; } #no, we're postpaid
288 return 0 if $self->start_on_hold;
290 ! $self->option('start_1st', 1) && ( ! $self->option('sync_bill_date',1)
291 || ! $self->option('prorate_defer_bill',1)
292 || ! $opt{'num_ncancelled_pkgs'}
296 sub can_discount { 1; }
298 sub can_usageprice { 1; }
300 sub recur_temporality {
302 $self->option('recur_temporality', 1);
305 sub usage_valuehash {
307 map { $_, $self->option($_) }
308 grep { $self->option($_, 'hush') }
309 qw(seconds upbytes downbytes totalbytes);
313 my($self, $cust_pkg, %opt) = @_;
314 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
315 my %values = $self->usage_valuehash;
316 if ($self->option('usage_rollover', 1)) {
317 $cust_pkg->recharge(\%values);
319 $cust_pkg->set_usage(\%values, %opt);