merge NG auth, RT#21563
[freeside.git] / FS / FS / part_event / Condition / inactive_age.pm
1 package FS::part_event::Condition::inactive_age;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5 use FS::Record qw( qsearch );
6
7 sub description { 'Days without billing activity' }
8
9 sub option_fields {
10   (
11     'age'  =>  { 'label'   => 'No activity within',
12                  'type'    => 'freq',
13                },
14     # flags to select kinds of activity, 
15     # like if you just want "no payments since"?
16     # not relevant yet
17   );
18 }
19
20 sub condition {
21   my( $self, $obj, %opt ) = @_;
22   my $custnum = $obj->custnum;
23   my $age = $self->option_age_from('age', $opt{'time'} );
24
25   foreach my $t (qw(cust_bill cust_pay cust_credit cust_refund)) {
26     my $class = "FS::$t";
27     return 0 if $class->count("custnum = $custnum AND _date >= $age");
28   }
29   1;
30 }
31
32 sub condition_sql {
33   my( $class, $table, %opt ) = @_;
34   my $age   = $class->condition_sql_option_age_from('age', $opt{'time'});
35   my @sql;
36   for my $t (qw(cust_bill cust_pay cust_credit cust_refund)) {
37     push @sql,
38       "NOT EXISTS( SELECT 1 FROM $t ".
39       "WHERE $t.custnum = cust_main.custnum AND $t._date >= $age".
40       ")";
41   }
42   join(' AND ', @sql);
43 }
44
45 1;
46