aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/SendReport/EmptyHomes.pm
blob: b5faf8ddcf6010333d4c2dca8cbe60353f1a4df6 (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
package FixMyStreet::SendReport::EmptyHomes;

use Moo;
use namespace::autoclean;

use mySociety::MaPit;

BEGIN { extends 'FixMyStreet::SendReport::Email'; }

sub build_recipient_list {
    my ( $self, $row, $h ) = @_;

    my $all_confirmed = 1;
    foreach my $body ( @{ $self->bodies } ) {
        my $contact = $row->result_source->schema->resultset("Contact")->find( {
            deleted => 0,
            body_id => $body->id,
            category => 'Empty property',
        } );

        my ($body_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note );

        unless ($confirmed) {
            $all_confirmed = 0;
            #$note = 'Council ' . $row->body . ' deleted'
                #unless $note;
            $body_email = 'N/A' unless $body_email;
            #$notgot{$body_email}{$row->category}++;
            #$note{$body_email}{$row->category} = $note;
        }

        push @{ $self->to }, [ $body_email, $body->name ];

        my $area_info = mySociety::MaPit::call('area', $body->body_areas->first->area_id);
        my $country = $area_info->{country};
        if ($country eq 'W') {
            push @{$self->bcc}, 'wales@' . FixMyStreet->config('EMAIL_DOMAIN');
        } elsif ($country eq 'S') {
            push @{$self->bcc}, 'scotland@' . FixMyStreet->config('EMAIL_DOMAIN');
        } else {
            push @{$self->bcc}, 'eha@' . FixMyStreet->config('EMAIL_DOMAIN');
        }
    }

    # Set address email parameter from added data
    $h->{address} = $row->extra->{address};

    return $all_confirmed && @{$self->to};
}

sub get_template {
    my ( $self, $row ) = @_;
    return Utils::read_file( FixMyStreet->path_to( "templates", "email", "emptyhomes", $row->lang, "submit.txt" )->stringify );
}

1;