Skip to content

Commit

Permalink
CRM-21577 add links to be able to add payments from the contribution …
Browse files Browse the repository at this point in the history
…block
  • Loading branch information
eileenmcnaughton committed Dec 19, 2017
1 parent f06b38e commit 87f25cd
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
71 changes: 67 additions & 4 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4104,12 +4104,18 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
}

$paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total);
$contributionIsPayLater = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'is_pay_later');
$contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $id, 'return' => array('is_pay_later', 'contribution_status_id', 'financial_type_id')));

$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
$info['payLater'] = $contribution['is_pay_later'];
$info['contribution_status'] = $contribution['contribution_status'];

$financialTypeId = $contribution['financial_type_id'];
$feeFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is');

if ($paymentBalance == 0 && $contributionIsPayLater) {
if ($paymentBalance == 0 && $info['payLater']) {
// @todo - review - this looks very unlikely to be correct.
// the balance should be correct based on payment transactions not
// assumptions.
$paymentBalance = $total;
}

Expand All @@ -4118,7 +4124,6 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
$info['balance'] = $paymentBalance;
$info['id'] = $id;
$info['component'] = $component;
$info['payLater'] = $contributionIsPayLater;
$rows = array();
if ($getTrxnInfo && $baseTrxnId) {
// Need to exclude fee trxn rows so filter out rows where TO FINANCIAL ACCOUNT is expense account
Expand Down Expand Up @@ -4197,6 +4202,8 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
}
$info['transaction'] = $rows;
}

$info['payment_links'] = self::getContributionPaymentLinks($id, $paymentBalance, $info['contribution_status']);
return $info;
}

Expand Down Expand Up @@ -5476,6 +5483,62 @@ protected static function updateMembershipBasedOnCompletionOfContribution($contr
}
}

/**
* Get payment links as they relate to a contribution.
*
* If a payment can be made then include a payment link & if a refund is appropriate
* then a refund link.
*
* @param int $id
* @param float $balance
* @param string $contributionStatus
*
* @return array $actionLinks Links array containing:
* -url
* -title
*/
protected static function getContributionPaymentLinks($id, $balance, $contributionStatus) {
if ($contributionStatus === 'Failed' || !CRM_Core_Permission::check('edit contributions')) {
// In general the balance is the best way to determine if a payment can be added or not,
// but not for Failed contributions, where we don't accept additional payments at the moment.
// (in some cases the contribution is 'Pending' and only the payment is failed. In those we
// do accept more payments agains them.
return array();
}
if ((int) $balance > 0) {
if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
$actionLinks[] = array(
'url' => CRM_Utils_System::url('civicrm/payment', array(
'action' => 'add',
'reset' => 1,
'id' => $id,
'mode' => 'live',
)),
'title' => ts('Submit Credit Card payment'),
);
}
$actionLinks[] = array(
'url' => CRM_Utils_System::url('civicrm/payment', array(
'action' => 'add',
'reset' => 1,
'id' => $id,
)),
'title' => ts('Record Payment'),
);
}
elseif ((int) $balance < 0) {
$actionLinks[] = array(
'url' => CRM_Utils_System::url('civicrm/payment', array(
'action' => 'add',
'reset' => 1,
'id' => $id,
)),
'title' => ts('Record Refund'),
);
}
return $actionLinks;
}

/**
* Assign Test Value.
*
Expand Down
12 changes: 12 additions & 0 deletions CRM/Contribute/Form/AbstractEditPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {
*/
public $_id;

/**
* Entity that $this->_id relates to.
*
* If set the contact id is not required in the url.
*
* @var string
*/
protected $entity;

/**
* The id of the premium that we are proceessing.
*
Expand Down Expand Up @@ -217,6 +226,9 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {
*/
public function preProcess() {
$this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
if (empty($this->_contactID) && !empty($this->_id) && $this->entity) {
$this->_contactID = civicrm_api3($this->entity, 'getvalue', array('id' => $this->_id, 'return' => 'contact_id'));
}
$this->assign('contactID', $this->_contactID);
CRM_Core_Resources::singleton()->addVars('coreForm', array('contact_id' => (int) $this->_contactID));
$this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
Expand Down
14 changes: 8 additions & 6 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
*/
public $_id = NULL;

protected $entity = 'Contribution';

protected $_owed = NULL;

protected $_refund = NULL;
Expand Down Expand Up @@ -80,11 +82,10 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract

public function preProcess() {

parent::preProcess();
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
// @todo don't set this - rely on parent $this->contactID
$this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
$this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, TRUE);
parent::preProcess();
$this->_contactId = $this->_contactID;
$this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, FALSE, 'contribution');
$this->_view = CRM_Utils_Request::retrieve('view', 'String', $this, FALSE);
$this->assign('component', $this->_component);
$this->assign('id', $this->_id);
Expand All @@ -100,6 +101,7 @@ public function preProcess() {
CRM_Utils_System::setTitle($title);
$this->assign('transaction', TRUE);
$this->assign('payments', $paymentInfo['transaction']);
$this->assign('paymentLinks', $paymentInfo['payment_links']);
return;
}
$this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
Expand Down Expand Up @@ -138,10 +140,10 @@ public function preProcess() {
CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use'));
}

list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);

$this->assign('contributionMode', $this->_mode);
$this->assign('contactId', $this->_contactId);
$this->assign('contactId', $this->_contactID);
$this->assign('paymentType', $this->_paymentType);
$this->assign('paymentAmt', abs($paymentAmt));

Expand Down
5 changes: 5 additions & 0 deletions templates/CRM/Contribute/Form/PaymentInfoBlock.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@
{/if}
{ts 1=$entity}No payments found for this %1 record{/ts}
{/if}

{foreach from=$paymentLinks item=paymentLink}
<a class="open-inline action-item crm-hover-button" href="{$paymentLink.url}">&raquo; {ts}{$paymentLink.title}{/ts}</a>
{/foreach}

{/crmRegion}

0 comments on commit 87f25cd

Please sign in to comment.