RT#42043: Check for cancelled packages with a cancellation date age option [age_newes...
[freeside.git] / FS / FS / part_event / Condition / hasnt_pkg_class_cancelled.pm
1 package FS::part_event::Condition::hasnt_pkg_class_cancelled;
2 use base qw( FS::part_event::Condition );
3
4 use strict;
5
6 sub description {
7   'Customer does not have canceled package with class';
8 }
9
10 sub eventtable_hashref {
11     { 'cust_main' => 1,
12       'cust_bill' => 1,
13       'cust_pkg'  => 1,
14     };
15 }
16
17 #something like this
18 sub option_fields {
19   (
20     'pkgclass'  => { 'label'    => 'Package Class',
21                      'type'     => 'select-pkg_class',
22                      'multiple' => 1,
23                    },
24     'age_newest' => { 'label'      => 'Cancelled more than',
25                       'type'       => 'freq',
26                       'post_text'  => ' ago (blank for no limit)',
27                       'allow_blank' => 1,
28                     },
29     'age'        => { 'label'      => 'Cancelled less than',
30                       'type'       => 'freq',
31                       'post_text'  => ' ago (blank for no limit)',
32                       'allow_blank' => 1,
33                     },
34   );
35 }
36
37 sub condition {
38   my( $self, $object, %opt ) = @_;
39
40   my $cust_main = $self->cust_main($object);
41
42   my $oldest = length($self->option('age')) ? $self->option_age_from('age', $opt{'time'} ) : 0;
43   my $newest = $self->option_age_from('age_newest', $opt{'time'} );
44
45   my $pkgclass = $self->option('pkgclass') || {};
46
47   ! grep { $pkgclass->{ $_->part_pkg->classnum } && ($_->get('cancel') > $oldest) && ($_->get('cancel') <= $newest) }
48     $cust_main->cancelled_pkgs;
49 }
50
51 1;
52