diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index 2a747a264773..1dfb5d75d08a 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -972,6 +972,19 @@ private function getCurrencyField(string $select):?string { } } } + // If the base entity has a field named 'currency', fall back on that. + if ($this->getField('currency')) { + return 'currency'; + } + // Finally, if there's a FK field to civicrm_contribution, we can use an implicit join + // E.G. the LineItem entity has no `currency` field of its own & uses that of the contribution record + if ($entityDao) { + foreach ($entityDao::getSupportedFields() as $fieldName => $field) { + if (($field['FKClassName'] ?? NULL) === 'CRM_Contribute_DAO_Contribution') { + return $prefix . $fieldName . '.currency'; + } + } + } return NULL; } diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 3e6cc7d1a4a0..b7f3e9d2519d 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -1396,6 +1396,29 @@ public function testContributionCurrency():void { $this->assertEquals('JPY', $result[2]['data']['currency']); $this->assertEquals('¥ 500.00', $result[2]['columns'][0]['val']); + + // Now do a search for the contribution line-items + $params['savedSearch'] = [ + 'api_entity' => 'LineItem', + 'api_params' => [ + 'version' => 4, + 'select' => ['line_total'], + 'where' => [['contribution_id', 'IN', $contributions->column('id')]], + ], + ]; + + $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertCount(3, $result); + + // An automatic join should have been added to fetch the contribution currency + $this->assertEquals('GBP', $result[0]['data']['contribution_id.currency']); + $this->assertEquals('£ 100.00', $result[0]['columns'][0]['val']); + + $this->assertEquals('USD', $result[1]['data']['contribution_id.currency']); + $this->assertEquals('$ 200.00', $result[1]['columns'][0]['val']); + + $this->assertEquals('JPY', $result[2]['data']['contribution_id.currency']); + $this->assertEquals('¥ 500.00', $result[2]['columns'][0]['val']); } }