From 898bfdcde64e9f3f0a056e8a916d7cc7ca84bc29 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 15 Mar 2017 18:00:05 +0530 Subject: [PATCH] CRM-20286, added card type form fields ---------------------------------------- * CRM-20286: Add card type field on search form https://issues.civicrm.org/jira/browse/CRM-20286 CRM-20286, code cleanup and defined $_wheretables and $_tables correctly for batch and is_payment fields ---------------------------------------- * CRM-20286: Add card type field on search form https://issues.civicrm.org/jira/browse/CRM-20286 CRM-20286, added where clause for card type ---------------------------------------- * CRM-20286: Add card type field on search form https://issues.civicrm.org/jira/browse/CRM-20286 CRM-20286, added unit test ---------------------------------------- * CRM-20286: Add card type field on search form https://issues.civicrm.org/jira/browse/CRM-20286 Use metadata based method to add field to form CRM-20286 Minor changes ---------------------------------------- * CRM-20286: Add card type field on search form https://issues.civicrm.org/jira/browse/CRM-20286 --- CRM/Contribute/BAO/Query.php | 30 +++-- .../CRM/Contribute/Form/Search/Common.tpl | 4 + tests/phpunit/CRM/Contribute/Form/Search.php | 105 ++++++++++++++++++ 3 files changed, 131 insertions(+), 8 deletions(-) diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index a78c2e444d..afbc0963e3 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -94,6 +94,7 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { if (!empty($query->_returnProperties['contribution_batch'])) { $query->_select['contribution_batch'] = "civicrm_batch.title as contribution_batch"; $query->_element['contribution_batch'] = 1; + $query->_tables['civicrm_financial_trxn'] = 1; $query->_tables['contribution_batch'] = 1; } @@ -424,6 +425,7 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $qillOp, 2 => $qillValue)); $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_entity_batch.batch_id', $op, $value); $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; + $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1; $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1; return; @@ -438,7 +440,15 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { case 'contribution_is_payment': $query->_where[$grouping][] = " civicrm_financial_trxn.is_payment $op $value"; - $query->_tables['contribution_financial_trxn'] = $query->_whereTables['contribution_financial_trxn'] = 1; + $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1; + return; + + case 'financial_trxn_card_type': + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_financial_trxn.card_type', $op, $value); + $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1; + $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; + list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Financial_DAO_FinancialTrxn', 'card_type', $value, $op); + $query->_qill[$grouping][] = ts('Card Type %1 %2', array(1 => $op, 2 => $value)); return; default: @@ -600,18 +610,20 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { break; case 'contribution_batch': - $from .= " $side JOIN civicrm_entity_financial_trxn ON ( - civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution' - AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )"; - - $from .= " $side JOIN civicrm_financial_trxn ON ( - civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )"; - $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn' AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )"; $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id"; break; + + case 'civicrm_financial_trxn': + $from .= " $side JOIN civicrm_entity_financial_trxn ON ( + civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution' + AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )"; + + $from .= " $side JOIN civicrm_financial_trxn ON ( + civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )"; + break; } return $from; } @@ -947,6 +959,8 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { ) ); + $form->addField('financial_trxn_card_type', array('entity' => 'FinancialTrxn', 'name' => 'card_type')); + // CRM-16713 - contribution search by premiums on 'Find Contribution' form. $form->add('select', 'contribution_product_id', ts('Premium'), diff --git a/templates/CRM/Contribute/Form/Search/Common.tpl b/templates/CRM/Contribute/Form/Search/Common.tpl index 6e2ff965a0..d90cc57bfd 100644 --- a/templates/CRM/Contribute/Form/Search/Common.tpl +++ b/templates/CRM/Contribute/Form/Search/Common.tpl @@ -47,6 +47,10 @@ {$form.contribution_check_number.label}
{$form.contribution_check_number.html} +
+ {$form.financial_trxn_card_type.label}
+ {$form.financial_trxn_card_type.html} +
{$form.contribution_trxn_id.label}
diff --git a/tests/phpunit/CRM/Contribute/Form/Search.php b/tests/phpunit/CRM/Contribute/Form/Search.php index fc92194725..612184e715 100644 --- a/tests/phpunit/CRM/Contribute/Form/Search.php +++ b/tests/phpunit/CRM/Contribute/Form/Search.php @@ -167,4 +167,109 @@ class CRM_Contribute_Form_Search extends CiviUnitTestCase { } } + /** + * CRM-20286: Test CRM_Contribute_Form_Search Card type filters + */ + public function testCardTypeFilter() { + $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' => 1, + 'contact_id' => $contactID1, + )); + $params = array( + 'to_financial_account_id' => 1, + 'status_id' => 1, + 'contribution_id' => $Contribution1['id'], + 'payment_instrument_id' => 1, + 'card_type' => 1, + 'total_amount' => 100, + ); + CRM_Core_BAO_FinancialTrxn::create($params); + $Contribution2 = $this->callAPISuccess('Contribution', 'create', array( + 'financial_type_id' => 1, + 'total_amount' => 150, + 'receive_date' => date('Ymd'), + 'receive_date_time' => NULL, + 'payment_instrument' => 1, + 'contribution_status_id' => 1, + 'contact_id' => $contactID1, + )); + $Contribution3 = civicrm_api3('Contribution', 'create', array( + 'financial_type_id' => 1, + 'total_amount' => 200, + 'receive_date' => date('Ymd'), + 'receive_date_time' => NULL, + 'payment_instrument' => 1, + 'contribution_status_id' => 1, + 'contact_id' => $contactID2, + )); + $params = array( + 'to_financial_account_id' => 1, + 'status_id' => 1, + 'contribution_id' => $Contribution3['id'], + 'payment_instrument_id' => 1, + 'card_type' => 2, + 'total_amount' => 200, + ); + CRM_Core_BAO_FinancialTrxn::create($params); + + $useCases = array( + // Case 1: Search for ONLY those contributions which have card type + array( + 'form_value' => array('financial_trxn_card_type' => 'IS NOT NULL'), + 'expected_count' => 2, + 'expected_contribution' => array($Contribution1['id'], $Contribution3['id']), + 'expected_qill' => 'Card Type Not Null', + ), + // Case 2: Search for ONLY those contributions which have Card Type as Visa + array( + 'form_value' => array('financial_trxn_card_type' => array(1)), + 'expected_count' => 1, + 'expected_contribution' => array($Contribution1['id']), + 'expected_qill' => 'Card Type In Visa', + ), + // Case 3: Search for ONLY those contributions which have Card Type as Amex + array( + 'form_value' => array('financial_trxn_card_type' => array(3)), + 'expected_count' => 0, + 'expected_contribution' => array(), + 'expected_qill' => 'Card Type In Amex', + ), + // Case 4: Search for ONLY those contributions which have Card Type as Visa or MasterCard + array( + 'form_value' => array('financial_trxn_card_type' => array(1, 2)), + 'expected_count' => 2, + 'expected_contribution' => array($Contribution1['id'], $Contribution3['id']), + 'expected_qill' => 'Card Type In Visa, MasterCard', + ), + ); + + foreach ($useCases as $case) { + $fv = $case['form_value']; + CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('financial_trxn_card_type')); + $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', $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); + } + } + } -- 2.25.1