add contract_end to Package age condition, RT#23171
[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' => [qw(
27                    setup last_bill bill adjourn susp expire cancel contract_end
28                  )],
29                  'labels'  => {
30                    'setup'        => 'Setup date',
31                    'last_bill'    => 'Last bill date',
32                    'bill'         => 'Next bill date',
33                    'adjourn'      => 'Adjournment date',
34                    'susp'         => 'Suspension date',
35                    'expire'       => 'Expiration date',
36                    'cancel'       => 'Cancellation date',
37                    'contract_end' => 'Contract end date',
38                  },
39                },
40   );
41 }
42
43 sub condition {
44   my( $self, $cust_pkg, %opt ) = @_;
45
46   my $age = $self->option_age_from('age', $opt{'time'} );
47
48   my $pkg_date = $cust_pkg->get( $self->option('field') );
49
50   $pkg_date && $pkg_date <= $age;
51
52 }
53
54 sub condition_sql {
55   my( $class, $table, %opt ) = @_;
56   my $age   = $class->condition_sql_option_age_from('age', $opt{'time'});
57   my $field = $class->condition_sql_option('field');
58 #amazingly, this is actually faster 
59   my $sql = '( CASE';
60   foreach( qw(setup last_bill bill adjourn susp expire cancel) ) {
61     $sql .= " WHEN $field = '$_' THEN (cust_pkg.$_ IS NOT NULL AND cust_pkg.$_ <= $age)";
62   }
63   $sql .= ' END )';
64   return $sql;
65 }
66
67 1;
68