Skip to content

Commit

Permalink
CRM-16189, added link on contribution edit form to record payment for…
Browse files Browse the repository at this point in the history
… pending pay later and partially paid contribution

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189
  • Loading branch information
pradpnayak committed Sep 20, 2016
1 parent 328a667 commit 65bf969
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 186 deletions.
33 changes: 15 additions & 18 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3942,6 +3942,10 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay
$entityObj = CRM_Event_BAO_Participant::getValues($inputParams, $values, $ids);
$entityObj = $entityObj[$participantId];
}
else {
$entityObj = $contributionDAO;
$component = 'contribution';
}
$activityType = ($paymentType == 'refund') ? 'Refund' : 'Payment';

self::addActivityForPayment($entityObj, $financialTrxn, $activityType, $component, $contributionId);
Expand All @@ -3960,14 +3964,17 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay
*/
public static function addActivityForPayment($entityObj, $trxnObj, $activityType, $component, $contributionId) {
if ($component == 'event') {
$date = CRM_Utils_Date::isoToMysql($trxnObj->trxn_date);
$paymentAmount = CRM_Utils_Money::format($trxnObj->total_amount, $trxnObj->currency);
$eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Event', $entityObj->event_id, 'title');
$subject = "{$paymentAmount} - Offline {$activityType} for {$eventTitle}";
$targetCid = $entityObj->contact_id;
// source record id would be the contribution id
$srcRecId = $contributionId;
$title = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Event', $entityObj->event_id, 'title');
}
else {
$title = ts('Contribution');
}
$paymentAmount = CRM_Utils_Money::format($trxnObj->total_amount, $trxnObj->currency);
$subject = "{$paymentAmount} - Offline {$activityType} for {$title}";
$date = CRM_Utils_Date::isoToMysql($trxnObj->trxn_date);
$targetCid = $entityObj->contact_id;
// source record id would be the contribution id
$srcRecId = $contributionId;

// activity params
$activityParams = array(
Expand Down Expand Up @@ -4035,17 +4042,7 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
$baseTrxnId = $baseTrxnId['financialTrxnId'];
}
if (!CRM_Utils_Array::value('total_amount', $total) || $usingLineTotal) {
// for additional participants
if ($entityTable == 'civicrm_participant') {
$ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
$total = 0;
foreach ($ids as $val) {
$total += CRM_Price_BAO_LineItem::getLineTotal($val, $entityTable);
}
}
else {
$total = CRM_Price_BAO_LineItem::getLineTotal($id, $entityTable);
}
$total = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
}
else {
$baseTrxnId = $total['trxn_id'];
Expand Down
5 changes: 0 additions & 5 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ public static function select(&$query) {
$query->_element['contribution_campaign_title'] = $query->_tables['civicrm_campaign'] = 1;
}

if (!empty($query->_returnProperties['contribution_participant_id'])) {
$query->_select['contribution_participant_id'] = "civicrm_participant.id as contribution_participant_id";
$query->_whereTables['contribution_participant'] = $query->_tables['contribution_participant'] = 1;
}

// Adding address_id in a way that is more easily extendable since the above is a bit ... wordy.
$supportedBasicReturnValues = array('address_id');
foreach ($supportedBasicReturnValues as $fieldName) {
Expand Down
115 changes: 52 additions & 63 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,17 @@ public function preProcess() {
$this->_formType = CRM_Utils_Array::value('formType', $_GET);

$enitityType = NULL;
$enitityType = 'contribution';
if ($this->_component == 'event') {
$enitityType = 'participant';
$this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
$eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
$this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($eventId);
}
else {
$this->_contributionId = $this->_id;
$this->_fromEmails['from_email_id'] = CRM_Core_BAO_Email::getFromEmail();
}
$eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
$this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($eventId);

$paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_id, $enitityType);
$paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, FALSE, TRUE);
Expand Down Expand Up @@ -369,56 +374,63 @@ public static function formRule($fields, $files, $self) {

public function postProcess() {
$participantId = NULL;
$childTab = 'contribute';
if ($this->_component == 'event') {
$participantId = $this->_id;
$childTab = 'participant';
}
$submittedValues = $this->controller->exportValues($this->_name);
$submittedValues['confirm_email_text'] = CRM_Utils_Array::value('receipt_text', $submittedValues);

$contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution',
'contribution_status_id',
array('labelColumn' => 'name')
);
$contributionStatusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id');
if ($contributionStatuses[$contributionStatusID] == 'Pending') {
CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id', array_search('Partially paid', $contributionStatuses));
}
$submittedValues['trxn_date'] = CRM_Utils_Date::processDate($submittedValues['trxn_date'], $submittedValues['trxn_date_time']);
if ($this->_mode) {
// process credit card
$this->assign('contributeMode', 'direct');
$this->processCreditCard($submittedValues);
$submittedValues = $this->_params;
}
else {
$defaults = array();
$contribution = civicrm_api3('Contribution', 'getsingle', array(
'return' => array("contribution_status_id"),
'id' => $this->_contributionId,
));
$contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
$result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
// Fetch the contribution & do proportional line item assignment
$params = array('id' => $this->_contributionId);
$contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_contributionId);
if (!empty($lineItems)) {
CRM_Contribute_BAO_Contribution::addPayments($lineItems, array($contribution), $contributionStatusId);
}

// email sending
if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
$submittedValues['contact_id'] = $this->_contactId;
$submittedValues['contribution_id'] = $this->_contributionId;

// to get 'from email id' for send receipt
$this->fromEmailId = $submittedValues['from_email_address'];
$sendReceipt = $this->emailReceipt($submittedValues);
}

$statusMsg = ts('The payment record has been processed.');
if (!empty($submittedValues['is_email_receipt']) && $sendReceipt) {
$statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
}

CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
$defaults = array();
$contribution = civicrm_api3('Contribution', 'getsingle', array(
'return' => array("contribution_status_id"),
'id' => $this->_contributionId,
));
$contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
$result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
// Fetch the contribution & do proportional line item assignment
$params = array('id' => $this->_contributionId);
$contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_contributionId);
if (!empty($lineItems)) {
CRM_Contribute_BAO_Contribution::addPayments($lineItems, array($contribution), $contributionStatusId);
}

// email sending
if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
$submittedValues['contact_id'] = $this->_contactId;
$submittedValues['contribution_id'] = $this->_contributionId;

// to get 'from email id' for send receipt
$this->fromEmailId = $submittedValues['from_email_address'];
$sendReceipt = $this->emailReceipt($submittedValues);
}

