commissions based on actual invoiced amounts, RT#21002
[freeside.git] / FS / FS / part_event / Action / Mixin / credit_pkg.pm
1 package FS::part_event::Action::Mixin::credit_pkg;
2
3 use strict;
4
5 sub eventtable_hashref {
6   { 'cust_pkg' => 1 };
7 }
8
9 sub option_fields {
10   ( 
11     'reasonnum' => { 'label'        => 'Credit reason',
12                      'type'         => 'select-reason',
13                      'reason_class' => 'R',
14                    },
15     'percent'   => { 'label'   => 'Percent',
16                      'type'    => 'input-percentage',
17                      'default' => '100',
18                    },
19     'what' => {
20       'label'   => 'Of',
21       'type'    => 'select',
22       #add additional ways to specify in the package def
23       'options' => [qw(
24         base_recur_permonth cust_bill_pkg_recur recur_cost_permonth
25         unit_setup setup_cost
26       )],
27       'labels'  => {
28         'base_recur_permonth' => 'Base monthly fee',
29         'cust_bill_pkg_recur' => 'Actual invoiced amount of most recent'.
30                                  ' recurring charge',
31         'recur_cost_permonth' => 'Monthly cost',
32         'unit_setup'          => 'Setup fee',
33         'setup_cost'          => 'Setup cost',
34       },
35     },
36   );
37 }
38
39 #my %no_cust_pkg = ( 'setup_cost' => 1 );
40
41 sub _calc_credit {
42   my( $self, $cust_pkg ) = @_;
43
44   my $cust_main = $self->cust_main($cust_pkg);
45
46   my $part_pkg = $cust_pkg->part_pkg;
47
48   my $what = $self->option('what');
49
50   #false laziness w/Condition/cust_payments_pkg.pm
51   if ( $what =~ /_permonth$/ ) { #huh.  yuck.
52     if ( $part_pkg->freq !~ /^\d+$/ ) {
53       die 'WARNING: Not crediting for package '. $cust_pkg->pkgnum.
54           ' ( customer '. $cust_pkg->custnum. ')'.
55           ' - credits not (yet) available for '.
56           ' packages with '. $part_pkg->freq_pretty. ' frequency';
57     }
58   }
59
60   my $percent = $self->_calc_credit_percent($cust_pkg);
61
62   #my @arg = $no_cust_pkg{$what} ? () : ($cust_pkg);
63   my @arg = ($what eq 'setup_cost') ? () : ($cust_pkg);
64
65   sprintf('%.2f', $part_pkg->$what(@arg) * $percent / 100 );
66
67 }
68
69 sub _calc_credit_percent {
70   my( $self, $cust_pkg ) = @_;
71   $self->option('percent');
72 }
73
74 1;