Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
storedocumenttable.php
1<?php
2namespace Bitrix\Catalog;
3
4use Bitrix\Main\Entity\ReferenceField;
18
65{
66 public const TYPE_ARRIVAL = 'A';
67 public const TYPE_STORE_ADJUSTMENT = 'S';
68 public const TYPE_MOVING = 'M';
69 public const TYPE_RETURN = 'R';
70 public const TYPE_DEDUCT = 'D';
71 public const TYPE_UNDO_RESERVE = 'U';
72 public const TYPE_SALES_ORDERS = 'W';
73 //public const TYPE_INVENTORY = 'I';
74
75 public const STATUS_CONDUCTED = 'Y';
76 public const STATUS_DRAFT = 'N';
77 public const STATUS_CANCELLED = 'C';
78
84 public static function getTableName()
85 {
86 return 'b_catalog_store_docs';
87 }
88
94 public static function getMap()
95 {
96 return [
97 'ID' => new IntegerField(
98 'ID',
99 [
100 'primary' => true,
101 'autocomplete' => true,
102 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_ID_FIELD'),
103 ]
104 ),
105 'TITLE' => new StringField(
106 'TITLE',
107 [
108 'validation' => [__CLASS__, 'validateTitle'],
109 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_TITLE_FIELD')
110 ]
111 ),
112 'DOC_TYPE' => new EnumField(
113 'DOC_TYPE',
114 [
115 'required' => true,
116 'values' => static::getTypeList(),
117 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DOC_TYPE_FIELD'),
118 ]
119 ),
120 'DOC_NUMBER' => new StringField(
121 'DOC_NUMBER',
122 [
123 'validation' => [__CLASS__, 'validateDocNumber'],
124 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DOC_NUMBER_FIELD'),
125 ]
126 ),
127 'SITE_ID' => new StringField(
128 'SITE_ID',
129 [
130 'validation' => [__CLASS__, 'validateSiteId'],
131 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_SITE_ID_FIELD'),
132 ]
133 ),
134 'CONTRACTOR' => new Main\ORM\Fields\Relations\Reference(
135 'CONTRACTOR',
136 ContractorTable::class,
137 Main\ORM\Query\Join::on('this.CONTRACTOR_ID', 'ref.ID')
138 ),
139 'CONTRACTOR_ID' => new IntegerField(
140 'CONTRACTOR_ID',
141 [
142 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_CONTRACTOR_ID_FIELD'),
143 ]
144 ),
145 'DATE_MODIFY' => new DatetimeField(
146 'DATE_MODIFY',
147 [
148 'default_value' => function()
149 {
150 return new Main\Type\DateTime();
151 },
152 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DATE_MODIFY_FIELD'),
153 ]
154 ),
155 'DATE_CREATE' => new DatetimeField(
156 'DATE_CREATE',
157 [
158 'default_value' => function()
159 {
160 return new Main\Type\DateTime();
161 },
162 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DATE_CREATE_FIELD'),
163 ]
164 ),
165 'CREATED_BY' => new IntegerField(
166 'CREATED_BY',
167 [
168 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_CREATED_BY_FIELD'),
169 ]
170 ),
171 'CREATED_BY_USER' => new Main\ORM\Fields\Relations\Reference(
172 'CREATED_BY_USER',
173 UserTable::class,
174 Main\ORM\Query\Join::on('this.CREATED_BY', 'ref.ID')
175 ),
176 'MODIFIED_BY' => new IntegerField(
177 'MODIFIED_BY',
178 [
179 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_MODIFIED_BY_FIELD'),
180 ]
181 ),
182 'MODIFIED_BY_USER' => new Main\ORM\Fields\Relations\Reference(
183 'MODIFIED_BY_USER',
184 UserTable::class,
185 Main\ORM\Query\Join::on('this.MODIFIED_BY', 'ref.ID')
186 ),
187 'RESPONSIBLE_ID' => new IntegerField(
188 'RESPONSIBLE_ID',
189 [
190 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_RESPONSIBLE_ID_FIELD')
191 ]
192 ),
193 'RESPONSIBLE' => new Main\ORM\Fields\Relations\Reference(
194 'RESPONSIBLE',
195 UserTable::class,
196 Main\ORM\Query\Join::on('this.RESPONSIBLE_ID', 'ref.ID')
197 ),
198 'CURRENCY' => new StringField(
199 'CURRENCY',
200 [
201 'validation' => [__CLASS__, 'validateCurrency'],
202 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_CURRENCY_FIELD'),
203 ]
204 ),
205 'STATUS' => new BooleanField(
206 'STATUS',
207 [
208 'values' => ['N', 'Y'],
209 'default' => 'N',
210 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_STATUS_FIELD'),
211 ]
212 ),
213 'WAS_CANCELLED' => new BooleanField(
214 'WAS_CANCELLED',
215 [
216 'values' => array('N', 'Y'),
217 'default' => 'N',
218 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_WAS_CANCELLED_FIELD'),
219 ]
220 ),
221 'DATE_STATUS' => new DatetimeField(
222 'DATE_STATUS',
223 [
224 'default_value' => null,
225 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DATE_STATUS_FIELD'),
226 ]
227 ),
228 'DATE_DOCUMENT' => new DatetimeField(
229 'DATE_DOCUMENT',
230 [
231 'default_value' => function()
232 {
233 return new Main\Type\DateTime();
234 },
235 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_DATE_DOCUMENT_FIELD'),
236 ]
237 ),
238 'STATUS_BY' => new IntegerField(
239 'STATUS_BY',
240 [
241 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_STATUS_BY_FIELD'),
242 ]
243 ),
244 'STATUS_BY_USER' => new Main\ORM\Fields\Relations\Reference(
245 'STATUS_BY_USER',
246 UserTable::class,
247 Main\ORM\Query\Join::on('this.STATUS_BY', 'ref.ID')
248 ),
249 'TOTAL' => new FloatField(
250 'TOTAL',
251 [
252 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_TOTAL_FIELD'),
253 ]
254 ),
255 'COMMENTARY' => new StringField(
256 'COMMENTARY',
257 [
258 'validation' => [__CLASS__, 'validateCommentary'],
259 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_COMMENTARY_FIELD'),
260 ]
261 ),
262 'ITEMS_ORDER_DATE' => new DatetimeField(
263 'ITEMS_ORDER_DATE',
264 [
265 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_ITEMS_ORDER_DATE_FIELD')
266 ]
267 ),
268 'ITEMS_RECEIVED_DATE' => new DatetimeField(
269 'ITEMS_RECEIVED_DATE',
270 [
271 'title' => Loc::getMessage('INVENTORY_DOCUMENT_ENTITY_ITEMS_RECEIVED_DATE_FIELD')
272 ]
273 ),
274 'ELEMENTS' => (new Main\ORM\Fields\Relations\OneToMany(
275 'ELEMENTS',
276 StoreDocumentElementTable::class,
277 'DOCUMENT'
278 ))->configureJoinType('left'),
279 ];
280 }
281
287 public static function validateDocNumber(): array
288 {
289 return [
290 new LengthValidator(null, 64),
291 ];
292 }
293
299 public static function validateTitle()
300 {
301 return [
302 new LengthValidator(null, 255),
303 ];
304 }
305
311 public static function validateSiteId(): array
312 {
313 return [
314 new LengthValidator(null, 2),
315 ];
316 }
317
323 public static function validateCurrency(): array
324 {
325 return [
326 new LengthValidator(3, 3),
327 ];
328 }
329
335 public static function validateCommentary(): array
336 {
337 return [
338 new LengthValidator(null, 1000),
339 ];
340 }
341
342 public static function getTypeList(bool $description = false): array
343 {
344 if ($description)
345 {
346 return [
347 self::TYPE_ARRIVAL => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_ARRIVAL'),
348 self::TYPE_STORE_ADJUSTMENT => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_STORE_ADJUSTMENT'),
349 self::TYPE_MOVING => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_MOVING'),
350 self::TYPE_RETURN => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_RETURN'),
351 self::TYPE_DEDUCT => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_DEDUCT'),
352 self::TYPE_UNDO_RESERVE => Loc::getMessage('INVENTORY_DOCUMENT_TYPE_UNDO_RESERVE'),
353 ];
354 }
355 else
356 {
357 return [
364 ];
365 }
366 }
367
368 public static function getStatusList(): array
369 {
370 return [
371 self::STATUS_CONDUCTED => Loc::getMessage('INVENTORY_DOCUMENT_STATUS_CONDUCTED'),
372 self::STATUS_DRAFT => Loc::getMessage('INVENTORY_DOCUMENT_STATUS_DRAFT'),
373 self::STATUS_CANCELLED => Loc::getMessage('INVENTORY_DOCUMENT_STATUS_CANCELLED'),
374 ];
375 }
376
377 public static function getStatusName(string $status): ?string
378 {
379 $statusList = static::getStatusList();
380
381 return $statusList[$status] ?? null;
382 }
383
384 public static function getOrmFilterByStatus(string $status): ?array
385 {
386 switch ($status)
387 {
389 $result = [
390 '=STATUS' => 'Y',
391 ];
392 break;
394 $result = [
395 '=STATUS' => 'N',
396 '=WAS_CANCELLED' => 'N',
397 ];
398 break;
400 $result = [
401 '=STATUS' => 'N',
402 '=WAS_CANCELLED' => 'Y',
403 ];
404 break;
405 default:
406 $result = null;
407 break;
408 }
409
410 return $result;
411 }
412
413 /*
414 * The following methods are used to select the documents that include a particular product/set of products.
415 * They are to be used with the \Bitrix\Main\ORM\Query\Query object (i.e. not with getList).
416 *
417 * Example:
418 * $docs = Catalog\StoreDocumentTable::query()->setSelect(['ID'])->withProduct($productId)->fetchAll();
419 */
420
421 public static function withProduct(Main\ORM\Query\Query $query, $productId)
422 {
423 $productId = (int)$productId;
424 if ($productId <= 0)
425 {
426 return;
427 }
428
429 $query->registerRuntimeField(
430 new ReferenceField(
431 'FILTER_ELEMENT_DOC_ID',
433 Join::on('ref.DOC_ID', 'this.ID')->where('ref.ELEMENT_ID', $productId),
434 ['join_type' => 'INNER']
435 )
436 );
437
438 $query->addGroup('ID');
439 }
440
441 public static function withProductList(Main\ORM\Query\Query $query, array $productIds)
442 {
443 Main\Type\Collection::normalizeArrayValuesByInt($productIds);
444 if (empty($productIds))
445 {
446 return;
447 }
448
449 $query->registerRuntimeField(
450 new ReferenceField(
451 'FILTER_ELEMENT_DOC_ID',
453 Join::on('ref.DOC_ID', 'this.ID')->whereIn('ref.ELEMENT_ID', $productIds),
454 ['join_type' => 'INNER']
455 )
456 );
457
458 $query->addGroup('ID');
459 }
460
461 public static function withStore(Main\ORM\Query\Query $query, $storeId)
462 {
463 $storeId = (int)$storeId;
464 if ($storeId <= 0)
465 {
466 return;
467 }
468
470 $filter
471 ->logic('or')
472 ->where('ref.STORE_FROM', $storeId)
473 ->where('ref.STORE_TO', $storeId)
474 ;
475 $query->registerRuntimeField(
476 new ReferenceField(
477 'FILTER_STORE_DOC_ID',
479 Join::on('ref.DOC_ID', 'this.ID')->where($filter),
480 ['join_type' => 'INNER']
481 )
482 );
483
484 $query->addGroup('ID');
485 }
486
487 public static function withStoreList(Main\ORM\Query\Query $query, array $storeIds)
488 {
489 Main\Type\Collection::normalizeArrayValuesByInt($storeIds);
490 if (empty($storeIds))
491 {
492 return;
493 }
494
496 $filter
497 ->logic('or')
498 ->whereIn('ref.STORE_FROM', $storeIds)
499 ->whereIn('ref.STORE_TO', $storeIds)
500 ;
501 $query->registerRuntimeField(
502 new ReferenceField(
503 'FILTER_STORE_DOC_ID',
505 Join::on('ref.DOC_ID', 'this.ID')->where($filter),
506 ['join_type' => 'INNER']
507 )
508 );
509
510 $query->addGroup('ID');
511 }
512
513 public static function withStoreFromList(Main\ORM\Query\Query $query, array $storeIds)
514 {
515 static::addSingleStoreFilterToQuery($query, 'STORE_FROM', $storeIds);
516 }
517
518 public static function withStoreToList(Main\ORM\Query\Query $query, array $storeIds)
519 {
520 static::addSingleStoreFilterToQuery($query, 'STORE_TO', $storeIds);
521 }
522
523 protected static function addSingleStoreFilterToQuery(Main\ORM\Query\Query $query, string $fieldName, array $storeIds): void
524 {
525 Main\Type\Collection::normalizeArrayValuesByInt($storeIds);
526 if (empty($storeIds))
527 {
528 return;
529 }
530
532 $filter
533 ->whereIn('ref.' . $fieldName, $storeIds)
534 ;
535 $query->registerRuntimeField(
536 new ReferenceField(
537 'FILTER_' . $fieldName . '_DOC_ID',
539 Join::on('ref.DOC_ID', 'this.ID')->where($filter),
540 ['join_type' => 'INNER']
541 )
542 );
543 $query->addGroup('ID');
544 }
545}
static addSingleStoreFilterToQuery(Main\ORM\Query\Query $query, string $fieldName, array $storeIds)
static getOrmFilterByStatus(string $status)
static withStoreToList(Main\ORM\Query\Query $query, array $storeIds)
static withStoreList(Main\ORM\Query\Query $query, array $storeIds)
static withStoreFromList(Main\ORM\Query\Query $query, array $storeIds)
static withProductList(Main\ORM\Query\Query $query, array $productIds)
static withStore(Main\ORM\Query\Query $query, $storeId)
static withProduct(Main\ORM\Query\Query $query, $productId)
static getTypeList(bool $description=false)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29