import torrus 1.0.9
[freeside.git] / FS / FS / part_event / Condition / pkg_next_bill_within.pm
1 package FS::part_event::Condition::pkg_next_bill_within;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5 use FS::Record qw( qsearch );
6
7 sub description {
8   'Next bill date within upcoming interval';
9 }
10
11 # Run the event when the next bill date is within X days.
12 # To clarify, that's within X days _after_ the current date,
13 # not before.
14 # Combine this with a "once_every" condition so that the event
15 # won't repeat every day until the bill date.
16
17 sub eventtable_hashref {
18     { 'cust_main' => 0,
19       'cust_bill' => 0,
20       'cust_pkg'  => 1,
21     };
22 }
23
24 sub option_fields {
25   (
26     'within'  =>  { 'label'   => 'Bill date within',
27                     'type'    => 'freq',
28                   },
29     # possibly "field" to allow date fields besides 'bill'?
30   );
31 }
32
33 sub condition {
34   my( $self, $cust_pkg, %opt ) = @_;
35
36   my $pkg_date = $cust_pkg->get('bill') or return 0;
37   $pkg_date = $self->option_age_from('within', $pkg_date );
38
39   $opt{'time'} >= $pkg_date;
40
41 }
42
43 #XXX write me for efficiency
44 sub condition_sql {
45   my ($self, $table, %opt) = @_;
46   $opt{'time'}.' >= '.
47     $self->condition_sql_option_age_from('within', 'cust_pkg.bill')
48 }
49
50 1;
51