1 package FS::part_pkg::flat;
4 use vars qw( @ISA %info
5 %usage_recharge_fields @usage_recharge_fieldorder
8 use List::Util qw(min); # max);
9 #use FS::Record qw(qsearch);
10 use FS::UI::bytecount;
14 @ISA = qw(FS::part_pkg
15 FS::part_pkg::prorate_Mixin
16 FS::part_pkg::discount_Mixin);
18 tie my %temporalities, 'Tie::IxHash',
19 'upcoming' => "Upcoming (future)",
20 'preceding' => "Preceding (past)",
23 tie my %contract_years, 'Tie::IxHash', (
25 map { $_*12 => $_ } (1..5),
29 'name' => 'Flat rate (anniversary billing)',
30 'shortname' => 'Anniversary',
31 'inherit_fields' => [ 'usage_Mixin', 'global_Mixin' ],
33 #false laziness w/voip_cdr.pm
34 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
36 'select_options' => \%temporalities,
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',
42 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
44 'contract_end_months'=> {
45 'name' => 'Auto-add a contract end date this number of years out',
47 'select_options' => \%contract_years,
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.',
53 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
54 'with the customer\'s other packages',
57 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
60 'unsuspend_adjust_bill' =>
61 { 'name' => 'Adjust next bill date forward when '.
66 'externalid' => { 'name' => 'Optional External ID',
70 'fieldorder' => [ qw( recur_temporality
71 expire_months adjourn_months
73 start_1st sync_bill_date
74 suspend_bill unsuspend_adjust_bill
82 my $conf = new FS::Conf;
83 my $money_char = $conf->config('money_char') || '$';
84 my $setup = $self->option('setup_fee') || 0;
85 my $recur = $self->option('recur_fee', 1) || 0;
87 $str = $money_char . $setup . ' one-time' if $setup;
88 $str .= ', ' if ($setup && $recur);
89 $str .= $money_char . $recur . ' recurring ' if $recur;
94 my($self, $cust_pkg, $sdate, $details ) = @_;
97 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
99 push @$details, $self->option( 'additional_info' . $i++ );
102 my $quantity = $cust_pkg->quantity || 1;
104 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
108 my($self, $cust_pkg, $sdate, $details ) = @_;
110 $self->option('setup_fee') || 0;
115 my($cust_pkg, $sdate, $details, $param ) = @_;
117 #my $last_bill = $cust_pkg->last_bill;
118 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
121 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
123 my $charge = $self->base_recur($cust_pkg, $sdate);
124 if ( $self->option('sync_bill_date',1) ) {
125 my $next_bill = $cust_pkg->cust_main->next_bill_date;
126 if ( defined($next_bill) ) {
127 my $cutoff_day = (localtime($next_bill))[3];
128 $charge = $self->calc_prorate(@_, $cutoff_day);
131 elsif ( $param->{freq_override} ) {
132 # XXX not sure if this should be mutually exclusive with sync_bill_date.
133 # Given the very specific problem that freq_override is meant to 'solve',
134 # it probably should.
135 $charge *= $param->{freq_override} if $param->{freq_override};
138 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
139 return sprintf('%.2f', $charge - $discount);
143 my($self, $cust_pkg, $sdate) = @_;
144 $self->option('recur_fee', 1) || 0;
147 sub base_recur_permonth {
148 my($self, $cust_pkg) = @_;
150 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
152 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
156 my ($self, $cust_pkg, %options) = @_;
159 if ($options{'time'}) {
160 $time = $options{'time'};
165 my $next_bill = $cust_pkg->getfield('bill') || 0;
167 return 0 if ! $self->base_recur($cust_pkg, \$time)
169 || $next_bill < $time;
172 'h' => 3600, # 60 * 60
173 'd' => 86400, # 60 * 60 * 24
174 'w' => 604800, # 60 * 60 * 24 * 7
175 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
178 $self->freq =~ /^(\d+)([hdwm]?)$/
179 or die 'unparsable frequency: '. $self->freq;
180 my $freq_sec = $1 * $sec{$2||'m'};
181 return 0 unless $freq_sec;
183 sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
187 sub is_free_options {
188 qw( setup_fee recur_fee );
191 sub is_prepaid { 0; } #no, we're postpaid
193 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
196 $self->freq =~ /^\d+$/ && $self->freq > 0;
199 sub usage_valuehash {
201 map { $_, $self->option($_) }
202 grep { $self->option($_, 'hush') }
203 qw(seconds upbytes downbytes totalbytes);
207 my($self, $cust_pkg, %opt) = @_;
208 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
209 my %values = $self->usage_valuehash;
210 if ($self->option('usage_rollover', 1)) {
211 $cust_pkg->recharge(\%values);
213 $cust_pkg->set_usage(\%values, %opt);