40d94e2d5b0783705297400f141fd26c0488eb4a
[freeside.git] / FS / FS / part_event / Action / referral_pkg_billdate.pm
1 package FS::part_event::Action::referral_pkg_billdate;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5
6 sub description { "Increment the referring customer's package's next bill date"; }
7
8 #sub eventtable_hashref {
9 #}
10
11 sub option_fields {
12   (
13     'if_pkgpart' => { 'label'    => 'Only packages',
14                       'type'     => 'select-part_pkg',
15                       'multiple' => 1,
16                     },
17     'increment'  => { 'label'    => 'Increment by',
18                       'type'     => 'freq',
19                       'value'    => '1m',
20                     },
21   );
22 }
23
24 sub do_action {
25   my( $self, $cust_object, $cust_event ) = @_;
26
27   my $cust_main = $self->cust_main($cust_object);
28
29   return 'No referring customer' unless $cust_main->referral_custnum;
30
31   my $referring_cust_main = $cust_main->referring_cust_main;
32   #return 'Referring customer is cancelled'
33   #  if $referring_cust_main->status eq 'cancelled';
34
35   my %if_pkgpart = map { $_=>1 } split(/\s*,\s*/, $self->option('if_pkgpart') );
36   my @cust_pkg = grep $if_pkgpart{ $_->pkgpart },
37                       $referring_cust_main->billing_pkgs;
38   return 'No qualifying billing package definition' unless @cust_pkg;
39
40   my $cust_pkg = $cust_pkg[0]; #only one
41
42   my $bill = $cust_pkg->bill || $cust_pkg->setup || time;
43
44   $cust_pkg->bill(
45     $cust_pkg->part_pkg->add_freq( $bill, $self->option('increment') )
46   );
47
48   my $error = $cust_pkg->replace;
49   die "Error incrementing next bill date: $error" if $error;
50
51   '';
52
53 }
54
55 1;