1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Department.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Integration\HumanResources\Department;
4
5use Bitrix\HumanResources\Compatibility\Utils\DepartmentBackwardAccessCode;
6use Bitrix\HumanResources\Contract\Repository\NodeRepository;
7use Bitrix\HumanResources\Contract\Repository\StructureRepository;
8use Bitrix\HumanResources\Contract\Service\NodeMemberService;
9use Bitrix\HumanResources\Contract\Service\NodeService;
10use Bitrix\HumanResources\Enum\DepthLevel;
11use Bitrix\HumanResources\Enum\NodeActiveFilter;
12use Bitrix\HumanResources\Item\Collection\NodeCollection;
13use Bitrix\HumanResources\Item\Node;
14use Bitrix\HumanResources\Item\NodeMember;
15use Bitrix\HumanResources\Service\Container;
16use Bitrix\Im\V2\Integration\HumanResources\Structure;
17use Bitrix\Main\Config\Option;
18
20{
21 protected static ?IDepartment $instance = null;
22
23 protected NodeRepository $nodeRepository;
24 protected NodeMemberService $nodeMemberService;
25 protected StructureRepository $structureRepository;
26 protected NodeService $nodeService;
27
28 private function __construct()
29 {
30 $this->nodeRepository = Container::getNodeRepository();
31 $this->nodeMemberService = Container::getNodeMemberService();
32 $this->structureRepository = Container::getStructureRepository();
33 $this->nodeService = Container::getNodeService();
34 }
35
36 public static function getInstance(): IDepartment
37 {
38 if (self::$instance !== null)
39 {
40 return self::$instance;
41 }
42
43 if (!Structure::isSyncAvailable() || Option::get('im', 'old_department_enabled', 'N') === 'Y')
44 {
45 self::$instance = new OldDepartment();
46
47 return self::$instance;
48 }
49
50 self::$instance = new self();
51
52 return self::$instance;
53 }
54
55 public function getTopId(): ?int
56 {
57 if (self::$wasSearchedTopId)
58 {
59 return self::$topId;
60 }
61
62 self::$wasSearchedTopId = true;
63
64 $rootNode = $this->getRootNode();
65 if ($rootNode === null)
66 {
67 return null;
68 }
69
70 preg_match('/D(\d+)/', $rootNode->accessCode ?? '', $matches);
71 self::$topId = isset($matches[1]) ? (int)$matches[1] : null;
72
73 return self::$topId;
74 }
75
76 public function getList(): array
77 {
78 if (!empty($this->structureDepartments))
79 {
81 }
82
83 $rootNode = $this->getRootNode();
84 if ($rootNode === null)
85 {
86 return [];
87 }
88
89 $nodes = $this->nodeRepository->getChildOf($rootNode, DepthLevel::FULL, NodeActiveFilter::ONLY_ACTIVE);
90
91 foreach ($nodes as $node)
92 {
93 $department = $this->formatNode($node);
94 $this->structureDepartments[$department->id] = $department;
95 }
96
98 }
99
100 public function getListByIds(array $ids): array
101 {
102 if (!empty($this->structureDepartments))
103 {
104 return $this->filterDepartmentsByIds($this->structureDepartments, $ids);
105 }
106
107 $departments = [];
108 $nodeCollection = $this->getNodesByIds($ids);
109
110 foreach ($nodeCollection as $node)
111 {
112 $department = $this->formatNode($node);
113 $departments[$department->id] = $department;
114 }
115
116 return $departments;
117 }
118
119 public function getListByXml(string $xmlId): array
120 {
121 $structure = $this->structureRepository->getByXmlId(\Bitrix\HumanResources\Item\Structure::DEFAULT_STRUCTURE_XML_ID);
122 if ($structure === null)
123 {
124 return [];
125 }
126
127 $departments = [];
128 foreach ($this->nodeRepository->findAllByXmlId($xmlId, NodeActiveFilter::ONLY_ACTIVE) as $node)
129 {
130 $department = $this->formatNode($node);
131 $departments[$department->id] = $department;
132 }
133
134 return $departments;
135 }
136
137 protected function formatNode(Node $node): Entity
138 {
139 $parent = $this->nodeRepository->getById($node->parentId);
140 $parentId = DepartmentBackwardAccessCode::extractIdFromCode($parent?->accessCode);
141
142 $headMembers = $this->nodeMemberService->getDefaultHeadRoleEmployees($node->id);
143 $id = DepartmentBackwardAccessCode::extractIdFromCode($node->accessCode);
144
145 return new Entity(
146 name: $node->name,
147 headUserID: $headMembers->getIterator()->current()?->entityId ?? 0,
148 id: $id,
149 depthLevel: $node->depth,
150 parent: $parentId,
151 nodeId: $node->id
152 );
153 }
154
155 public function getEmployeeIdsWithLimit(array $ids, int $limit = 50): array
156 {
157 $employees = $managers = [];
158 $nodeCollection = $this->getNodesByIds($ids);
159 $headRole = Container::getRoleRepository()->findByXmlId(NodeMember::DEFAULT_ROLE_XML_ID['HEAD'])?->id;
160
161 $count = 0;
162 foreach ($nodeCollection as $node)
163 {
164 $memberCollection = $this->nodeMemberService->getPagedEmployees(
165 $node->id,
166 false,
167 0,
168 $limit - $count
169 );
170
171 foreach ($memberCollection->getItemMap() as $member)
172 {
173 if (in_array($headRole, $member->roles ?? [], true))
174 {
175 $managers[$member->entityId] = $member->entityId;
176 }
177 else
178 {
179 $employees[$member->entityId] = $member->entityId;
180 }
181 }
182
183 $count = count($managers + $employees);
184
185 if ($count >= $limit)
186 {
187 break;
188 }
189 }
190
191 return array_slice($managers + $employees, 0, $limit);
192 }
193
194 protected function getNodesByIds(array $ids): NodeCollection
195 {
196 $codes = array_map(
197 static fn($id) => DepartmentBackwardAccessCode::makeById($id),
198 $ids
199 );
200
201 return $this->nodeRepository->findAllByAccessCodes($codes);
202 }
203
204 protected function getRootNode(): ?Node
205 {
206 $structure = $this->structureRepository->getByXmlId(\Bitrix\HumanResources\Item\Structure::DEFAULT_STRUCTURE_XML_ID);
207 if ($structure === null)
208 {
209 return null;
210 }
211
212 return $this->nodeRepository->getRootNodeByStructureId($structure->id);
213 }
214
215 protected function filterDepartmentsByIds(array $departments, array $ids): array
216 {
217 $departmentByIds = [];
218
219 foreach ($departments as $department)
220 {
221 if (in_array($department->id, $ids, true))
222 {
223 $departmentByIds[$department->id] = $department;
224 }
225 }
226
227 return $departmentByIds;
228 }
229}
$count
Определения admin_tab.php:4
filterDepartmentsByIds(array $departments, array $ids)
Определения Department.php:215
getEmployeeIdsWithLimit(array $ids, int $limit=50)
Определения Department.php:155
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$matches
Определения index.php:22