1 package FS::part_pkg::flat;
4 use base qw( FS::part_pkg
5 FS::part_pkg::prorate_Mixin
6 FS::part_pkg::discount_Mixin
8 use vars qw( %info %usage_recharge_fields @usage_recharge_fieldorder );
10 use List::Util qw(min); # max);
11 #use FS::Record qw(qsearch);
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 '.
75 'externalid' => { 'name' => 'Optional External ID',
79 'fieldorder' => [ qw( recur_temporality
80 expire_months adjourn_months
83 sync_bill_date prorate_defer_bill prorate_round_day
84 suspend_bill unsuspend_adjust_bill
92 my $conf = new FS::Conf;
93 my $money_char = $conf->config('money_char') || '$';
94 my $setup = $self->option('setup_fee') || 0;
95 my $recur = $self->option('recur_fee', 1) || 0;
97 $str = $money_char . $setup . ' one-time' if $setup;
98 $str .= ', ' if ($setup && $recur);
99 $str .= $money_char . $recur . ' recurring ' if $recur;
104 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
106 return 0 if $self->prorate_setup($cust_pkg, $sdate);
109 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
110 while ($i < $count) {
111 push @$details, $self->option( 'additional_info' . $i++ );
114 my $quantity = $cust_pkg->quantity || 1;
116 my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
120 $param->{'setup_charge'} = $charge;
121 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
122 delete $param->{'setup_charge'};
125 sprintf('%.2f', $charge - $discount);
129 my($self, $cust_pkg, $sdate, $details ) = @_;
131 $self->option('setup_fee') || 0;
136 my($cust_pkg, $sdate, $details, $param ) = @_;
138 #my $last_bill = $cust_pkg->last_bill;
139 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
142 if $self->recur_temporality eq 'preceding' && $last_bill == 0;
144 my $charge = $self->base_recur($cust_pkg, $sdate);
145 if ( my $cutoff_day = $self->cutoff_day($cust_pkg) ) {
146 $charge = $self->calc_prorate(@_, $cutoff_day);
148 elsif ( $param->{freq_override} ) {
149 # XXX not sure if this should be mutually exclusive with sync_bill_date.
150 # Given the very specific problem that freq_override is meant to 'solve',
151 # it probably should.
152 $charge *= $param->{freq_override} if $param->{freq_override};
155 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
156 return sprintf('%.2f', $charge - $discount);
161 my $cust_pkg = shift;
162 if ( $self->option('sync_bill_date',1) ) {
163 my $next_bill = $cust_pkg->cust_main->next_bill_date;
164 if ( defined($next_bill) ) {
165 return (localtime($next_bill))[3];
172 my($self, $cust_pkg, $sdate) = @_;
173 $self->option('recur_fee', 1) || 0;
176 sub base_recur_permonth {
177 my($self, $cust_pkg) = @_;
179 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
181 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
185 my ($self, $cust_pkg, %options) = @_;
188 if ($options{'time'}) {
189 $time = $options{'time'};
194 my $next_bill = $cust_pkg->getfield('bill') || 0;
196 return 0 if ! $self->base_recur($cust_pkg, \$time)
198 || $next_bill < $time;
201 'h' => 3600, # 60 * 60
202 'd' => 86400, # 60 * 60 * 24
203 'w' => 604800, # 60 * 60 * 24 * 7
204 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
207 $self->freq =~ /^(\d+)([hdwm]?)$/
208 or die 'unparsable frequency: '. $self->freq;
209 my $freq_sec = $1 * $sec{$2||'m'};
210 return 0 unless $freq_sec;
212 sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
216 sub is_free_options {
217 qw( setup_fee recur_fee );
220 sub is_prepaid { 0; } #no, we're postpaid
222 sub can_start_date { ! shift->option('start_1st', 1) }
224 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
227 $self->freq =~ /^\d+$/ && $self->freq > 0;
230 sub recur_temporality {
232 $self->option('recur_temporality', 1);
235 sub usage_valuehash {
237 map { $_, $self->option($_) }
238 grep { $self->option($_, 'hush') }
239 qw(seconds upbytes downbytes totalbytes);
243 my($self, $cust_pkg, %opt) = @_;
244 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
245 my %values = $self->usage_valuehash;
246 if ($self->option('usage_rollover', 1)) {
247 $cust_pkg->recharge(\%values);
249 $cust_pkg->set_usage(\%values, %opt);