employee (otaker / access_user) commissioning, RT#6991
[freeside.git] / FS / FS / part_event / Action / pkg_referral_credit.pm
1 package FS::part_event::Action::pkg_referral_credit;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5
6 sub description { 'Credit the referring customer a specific amount'; }
7
8 sub eventtable_hashref {
9   { 'cust_pkg' => 1 };
10 }
11
12 sub option_fields {
13   ( 
14     'reasonnum' => { 'label'        => 'Credit reason',
15                      'type'         => 'select-reason',
16                      'reason_class' => 'R',
17                    },
18     'amount'    => { 'label'        => 'Credit amount',
19                      'type'         => 'money',
20                    },
21   );
22
23 }
24
25 sub do_action {
26   my( $self, $cust_pkg ) = @_;
27
28   my $cust_main = $self->cust_main($cust_pkg);
29
30 #  my $part_pkg = $cust_pkg->part_pkg;
31
32   return 'No referring customer' unless $cust_main->referral_custnum;
33
34   my $referring_cust_main = $cust_main->referring_cust_main;
35   return 'Referring customer is cancelled'
36     if $referring_cust_main->status eq 'cancelled';
37
38   my $amount    = $self->_calc_credit($cust_pkg);
39   return '' unless $amount > 0;
40
41   my $reasonnum = $self->option('reasonnum');
42
43   my $error = $referring_cust_main->credit(
44     $amount, 
45     \$reasonnum,
46     'addlinfo' =>
47       'for customer #'. $cust_main->display_custnum. ': '.$cust_main->name,
48   );
49   die "Error crediting customer ". $cust_main->referral_custnum.
50       " for referral: $error"
51     if $error;
52
53 }
54
55 sub _calc_credit {
56   my( $self, $cust_pkg ) = @_;
57
58   $self->option('amount');
59 }
60
61 1;