further optimize condition_sql for "Invoice eligible for automatic collection" condit...
[freeside.git] / FS / FS / part_event / Condition / stagger.pm
1 package FS::part_event::Condition::stagger;
2
3 use strict;
4 use FS::Record qw( qsearch );
5 use FS::part_event;
6 use FS::cust_event;
7
8 use base qw( FS::part_event::Condition );
9
10 sub description { "Stagger this event across the month" }; #could be clearer?
11
12 sub option_fields {
13   # delay? it's supposed to be arbitrary anyway
14 }
15
16 sub condition {
17   my($self, $object, %opt) = @_;
18
19   my $obj_pkey = $object->primary_key;
20   my $tablenum = $object->$obj_pkey();
21
22   my ($today) = (localtime($opt{'time'}))[3];
23
24   $today - 1 == ($tablenum - 1) % 28;
25 }
26
27 sub condition_sql {
28   my( $class, $table, %opt ) = @_;
29
30   my %tablenum = %{ FS::part_event->eventtable_pkey_sql };
31
32   my $today;
33   if ( $opt{'driver_name'} eq 'Pg' ) {
34     $today = "EXTRACT( DAY FROM TO_TIMESTAMP(".$opt{'time'}.") )::INTEGER";
35   }
36   elsif ( $opt{'driver_name'} eq 'mysql' ) {
37     $today = "DAY( FROM_UNIXTIME(".$opt{'time'}.") )";
38   }
39   else {
40     return 'true';
41   }
42   "($today - 1) = ($tablenum{$table} - 1) % 28";
43 }
44
45 1;