Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a txn + litres total summary for the past week

  • Loading branch information...
commit b6d7ba2cb2166381d51ac67adfabe7054bb8b865 1 parent b24ac63
Luke Closs authored
Showing with 56 additions and 25 deletions.
  1. +48 −22 bin/weekly-unpaid-fuel-report
  2. +8 −3 views/email/weekly-unpaid-summary.tt
View
70 bin/weekly-unpaid-fuel-report
@@ -28,29 +28,55 @@ for my $t (@$txns) {
push @{ $by_member{ $t->member_id } }, $t;
}
-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;
+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;
}
-@txns_by_member = sort { $b->{amount} <=> $a->{amount} } @txns_by_member;
-my $html = template "email/weekly-unpaid-summary", {
- txns => \@txns_by_member,
- total_amount => sprintf('%.02f', $total_amount),
- total_count => $total_count,
- host => host(),
-}, { layout => 'email' };
+
+my $html = template "email/weekly-unpaid-summary", \%opts, { layout => 'email' };
email {
to => $to,
from => config->{email_from},
View
11 views/email/weekly-unpaid-summary.tt
@@ -1,13 +1,18 @@
-<h1>Weekly un-paid fuel summary</h1>
+<h1>This week in Biodiesel</h1>
+<h2>Summary</h2>
+
+<p>This past week we sold [% litres_this_week %] litres in [% txns_this_week %] transactions.</p>
+
+<h2>Weekly un-paid fuel summary</h2>
<p>This is an up-to-date list of <a href="[% host %]/txns">biodiesel purchases</a> which have <a href="[% host %]/unpaid">not been paid for</a>. Please organize amongst yourselves to contact these members and have them update their billing info.</p>
[% IF txns.size %]
<h2>Summary</h2>
-<p>Right now, a total of $[% total_amount %] is un-paid across [% total_count %] transactions.</p>
+<p>Right now, a total of $[% unpaid.total_amount %] is un-paid across [% unpaid.total_count %] transactions.</p>
<h2>Detailed Breakdown</h2>
<ul>
- [% FOREACH i IN txns %]
+ [% FOREACH i IN unpaid.txns %]
<li>
<strong><a href="[% host %]/members/[% i.member.id %]">Member #[% i.member.id %] - [% i.member.name %]</a> [% IF i.member.frozen %](FROZEN) [% END %]- owes $[% i.amount %] from <a href="[% host %]/members/[% i.member.id %]/unpaid">[% i.count %] purchases</a>.</strong>
<ul>
Please sign in to comment.
Something went wrong with that request. Please try again.