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