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-20610: Handle payment instrument change on Payment Edit form #10814

Merged
merged 5 commits into from
Aug 9, 2017

Conversation

monishdeb
Copy link
Member

@monishdeb monishdeb commented Aug 3, 2017

Overview

  1. Handle financial accounts if payment instrument is changed via payment edit form
  2. Modify Financial.create API to call CRM_Core_BAO_FinancialTrxn::create(...)
  3. Added CRM unit test for PaymentEdit

Screencast

test-case-5


@monishdeb
Copy link
Member Author

@eileenmcnaughton can you please check now? I have mentioned the changes in PR description.

@monishdeb
Copy link
Member Author

Jenkin test this please

@@ -4186,11 +4186,13 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
CRM_Core_Action::mask(array(CRM_Core_Permission::EDIT)),
array(
'id' => $resultDAO->id,
'contri_id' => $contributionId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to use this abbreviation - what is so long about the word contribution that we have to abbreviate it into something hard to read?

elseif (!empty($params['entity_id']) && !empty($params['entity_table'])) {
$entityFinancialTrxnParams['entity_table'] = $params['entity_table'];
$entityFinancialTrxnParams['entity_id'] = $params['entity_id'];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing this function to this -

  /**
   * Create a financial transaction object.
   *
   * @param array $params
   *   Values relating to the transaction..
   *
   * @param array $trxnEntityTable
   *   Deprecated: array of entity_id & entity_table. Put
   *   these in params instead.
   *
   * @return CRM_Core_BAO_FinancialTrxn
   */
  public static function create($params, $trxnEntityTable = array()) {
    $trxn = new CRM_Financial_DBO_FinancialTrxn();
    $trxn->copyValues($params);

    if (!CRM_Utils_Rule::currencyCode($trxn->currency)) {
      $trxn->currency = CRM_Core_Config::singleton()->defaultCurrency;
    }

    $trxn->save();

    // save to entity_financial_trxn table
    $entityFinancialTrxnParams
      = array_merge(array(
        'entity_table' => "civicrm_contribution",
        'financial_trxn_id' => $trxn->id,
        'amount' => $params['total_amount'],
      ), $trxnEntityTable, $params);

    if (empty($entityFinancialTrxnParams['entity_id'])) {
      $entityFinancialTrxnParams['entity_id'] = $params['contribution_id'];
    }

    $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
    $entityTrxn->copyValues($entityFinancialTrxnParams);
    $entityTrxn->save();
    return $trxn;
  }

I could only find two instances where $trxnEntityTable was passed in

BAO_LineItem

          $trxnTable = array(
            'entity_id' => $newFinancialItem->id,
            'entity_table' => 'civicrm_financial_item',
          );
          $reverseTrxn = CRM_Core_BAO_FinancialTrxn::create($updateFinancialItemInfoValues['financialTrxn'], $trxnTable);

&&
FinancialTrxn::createPremiumTrxn (function looks a bit broken to me but that is by the by)

      $trxnEntityTable['entity_table'] = 'civicrm_contribution';
      $trxnEntityTable['entity_id'] = $params['contributionId'];
      CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable);

@@ -39,7 +39,10 @@
* @return array
*/
function civicrm_api3_financial_trxn_create($params) {
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
if (empty($params['contribution_id']) && (empty($params['entity_table']) && empty($params['entity_id']))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this be

civicrm_api3_verify_mandatory($params, NULL, array(array('contribution_id', array('entity_table', 'entity_id')));

verify_mandatory can do OR - I Think I got that right for this OR this AND that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the big difference between what you did, functionally, and verify_mandatory is that if id is passed in verify_mandatory passes it through. The BAO function doesn't seem to handle passing in id well. I don't think the change proposed makes it worse though.

@eileenmcnaughton
Copy link
Contributor

@monishdeb I'm mostly OK with this - I had some suggestions. I can't do a reviewer's Pr due to the temporary rules which make it harder to collaborate so have posted comments on here.

I'm tempted to add a return after $trxn->save() if (!empty($params['id') but I guess that would be a change & it seems to be currently not broken - perhaps it's worth commenting that code at least to point out that it is weird in that scenario

@@ -39,7 +39,11 @@
* @return array
*/
function civicrm_api3_financial_trxn_create($params) {
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
if (empty($params['id']) && empty($params['contribution_id']) && empty($params['entity_id'])) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton actually civicrm_api3_verify_mandatory(...) perform OR operation between keys but here we need AND. I changed it to this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can do a combo of AND + OR ie

array('x', 'y') is x +y

array (('x', 'y') is x OR y

array (('x', array('y', 'z') ) is x AND y or Z

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can use verify_one_mandatory as a clearing way of doing x or y

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still it does x OR y check and we need x AND y here :(

@monishdeb
Copy link
Member Author

monishdeb commented Aug 4, 2017

@eileenmcnaughton I have made the additional fixes you mentioned - 50f8ceb. Also added concatenation fix and added unit-test - 650b79b

Can you please check now?

@@ -50,37 +50,38 @@ public function __construct() {
* @param array $params
* (reference ) an assoc array of name/value pairs.
*
* @param string $trxnEntityTable
* Entity_table.
* @param array $ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not actually $ids - it's just a deprecated parameter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

($ids is generally deprecated on create too but this doesn't hold ids)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On re-reading, it looks like $ids should just be removed as a parameter as you are altering the places that use it

$trxn = new CRM_Financial_DAO_FinancialTrxn();
$trxn->copyValues($params);

if (!CRM_Utils_Rule::currencyCode($trxn->currency)) {
$trxn->currency = CRM_Core_Config::singleton()->defaultCurrency;
}

$trxn->save();
//per http://wiki.civicrm.org/confluence/display/CRM/Database+layer we are moving away from $ids array
$financialTrxnID = CRM_Utils_Array::value('FinancialTrxn', $ids, CRM_Utils_Array::value('id', $params));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't passing in 'id' as a field anywhere so we shouldn't start

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie. just CRM_Utils_Array::value('id', $params)) is fine & we don't need to specially set it - copyValues will do that

);
$reverseTrxn = CRM_Core_BAO_FinancialTrxn::create($updateFinancialItemInfoValues['financialTrxn'], $trxnTable);
));
$reverseTrxn = CRM_Core_BAO_FinancialTrxn::create($updateFinancialItemInfoValues['financialTrxn']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue here - ie. you are passing in $updateFinancialItemInfoValues['financialTrxn'] but it should be $updateFinancialItemInfoValues

);
$trxnEntityTable['entity_table'] = 'civicrm_contribution';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better practice to explicitly state the table?

}

// save to entity_financial_trxn table
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick - whitespace std should use capital letter & full stop

$trxn->save();
//per http://wiki.civicrm.org/confluence/display/CRM/Database+layer we are moving away from $ids array
$financialTrxnID = CRM_Utils_Array::value('FinancialTrxn', $ids, CRM_Utils_Array::value('id', $params));
$trxn->id = $financialTrxnID;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done by copyValues

@@ -897,7 +897,6 @@ public function callAPISuccessGetValue($entity, $params, $type = NULL) {
public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) {
$params += array(
'version' => $this->_apiversion,
'debug' => 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because passing this attribute add xdebug properties in the array, which later impacts checkArrayEquals where it only concern about matching the values of the array, the reason why allowing this attribute mismatches with expected array and leads to test failures.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - it also improves test information available I think - I know the getAndCheck function handles this parameter

Copy link
Member Author

@monishdeb monishdeb Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton in fact getAndCheck doesn't pass this parameter debug as in here and for API's result array comparison, this extraneous information are avoided while checking:

public function assertAPIArrayComparison($result, $expected, $valuesToExclude = array(), $prefix = '') {
  $valuesToExclude = array_merge($valuesToExclude, array('debug', 'xdebug', 'sequential'));
...

@monishdeb
Copy link
Member Author

@eileenmcnaughton I have updated the PR can you please check now?

@monishdeb
Copy link
Member Author

@eileenmcnaughton is it ok to merge?

@eileenmcnaughton
Copy link
Contributor

@monishdeb I think we should still not be removing debug & the verify_mandatory can do what we are after. However, those are non functional & I think merging this into the rc should have some priority so I will merge now & try to pick that up against master

@eileenmcnaughton eileenmcnaughton merged commit 743f8e1 into civicrm:4.7.24-rc Aug 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants