delayed late fee conditions, #15957
[freeside.git] / FS / FS / part_event / Condition / balance_age_on_day.pm
1 package FS::part_event::Condition::balance_age_on_day;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5 use Time::Local qw(timelocal);
6
7 sub description { 'Customer balance age on a day last month'; }
8
9 sub option_fields {
10   (
11     'balance' => { 'label'      => 'Balance over',
12                    'type'       => 'money',
13                    'value'      => '0.00', #default
14                  },
15     'day'     => { 'label'      => 'Day of month',
16                    'type'       => 'select',
17                    'options'    => [ 1..28 ]
18                  },
19     'age'     => { 'label'      => 'Balance age on that day',
20                    'type'       => 'freq',
21                  },
22   );
23 }
24
25 sub condition {
26   my($self, $object, %opt) = @_;
27
28   my $cust_main = $self->cust_main($object);
29
30   my $over = $self->option('balance');
31   $over = 0 unless length($over);
32
33   my $day = $self->option('day');
34   my $as_of = $opt{'time'};
35
36   if ( $day ) {
37     my ($month, $year) = (localtime($opt{'time'}))[4,5];
38     $month--;
39     if ( $month < 0 ) {
40       $month = 11;
41       $year--;
42     }
43     $as_of = timelocal(0,0,0,$day,$month,$year);
44   }
45
46   my $age = $self->option_age_from('age', $as_of );
47
48   my $sql = $cust_main->balance_date_sql(
49     $age,                 # latest invoice date to consider
50     undef,                # earliest invoice date
51     'cutoff' => $as_of,   # ignore applications after this date
52     'unapplied_date' => 1 # ignore unapplied payments after $age
53   );
54   $sql = "SELECT ($sql) FROM cust_main WHERE custnum = ".$cust_main->custnum;
55   FS::Record->scalar_sql($sql) > $over;
56 }
57
58 # XXX do this if needed
59 #sub condition_sql { }
60
61 1;