merge NG auth, RT#21563
[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       return -1 * $balance;
36     }
37   }
38
39   $self->option('charge');
40 }
41
42 sub do_action {
43   my( $self, $cust_object ) = @_;
44
45   my $cust_main = $self->cust_main($cust_object);
46
47   my $conf = new FS::Conf;
48
49   my %charge = (
50     'amount'   => $self->_calc_fee($cust_object),
51     'pkg'      => $self->option('reason'),
52     'taxclass' => $self->option('taxclass'),
53     'classnum' => ( $self->option('classnum')
54                       || scalar($conf->config('finance_pkgclass')) ),
55     'setuptax' => $self->option('setuptax'),
56   );
57
58   # amazingly, FS::cust_main::charge will allow a charge of zero
59   return '' if $charge{'amount'} == 0;
60
61   #unless its more than N months away?
62   $charge{'start_date'} = $cust_main->next_bill_date
63     if $self->option('nextbill');
64
65   my $error = $cust_main->charge( \%charge );
66
67   die $error if $error;
68
69   '';
70 }
71
72 1;