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.
53 # 3. optional scalar reference for recording a warning message
57 my $cust_bill_pkg = shift;
62 my $what = $self->option('what');
63 my $cost = ($what =~ /_cost/ ? 1 : 0);
64 my $margin = ($what =~ /_margin/ ? 1 : 0);
66 my $pkgnum = $cust_bill_pkg->pkgnum;
67 my $cust_pkg = $cust_bill_pkg->cust_pkg;
70 if ( $self->can('_calc_credit_percent') ) {
71 $percent = $self->_calc_credit_percent($cust_pkg, $who);
72 $warning = 'Percent calculated to zero ' unless $percent+0;
74 $percent = $self->option('percent') || 0;
75 $warning = 'Percent set to zero ' unless $percent+0;
79 if ( $margin or $cost ) {
80 # look up package costs only if we need them
81 my $pkgpart = $cust_bill_pkg->pkgpart_override || $cust_pkg->pkgpart;
82 my $part_pkg = $part_pkg_cache{$pkgpart}
83 ||= FS::part_pkg->by_key($pkgpart);
86 $charge = $part_pkg->get($what);
88 $charge = $part_pkg->$what($cust_pkg);
91 $charge = ($charge || 0) * ($cust_pkg->quantity || 1);
92 $warning .= 'Charge calculated to zero ' unless $charge+0;
94 } else { # setup, recur, or setuprecur
96 if ( $what eq 'setup' ) {
97 $charge = $cust_bill_pkg->get('setup');
98 $warning .= 'Setup is zero ' unless $charge+0;
99 } elsif ( $what eq 'recur' ) {
100 $charge = $cust_bill_pkg->get('recur');
101 $warning .= 'Recur is zero ' unless $charge+0;
102 } elsif ( $what eq 'setuprecur' ) {
103 $charge = $cust_bill_pkg->get('setup') + $cust_bill_pkg->get('recur');
104 $warning .= 'Setup and recur are zero ' unless $charge+0;
107 # don't multiply by quantity here; it's already included
110 if ($charge < 0) { # e.g. prorate
112 $warning .= 'Negative charge set to zero ';
115 $$warnref .= $warning if ref($warnref);
116 return ($percent * $charge / 100);