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

Fix dev/core#4207 - Handle currency symbol in SK aggregates #26498

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private function getCurrencyField(string $select):?string {
$clause = $this->getSelectExpression($select);
// Only deal with fields of type money.
// TODO: In theory it might be possible to support aggregated columns but be careful about FULL_GROUP_BY errors
if (!($clause && $clause['expr']->isType('SqlField') && $clause['dataType'] === 'Money' && $clause['fields'])) {
if (!($clause && $clause['dataType'] === 'Money' && $clause['fields'])) {
return NULL;
}
$moneyFieldAlias = array_keys($clause['fields'])[0];
Expand All @@ -973,7 +973,15 @@ private function getCurrencyField(string $select):?string {
if ($moneyField['type'] === 'Custom') {
return NULL;
}

$prefix = substr($moneyFieldAlias, 0, strrpos($moneyFieldAlias, $moneyField['name']));

// If using aggregation, this will only work if grouping by currency
if ($clause['expr']->isType('SqlFunction')) {
$groupingByCurrency = array_intersect([$prefix . 'currency', 'currency'], $this->savedSearch['api_params']['groupBy'] ?? []);
return \CRM_Utils_Array::first($groupingByCurrency);
}

// If the entity has a field named 'currency', just assume that's it.
if ($this->getField($prefix . 'currency')) {
return $prefix . 'currency';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,49 @@ public function testContributionCurrency():void {
$this->assertEquals('¥500', $result[0]['columns'][0]['val']);
}

public function testContributionAggregateCurrency():void {
$contributions = $this->saveTestRecords('Contribution', [
'records' => [
['total_amount' => 100, 'currency' => 'GBP'],
['total_amount' => 200, 'currency' => 'USD'],
['total_amount' => 500, 'currency' => 'JPY'],
['total_amount' => 200, 'currency' => 'USD'],
],
]);

$params = [
'checkPermissions' => FALSE,
'return' => 'page:1',
'savedSearch' => [
'api_entity' => 'Contribution',
'api_params' => [
'version' => 4,
'select' => ['SUM(total_amount) AS total', 'COUNT(id) AS count', 'currency'],
'where' => [['id', 'IN', $contributions->column('id')]],
'groupBy' => ['currency'],
],
],
'display' => NULL,
'sort' => [['currency', 'ASC']],
];

$result = civicrm_api4('SearchDisplay', 'run', $params);
$this->assertCount(3, $result);

// Currency should have been used to format the aggregated values
$this->assertEquals('GBP', $result[0]['data']['currency']);
$this->assertEquals('£100.00', $result[0]['columns'][0]['val']);
$this->assertEquals(1, $result[0]['columns'][1]['val']);

$this->assertEquals('JPY', $result[1]['data']['currency']);
$this->assertEquals('¥500', $result[1]['columns'][0]['val']);
$this->assertEquals(1, $result[1]['columns'][1]['val']);

$this->assertEquals('USD', $result[2]['data']['currency']);
$this->assertEquals('$400.00', $result[2]['columns'][0]['val']);
$this->assertEquals(2, $result[2]['columns'][1]['val']);
}

public function testSelectEquations() {
$activities = $this->saveTestRecords('Activity', [
'records' => [
Expand Down