4 use base qw( FS::o2m_Common FS::Record );
6 use FS::Record qw( qsearch qsearchs );
8 use FS::part_pkg_taxproduct;
10 use FS::part_fee_usage;
16 FS::part_fee - Object methods for part_fee records
22 $record = new FS::part_fee \%hash;
23 $record = new FS::part_fee { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::part_fee object represents the definition of a fee
37 Fees are like packages, but instead of being ordered and then billed on a
38 cycle, they are created by the operation of events and added to a single
39 invoice. The fee definition specifies the fee's description, how the amount
40 is calculated (a flat fee or a percentage of the customer's balance), and
41 how to classify the fee for tax and reporting purposes.
43 FS::part_fee inherits from FS::Record. The following fields are currently
48 =item feepart - primary key
50 =item comment - a description of the fee for employee use, not shown on
53 =item disabled - 'Y' if the fee is disabled
55 =item classnum - the L<FS::pkg_class> that the fee belongs to, for reporting
57 =item taxable - 'Y' if this fee should be considered a taxable sale.
58 Currently, taxable fees will be treated like they exist at the customer's
59 default service location.
61 =item taxclass - the tax class the fee belongs to, as a string, for the
64 =item taxproductnum - the tax product family the fee belongs to, for the
65 external tax system in use, if any
67 =item pay_weight - Weight (relative to credit_weight and other package/fee
68 definitions) that controls payment application to specific line items.
70 =item credit_weight - Weight that controls credit application to specific
73 =item agentnum - the agent (L<FS::agent>) who uses this fee definition.
75 =item amount - the flat fee to charge, as a decimal amount
77 =item percent - the percentage of the base to charge (out of 100). If both
78 this and "amount" are specified, the fee will be the sum of the two.
80 =item basis - the method for calculating the base: currently one of "charged",
83 =item minimum - the minimum fee that should be charged
85 =item maximum - the maximum fee that should be charged
87 =item limit_credit - 'Y' to set the maximum fee at the customer's credit
90 =item setuprecur - whether the fee should be classified as 'setup' or
91 'recur', for reporting purposes.
101 Creates a new fee definition. To add the record to the database, see
106 sub table { 'part_fee'; }
110 Adds this record to the database. If there is an error, returns the error,
111 otherwise returns false.
115 Delete this record from the database.
117 =item replace OLD_RECORD
119 Replaces the OLD_RECORD with this one in the database. If there is an error,
120 returns the error, otherwise returns false.
124 Checks all fields to make sure this is a valid example. If there is
125 an error, returns the error, otherwise returns false. Called by the insert
133 $self->set('amount', 0) unless $self->amount;
134 $self->set('percent', 0) unless $self->percent;
137 $self->ut_numbern('feepart')
138 || $self->ut_textn('comment')
139 || $self->ut_flag('disabled')
140 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
141 || $self->ut_flag('taxable')
142 || $self->ut_textn('taxclass')
143 || $self->ut_numbern('taxproductnum')
144 || $self->ut_floatn('pay_weight')
145 || $self->ut_floatn('credit_weight')
146 || $self->ut_agentnum_acl('agentnum',
147 [ 'Edit global package definitions' ])
148 || $self->ut_money('amount')
149 || $self->ut_float('percent')
150 || $self->ut_moneyn('minimum')
151 || $self->ut_moneyn('maximum')
152 || $self->ut_flag('limit_credit')
153 || $self->ut_enum('basis', [ 'charged', 'owed', 'usage' ])
154 || $self->ut_enum('setuprecur', [ 'setup', 'recur' ])
156 return $error if $error;
158 if ( $self->get('limit_credit') ) {
159 $self->set('maximum', '');
162 if ( $self->get('basis') eq 'usage' ) {
163 # to avoid confusion, don't also allow charging a percentage
164 $self->set('percent', 0);
172 Returns a string describing how this fee is calculated.
178 # XXX customer currency
179 my $money_char = FS::Conf->new->config('money_char') || '$';
180 my $money = $money_char . '%.2f';
181 my $percent = '%.1f%%';
183 if ( $self->amount > 0 ) {
184 $string = sprintf($money, $self->amount);
186 if ( $self->percent > 0 ) {
190 $string .= sprintf($percent, $self->percent);
191 $string .= ' of the ';
192 if ( $self->basis eq 'charged' ) {
193 $string .= 'invoice amount';
194 } elsif ( $self->basis('owed') ) {
195 $string .= 'unpaid invoice balance';
197 } elsif ( $self->basis eq 'usage' ) {
199 $string .= " plus \n";
201 # append per-class descriptions
202 $string .= join("\n", map { $_->explanation } $self->part_fee_usage);
205 if ( $self->minimum or $self->maximum or $self->limit_credit ) {
207 if ( $self->minimum ) {
208 $string .= ' at least '.sprintf($money, $self->minimum);
210 if ( $self->maximum ) {
211 $string .= ' and' if $self->minimum;
212 $string .= ' at most '.sprintf($money, $self->maximum);
214 if ( $self->limit_credit ) {
215 if ( $self->maximum ) {
216 $string .= ", or the customer's credit balance, whichever is less.";
218 $string .= ' and' if $self->minimum;
219 $string .= " not more than the customer's credit balance";
226 =item lineitem INVOICE
228 Given INVOICE (an L<FS::cust_bill>), returns an L<FS::cust_bill_pkg> object
229 representing the invoice line item for the fee, with linked
230 L<FS::cust_bill_pkg_fee> record(s) allocating the fee to the invoice or
231 its line items, as appropriate.
233 If the fee is going to be charged on the upcoming invoice (credit card
234 processing fees, postal invoice fees), INVOICE should be an uninserted
235 L<FS::cust_bill> object where the 'cust_bill_pkg' property is an arrayref
236 of the non-fee line items that will appear on the invoice.
242 my $cust_bill = shift;
243 my $cust_main = $cust_bill->cust_main;
245 my $amount = 0 + $self->get('amount');
246 my $total_base; # sum of base line items
247 my @items; # base line items (cust_bill_pkg records)
248 my @item_base; # charged/owed of that item (sequential w/ @items)
249 my @item_fee; # fee amount of that item (sequential w/ @items)
250 my @cust_bill_pkg_fee; # link record
252 warn "Calculating fee: ".$self->itemdesc." on ".
253 ($cust_bill->invnum ? "invoice #".$cust_bill->invnum : "current invoice").
255 my $basis = $self->basis;
257 # $total_base: the total charged/owed on the invoice
258 # %item_base: billpkgnum => fraction of base amount
259 if ( $cust_bill->invnum ) {
261 # calculate the fee on an already-inserted past invoice. This may have
262 # payments or credits, so if basis = owed, we need to consider those.
263 @items = $cust_bill->cust_bill_pkg;
264 if ( $basis ne 'usage' ) {
266 $total_base = $cust_bill->$basis; # "charged", "owed"
267 my $basis_sql = $basis.'_sql';
268 my $sql = 'SELECT ' . FS::cust_bill_pkg->$basis_sql .
269 ' FROM cust_bill_pkg WHERE billpkgnum = ?';
270 @item_base = map { FS::Record->scalar_sql($sql, $_->billpkgnum) }
274 # the fee applies to _this_ invoice. It has no payments or credits, so
275 # "charged" and "owed" basis are both just the invoice amount, and
276 # the line item amounts (setup + recur)
277 @items = @{ $cust_bill->get('cust_bill_pkg') };
278 if ( $basis ne 'usage' ) {
279 $total_base = $cust_bill->charged;
280 @item_base = map { $_->setup + $_->recur }
285 if ( $basis eq 'usage' ) {
287 my %part_fee_usage = map { $_->classnum => $_ } $self->part_fee_usage;
289 foreach my $item (@items) { # cust_bill_pkg objects
291 $item->regularize_details;
293 if ( $item->billpkgnum ) {
295 qsearch('cust_bill_pkg_detail', { billpkgnum => $item->billpkgnum })
298 $details = $item->get('details') || [];
300 foreach my $d (@$details) {
301 # if there's a usage fee defined for this class...
302 next if $d->amount eq '' # not a real usage detail
303 or $d->amount == 0 # zero charge, probably shouldn't charge fee
305 my $p = $part_fee_usage{$d->classnum} or next;
306 $usage_fee += ($d->amount * $p->percent / 100)
308 # we'd create detail records here if we were doing that
310 # bypass @item_base entirely
311 push @item_fee, $usage_fee;
312 $amount += $usage_fee;
315 } # if $basis eq 'usage'
317 if ( $self->minimum ne '' and $amount < $self->minimum ) {
318 warn "Applying mininum fee\n" if $DEBUG;
319 $amount = $self->minimum;
322 my $maximum = $self->maximum;
323 if ( $self->limit_credit ) {
324 my $balance = $cust_bill->cust_main->balance;
325 if ( $balance >= 0 ) {
326 warn "Credit balance is zero, so fee is zero" if $DEBUG;
327 return; # don't bother doing estimated tax, etc.
328 } elsif ( -1 * $balance < $maximum ) {
329 $maximum = -1 * $balance;
332 if ( $maximum ne '' and $amount > $maximum ) {
333 warn "Applying maximum fee\n" if $DEBUG;
337 # at this point, if the fee is zero, return nothing
338 return if $amount < 0.005;
339 $amount = sprintf('%.2f', $amount);
341 my $cust_bill_pkg = FS::cust_bill_pkg->new({
342 feepart => $self->feepart,
344 # no sdate/edate, right?
349 if ( $maximum and $self->taxable ) {
350 warn "Estimating taxes on fee.\n" if $DEBUG;
351 # then we need to estimate tax to respect the maximum
352 # XXX currently doesn't work with external (tax_rate) taxes
353 # or batch taxes, obviously
354 my $taxlisthash = {};
355 my $error = $cust_main->_handle_taxes(
358 location => $cust_main->ship_location
361 # $taxlisthash: tax identifier => [ cust_main_county, cust_bill_pkg... ]
362 my @taxes = map { $_->[0] } values %$taxlisthash;
364 $total_rate += $_->tax;
366 if ($total_rate > 0) {
367 my $max_cents = $maximum * 100;
368 my $charge_cents = sprintf('%0.f', $max_cents * 100/(100 + $total_rate));
369 # the actual maximum that we can charge...
370 $maximum = sprintf('%.2f', $charge_cents / 100.00);
371 $amount = $maximum if $amount > $maximum;
373 } # if $maximum and $self->taxable
375 # set the amount that we'll charge
376 $cust_bill_pkg->set( $self->setuprecur, $amount );
378 if ( $self->classnum ) {
379 my $pkg_category = $self->pkg_class->pkg_category;
380 $cust_bill_pkg->set('section' => $pkg_category->categoryname)
384 # if this is a percentage fee and has line item fractions,
385 # adjust them to be proportional and to add up correctly.
387 my $cents = $amount * 100;
388 # not necessarily the same as percent
389 my $multiplier = $amount / $total_base;
390 for (my $i = 0; $i < scalar(@items); $i++) {
391 my $fee = sprintf('%.2f', $item_base[$i] * $multiplier);
392 $item_fee[$i] = $fee;
393 $cents -= $fee * 100;
395 # correct rounding error
396 while ($cents >= 0.5 or $cents < -0.5) {
397 foreach my $fee (@item_fee) {
398 if ( $cents >= 0.5 ) {
401 } elsif ( $cents < -0.5 ) {
409 # add allocation records to the cust_bill_pkg
410 for (my $i = 0; $i < scalar(@items); $i++) {
411 if ( $item_fee[$i] > 0 ) {
412 push @cust_bill_pkg_fee, FS::cust_bill_pkg_fee->new({
413 cust_bill_pkg => $cust_bill_pkg,
414 base_invnum => $cust_bill->invnum, # may be null
415 amount => $item_fee[$i],
416 base_cust_bill_pkg => $items[$i], # late resolve
420 } else { # if !@item_fee
421 # then this isn't a proportional fee, so it just applies to the
423 push @cust_bill_pkg_fee, FS::cust_bill_pkg_fee->new({
424 cust_bill_pkg => $cust_bill_pkg,
425 base_invnum => $cust_bill->invnum, # may be null
430 # cust_bill_pkg::insert will handle this
431 $cust_bill_pkg->set('cust_bill_pkg_fee', \@cust_bill_pkg_fee);
432 # avoid misbehavior by usage() and some other things
433 $cust_bill_pkg->set('details', []);
435 return $cust_bill_pkg;
438 =item itemdesc_locale LOCALE
440 Returns a customer-viewable description of this fee for the given locale,
441 from the part_fee_msgcat table. If the locale is empty or no localized fee
442 description exists, returns part_fee.itemdesc.
446 sub itemdesc_locale {
447 my ( $self, $locale ) = @_;
448 return $self->itemdesc unless $locale;
449 my $part_fee_msgcat = qsearchs('part_fee_msgcat', {
450 feepart => $self->feepart,
452 }) or return $self->itemdesc;
453 $part_fee_msgcat->itemdesc;
456 =item tax_rates DATA_PROVIDER, GEOCODE
458 Returns the external taxes (L<FS::tax_rate> objects) that apply to this
459 fee, in the location specified by GEOCODE.
465 my ($vendor, $geocode) = @_;
466 return unless $self->taxproductnum;
467 my $taxproduct = FS::part_pkg_taxproduct->by_key($self->taxproductnum);
469 my @taxclassnums = map { $_->taxclassnum }
470 $taxproduct->part_pkg_taxrate($geocode);
471 return unless @taxclassnums;
473 warn "Found taxclassnum values of ". join(',', @taxclassnums) ."\n"
475 my $extra_sql = "AND taxclassnum IN (". join(',', @taxclassnums) . ")";
476 my @taxes = qsearch({ 'table' => 'tax_rate',
477 'hashref' => { 'geocode' => $geocode,
478 'data_vendor' => $vendor },
479 'extra_sql' => $extra_sql,
481 warn "Found taxes ". join(',', map {$_->taxnum} @taxes) ."\n"
487 sub part_pkg_taxoverride {} # we don't do overrides here
491 return ($self->taxproductnum ? 1 : 0);
494 # stubs that will go away under 4.x
499 ? FS::pkg_class->by_key($self->classnum)
503 sub part_pkg_taxproduct {
506 ? FS::part_pkg_taxproduct->by_key($self->taxproductnum)
513 ? FS::agent->by_key($self->agentnum)
517 sub part_fee_msgcat {
519 qsearch( 'part_fee_msgcat', { feepart => $self->feepart } );
524 qsearch( 'part_fee_usage', { feepart => $self->feepart } );