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 );
11 use List::Util qw( min );
12 use FS::UI::bytecount;
15 tie my %temporalities, 'Tie::IxHash',
16 'upcoming' => "Upcoming (future)",
17 'preceding' => "Preceding (past)",
20 tie my %contract_years, 'Tie::IxHash', (
22 map { $_*12 => $_ } (1..5),
26 'name' => 'Flat rate (anniversary billing)',
27 'shortname' => 'Anniversary',
28 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
30 #false laziness w/voip_cdr.pm
31 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
33 'select_options' => \%temporalities,
36 #used in cust_pkg.pm so could add to any price plan
37 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
39 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
41 'contract_end_months'=> {
42 'name' => 'Auto-add a contract end date this number of years out',
44 'select_options' => \%contract_years,
46 #used in cust_pkg.pm so could add to any price plan where it made sense
47 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
50 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
51 'with the customer\'s other packages',
54 'prorate_defer_bill' => {
55 'name' => 'When synchronizing, defer the bill until '.
56 'the customer\'s next bill date',
59 'prorate_round_day' => {
60 'name' => 'When synchronizing, round the prorated '.
61 'period to the nearest full day',
64 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
66 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
69 'unsuspend_adjust_bill' =>
70 { 'name' => 'Adjust next bill date forward when '.
74 'bill_recur_on_cancel' => {
75 'name' => 'Bill the last period on cancellation',
78 'bill_suspend_as_cancel' => {
79 'name' => 'Bill immediately upon suspension', #desc?
82 'externalid' => { 'name' => 'Optional External ID',
86 'fieldorder' => [ qw( recur_temporality
87 expire_months adjourn_months
90 sync_bill_date prorate_defer_bill prorate_round_day
91 suspend_bill unsuspend_adjust_bill
93 bill_suspend_as_cancel
101 my $conf = new FS::Conf;
102 my $money_char = $conf->config('money_char') || '$';
103 my $setup = $self->option('setup_fee') || 0;
104 my $recur = $self->option('recur_fee', 1) || 0;
106 $str = $money_char . $setup . ($recur ? ' setup ' : ' one-time') if $setup;
107 $str .= ', ' if ($setup && $recur);
108 $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
113 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
115 return 0 if $self->prorate_setup($cust_pkg, $sdate);
118 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
119 while ($i < $count) {
120 push @$details, $self->option( 'additional_info' . $i++ );
123 my $quantity = $cust_pkg->quantity || 1;
125 my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
129 $param->{'setup_charge'} = $charge;
130 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
131 delete $param->{'setup_charge'};
134 sprintf('%.2f', $charge - $discount);
138 my($self, $cust_pkg, $sdate, $details ) = @_;
140 $self->option('setup_fee') || 0;
145 my($cust_pkg, $sdate, $details, $param ) = @_;
147 #my $last_bill = $cust_pkg->last_bill;
148 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
151 if $self->recur_temporality eq 'preceding' && !$last_bill;
153 my $charge = $self->base_recur($cust_pkg, $sdate);
154 # always treat cutoff_day as a list
155 if ( my @cutoff_day = $self->cutoff_day($cust_pkg) ) {
156 $charge = $self->calc_prorate(@_, @cutoff_day);
158 elsif ( $param->{freq_override} ) {
159 # XXX not sure if this should be mutually exclusive with sync_bill_date.
160 # Given the very specific problem that freq_override is meant to 'solve',
161 # it probably should.
162 $charge *= $param->{freq_override} if $param->{freq_override};
165 my $quantity = $cust_pkg->quantity || 1;
166 $charge *= $quantity;
168 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
169 return sprintf('%.2f', $charge - $discount);
174 my $cust_pkg = shift;
175 if ( $self->option('sync_bill_date',1) ) {
176 my $next_bill = $cust_pkg->cust_main->next_bill_date;
177 if ( defined($next_bill) ) {
178 return (localtime($next_bill))[3];
185 my($self, $cust_pkg, $sdate) = @_;
186 $self->option('recur_fee', 1) || 0;
189 sub base_recur_permonth {
190 my($self, $cust_pkg) = @_;
192 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
194 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
199 my $conf = new FS::Conf;
200 if ( $self->recur_temporality eq 'preceding'
201 and $self->option('bill_recur_on_cancel', 1) ) {
202 # run another recurring cycle
203 return $self->calc_recur(@_);
205 elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option?
206 and $self->can('calc_usage') ) {
207 # bill for outstanding usage
208 return $self->calc_usage(@_);
214 my ($self, $cust_pkg, %options) = @_;
217 if ($options{'time'}) {
218 $time = $options{'time'};
223 my $next_bill = $cust_pkg->getfield('bill') || 0;
225 return 0 if ! $self->base_recur($cust_pkg, \$time)
227 || $next_bill < $time;
229 # Use actual charge for this period, not base_recur (for discounts).
230 # Use sdate < $time and edate >= $time because when billing on
231 # cancellation, edate = $time.
234 qsearch('cust_bill_pkg', {
235 pkgnum => $cust_pkg->pkgnum,
236 sdate => {op => '<' , value => $time},
237 edate => {op => '>=', value => $time},
238 recur => {op => '>' , value => 0},
241 # hack to deal with the weird behavior of edate on package cancellation
242 my $edate = $item->edate;
243 if ( $self->recur_temporality eq 'preceding' ) {
244 $edate = $self->add_freq($item->sdate);
246 $credit += ($item->recur - $item->usage) *
247 ($edate - $time) / ($edate - $item->sdate);
249 sprintf('%.2f', $credit);
250 #sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
254 sub is_free_options {
255 qw( setup_fee recur_fee );
258 sub is_prepaid { 0; } #no, we're postpaid
260 sub can_start_date { ! shift->option('start_1st', 1) }
262 sub can_discount { 1; }
264 sub recur_temporality {
266 $self->option('recur_temporality', 1);
269 sub usage_valuehash {
271 map { $_, $self->option($_) }
272 grep { $self->option($_, 'hush') }
273 qw(seconds upbytes downbytes totalbytes);
277 my($self, $cust_pkg, %opt) = @_;
278 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
279 my %values = $self->usage_valuehash;
280 if ($self->option('usage_rollover', 1)) {
281 $cust_pkg->recharge(\%values);
283 $cust_pkg->set_usage(\%values, %opt);