Skip to content

Commit

Permalink
SearchKit - Use contribution currency when formatting LineItem amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Sep 15, 2022
1 parent e2bb96a commit 4201ec3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

}

0 comments on commit 4201ec3

Please sign in to comment.