Rename var oldestPledgePayment to nextInstallmentdue.
authoreileen <emcnaughton@wikimedia.org>
Tue, 15 Aug 2017 20:11:33 +0000 (16:11 -0400)
committereileen <emcnaughton@wikimedia.org>
Tue, 15 Aug 2017 20:31:38 +0000 (16:31 -0400)
I'm having serious serious issues reading this code. Renaming the variable is a step towards making sense of
it.  actually refers to the next pledge payment installment that is unpaid,
matching from the oldest

CRM/Pledge/BAO/PledgePayment.php

index b0cfcf84cdc31648795033009bccca790e2e33d2..ed1900f8ac03c5005024af75f89da2d66fd0c062 100644 (file)
@@ -495,7 +495,7 @@ WHERE  civicrm_pledge.id = %2
       2 => array($pledgeID, 'Integer'),
     );
 
-    $dao = CRM_Core_DAO::executeQuery($query, $params);
+    CRM_Core_DAO::executeQuery($query, $params);
 
     return $pledgeStatusID;
   }
@@ -823,47 +823,46 @@ WHERE civicrm_pledge_payment.contribution_id = {$paymentContributionId}
       $payment = self::add($pledgeParams);
     }
     else {
-      $oldestPayment = self::getOldestPledgePayment($pledgeID);
+      $nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID);
       if (!$paymentContributionId) {
         // means we are editing payment scheduled payment, so get the second pending to update.
-        $oldestPayment = self::getOldestPledgePayment($pledgeID, 2);
-        if (($oldestPayment['count'] != 1) && ($oldestPayment['id'] == $pPaymentId)) {
-          $oldestPayment = self::getOldestPledgePayment($pledgeID);
+        $nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID, 2);
+        if (($nextPledgeInstallmentDue['count'] != 1) && ($nextPledgeInstallmentDue['id'] == $pPaymentId)) {
+          $nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID);
         }
       }
 
-      if ($oldestPayment) {
+      if ($nextPledgeInstallmentDue) {
         // not the last scheduled payment and the actual amount is less than the expected , add it to oldest pending.
-        if (($actualAmount != $pledgeScheduledAmount) && (($actualAmount < $pledgeScheduledAmount) || (($actualAmount - $pledgeScheduledAmount) < $oldestPayment['amount']))) {
-          $oldScheduledAmount = $oldestPayment['amount'];
+        if (($actualAmount != $pledgeScheduledAmount) && (($actualAmount < $pledgeScheduledAmount) || (($actualAmount - $pledgeScheduledAmount) < $nextPledgeInstallmentDue['amount']))) {
+          $oldScheduledAmount = $nextPledgeInstallmentDue['amount'];
           $newScheduledAmount = $oldScheduledAmount + ($pledgeScheduledAmount - $actualAmount);
           // store new amount in oldest pending payment record.
           CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment',
-            $oldestPayment['id'],
+            $nextPledgeInstallmentDue['id'],
             'scheduled_amount',
             $newScheduledAmount
           );
-          if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $oldestPayment['id'], 'contribution_id', 'id')) {
+          if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'contribution_id', 'id')) {
             CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment',
-              $oldestPayment['id'],
+              $nextPledgeInstallmentDue['id'],
               'contribution_id',
               $paymentContributionId
             );
           }
         }
-        elseif (($actualAmount > $pledgeScheduledAmount) && (($actualAmount - $pledgeScheduledAmount) >= $oldestPayment['amount'])) {
+        elseif (($actualAmount > $pledgeScheduledAmount) && (($actualAmount - $pledgeScheduledAmount) >= $nextPledgeInstallmentDue['amount'])) {
           // here the actual amount is greater than expected and also greater than the next installment amount, so update the next installment as complete and again add it to next subsequent pending payment
           // set the actual amount of the next pending to '0', set contribution Id to current contribution Id and status as completed
-          $paymentId = array($oldestPayment['id']);
+          $paymentId = array($nextPledgeInstallmentDue['id']);
           self::updatePledgePayments($pledgeID, array_search('Completed', $allStatus), $paymentId, 0, $paymentContributionId);
-          CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $oldestPayment['id'], 'scheduled_amount', 0, 'id');
-
+          CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'scheduled_amount', 0, 'id');
           if (!$paymentContributionId) {
             // means we are editing payment scheduled payment.
             $oldestPaymentAmount = self::getOldestPledgePayment($pledgeID, 2);
           }
           $newActualAmount = round(($actualAmount - $pledgeScheduledAmount), CRM_Utils_Money::getCurrencyPrecision());
-          $newPledgeScheduledAmount = $oldestPayment['amount'];
+          $newPledgeScheduledAmount = $nextPledgeInstallmentDue['amount'];
           if (!$paymentContributionId) {
             $newActualAmount = ($actualAmount - $pledgeScheduledAmount);
             $newPledgeScheduledAmount = $oldestPaymentAmount['amount'];