1 package FS::part_event::Action::Mixin::credit_pkg;
5 # credit_pkg: calculates a credit amount that is some percentage of the
6 # package charge / cost / margin / some other amount of a package
8 # also provides an option field for the percentage, unless the action knows
9 # how to calculate its own percentage somehow (has a _calc_credit_percent)
11 sub eventtable_hashref {
18 'reasonnum' => { 'label' => 'Credit reason',
19 'type' => 'select-reason',
20 'reason_class' => 'R',
22 'percent' => { 'label' => 'Percent',
23 'type' => 'input-percentage',
29 #add additional ways to specify in the package def
31 base_recur_permonth cust_bill_pkg_recur recur_cost_permonth recur_margin_permonth
32 unit_setup setup_cost setup_margin
35 'base_recur_permonth' => 'Base monthly fee',
36 'cust_bill_pkg_recur' => 'Actual invoiced amount of most recent'.
38 'recur_cost_permonth' => 'Monthly cost',
39 'unit_setup' => 'Setup fee',
40 'setup_cost' => 'Setup cost',
41 'setup_margin' => 'Setup margin (fee minus cost)',
42 'recur_margin_permonth' => 'Monthly margin (fee minus cost)',
46 if ($class->can('_calc_credit_percent')) {
47 splice @fields, 2, 2; #remove the percentage option
54 # 2. recipient of the credit (passed through to _calc_credit_percent)
55 # 3. optional scalar reference for recording a warning message
64 my $cust_main = $self->cust_main($cust_pkg);
66 my $part_pkg = $cust_pkg->part_pkg;
68 my $what = $self->option('what');
70 #false laziness w/Condition/cust_payments_pkg.pm
71 if ( $what =~ /_permonth$/ ) { #huh. yuck.
72 if ( $part_pkg->freq !~ /^\d+$/ ) {
73 die 'WARNING: Not crediting for package '. $cust_pkg->pkgnum.
74 ' ( customer '. $cust_pkg->custnum. ')'.
75 ' - credits not (yet) available for '.
76 ' packages with '. $part_pkg->freq_pretty. ' frequency';
81 if ( $self->can('_calc_credit_percent') ) {
82 $percent = $self->_calc_credit_percent($cust_pkg, $who) || 0;
83 $warning = 'Percent calculated to zero ' unless $percent+0;
85 $percent = $self->option('percent') || 0;
86 $warning = 'Percent set to zero ' unless $percent+0;
89 my @arg = ($what eq 'setup_cost') ? () : ($cust_pkg);
90 my $charge = $part_pkg->$what(@arg) || 0;
91 $warning .= "$what is zero" unless $charge+0;
93 $$warnref .= $warning if ref($warnref);
94 return sprintf('%.2f', $charge * $percent / 100 );