Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_event / Action / cust_bill_fee_greater_percent_or_flat.pm
1 package FS::part_event::Action::cust_bill_fee_greater_percent_or_flat;
2
3 use strict;
4 use base qw( FS::part_event::Action::fee );
5 use Tie::IxHash;
6
7 sub description { 'Late fee (greater of percentage of invoice or flat fee)'; }
8
9 sub eventtable_hashref {
10   { 'cust_bill' => 1 };
11 }
12
13 sub option_fields {
14   my $class = shift;
15
16   my $t = tie my %option_fields, 'Tie::IxHash', $class->SUPER::option_fields();
17   $t->Shift; #assumes charge is first
18   $t->Unshift( 'flat_fee'  => { label=>'Flat Fee', type=>'money', } );
19   $t->Unshift( 'percent'   => { label=>'Percent', size=>2, } );
20
21   %option_fields;
22 }
23
24 sub _calc_fee {
25   my( $self, $cust_bill ) = @_;
26   my $percent  = sprintf('%.2f', $cust_bill->owed * $self->option('percent') / 100 );
27   my $flat_fee = $self->option('flat_fee');
28
29   my $num = $flat_fee - $percent;
30   if ($num == 0) {
31     return($percent);
32   }
33   elsif ($num > 0) {
34     return($flat_fee);
35   }
36   else {
37     return($percent);
38   }
39 }
40
41 1;