default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / part_event / Action / fee.pm
1 package FS::part_event::Action::fee;
2
3 # DEPRECATED; will most likely be removed in 4.x
4
5 use strict;
6 use base qw( FS::part_event::Action );
7
8 sub description { 'Late fee (flat)'; }
9
10 sub event_stage { 'pre-bill'; }
11
12 sub option_fields {
13   ( 
14     'charge'   => { label=>'Amount', type=>'money', }, # size=>7, },
15     'reason'   => 'Reason (invoice line item)',
16     'classnum' => { label=>'Package class' => type=>'select-pkg_class', },
17     'taxclass' => { label=>'Tax class', type=>'select-taxclass', },
18     'setuptax' => { label=>'Late fee is tax exempt',
19                     type=>'checkbox', value=>'Y' },
20     'nextbill' => { label=>'Hold late fee until next invoice',
21                     type=>'checkbox', value=>'Y' },
22     'limit_to_credit'=>
23                   { label=>"Charge no more than the customer's credit balance",
24                     type=>'checkbox', value=>'Y' },
25   );
26 }
27
28 sub default_weight { 10; }
29
30 sub _calc_fee {
31   my( $self, $cust_object ) = @_;
32   if ( $self->option('limit_to_credit') ) {
33     my $balance = $cust_object->cust_main->balance;
34     if ( $balance >= 0 ) {
35       return 0;
36     } elsif ( (-1 * $balance) < $self->option('charge') ) {
37       my $total = -1 * $balance;
38       # if it's tax exempt, then we're done
39       # XXX we also bail out if you're using external tax tables, because
40       # they're definitely NOT linear and we haven't yet had a reason to 
41       # make that case work.
42       return $total if $self->option('setuptax') eq 'Y'
43                     or FS::Conf->new->config('tax_data_vendor');
44
45       # estimate tax rate
46       # false laziness with xmlhttp-calculate_taxes, cust_main::Billing, etc.
47       # XXX not accurate with monthly exemptions
48       my $cust_main = $cust_object->cust_main;
49       my $taxlisthash = {};
50       my $charge = FS::cust_bill_pkg->new({
51           setup => $total,
52           recur => 0,
53           details => []
54       });
55       my $part_pkg = FS::part_pkg->new({
56           taxclass => $self->option('taxclass')
57       });
58       my $error = $cust_main->_handle_taxes( $taxlisthash, $charge,
59         location  => $cust_main->ship_location,
60         part_item => $part_pkg,
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;