Show 'Pay Now' on user dashboard for partially paid contributions
authorJon Goldberg <jon@megaphonetech.com>
Fri, 21 May 2021 15:10:47 +0000 (11:10 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Thu, 27 May 2021 18:25:11 +0000 (14:25 -0400)
CRM/Contribute/Page/UserDashboard.php
templates/CRM/Contribute/Page/UserDashboard.tpl
tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php

index 11adcd89e3a21913fb3c0c57630cdecde267071b..0a4c9f0fe1ced243b317bf9644608a68b5fc01b9 100644 (file)
@@ -48,7 +48,10 @@ class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBo
       // This is required for tpl logic. We should move away from hard-code this to adding an array of actions to the row
       // which the tpl can iterate through - this should allow us to cope with competing attempts to add new buttons
       // and allow extensions to assign new ones through the pageRun hook
-      if ('Pending' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id'])) {
+      $row['balance_amount'] = CRM_Contribute_BAO_Contribution::getContributionBalance($row['contribution_id']);
+      $contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id']);
+
+      if (in_array($contributionStatus, ['Pending', 'Partially paid'])) {
         $row['buttons']['pay'] = [
           'class' => 'button',
           'label' => ts('Pay Now'),
index 0520ebc1a300c60db90d00034a93b8ffb92a9f4c..d856cf4d9869a1bf05317ebd03c39d525110cbed 100644 (file)
                     <th>{ts}Financial Type{/ts}</th>
                     <th>{ts}Received date{/ts}</th>
                     <th>{ts}Receipt Sent{/ts}</th>
+                    <th>{ts}Balance{/ts}</th>
                     <th>{ts}Status{/ts}</th>
                     {if $isIncludeInvoiceLinks}
                       <th></th>
                     {/if}
-                    {foreach from=$row.buttons item=button}
-                      <th></th>
-                    {/foreach}
+                    <th></th>
                 </tr>
 
                 {foreach from=$contribute_rows item=row}
@@ -39,6 +38,7 @@
                         <td>{$row.financial_type}</td>
                         <td>{$row.receive_date|truncate:10:''|crmDate}</td>
                         <td>{$row.receipt_date|truncate:10:''|crmDate}</td>
+                        <td>{$row.balance_amount|crmMoney:$row.currency}</td>
                         <td>{$row.contribution_status}</td>
                         {if $isIncludeInvoiceLinks}
                           <td>
                             {/if}
                           </td>
                         {/if}
+                        <td>
                         {foreach from=$row.buttons item=button}
-                          <td><a class="{$button.class}" href="{$button.url}"><span class='nowrap'>{$button.label}</span></a></td>
+                          <a class="{$button.class}" href="{$button.url}"><span class='nowrap'>{$button.label}</span></a>
                         {/foreach}
+                        </td>
                     </tr>
                 {/foreach}
             </table>
index 9d0abd24333ae482c17364724527be48f148636b..9ae687e1a5c896f18ec2933ff3f313d2e5f9f147 100644 (file)
@@ -113,8 +113,8 @@ class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
     $this->runUserDashboard();
     $expectedStrings = [
       'Your Contribution(s)',
-      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Status</th><th></th>',
-      '<td>Completed</td><td><a class="button no-popup nowrap"href="/index.php?q=civicrm/contribute/invoice&amp;reset=1&amp;id=1&amp;cid=' . $this->contactID . '"><i class="crm-i fa-print" aria-hidden="true"></i><span>Print Invoice</span></a></td></tr><tr id=\'rowid2\'',
+      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th><th></th>',
+      '<td>Completed</td><td><a class="button no-popup nowrap"href="/index.php?q=civicrm/contribute/invoice&amp;reset=1&amp;id=1&amp;cid=' . $this->contactID . '"><i class="crm-i fa-print" aria-hidden="true"></i><span>Print Invoice</span></a></td><td></td></tr><tr id=\'rowid2\'',
       'Pay Now',
     ];
 
@@ -149,13 +149,46 @@ class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
     $this->runUserDashboard();
     $expectedStrings = [
       'Your Contribution(s)',
-      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Status</th>',
+      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th>',
       '<td>$ 100.00 </td><td>Donation</td>',
       '<td>Completed</td>',
     ];
     $this->assertPageContains($expectedStrings);
   }
 
+  /**
+   * Test the presence of a "Pay Now" button on partial payments
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testDashboardPartialPayments() {
+    $contributionId = $this->contributionCreate([
+      'contact_id' => $this->contactID,
+      'contribution_status_id' => 'Pending',
+      'total_amount' => 25,
+    ]);
+    $result = civicrm_api3('Payment', 'create', [
+      'contribution_id' => $contributionId,
+      'total_amount' => 11,
+      'trxn_date' => "2021-05-11",
+    ]);
+    $this->contributions[] = civicrm_api3('Contribution', 'get', [
+      'contact_id' => $this->contactID,
+      'options' => ['limit' => 12, 'sort' => 'receive_date DESC'],
+      'sequential' => 1,
+    ])['values'];
+    $this->runUserDashboard();
+    $expectedStrings = [
+      'Your Contribution(s)',
+      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th>',
+      '<td>$ 25.00 </td><td>Donation</td>',
+      '<td>$ 14.00</td><td>Partially paid</td>',
+      'Pay Now',
+    ];
+    $this->assertPageContains($expectedStrings);
+  }
+
   /**
    * Run the user dashboard.
    */