From cdf9526b3d40b446499a3bd8aaeab8acfac73d90 Mon Sep 17 00:00:00 2001
From: "Bradley M. Kuhn" <bkuhn@ebb.org>
Date: Sun, 22 Jul 2018 15:08:59 -0700
Subject: [PATCH] More processing work.

---
 scripts/rt-outreachy-payment-next.plx | 46 ++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/scripts/rt-outreachy-payment-next.plx b/scripts/rt-outreachy-payment-next.plx
index d04631a..cb68473 100755
--- a/scripts/rt-outreachy-payment-next.plx
+++ b/scripts/rt-outreachy-payment-next.plx
@@ -210,16 +210,46 @@ while (my $file = readdir $dh) {
     } else {
       print STDERR "... BIG PROBLEM: the intern actually failed but got this payment.\n";
     }
-  }
-  # Check to see if previous payment was sent payment
-  my $prevPay = $PAYMENT_NUMBER - 1;
-
-  my $lastPayDate = PaymentDateByTicket($ticket, $prevPay);
-  if (not defined $lastPayDate) {
-    print STDERR "\"$file\": \"$ticket\": Skipped: payment $prevPay was not made yet.\n";
     next;
   }
-  
+  # Check to see if previous payment was sent payment
+  my $prevPayNum = $PAYMENT_NUMBER - 1;
+
+  my $lastPayDate = PaymentDateByTicket($ticket, $prevPayNum);
+  if (not defined $lastPayDate) {
+    print STDERR "\"$file\": \"$ticket\": Skipped: payment $prevPayNum was not made yet";
+    if ($pass) {
+      print STDERR ".\n";
+    } else {
+      print STDERR "... NOTE: previous payment was not sent; should it be sent now?\n";
+    }
+    next;
+  }
+  my $completedInternshipField = GetCustomFieldForTicket($ticket, "completed-internship");
+  my $expectVal = 'payment-' . $prevPayNum . "-approved";
+  if ($completedInternshipField ne $expectVal) {
+    print STDERR "\"$file\": \"$ticket\": Skipped: completed-internship field was ",
+      "\"$completedInternshipField\" instead of \"$expectVal\".\n";
+    next;
+  }
+
+  my(@links) = GetLinksForTicket($ticket);
+  my $taxTicket = FindTaxTicketFromList(@links);
+  if (not defined $taxTicket) {
+    print STDERR "\"$file\": \"$ticket\": Skipped: no tax ticket found.\n";
+  }
+  my $taxTicketStatus = GetStatusFromTicket($taxTicket);
+  if ($taxTicketStatus ne "resolved") {
+    print STDERR "\"$file\": \"$ticket\": Tax Ticket: \"$taxTicket\": Skipped:  ",
+      "tax ticket is in status \"$taxTicketStatus\" instead of \"resolved\"\n";
+    next;
+  }
+  my $mainTicketStatus = GetStatusFromTicket($ticket);
+  if ($mainTicketStatus ne "needs-project-ok") {
+    print STDERR "\"$file\": \"$ticket\": Skipped:  ",
+      "ticket is in status \"$mainTicketStatus\" instead of \"needs-project-ok\"\n";
+    next;
+  }
 }
 ###############################################################################
 #