1 package FS::part_event::Action::Mixin::credit_bill;
5 # credit_bill: calculates a credit amount that is some percentage of each
6 # line item of an invoice
8 sub eventtable_hashref {
15 'reasonnum' => { 'label' => 'Credit reason',
16 'type' => 'select-reason',
17 'reason_class' => 'R',
19 'percent' => { 'label' => 'Percent',
20 'type' => 'input-percentage',
26 #add additional ways to specify in the package def
27 'options' => [qw( setuprecur setup recur setup_cost recur_cost setup_margin recur_margin_permonth )],
29 'setuprecur' => 'Amount charged on this invoice',
30 'setup' => 'Setup fee charged on this invoice',
31 'recur' => 'Recurring fee charged on this invoice',
32 'setup_cost' => 'Setup cost',
33 'recur_cost' => 'Recurring cost',
34 'setup_margin' => 'Package setup fee minus setup cost',
35 'recur_margin_permonth' => 'Monthly recurring fee minus recurring cost',
39 if ($class->can('_calc_credit_percent')) {
40 splice @fields, 2, 2; #remove the percentage option
50 # 2. the recipient of the commission; may be FS::sales, FS::agent,
51 # FS::access_user, etc. Here we don't use it, but it will be passed through
52 # to _calc_credit_percent.
56 my $cust_bill_pkg = shift;
58 my $what = $self->option('what');
59 my $cost = ($what =~ /_cost/ ? 1 : 0);
60 my $margin = ($what =~ /_margin/ ? 1 : 0);
62 my $pkgnum = $cust_bill_pkg->pkgnum;
63 my $cust_pkg = $cust_bill_pkg->cust_pkg;
66 if ( $self->can('_calc_credit_percent') ) {
67 $percent = $self->_calc_credit_percent($cust_pkg, @_);
69 $percent = $self->option('percent') || 0;
73 if ( $margin or $cost ) {
74 # look up package costs only if we need them
75 my $pkgpart = $cust_bill_pkg->pkgpart_override || $cust_pkg->pkgpart;
76 my $part_pkg = $part_pkg_cache{$pkgpart}
77 ||= FS::part_pkg->by_key($pkgpart);
80 $charge = $part_pkg->get($what);
82 $charge = $part_pkg->$what($cust_pkg);
85 $charge = ($charge || 0) * ($cust_pkg->quantity || 1);
87 } else { # setup, recur, or setuprecur
89 if ( $what eq 'setup' ) {
90 $charge = $cust_bill_pkg->get('setup');
91 } elsif ( $what eq 'recur' ) {
92 $charge = $cust_bill_pkg->get('recur');
93 } elsif ( $what eq 'setuprecur' ) {
94 $charge = $cust_bill_pkg->get('setup') + $cust_bill_pkg->get('recur');
97 # don't multiply by quantity here; it's already included
100 $charge = 0 if $charge < 0; # e.g. prorate
101 return ($percent * $charge / 100);