further optimize condition_sql for "Invoice eligible for automatic collection" condit...
[freeside.git] / FS / FS / part_event / Condition / svc_acct_overlimit.pm
1 package FS::part_event::Condition::svc_acct_overlimit;
2
3 use strict;
4 use FS::svc_acct;
5
6 use base qw( FS::part_event::Condition );
7
8 sub description { 'Service is over its usage limit' };
9
10 sub eventtable_hashref {
11   { 'svc_acct' => 1 }
12 }
13
14 tie my %usage_types, 'Tie::IxHash', (
15   'seconds'   => 'Time',
16   'upbytes'   => 'Upload',
17   'downbytes' => 'Download',
18   'totalbytes'=> 'Total transfer',
19 );
20
21 sub option_fields {
22   (
23     'usage_types' => {
24       type          => 'checkbox-multiple',
25       options       => [ keys %usage_types ],
26       option_labels => \%usage_types,
27     },
28   );
29 }
30
31
32 sub condition {
33   my($self, $svc_acct) = @_;
34
35   my $types = $self->option('usage_types') || {};
36   foreach my $column (keys %$types) {
37     # don't trigger if this type of usage isn't tracked on the service
38     next if $svc_acct->$column eq '';
39
40     return 1 if ( $svc_acct->$column <= 0 );
41   }
42   return 0;
43 }
44
45 sub condition_sql {
46   my($self) = @_;
47
48   # not an exact condition_sql--ignores the usage_types option
49   '(' . join(' OR ', 
50     map {
51       "( svc_acct.$_ IS NOT NULL AND svc_acct.$_ <= 0 )"
52     } keys %usage_types
53   ) . ')'
54 }
55
56 1;
57