$statusMsg = ts('The payment record has been processed.');
if (!empty($submittedValues['is_email_receipt']) && $sendReceipt) {
$statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
}
CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');

$session = CRM_Core_Session::singleton();
$session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
"reset=1&cid={$this->_contactId}&selectedChild=participant"
));
}
$session = CRM_Core_Session::singleton();
$session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
"reset=1&cid={$this->_contactId}&selectedChild={$childTab}"
));
}

/**
Expand Down Expand Up @@ -621,29 +633,6 @@ public function processCreditCard($submittedValues) {
);
$this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
}

// process the additional payment
$participantId = NULL;
if ($this->_component == 'event') {
$participantId = $this->_id;
}
$trxnRecord = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);

if ($trxnRecord->id && !empty($this->_params['is_email_receipt'])) {
$sendReceipt = $this->emailReceipt($this->_params);
}

if ($trxnRecord->id) {
$statusMsg = ts('The payment record has been processed.');
if (!empty($this->_params['is_email_receipt']) && $sendReceipt) {
$statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
}

CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
$session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
"reset=1&cid={$this->_contactId}&selectedChild=participant"
));
}
}

/**
Expand Down
22 changes: 15 additions & 7 deletions CRM/Contribute/Form/ContributionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,21 @@ public function preProcess() {
NULL,
$recentOther
);

$participantID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
$values['id'], 'participant_id', 'contribution_id'
);
$this->assign('contactId', $values['contact_id']);
$this->assign('hasPayment', $values['id']);
$this->assign('participantId', $participantID);
$contributionStatus = $status[$values['contribution_status_id']];
if (in_array($contributionStatus, array('Partially paid', 'Pending refund'))
|| ($contributionStatus == 'Pending' && $values['is_pay_later'])
) {
if ($contributionStatus == 'Pending refund') {
$this->assign('paymentButtonName', ts('Record Refund'));
}
else {
$this->assign('paymentButtonName', ts('Record Payment'));
}
$this->assign('addRecordPayment', TRUE);
$this->assign('contactId', $values['contact_id']);
$this->assign('componentId', $id);
$this->assign('component', 'contribution');
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions CRM/Contribute/Page/PaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function preProcess() {
$this->assign('id', $this->_id);
$this->assign('context', $this->_context);
$this->assign('component', $this->_component);
if ($this->_component != 'event') {
$this->assign('hideButtonLinks', TRUE);
}
}

public function browse() {
Expand Down
40 changes: 20 additions & 20 deletions CRM/Contribute/Selector/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
'contribution_soft_credit_contact_id',
'contribution_soft_credit_amount',
'contribution_soft_credit_type',
'contribution_participant_id',
);

/**
Expand Down Expand Up @@ -185,7 +184,6 @@ public function __construct(
// type of selector
$this->_action = $action;
$returnProperties = CRM_Contribute_BAO_Query::selectorReturnProperties();
$returnProperties['contribution_participant_id'] = 1;
$this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
$this->_query = new CRM_Contact_BAO_Query(
$this->_queryParams,
Expand Down Expand Up @@ -418,7 +416,9 @@ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
$contributionStatuses
);

$isPayLater = FALSE;
if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
$isPayLater = TRUE;
$row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
$links[CRM_Core_Action::ADD] = array(
'name' => ts('Pay with Credit Card'),
Expand All @@ -441,26 +441,26 @@ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
'id' => $result->contribution_id,
'cid' => $result->contact_id,
'cxt' => $this->_context,
'pid' => $result->contribution_participant_id,
'compId' => $result->contribution_id,
'comp' => 'contribution',
);
if (!empty($row['contribution_participant_id'])) {
if ($row['contribution_status_name'] == 'Partially paid') {
$links[CRM_Core_Action::ADD] = array(
'name' => ts('Record Payment'),
'url' => 'civicrm/payment',
'qs' => 'reset=1&id=%%pid%%&cid=%%cid%%&action=add&component=event',
'title' => ts('Record Payment'),
);
}

if ($row['contribution_status_name'] == 'Pending refund') {
$links[CRM_Core_Action::ADD] = array(
'name' => ts('Record Refund'),
'url' => 'civicrm/payment',
'qs' => 'reset=1&id=%%pid%%&cid=%%cid%%&action=add&component=event',
'title' => ts('Record Refund'),
);
}
if ($row['contribution_status_name'] == 'Partially paid' || $isPayLater) {
$links[CRM_Core_Action::ADD] = array(
'name' => ts('Record Payment'),
'url' => 'civicrm/payment',
'qs' => 'reset=1&id=%%compId%%&cid=%%cid%%&action=add&component=%%comp%%',
'title' => ts('Record Payment'),
);
}

if ($row['contribution_status_name'] == 'Pending refund') {
$links[CRM_Core_Action::ADD] = array(
'name' => ts('Record Refund'),
'url' => 'civicrm/payment',
'qs' => 'reset=1&id=%%pid%%&cid=%%cid%%&action=add&component=event',
'title' => ts('Record Refund'),
);
}

$row['action'] = CRM_Core_Action::formLink(
Expand Down
Loading

0 comments on commit 65bf969

Please sign in to comment.