import torrus 1.0.9
[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' => { 'label'   => 'Of',
20                 'type'    => 'select',
21                 #add additional ways to specify in the package def
22                 'options' => [ qw( base_recur_permonth unit_setup recur_cost_permonth setup_cost ) ],
23                 'labels'  => { 'base_recur_permonth' => 'Base monthly fee',
24                                'unit_setup'          => 'Setup fee',
25                                'recur_cost_permonth' => 'Monthly cost',
26                                'setup_cost'          => 'Setup cost',
27                              },
28               },
29   );
30
31 }
32
33 #my %no_cust_pkg = ( 'setup_cost' => 1 );
34
35 sub _calc_credit {
36   my( $self, $cust_pkg ) = @_;
37
38   my $cust_main = $self->cust_main($cust_pkg);
39
40   my $part_pkg = $cust_pkg->part_pkg;
41
42   my $what = $self->option('what');
43
44   #false laziness w/Condition/cust_payments_pkg.pm
45   if ( $what =~ /_permonth$/ ) { #huh.  yuck.
46     if ( $part_pkg->freq !~ /^\d+$/ ) {
47       die 'WARNING: Not crediting for package '. $cust_pkg->pkgnum.
48           ' ( customer '. $cust_pkg->custnum. ')'.
49           ' - credits not (yet) available for '.
50           ' packages with '. $part_pkg->freq_pretty. ' frequency';
51     }
52   }
53
54   my $percent = $self->option('percent');
55
56   #my @arg = $no_cust_pkg{$what} ? () : ($cust_pkg);
57   my @arg = ($what eq 'setup_cost') ? () : ($cust_pkg);
58
59   sprintf('%.2f', $part_pkg->$what(@arg) * $percent / 100 );
60
61 }
62
63 1;