import torrus 1.0.9
[freeside.git] / FS / FS / part_event / Condition / pkg_age.pm
1 package FS::part_event::Condition::pkg_age;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5 use FS::Record qw( qsearch );
6
7 sub description {
8   'Package Age';
9 }
10
11 sub eventtable_hashref {
12     { 'cust_main' => 0,
13       'cust_bill' => 0,
14       'cust_pkg'  => 1,
15     };
16 }
17
18 #something like this
19 sub option_fields {
20   (
21     'age'  =>  { 'label'   => 'Package date age',
22                  'type'    => 'freq',
23                },
24     'field' => { 'label'   => 'Compare date',
25                  'type'    => 'select',
26                  'options' =>
27                    [qw( setup last_bill bill adjourn susp expire cancel )],
28                  'labels'  => {
29                    'setup'     => 'Setup date',
30                    'last_bill' => 'Last bill date',
31                    'bill'      => 'Next bill date',
32                    'adjourn'   => 'Adjournment date',
33                    'susp'      => 'Suspension date',
34                    'expire'    => 'Expiration date',
35                    'cancel'    => 'Cancellation date',
36                  },
37                },
38   );
39 }
40
41 sub condition {
42   my( $self, $cust_pkg, %opt ) = @_;
43
44   my $age = $self->option_age_from('age', $opt{'time'} );
45
46   my $pkg_date = $cust_pkg->get( $self->option('field') );
47
48   $pkg_date && $pkg_date <= $age;
49
50 }
51
52 sub condition_sql {
53   my( $class, $table, %opt ) = @_;
54   my $age   = $class->condition_sql_option_age_from('age', $opt{'time'});
55   my $field = $class->condition_sql_option('field');
56 #amazingly, this is actually faster 
57   my $sql = '( CASE';
58   foreach( qw(setup last_bill bill adjourn susp expire cancel) ) {
59     $sql .= " WHEN $field = '$_' THEN (cust_pkg.$_ IS NOT NULL AND cust_pkg.$_ <= $age)";
60   }
61   $sql .= ' END )';
62   return $sql;
63 }
64
65 1;
66