Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21343: Add cancel filters for Contribution search #11638

Merged
merged 2 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function whereClauseSingle(&$values, &$query) {
case 'contribution_date_high_time':
// process to / from date
$query->dateQueryBuilder($values,
'civicrm_contribution', 'contribution_date', 'receive_date', 'Contribution Date'
'civicrm_contribution', 'contribution_date', 'receive_date', ts('Contribution Date')
);
return;

Expand Down Expand Up @@ -227,6 +227,17 @@ public static function whereClauseSingle(&$values, &$query) {
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
return;

case 'contribution_cancel_date':
case 'contribution_cancel_date_low':
case 'contribution_cancel_date_low_time':
case 'contribution_cancel_date_high':
case 'contribution_cancel_date_high_time':
// process to / from date
$query->dateQueryBuilder($values,
'civicrm_contribution', 'contribution_cancel_date', 'cancel_date', ts('Cancelled / Refunded Date')
);
return;

case 'financial_type_id':
// @todo we need to make this resemble a hook approach.
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
Expand Down Expand Up @@ -905,6 +916,11 @@ public static function buildSearchForm(&$form) {
$form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
$form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');

// Adding Cancelled Contribution fields -- CRM-21343
$form->add('text', 'contribution_cancel_reason', ts('Cancellation / Refund Reason'), array('size' => 40));
CRM_Core_Form_Date::buildDateRange($form, 'contribution_cancel_date', 1, '_low', '_high', ts('From:'), FALSE);
$form->addElement('hidden', 'contribution_cancel_date_range_error');

// Adding select option for curreny type -- CRM-4711
$form->add('select', 'contribution_currency_type',
ts('Currency Type'),
Expand Down Expand Up @@ -1082,11 +1098,12 @@ public static function buildDateWhere(&$values, $query, $name, $field, $title) {
public static function formRule($fields, $files, $form) {
$errors = array();

if (empty($fields['contribution_date_high']) || empty($fields['contribution_date_low'])) {
return TRUE;
if (!empty($fields['contribution_date_high']) && !empty($fields['contribution_date_low'])) {
CRM_Utils_Rule::validDateRange($fields, 'contribution_date', $errors, ts('Date Received'));
}
if (!empty($fields['contribution_cancel_date_high']) && !empty($fields['contribution_cancel_date_low'])) {
CRM_Utils_Rule::validDateRange($fields, 'contribution_cancel_date', $errors, ts('Cancel Date'));
}

CRM_Utils_Rule::validDateRange($fields, 'contribution_date', $errors, ts('Date Received'));

return empty($errors) ? TRUE : $errors;
}
Expand Down
39 changes: 28 additions & 11 deletions templates/CRM/Contribute/Form/Search/Common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
<td><label>{$form.contribution_status_id.label}</label> <br />
{$form.contribution_status_id.html} </td>
</tr>
<tr>
<td>
<label>{ts}Currency{/ts}</label> <br />
{$form.contribution_currency_type.html|crmAddClass:twenty}
</td>
{if $form.contribution_batch_id.html }
<td>
{$form.contribution_batch_id.label}<br />
{$form.contribution_batch_id.html}
</td>
{/if}
</tr>
<tr>
<td>
<div class="float-left">
Expand Down Expand Up @@ -146,22 +158,27 @@
{include file="CRM/Contribute/Form/PCP.js.tpl"}
</td>
<td>
{$form.contribution_pcp_display_in_roll.label}
{$form.contribution_pcp_display_in_roll.html}
{$form.contribution_cancel_reason.label}<br />
{$form.contribution_cancel_reason.html}
</td>
</tr>

<tr>
<td>
<label>{ts}Currency{/ts}</label> <br />
{$form.contribution_currency_type.html|crmAddClass:twenty}
{$form.contribution_pcp_display_in_roll.label}
{$form.contribution_pcp_display_in_roll.html}
</td>
<td>
<table style="width:auto">
<tr>
<td>
<label>{ts}Cancelled / Refunded Date{/ts}</label>
</td>
</tr>
<tr>
{include file="CRM/Core/DateRange.tpl" fieldName="contribution_cancel_date" from='_low' to='_high'}
</tr>
</table>
</td>
{if $form.contribution_batch_id.html }
<td>
{$form.contribution_batch_id.label}<br />
{$form.contribution_batch_id.html}
</td>
{/if}
</tr>

{* campaign in contribution search *}
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,90 @@ public function testContributionRecurStatusFilter() {
}
}

/**
* CRM-21343: Test CRM_Contribute_Form_Search Cancelled filters
*/
public function testCancelledFilter() {
$this->quickCleanup($this->_tablesToTruncate);
$contactID1 = $this->individualCreate(array(), 1);
$contactID2 = $this->individualCreate(array(), 2);
$Contribution1 = $this->callAPISuccess('Contribution', 'create', array(
'financial_type_id' => 1,
'total_amount' => 100,
'receive_date' => date('Ymd'),
'receive_date_time' => NULL,
'payment_instrument' => 1,
'contribution_status_id' => 3,
'cancel_date' => date('Ymd'),
'cancel_reason' => 'Insufficient funds',
'contact_id' => $contactID1,
));
$Contribution2 = $this->callAPISuccess('Contribution', 'create', array(
'financial_type_id' => 1,
'total_amount' => 150,
'receive_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')),
'receive_date_time' => NULL,
'payment_instrument' => 1,
'contribution_status_id' => 3,
'cancel_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')),
'cancel_reason' => 'Insufficient funds',
'contact_id' => $contactID2,
));
$Contribution3 = $this->callAPISuccess('Contribution', 'create', array(
'financial_type_id' => 1,
'total_amount' => 200,
'receive_date' => date('Ymd'),
'receive_date_time' => NULL,
'payment_instrument' => 1,
'contribution_status_id' => 3,
'cancel_date' => date('Ymd'),
'cancel_reason' => 'Invalid Credit Card Number',
'contact_id' => $contactID1,
));

$useCases = array(
// Case 1: Search for Cancelled Date
array(
'form_value' => array('cancel_date' => date('Y-m-d')),
'expected_count' => 2,
'expected_contribution' => array($Contribution1['id'], $Contribution3['id']),
'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'",
),
// Case 2: Search for Cancelled Reason
array(
'form_value' => array('cancel_reason' => 'Invalid Credit Card Number'),
'expected_count' => 1,
'expected_contribution' => array($Contribution3['id']),
'expected_qill' => "Cancel Reason Like '%Invalid Credit Card Number%'",
),
// Case 3: Search for Cancelled Date and Cancelled Reason
array(
'form_value' => array('cancel_date' => date('Y-m-d'), 'cancel_reason' => 'Insufficient funds'),
'expected_count' => 1,
'expected_contribution' => array($Contribution1['id']),
'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'ANDCancel Reason Like '%Insufficient funds%'",
),
);

foreach ($useCases as $case) {
$fv = $case['form_value'];
CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('cancel_date', 'cancel_reason'));
$query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
list($select, $from, $where, $having) = $query->query();

// get and assert contribution count
$contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL AND civicrm_contribution.contribution_status_id = 3', $from, $where))->fetchAll();
foreach ($contributions as $key => $value) {
$contributions[$key] = $value['id'];
}
// assert the contribution count
$this->assertEquals($case['expected_count'], count($contributions));
// assert the contribution IDs
$this->checkArrayEquals($case['expected_contribution'], $contributions);
// get and assert qill string
$qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
$this->assertEquals($case['expected_qill'], $qill);
}
}

}