typo
[freeside.git] / FS / FS / part_event / Condition / billday.pm
1 package FS::part_event::Condition::billday;
2
3 use strict;
4 use Tie::IxHash;
5
6 use base qw( FS::part_event::Condition );
7
8 sub description {
9   "Customer's monthly billing day is before or on current day or customer has no billing day";
10 }
11
12 sub option_fields {
13   (
14     'delay' => { label  => 'Delay additional days',
15                  type   => 'text',
16                  value  => '0',
17                },
18   );
19 }
20
21 sub condition {
22   my( $self, $object, %opt ) = @_;
23
24   my $cust_main = $self->cust_main($object);
25
26   my $delay = $self->option('delay') || 0;
27   my $as_of = $opt{'time'} - $delay * 86400; # $opt{'time'}, not time()
28
29   my ($mday) = (localtime($as_of))[3]; # what day it was $delay days before now
30   
31   (!$cust_main->billday) || ($mday >= $cust_main->billday);
32 }
33
34 sub condition_sql {
35   my( $class, $table, %opt ) = @_;
36   my $delay = $class->condition_sql_option_integer('delay', 
37     $opt{'driver_name'}); # returns 0 for null
38   my $as_of = $opt{'time'} . " - ($delay * 86400)"; # in seconds
39   my $mday;
40   if ( $opt{'driver_name'} eq 'Pg' ) {
41     $mday = "EXTRACT( DAY FROM TO_TIMESTAMP($as_of) )";
42   }
43   elsif ( $opt{'driver_name'} eq 'mysql' ) {
44     $mday = "DAY( FROM_UNIXTIME($as_of) )";
45   }
46   else { 
47     return 'true'
48   }
49   
50   "cust_main.billday is null or $mday >= cust_main.billday";
51 }
52
53 1;