promised payment date for invoices, #13554
[freeside.git] / FS / FS / part_event / Condition / cust_bill_past_promised.pm
1 package FS::part_event::Condition::cust_bill_past_promised;
2
3 use strict;
4 use FS::cust_bill;
5 use Time::Local 'timelocal';
6
7 use base qw( FS::part_event::Condition );
8
9 sub description {
10   'Promised payment date has passed',
11 }
12
13 sub eventtable_hashref {
14     { 'cust_main' => 0,
15       'cust_bill' => 1,
16       'cust_pkg'  => 0,
17     };
18 }
19
20 sub option_fields {
21   (
22     'delay' => { label  => 'Delay additional days',
23       type   => 'text',
24       value  => '0',
25     },
26   );
27 }
28
29 sub condition {
30   # always return true if there is no promised_date
31   my($self, $cust_bill, %opt) = @_;
32
33   my $delay = $self->option('delay') || 0;
34   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($opt{'time'}))[0..5];
35   my $as_of = timelocal(0,0,0,$mday,$mon,$year) - $delay * 86400;
36   $as_of >= ($cust_bill->promised_date || 0);
37 }
38
39 sub condition_sql {
40   my( $class, $table, %opt ) = @_;
41   return 'true' if $opt{'driver_name'} ne 'Pg';
42   my $delay = $class->condition_sql_option_integer('delay', 'Pg');
43   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($opt{'time'}))[0..5];
44   my $as_of = timelocal(0,0,0,$mday,$mon,$year) . " - ($delay * 86400)";
45   "(cust_bill.promised_date IS NULL OR $as_of >= cust_bill.promised_date)";
46 }
47
48 1;