import torrus 1.0.9
[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, $cust_event ) = @_;
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     'eventnum' => $cust_event->eventnum,
47     'addlinfo' => 'for customer #'. $cust_main->display_custnum.
48                                ': '.$cust_main->name,
49   );
50   die "Error crediting customer ". $cust_main->referral_custnum.
51       " for referral: $error"
52     if $error;
53
54 }
55
56 sub _calc_credit {
57   my( $self, $cust_pkg ) = @_;
58
59   $self->option('amount');
60 }
61
62 1;