Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
documentcontractor.php
1<?php
2
4
8use Bitrix\Crm\Integration\Catalog\Contractor\StoreDocumentContractorTable;
14
16{
20 protected function processBeforeAction(Action $action): ?bool
21 {
22 $result = parent::processBeforeAction($action);
23
24 if (
25 empty($this->getErrors())
26 && !Manager::isActiveProviderByModule(Manager::PROVIDER_STORE_DOCUMENT, 'crm')
27 )
28 {
29 $this->addError(new Error('Contractors should be provided by CRM'));
30
31 return null;
32 }
33
34 return $result;
35 }
36
37 // region Actions
38
48 public function getFieldsAction(): array
49 {
50 return [
51 'DOCUMENT_CONTRACTOR' => $this->getViewFields(),
52 ];
53 }
54
62 public function addAction(array $fields): ?array
63 {
64 $canModify = $this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY);
65 if (!$canModify->isSuccess())
66 {
67 return null;
68 }
69
70 $checkFieldsResult = $this->checkFields($fields);
71 if (!$checkFieldsResult->isSuccess())
72 {
73 $this->addErrors($checkFieldsResult->getErrors());
74
75 return null;
76 }
77
78 $addResult = StoreDocumentContractorTable::add([
79 'DOCUMENT_ID' => (int)$fields['DOCUMENT_ID'],
80 'ENTITY_ID' => (int)$fields['ENTITY_ID'],
81 'ENTITY_TYPE_ID' => (int)$fields['ENTITY_TYPE_ID'],
82 ]);
83
84 if (!$addResult->isSuccess())
85 {
86 $this->addErrors($checkFieldsResult->getErrors());
87
88 return null;
89 }
90
91 return [
92 'DOCUMENT_CONTRACTOR' => $this->get($addResult->getId()),
93 ];
94 }
95
102 public function deleteAction(int $id): ?bool
103 {
104 $existsResult = $this->exists($id);
105 if (!$existsResult->isSuccess())
106 {
107 $this->addError(new Error('Binding was not found'));
108
109 return null;
110 }
111
112 $canModify = $this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY);
113 if (!$canModify->isSuccess())
114 {
115 return null;
116 }
117
118 $deleteResult = StoreDocumentContractorTable::delete($id);
119 if (!$deleteResult)
120 {
121 $this->addErrors($deleteResult->getErrors());
122
123 return null;
124 }
125
126 return true;
127 }
128
136 public function listAction(
137 PageNavigation $pageNavigation,
138 array $select = [],
139 array $filter = [],
140 array $order = []
141 ): Page
142 {
143 return new Page(
144 'DOCUMENT_CONTRACTOR',
145 $this->getList($select, $filter, $order, $pageNavigation),
146 $this->count($filter)
147 );
148 }
149
150 // end region Actions
151
158 private function checkFields(array $fields): Result
159 {
160 $result = new Result();
161 $documentId = (int)$fields['DOCUMENT_ID'];
162 $entityTypeId = (int)$fields['ENTITY_TYPE_ID'];
163 $entityId = (int)$fields['ENTITY_ID'];
164
165 if (!$documentId)
166 {
167 $result->addError(new Error('Store document was not found'));
168
169 return $result;
170 }
171
172 $document = StoreDocumentTable::getRow([
173 'select' => [
174 'ID',
175 'DOC_TYPE',
176 'STATUS',
177 ],
178 'filter' => [
179 'ID' => $documentId,
180 ],
181 ]);
182
183 if (!$document)
184 {
185 $result->addError(new Error('Store document was not found'));
186
187 return $result;
188 }
189
190 if ($document['DOC_TYPE'] !== StoreDocumentTable::TYPE_ARRIVAL)
191 {
192 $result->addError(new Error('Type of store document is wrong'));
193 }
194
195 if ($document['STATUS'] === 'Y')
196 {
197 $result->addError(new Error('Unable to edit conducted document'));
198 }
199
200 if (
201 $entityTypeId !== \CCrmOwnerType::Contact
202 && $entityTypeId !== \CCrmOwnerType::Company
203 )
204 {
205 $result->addError(new Error('Wrong entity type id'));
206 }
207
208 if (!$entityId)
209 {
210 $result->addError(new Error('Wrong entity id'));
211 }
212
213 $bindingExists = $this->existsByFilter([
214 'DOCUMENT_ID' => $documentId,
215 'ENTITY_TYPE_ID' => $entityTypeId,
216 'ENTITY_ID' => $entityId,
217 ]);
218 if ($bindingExists->isSuccess())
219 {
220 $result->addError(new Error('This contractor has been already bound to this document'));
221 }
222
223 if ($entityTypeId === \CCrmOwnerType::Company)
224 {
225 $documentCompanyBinding = StoreDocumentContractorTable::getRow([
226 'select' => ['ID'],
227 'filter' => [
228 'DOCUMENT_ID' => $documentId,
229 'ENTITY_TYPE_ID' => \CCrmOwnerType::Company,
230 ],
231 ]);
232
233 if (!empty($documentCompanyBinding))
234 {
235 $result->addError(new Error('This document already has a Company contractor'));
236 }
237 }
238
239 return $result;
240 }
241
242 protected function checkReadPermissionEntity(): Result
243 {
244 $result = $this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_VIEW);
245 if (!$result->isSuccess())
246 {
247 return $result;
248 }
249
250 $canReadDocument =
251 $this->accessController->check(Controller::CATALOG_STORE)
252 && $this->accessController->check(Controller::CATALOG_READ);
253
254 if (!$canReadDocument)
255 {
256 $result->addError(new Error('Access denied'));
257 }
258
259 return $result;
260 }
261
263 {
264 $result = $this->checkReadPermissionEntity();
265 if (!$result->isSuccess())
266 {
267 return $result;
268 }
269
270 return $this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY);
271 }
272
273 protected function checkPermissionEntity($name, $arguments = []): Result
274 {
275 return new Result();
276 }
277
284 private function checkDocumentAccess(string $action): Result
285 {
286 $result = new Result();
287 $can = $this->accessController->checkByValue($action, StoreDocumentTable::TYPE_ARRIVAL);
288 if (!$can)
289 {
290 $result->addError(new Error('Access denied'));
291
292 return $result;
293 }
294
295 return $result;
296 }
297
301 public function getEntityTable(): StoreDocumentContractorTable
302 {
303 return new StoreDocumentContractorTable;
304 }
305}
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
static getRow(array $parameters)