From 491939eaa2a9d4a7cd1b1044ce3fcb5c00020609 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 10 Feb 2022 17:03:43 +1300 Subject: [PATCH] Fix apiv4 Contribution delete & all line items --- CRM/Contribute/BAO/Contribution.php | 39 ++++++++++++------- .../CRM/Contribute/BAO/ContributionTest.php | 20 +++++----- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 126527ebf9..32488256da 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1235,13 +1235,14 @@ INNER JOIN civicrm_contact contact ON ( contact.id = c.contact_id ) * * @return mixed|null * $results no of deleted Contribution on success, false otherwise + * @throws \API_Exception + * @throws \CRM_Core_Exception */ public static function deleteContribution($id) { CRM_Utils_Hook::pre('delete', 'Contribution', $id); $transaction = new CRM_Core_Transaction(); - $results = NULL; //delete activity record $params = [ 'source_record_id' => $id, @@ -1261,18 +1262,10 @@ INNER JOIN civicrm_contact contact ON ( contact.id = c.contact_id ) if (CRM_Price_BAO_PriceSet::getFor('civicrm_contribution', $id)) { CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution', $id); } - // cleanup line items. - $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $id, 'participant_id', 'contribution_id'); - // delete any related entity_financial_trxn, financial_trxn and financial_item records. + // delete any related financial records. CRM_Core_BAO_FinancialTrxn::deleteFinancialTrxn($id); - - if ($participantId) { - CRM_Price_BAO_LineItem::deleteLineItems($participantId, 'civicrm_participant'); - } - else { - CRM_Price_BAO_LineItem::deleteLineItems($id, 'civicrm_contribution'); - } + LineItem::delete(FALSE)->addWhere('contribution_id', '=', $id)->execute(); //delete note. $note = CRM_Core_BAO_Note::getNote($id, 'civicrm_contribution'); @@ -1283,16 +1276,34 @@ INNER JOIN civicrm_contact contact ON ( contact.id = c.contact_id ) $dao = new CRM_Contribute_DAO_Contribution(); $dao->id = $id; - $results = $dao->delete(); - $transaction->commit(); - CRM_Utils_Hook::post('delete', 'Contribution', $dao->id, $dao); return $results; } + /** + * Bulk delete multiple records. + * + * @param array[] $records + * + * @return static[] + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public static function deleteRecords(array $records): array { + $results = []; + foreach ($records as $record) { + if (self::deleteContribution($record['id'])) { + $returnObject = new CRM_Contribute_BAO_Contribution(); + $returnObject->id = $record['id']; + $results[] = $returnObject; + } + } + return $results; + } + /** * React to a financial transaction (payment) failure. * diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index e14a1291d8..498426807a 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -184,9 +184,14 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { } /** - * DeleteContribution() method + * Test contributions are deleted with assets in v3 and v4 api.@\ + * + * @dataProvider versionThreeAndFour + * + * @param int $version */ - public function testDeleteContribution() { + public function testDeleteContribution(int $version): void { + $this->_apiversion = $version; $contactId = $this->individualCreate(); $params = [ @@ -203,19 +208,14 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'total_amount' => 200.00, 'fee_amount' => 5, 'net_amount' => 195, - 'trxn_id' => '33ereerwww322323', - 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70', - 'thankyou_date' => '20080522', 'sequential' => TRUE, ]; $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; - CRM_Contribute_BAO_Contribution::deleteContribution($contribution['id']); - - $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution['trxn_id'], - 'id', 'trxn_id', 'Database check for deleted Contribution.' - ); + $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]); + $this->callAPISuccessGetCount('Contribution', [], 0); + $this->callAPISuccessGetCount('LineItem', [], 0); } /** -- 2.25.1