82b215d2c67958862e3a1309af79f23e1987fb4e
[freeside.git] / FS / FS / part_event / Action / Mixin / credit_bill.pm
1 package FS::part_event::Action::Mixin::credit_bill;
2
3 use strict;
4
5 # credit_bill: calculates a credit amount that is some percentage of each 
6 # line item of an invoice
7
8 sub eventtable_hashref {
9   { 'cust_bill' => 1 };
10 }
11
12 sub option_fields {
13   my $class = shift;
14   my @fields = (
15     'reasonnum' => { 'label'        => 'Credit reason',
16                      'type'         => 'select-reason',
17                      'reason_class' => 'R',
18                    },
19     'percent'   => { 'label'   => 'Percent',
20                      'type'    => 'input-percentage',
21                      'default' => '100',
22                    },
23     'what' => {
24       'label'   => 'Of',
25       'type'    => 'select',
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 )],
28       'labels'  => {
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',
36       },
37     },
38   );
39   if ($class->can('_calc_credit_percent')) {
40     splice @fields, 2, 2; #remove the percentage option
41   }
42   @fields;
43     
44 }
45
46 our %part_pkg_cache;
47
48 # arguments:
49 # 1. the line item
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
54 sub _calc_credit {
55   my $self = shift;
56   my $cust_bill_pkg = shift;
57
58   my $what = $self->option('what');
59   my $cost = ($what =~ /_cost/ ? 1 : 0);
60   my $margin = ($what =~ /_margin/ ? 1 : 0);
61
62   my $pkgnum = $cust_bill_pkg->pkgnum;
63   my $cust_pkg = $cust_bill_pkg->cust_pkg;
64
65   my $percent;
66   if ( $self->can('_calc_credit_percent') ) {
67     $percent = $self->_calc_credit_percent($cust_pkg, @_);
68   } else {
69     $percent = $self->option('percent') || 0;
70   }
71
72   my $charge = 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);
78
79     if ( $cost ) {
80       $charge = $part_pkg->get($what);
81     } else { # $margin
82       $charge = $part_pkg->$what($cust_pkg);
83     }
84
85     $charge = ($charge || 0) * ($cust_pkg->quantity || 1);
86
87   } else { # setup, recur, or setuprecur
88
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');
95     }
96
97     # don't multiply by quantity here; it's already included
98   }
99
100   $charge = 0 if $charge < 0; # e.g. prorate
101   return ($percent * $charge / 100);
102 }
103
104 1;