fix formula sumRelated
This commit is contained in:
@@ -113,11 +113,31 @@ class SumRelatedType extends \Espo\Core\Formula\Functions\Base
|
||||
$foreignSelectManager->addJoin($foreignLink, $selectParams);
|
||||
}
|
||||
|
||||
$selectParams['groupBy'] = [$foreignLink . '.id'];
|
||||
if (!empty($selectParams['distinct'])) {
|
||||
$sqSelectParams = $selectParams;
|
||||
|
||||
$selectParams['whereClause'][] = [
|
||||
$foreignLink . '.id' => $entity->id
|
||||
];
|
||||
$sqSelectParams['whereClause'][] = [
|
||||
$foreignLink . '.id' => $entity->id
|
||||
];
|
||||
|
||||
$sqSelectParams['select'] = ['id'];
|
||||
unset($sqSelectParams['distinct']);
|
||||
unset($sqSelectParams['orderBy']);
|
||||
unset($sqSelectParams['order']);
|
||||
|
||||
$selectParams['whereClause'][] = [
|
||||
'id=s' => [
|
||||
'entityType' => $foreignEntityType,
|
||||
'selectParams' => $sqSelectParams,
|
||||
]
|
||||
];
|
||||
} else {
|
||||
$selectParams['whereClause'][] = [
|
||||
$foreignLink . '.id' => $entity->id
|
||||
];
|
||||
}
|
||||
|
||||
$selectParams['groupBy'] = [$foreignLink . '.id'];
|
||||
|
||||
$entityManager->getRepository($foreignEntityType)->handleSelectParams($selectParams);
|
||||
|
||||
|
||||
@@ -95,6 +95,37 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase
|
||||
$this->assertEquals(40, $result);
|
||||
}
|
||||
|
||||
public function testSumRelated()
|
||||
{
|
||||
$em = $this->getContainer()->get('entityManager');
|
||||
$fm = $this->getContainer()->get('formulaManager');
|
||||
|
||||
$contact1 = $em->createEntity('Contact', [
|
||||
'lastName' => '1',
|
||||
]);
|
||||
$contact2 = $em->createEntity('Contact', [
|
||||
'lastName' => '2',
|
||||
]);
|
||||
|
||||
$opportunity1 = $em->createEntity('Opportunity', [
|
||||
'name' => '1',
|
||||
'amount' => 1,
|
||||
'stage' => 'Closed Won',
|
||||
'contactsIds' => [$contact1->id, $contact2->id],
|
||||
]);
|
||||
|
||||
$opportunity2 = $em->createEntity('Opportunity', [
|
||||
'name' => '2',
|
||||
'amount' => 1,
|
||||
'stage' => 'Closed Won',
|
||||
'contactsIds' => [$contact1->id, $contact2->id],
|
||||
]);
|
||||
|
||||
$script = "entity\sumRelated('opportunities', 'amountConverted', 'won')";
|
||||
$result = $fm->run($script, $contact1);
|
||||
$this->assertEquals(2, $result);
|
||||
}
|
||||
|
||||
public function testRecordExists()
|
||||
{
|
||||
$em = $this->getContainer()->get('entityManager');
|
||||
|
||||
Reference in New Issue
Block a user