/
weekly-unpaid-fuel-report
executable file
·87 lines (80 loc) · 2.51 KB
/
weekly-unpaid-fuel-report
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
#!/usr/bin/env perl
use 5.14.1;
use DateTime;
use DateTime::Duration;
use Dancer qw/:syntax/;
use Dancer::Plugin::Email;
use FindBin;
use Cwd qw/realpath/;
use lib "$FindBin::Bin/../lib";
use Biopay::Receipt;
use Biopay::Prices;
use Biopay::Transaction;
use Biopay::Member;
use Biopay::Util qw/host/;
use List::Util qw/sum/;
# Load appdir and templates
Dancer::Config::setting('appdir',realpath("$FindBin::Bin/.."));
Dancer::Config::setting('views',realpath("$FindBin::Bin/../views"));
Dancer::Config::load();
my $membership_price = Biopay::Prices->new->annual_membership_price;
my $to = shift || config->{board_email};
my $txns = Biopay::Transaction->All_unpaid();
my %by_member;
for my $t (@$txns) {
push @{ $by_member{ $t->member_id } }, $t;
}
my %opts = (host => host());
Unpaid_data: {
my @txns_by_member;
my ($total_amount, $total_count) = (0,0);
for my $mid (keys %by_member) {
my $mtxns = $by_member{$mid};
my $sum = sum map { $_->price } @$mtxns;
next if $sum < 5;
my $m = Biopay::Member->By_id($mid);
push @txns_by_member, {
member => $m,
txns => $mtxns,
count => scalar(@$mtxns),
amount => sprintf('%.02f', $sum),
};
$total_amount += $sum;
$total_count += @$mtxns;
}
@txns_by_member = sort { $b->{amount} <=> $a->{amount} } @txns_by_member;
$opts{unpaid}{txns} = \@txns_by_member;
$opts{unpaid}{total_amount} = sprintf('%.02f', $total_amount);
$opts{unpaid}{total_count} = $total_count;
}
Summary: {
my $now = DateTime->now;
$now->set_time_zone('America/Vancouver');
while ($now->day_name ne 'Sunday') {
$now -= DateTime::Duration->new(days => 1);
}
$now->set_hour(0); $now->set_minute(0); $now->set_second(0);
my $week_start = $now - DateTime::Duration->new(weeks => 1);
my $week_end = $now - DateTime::Duration->new(seconds => 1);
my $txns = Biopay::Transaction->By_date({
startkey => $week_start->epoch,
endkey => $week_end->epoch,
});
my $litres = 0;
my $txn_count = 0;
for my $txn (@$txns) {
$litres += $txn->litres;
$txn_count++;
}
$opts{litres_this_week} = $litres;
$opts{txns_this_week} = $txn_count;
}
my $html = template "email/weekly-unpaid-summary", \%opts, { layout => 'email' };
email {
to => $to,
cc => config->{weekly_fuel_report_cc},
from => config->{email_from},
subject => "Weekly un-paid biodiesel summary",
message => $html,
type => 'html',
}