allow for taxes when using "fee" event to negate credit balance, #24991
[freeside.git] / FS / FS / part_event / Action / fee.pm
1 package FS::part_event::Action::fee;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5
6 sub description { 'Late fee (flat)'; }
7
8 sub event_stage { 'pre-bill'; }
9
10 sub option_fields {
11   ( 
12     'charge'   => { label=>'Amount', type=>'money', }, # size=>7, },
13     'reason'   => 'Reason (invoice line item)',
14     'classnum' => { label=>'Package class' => type=>'select-pkg_class', },
15     'taxclass' => { label=>'Tax class', type=>'select-taxclass', },
16     'setuptax' => { label=>'Late fee is tax exempt',
17                     type=>'checkbox', value=>'Y' },
18     'nextbill' => { label=>'Hold late fee until next invoice',
19                     type=>'checkbox', value=>'Y' },
20     'limit_to_credit'=>
21                   { label=>"Charge no more than the customer's credit balance",
22                     type=>'checkbox', value=>'Y' },
23   );
24 }
25
26 sub default_weight { 10; }
27
28 sub _calc_fee {
29   my( $self, $cust_object ) = @_;
30   if ( $self->option('limit_to_credit') ) {
31     my $balance = $cust_object->cust_main->balance;
32     if ( $balance >= 0 ) {
33       return 0;
34     } elsif ( (-1 * $balance) < $self->option('charge') ) {
35       my $total = -1 * $balance;
36       # if it's tax exempt, then we're done
37       # XXX we also bail out if you're using external tax tables, because
38       # they're definitely NOT linear and we haven't yet had a reason to 
39       # make that case work.
40       return $total if $self->option('setuptax') eq 'Y'
41                     or FS::Conf->new->exists('enable_taxproducts');
42
43       # estimate tax rate
44       # false laziness with xmlhttp-calculate_taxes, cust_main::Billing, etc.
45       # XXX not accurate with monthly exemptions
46       my $cust_main = $cust_object->cust_main;
47       my $taxlisthash = {};
48       my $charge = FS::cust_bill_pkg->new({
49           setup => $total,
50           recur => 0,
51           details => []
52       });
53       my $part_pkg = FS::part_pkg->new({
54           taxclass => $self->option('taxclass')
55       });
56       my $error = $cust_main->_handle_taxes(
57         FS::part_pkg->new({ taxclass => ($self->option('taxclass') || '') }),
58         $taxlisthash,
59         $charge,
60         FS::cust_pkg->new({custnum => $cust_main->custnum}),
61       );
62       if ( $error ) {
63         warn "error estimating taxes for breakage charge: custnum ".$cust_main->custnum."\n";
64         return $total;
65       }
66       # $taxlisthash: tax identifier => [ cust_main_county, cust_bill_pkg... ]
67       my $total_rate = 0;
68       my @taxes = map { $_->[0] } values %$taxlisthash;
69       foreach (@taxes) {
70         $total_rate += $_->tax;
71       }
72       return $total if $total_rate == 0; # no taxes apply
73
74       my $total_cents = $total * 100;
75       my $charge_cents = sprintf('%.0f', $total_cents * 100/(100 + $total_rate));
76       return ($charge_cents / 100);
77     }
78   }
79
80   $self->option('charge');
81 }
82
83 sub do_action {
84   my( $self, $cust_object ) = @_;
85
86   my $cust_main = $self->cust_main($cust_object);
87
88   my $conf = new FS::Conf;
89
90   my %charge = (
91     'amount'   => $self->_calc_fee($cust_object),
92     'pkg'      => $self->option('reason'),
93     'taxclass' => $self->option('taxclass'),
94     'classnum' => ( $self->option('classnum')
95                       || scalar($conf->config('finance_pkgclass')) ),
96     'setuptax' => $self->option('setuptax'),
97   );
98
99   # amazingly, FS::cust_main::charge will allow a charge of zero
100   return '' if $charge{'amount'} == 0;
101
102   #unless its more than N months away?
103   $charge{'start_date'} = $cust_main->next_bill_date
104     if $self->option('nextbill');
105
106   my $error = $cust_main->charge( \%charge );
107
108   die $error if $error;
109
110   '';
111 }
112
113 1;