aboutsummaryrefslogtreecommitdiffstats
path: root/bin/send-reports
blob: 626eb63cfd46120a45a3eae15836e5a03d4c162b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/perl -w

# send-reports:
# Send new problem reports to councils
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: send-reports,v 1.32 2007-04-19 12:08:53 matthew Exp $

use strict;
require 5.8.0;

# Horrible boilerplate to set up appropriate library paths.
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../../perllib";
use File::Slurp;

use mySociety::Config;
use mySociety::DBHandle qw(dbh select_all);
use mySociety::Email;
use mySociety::MaPit;
use mySociety::Util;

BEGIN {
    mySociety::Config::set_file("$FindBin::Bin/../conf/general");
    mySociety::DBHandle::configure(
        Name => mySociety::Config::get('BCI_DB_NAME'),
        User => mySociety::Config::get('BCI_DB_USER'),
        Password => mySociety::Config::get('BCI_DB_PASS'),
        Host => mySociety::Config::get('BCI_DB_HOST', undef),
        Port => mySociety::Config::get('BCI_DB_PORT', undef)
    );
}

use mySociety::Dress;

die "No arguments or specify --nomail" if (@ARGV>1);
my $nomail = 0;
$nomail = 1 if (@ARGV==1 && $ARGV[0] eq '--nomail');

my (%notgot, %note);
my $unsent = dbh()->selectall_arrayref(
    "SELECT id, council, category, title, detail, name, email, phone, used_map,
    easting, northing
    FROM problem WHERE state in ('confirmed','fixed') AND whensent IS NULL
    AND council IS NOT NULL", { Slice => {} });

foreach my $row (@$unsent) {
    # XXX Needs locks!
    my @all_councils = split /,|\|/, $row->{council};
    my ($councils, $missing) = $row->{council} =~ /^([\d,]+)(?:\|([\d,]+))?/;
    my @councils = split /,/, $councils;
    my $areas_info = mySociety::MaPit::get_voting_areas_info(\@all_councils);
    my (@to, @dear, %recips);
    my $all_confirmed = 1;
    foreach my $council (@councils) {
        my $name = $areas_info->{$council}->{name};
        my ($council_email, $confirmed, $note) = dbh()->selectrow_array(
            "SELECT email,confirmed,note FROM contacts WHERE deleted='f'
                and area_id=? AND category=?", {}, $council, $row->{category});
        unless ($confirmed) {
            $all_confirmed = 0;
            $notgot{$council_email}{$row->{category}}++;
            $note{$council_email}{$row->{category}} = $note;
        }
        push @to, [ $council_email, $name ];
        push @dear, $name;
        $recips{$council_email} = 1;
    }
    my @recips = keys %recips;
    if (!@to) {
        print 'Need to send problem #' . $row->{id} . ' to council(s) ' . join(',',@recips) . "\n";
        print "  ...but we have no contact details for any of them!\n";
        next;
    }
    next unless ($all_confirmed);

    push @recips, mySociety::Config::get('CONTACT_EMAIL');

    my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/submit-council");
    my %h = map { $_ => $row->{$_} } qw/title detail name email phone category/;
    $h{phone} = "Phone: $h{phone}\n\n" if $h{phone};
    if ($h{category} eq 'Other') {
        $h{category_footer} = 'this type of local problem';
        $h{category} = '';
    } else {
        $h{category_footer} = "'" . $h{category} . "'";
        $h{category} = "Category: $h{category}\n\n";
    }
    $h{url} = mySociety::Config::get('BASE_URL') . '/?id=' . $row->{id};
    $h{councils_name} = join(' and ', @dear);
    $h{fuzzy} = $row->{used_map} ? 'To view a map of the precise location of this issue'
        : 'The user could not locate the problem on a map, but to see the area around the location they entered';
    $h{multiple} = @dear>1 ? "[ This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system. ]\n\n"
        : '';
    $h{missing} = ''; 
    if ($missing) {
        my $name = $areas_info->{$missing}->{name};
        $h{missing} = '[ We realise this problem might be the responsibility of ' . $name
            . "; however, we don't currently have any contact details for them.
If you know of an appropriate contact address, please do get in touch. ]\n\n";
    }

    $h{closest_address} = '';
    my ($address, $distance) = mySociety::Dress::find_nearest($row->{easting}, $row->{northing});
    $h{closest_address} = sprintf("The closest address to the location of this problem, %.0fm away, is: %s\n\n",
        $distance, $address) if ($address);
    my $email = mySociety::Email::construct_email({
        _template_ => $template,
        _parameters_ => \%h,
        To => \@to,
        From => [ $row->{email}, $row->{name} ]
    });

    my $result;
    if (mySociety::Config::get('STAGING_SITE') || $nomail) {
        $result = -1;
    } else {
        $result = mySociety::Util::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips);
    }
    if ($result == mySociety::Util::EMAIL_SUCCESS) {
        dbh()->do('UPDATE problem SET whensent=ms_current_timestamp() WHERE id=?', {}, $row->{id});
        dbh()->commit();
    } else {
        dbh()->rollback();
    }
}

print "Council email addresses that need checking:\n" if keys %notgot;
foreach my $e (keys %notgot) {
    foreach my $c (keys %{$notgot{$e}}) {
        print $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n";
    }
}