further optimize condition_sql for "Invoice eligible for automatic collection" condit...
[freeside.git] / FS / FS / part_event / Condition / signupdate.pm
1 package FS::part_event::Condition::signupdate;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5 use FS::Record qw(str2time_sql str2time_sql_closing);
6 use Date::Parse 'str2time';
7
8 sub description { 'Customer signed up during date range' }
9
10 sub option_fields {
11   (
12     # actually stored as strings
13     'start' => {  label   => 'First day',
14                   type    => 'date',
15                   format  => '%Y-%m-%d', },
16     'end'   => {  label => 'Last day',
17                   type  => 'date', 
18                   format  => '%Y-%m-%d', },
19   );
20 }
21
22 sub condition {
23
24   my($self, $object, %opt) = @_;
25
26   my $cust_main = $self->cust_main($object);
27   my $start = $self->option('start');
28   my $end = $self->option('end');
29
30   (!$start or $cust_main->signupdate >= str2time($start)) and
31   (!$end   or $cust_main->signupdate <  str2time($end) + 86400);
32 }
33
34 sub condition_sql {
35   my( $class, $table, %opt ) = @_;
36   return 'true' if $opt{'driver_name'} ne 'Pg';
37
38   my $start = $class->condition_sql_option('start');
39   my $end = $class->condition_sql_option('end');
40
41   $start = "$start IS NULL OR cust_main.signupdate >=" . 
42            str2time_sql . "($start)::timestamp" . str2time_sql_closing;
43   $end   = "$end IS NULL OR cust_main.signupdate < 86400 + " . 
44            str2time_sql . "($end)::timestamp" . str2time_sql_closing;
45   "($start) AND ($end)";
46 }
47
48 1;