e586f8532c434b5b9a8f9565fab80a070911b7a7
[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 recur_margin_permonth
25         unit_setup setup_cost setup_margin
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         'setup_margin'        => 'Setup margin (fee minus cost)',
35         'recur_margin_permonth' => 'Monthly margin (fee minus cost)',
36       },
37     },
38   );
39 }
40
41 #my %no_cust_pkg = ( 'setup_cost' => 1 );
42
43 sub _calc_credit {
44   my( $self, $cust_pkg ) = @_;
45
46   my $cust_main = $self->cust_main($cust_pkg);
47
48   my $part_pkg = $cust_pkg->part_pkg;
49
50   my $what = $self->option('what');
51
52   #false laziness w/Condition/cust_payments_pkg.pm
53   if ( $what =~ /_permonth$/ ) { #huh.  yuck.
54     if ( $part_pkg->freq !~ /^\d+$/ ) {
55       die 'WARNING: Not crediting for package '. $cust_pkg->pkgnum.
56           ' ( customer '. $cust_pkg->custnum. ')'.
57           ' - credits not (yet) available for '.
58           ' packages with '. $part_pkg->freq_pretty. ' frequency';
59     }
60   }
61
62   my $percent = $self->_calc_credit_percent($cust_pkg);
63
64   #my @arg = $no_cust_pkg{$what} ? () : ($cust_pkg);
65   my @arg = ($what eq 'setup_cost') ? () : ($cust_pkg);
66
67   sprintf('%.2f', $part_pkg->$what(@arg) * $percent / 100 );
68
69 }
70
71 sub _calc_credit_percent {
72   my( $self, $cust_pkg ) = @_;
73   $self->option('percent');
74 }
75
76 1;