fix day_of_month condition on mysql, #28895
[freeside.git] / FS / FS / part_event / Condition / day_of_month.pm
1 package FS::part_event::Condition::day_of_month;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5
6 sub description {
7   "Run only on a certain day of the month",
8 }
9
10 sub option_fields {
11   (
12     'day'   => { label  => 'Day (1-28, separate multiple days with commas)',
13                  type   => 'text',
14                },
15   );
16 }
17
18 sub condition { # is this even necessary? condition_sql is exact.
19   my( $self, $object, %opt ) = @_;
20
21   my $today = (localtime($opt{'time'}))[3];
22   if (grep { $_ == $today } split(',', $self->option('day'))) {
23     return 1;
24   }
25   '';
26 }
27
28 sub condition_sql {
29   my( $class, $table, %opt ) = @_;
30   my $today = (localtime($opt{'time'}))[3];
31   my $day = $class->condition_sql_option('day');
32   if ($opt{'driver_name'} eq 'Pg') {
33     "$today = ANY( string_to_array($day, ',')::integer[] )";
34   } elsif ( $opt{'driver_name'} eq 'mysql' ) {
35     "find_in_set($today, $day) > 0";
36   }
37 }
38
39 1;