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 );
10 use List::Util qw( min );
11 use FS::UI::bytecount;
14 tie my %temporalities, 'Tie::IxHash',
15 'upcoming' => "Upcoming (future)",
16 'preceding' => "Preceding (past)",
19 tie my %contract_years, 'Tie::IxHash', (
21 map { $_*12 => $_ } (1..5),
25 'name' => 'Flat rate (anniversary billing)',
26 'shortname' => 'Anniversary',
27 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
29 #false laziness w/voip_cdr.pm
30 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
32 'select_options' => \%temporalities,
35 #used in cust_pkg.pm so could add to any price plan
36 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
38 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
40 'contract_end_months'=> {
41 'name' => 'Auto-add a contract end date this number of years out',
43 'select_options' => \%contract_years,
45 #used in cust_pkg.pm so could add to any price plan where it made sense
46 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
49 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
50 'with the customer\'s other packages',
53 'prorate_defer_bill' => {
54 'name' => 'When synchronizing, defer the bill until '.
55 'the customer\'s next bill date',
58 'prorate_round_day' => {
59 'name' => 'When synchronizing, round the prorated '.
60 'period to the nearest full day',
63 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
65 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
68 'unsuspend_adjust_bill' =>
69 { 'name' => 'Adjust next bill date forward when '.
74 'externalid' => { 'name' => 'Optional External ID',
78 'fieldorder' => [ qw( recur_temporality
79 expire_months adjourn_months
82 sync_bill_date prorate_defer_bill prorate_round_day
83 suspend_bill unsuspend_adjust_bill
91 my $conf = new FS::Conf;
92 my $money_char = $conf->config('money_char') || '$';
93 my $setup = $self->option('setup_fee') || 0;
94 my $recur = $self->option('recur_fee', 1) || 0;
96 $str = $money_char . $setup . ' one-time' if $setup;
97 $str .= ', ' if ($setup && $recur);
98 $str .= $money_char . $recur . ' recurring ' if $recur;
103 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
105 return 0 if $self->prorate_setup($cust_pkg, $sdate);
108 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
109 while ($i < $count) {
110 push @$details, $self->option( 'additional_info' . $i++ );
113 my $quantity = $cust_pkg->quantity || 1;
115 my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
119 $param->{'setup_charge'} = $charge;
120 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
121 delete $param->{'setup_charge'};
124 sprintf('%.2f', $charge - $discount);
128 my($self, $cust_pkg, $sdate, $details ) = @_;
130 $self->option('setup_fee') || 0;
135 my($cust_pkg, $sdate, $details, $param ) = @_;
137 #my $last_bill = $cust_pkg->last_bill;
138 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
141 if $self->recur_temporality eq 'preceding' && $last_bill == 0;
143 my $charge = $self->base_recur($cust_pkg, $sdate);
144 if ( my $cutoff_day = $self->cutoff_day($cust_pkg) ) {
145 $charge = $self->calc_prorate(@_, $cutoff_day);
147 elsif ( $param->{freq_override} ) {
148 # XXX not sure if this should be mutually exclusive with sync_bill_date.
149 # Given the very specific problem that freq_override is meant to 'solve',
150 # it probably should.
151 $charge *= $param->{freq_override} if $param->{freq_override};
154 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
155 return sprintf('%.2f', $charge - $discount);
160 my $cust_pkg = shift;
161 if ( $self->option('sync_bill_date',1) ) {
162 my $next_bill = $cust_pkg->cust_main->next_bill_date;
163 if ( defined($next_bill) ) {
164 return (localtime($next_bill))[3];
171 my($self, $cust_pkg, $sdate) = @_;
172 $self->option('recur_fee', 1) || 0;
175 sub base_recur_permonth {
176 my($self, $cust_pkg) = @_;
178 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
180 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
184 my ($self, $cust_pkg, %options) = @_;
187 if ($options{'time'}) {
188 $time = $options{'time'};
193 my $next_bill = $cust_pkg->getfield('bill') || 0;
195 return 0 if ! $self->base_recur($cust_pkg, \$time)
197 || $next_bill < $time;
200 'h' => 3600, # 60 * 60
201 'd' => 86400, # 60 * 60 * 24
202 'w' => 604800, # 60 * 60 * 24 * 7
203 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
206 $self->freq =~ /^(\d+)([hdwm]?)$/
207 or die 'unparsable frequency: '. $self->freq;
208 my $freq_sec = $1 * $sec{$2||'m'};
209 return 0 unless $freq_sec;
211 sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
215 sub is_free_options {
216 qw( setup_fee recur_fee );
219 sub is_prepaid { 0; } #no, we're postpaid
221 sub can_start_date { ! shift->option('start_1st', 1) }
223 sub can_discount { 1; }
225 sub recur_temporality {
227 $self->option('recur_temporality', 1);
230 sub usage_valuehash {
232 map { $_, $self->option($_) }
233 grep { $self->option($_, 'hush') }
234 qw(seconds upbytes downbytes totalbytes);
238 my($self, $cust_pkg, %opt) = @_;
239 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
240 my %values = $self->usage_valuehash;
241 if ($self->option('usage_rollover', 1)) {
242 $cust_pkg->recharge(\%values);
244 $cust_pkg->set_usage(\%values, %opt);