summaryrefslogtreecommitdiff
path: root/FS/FS/part_event/Condition/cust_birthdate.pm
blob: e8ecb061a97b9733f2be68f583bab5b98c69f638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package FS::part_event::Condition::cust_birthdate;
use base qw( FS::part_event::Condition );
use strict;
use warnings;
use DateTime;

=head2 NAME

FS::part_event::Condition::cust_birthdate

=head1 DESCRIPTION

Billing event triggered by the time until the customer's next
birthday (cust_main.birthdate)

=cut

sub description {
  'Customer birthday is within time window after billing date';
}

sub option_fields {
  (
    timeframe => {
      label => 'Time window after bill date',
      type   => 'freq',
      value  => '1m',
    }
  );
}

sub condition {
  my( $self, $object, %opt ) = @_;
  my $cust_main = $self->cust_main($object);

  my $birthdate = $cust_main->birthdate || return 0;

  my %timeframe;
  if ( $self->option('timeframe') =~ /(\d+)([mwdh])/ ) {
    my $k = {qw|m months w weeks d days h hours|}->{$2};
    $timeframe{ $k } = $1;
  } else {
    die "Unparsable timeframe given: ".$self->option('timeframe');
  }

  my $ck_dt = DateTime->from_epoch( epoch => $opt{time} )
                      ->truncate( to => 'day' );
  my $bd_dt = DateTime->from_epoch( epoch => $birthdate )
                      ->truncate( to => 'day' );

  # Find the birthday for this calendar year.  If customer birthday
  # has already passed this year, find the birthday for next year.
  my $next_bd_dt = DateTime->new(
    month => $bd_dt->month,
    day   => $bd_dt->day,
    year  => $ck_dt->year,
  );
  $next_bd_dt->add( years => 1 )
    if DateTime->compare( $next_bd_dt, $ck_dt ) == -1;

  # Does next birthday occur between now and specified duration?
  $ck_dt->add( %timeframe );
  DateTime->compare( $next_bd_dt, $ck_dt ) != 1 ? 1 : 0;
}

1;