|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\Autocomplete; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
|
|
7
|
|
|
use OroCRM\Bundle\ChannelBundle\Autocomplete\ChannelLimitationHandler; |
|
8
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer; |
|
9
|
|
|
|
|
10
|
|
|
class BusinessCustomerSearchHandler extends ChannelLimitationHandler |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
public function convertItem($item) |
|
16
|
|
|
{ |
|
17
|
|
|
$result = []; |
|
18
|
|
|
|
|
19
|
|
|
if ($this->idFieldName) { |
|
20
|
|
|
$result[$this->idFieldName] = $this->getPropertyValue($this->idFieldName, $item); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
foreach ($this->properties as $property) { |
|
24
|
|
|
if ($property === 'name') { |
|
25
|
|
|
$result[$property] = $this->getCustomerName($item); |
|
26
|
|
|
} else { |
|
27
|
|
|
$result[$property] = $this->getPropertyValue($property, $item); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $result; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns customer name with account name in parentheses if their names not identical. |
|
36
|
|
|
* Otherwise returns only customer name. |
|
37
|
|
|
* |
|
38
|
|
|
* @param B2bCustomer $entity |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function getCustomerName(B2bCustomer $entity) |
|
43
|
|
|
{ |
|
44
|
|
|
$customerName = $entity->getName(); |
|
45
|
|
|
$accountName = $entity->getAccount() ? $entity->getAccount()->getName() : $customerName; |
|
46
|
|
|
|
|
47
|
|
|
if ($accountName === $customerName) { |
|
48
|
|
|
return $customerName; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return sprintf('%s (%s)', $customerName, $accountName); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getEntitiesByIds(array $entityIds) |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var QueryBuilder $queryBuilder */ |
|
60
|
|
|
$queryBuilder = $this->entityRepository->createQueryBuilder('c'); |
|
61
|
|
|
$queryBuilder->select('c', 'account'); |
|
62
|
|
|
$queryBuilder->leftJoin('c.account', 'account'); |
|
63
|
|
|
$queryBuilder->where($queryBuilder->expr()->in('c.' . $this->idFieldName, $entityIds)); |
|
64
|
|
|
|
|
65
|
|
|
return $queryBuilder->getQuery()->getResult(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|