Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
businessvalue.php
1<?php
2
3namespace Bitrix\Sale;
4
11
12Loc::loadMessages(__FILE__);
13
14final class BusinessValue
15{
16 const ENTITY_DOMAIN = 'E';
17 const INDIVIDUAL_DOMAIN = 'I';
18
19 private const EVENT_ON_BUSINESS_VALUE_SET_MAPPING = 'OnBusinessValueSetMapping';
20
21 private static $redefinedFields = array();
22 private static $consumers = array();
23 private static $consumerCodePersonMapping = array();
24
26 public static function getValueFromProvider(IBusinessValueProvider $provider = null, $codeKey, $consumerKey)
27 {
28 return self::get($codeKey, $consumerKey, $provider);
29 }
30
34 public static function redefineProviderField(array $fields)
35 {
36 self::$redefinedFields = array_replace_recursive(self::$redefinedFields, $fields);
37 }
38
46 public static function get($codeKey, $consumerKey = null, $personTypeId = null, $providerInstance = null)
47 {
48 $value = null;
49
50 if ($personTypeId instanceof IBusinessValueProvider)
51 {
52 $provider = $personTypeId;
53 $personTypeId = $provider->getPersonTypeId();
54 $mapping = self::getMapping(
55 $codeKey,
56 $consumerKey,
57 $personTypeId,
58 [
59 'GET_VALUE' => [
60 'PROPERTY' => 'BY_ID',
61 'PROVIDER' => $provider,
62 ],
63 ]
64 );
65
66 $providerInstance = $provider->getBusinessValueProviderInstance($mapping);
67 }
68 else
69 {
70 if ($personTypeId <= 0)
71 {
72 $personTypeId = null;
73 }
74 $mapping = self::getMapping(
75 $codeKey,
76 $consumerKey,
77 $personTypeId,
78 [
79 'GET_VALUE' => [
80 'PROPERTY' => 'BY_ID',
81 ],
82 ]
83 );
84 }
85
86 if (
87 isset($mapping['PROVIDER_VALUE'])
88 && isset($mapping['PROVIDER_KEY'])
89 && isset(self::$redefinedFields[$mapping['PROVIDER_KEY']][$mapping['PROVIDER_VALUE']]))
90 {
91 return self::$redefinedFields[$mapping['PROVIDER_KEY']][$mapping['PROVIDER_VALUE']];
92 }
93
94 if (
95 isset($mapping['PROVIDER_KEY'])
96 && $mapping['PROVIDER_KEY']
97 && isset($mapping['PROVIDER_VALUE'])
98 && $mapping['PROVIDER_VALUE']
99 )
100 {
101 $value = self::getValueFromMapping($mapping, $providerInstance, $personTypeId);
102 }
103
104 return $value;
105 }
106
113 public static function isSetMapping($codeKey, $consumerKey = null, $personTypeId = null)
114 {
115 $codeKey = ToUpper($codeKey);
116 return isset(self::$consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId]);
117 }
118
119 const MATCH_ALL = -1;
120 const MATCH_EXACT = 1;
121 const MATCH_COMMON = 2;
122 const MATCH_DEFAULT = 4;
123
131 public static function getMapping($codeKey, $consumerKey = null, $personTypeId = null, array $options = array())
132 {
133 $mapping = array();
134 $codeKeyUp = ToUpper($codeKey);
135
136 if ((int)$personTypeId === 0)
137 {
138 $personTypeId = null;
139 }
140
141 $match = isset($options['MATCH']) && is_int($options['MATCH']) ? $options['MATCH'] : self::MATCH_ALL;
142
143 $consumerCodePersonMapping =
144 isset($options['consumerCodePersonMapping']) && is_array($options['consumerCodePersonMapping']) // internal, do not use!
145 ? $options['consumerCodePersonMapping']
147 ;
148
149 if ($match & self::MATCH_EXACT && isset($consumerCodePersonMapping[$consumerKey][$codeKeyUp][$personTypeId]))
150 {
151 $mapping = $consumerCodePersonMapping[$consumerKey][$codeKeyUp][$personTypeId];
152 }
153 else
154 {
155 if ($match & self::MATCH_COMMON)
156 {
157 if ($personTypeId && isset($consumerCodePersonMapping[$consumerKey][$codeKeyUp]['']))
158 $mapping = $consumerCodePersonMapping[$consumerKey][$codeKeyUp][''];
159 elseif ($consumerKey && isset($consumerCodePersonMapping[''][$codeKeyUp][$personTypeId]))
160 $mapping = $consumerCodePersonMapping[''][$codeKeyUp][$personTypeId];
161 elseif ($consumerKey && $personTypeId && isset($consumerCodePersonMapping[''][$codeKeyUp]['']))
162 $mapping = $consumerCodePersonMapping[''][$codeKeyUp][''];
163 }
164
165 if (! $mapping && $match & self::MATCH_DEFAULT && ($consumers = self::getConsumers()))
166 {
167 $mappingValue = $consumers[$consumerKey]['CODES'][$codeKey]['DEFAULT'] ?? null;
168 if (is_array($mappingValue))
169 {
170 $mapping = $mappingValue;
171 }
172// elseif ($consumerKey && is_array($consumers['']['CODES'][$codeKey]['DEFAULT']))
173// {
174// $mapping = $consumers['']['CODES'][$codeKey]['DEFAULT'];
175// }
176 }
177 }
178
179 // TODO universal approach maybe later
180 if ($mapping
181 && $mapping['PROVIDER_KEY'] == 'PROPERTY'
182 && $mapping['PROVIDER_VALUE']
183 && ($providers = BusinessValue::getProviders())
184 && is_callable($providers['PROPERTY']['GET_VALUE']))
185 {
186 $mapping['PROVIDER_VALUE'] = call_user_func($providers['PROPERTY']['GET_VALUE'],
187 $mapping['PROVIDER_VALUE'],
188 $personTypeId,
189 $options['GET_VALUE']['PROPERTY'] ?? null,
190 $options['GET_VALUE']['PROVIDER'] ?? null
191 );
192 }
193
194 return $mapping;
195 }
196
206 public static function setMapping($codeKey, $consumerKey, $personTypeId, array $mapping, $withCommon = false)
207 {
208 $codeKey = ToUpper($codeKey);
209
210 $oldMapping = self::getMapping($codeKey, $consumerKey, $personTypeId, ['MATCH' => self::MATCH_EXACT]);
211
212 if (! $consumerKey || $consumerKey === BusinessValueTable::COMMON_CONSUMER_KEY)
213 $consumerKey = null;
214
215 if (! $personTypeId || $personTypeId === BusinessValueTable::COMMON_PERSON_TYPE_ID)
216 $personTypeId = null;
217
218 if (!array_key_exists('PROVIDER_KEY', $mapping) || !array_key_exists('PROVIDER_VALUE', $mapping))
219 $mapping = array();
220
221 $primary = array(
222 'CODE_KEY' => $codeKey,
223 // TODO remove save_data_modification hack
224 'CONSUMER_KEY' => $consumerKey ?: BusinessValueTable::COMMON_CONSUMER_KEY,
225 'PERSON_TYPE_ID' => $personTypeId ?: BusinessValueTable::COMMON_PERSON_TYPE_ID,
226 );
227
228 $consumerCodePersonMapping = self::getConsumerCodePersonMapping();
229
230 if (isset($consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId]))
231 {
232 if ($mapping)
233 {
234 $savedMapping = $consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId];
235
236 if (! ($mapping['PROVIDER_KEY'] == $savedMapping['PROVIDER_KEY']
237 && $mapping['PROVIDER_VALUE'] == $savedMapping['PROVIDER_VALUE']))
238 $result = BusinessValueTable::update($primary, $mapping);
239 }
240 else
241 {
242 $result = BusinessValueTable::delete($primary);
243 }
244 }
245 elseif ($mapping)
246 {
247 if ($withCommon
248 && $consumerKey
249 && !isset($consumerCodePersonMapping[''][$codeKey][''])
250 )
251 {
252 $consumerKey = null;
253 $personTypeId = null;
254 $primary['CONSUMER_KEY'] = BusinessValueTable::COMMON_CONSUMER_KEY;
255 $primary['PERSON_TYPE_ID'] = BusinessValueTable::COMMON_PERSON_TYPE_ID;
256 }
257
258 $result = BusinessValueTable::add($primary + $mapping);
259 }
260
261 if (isset($result))
262 {
263 if ($result->isSuccess())
264 {
265 if ($mapping)
266 {
267 self::$consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId] = $mapping;
268 }
269 else
270 {
271 unset(self::$consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId]);
272 }
273
274 $eventParams = [
275 'CODE_KEY' => $codeKey,
276 'CONSUMER_KEY' => $consumerKey,
277 'PERSON_TYPE_ID' => $personTypeId,
278 'OLD_MAPPING' => $oldMapping,
279 'NEW_MAPPING' => $mapping,
280 ];
281 $onSetMappingEvent = new Event('sale', self::EVENT_ON_BUSINESS_VALUE_SET_MAPPING, $eventParams);
282 EventManager::getInstance()->send($onSetMappingEvent);
283 }
284 }
285 else
286 {
287 $result = new \Bitrix\Main\Entity\Result;
288 }
289
290 return $result;
291 }
292
298 public static function delete($consumerKey, $personTypeId = null)
299 {
300 $consumerCodePersonMapping = self::getConsumerCodePersonMapping();
301 if (!isset($consumerCodePersonMapping[$consumerKey]))
302 {
303 return;
304 }
305
306 foreach ($consumerCodePersonMapping[$consumerKey] as $code => $personTypes)
307 {
308 if ($personTypeId === null)
309 {
310 foreach ($personTypes as $id => $mapping)
311 {
312 self::deleteInternal($consumerKey, $code, $id, $mapping);
313 }
314 }
315 else
316 {
317 $mapping = $personTypes[$personTypeId];
318 self::deleteInternal($consumerKey, $code, $personTypeId, $mapping);
319 }
320 }
321 }
322
330 private static function deleteInternal($consumerKey, $code, $personTypeId, $mapping)
331 {
332 $consumers = static::getConsumers();
333
334 if ($mapping['PROVIDER_KEY'] === 'INPUT'
335 && $consumers[$consumerKey]['CODES'][$code]['INPUT']['TYPE'] === 'FILE'
336 )
337 {
338 \CFile::Delete($mapping['PROVIDER_VALUE']);
339 }
340
341 BusinessValueTable::delete([
342 'CONSUMER_KEY' => $consumerKey,
343 'CODE_KEY' => $code,
344 'PERSON_TYPE_ID' => $personTypeId,
345 ]);
346 }
347
349 public static function getConsumerCodePersonMapping()
350 {
351 if (! self::$consumerCodePersonMapping)
352 {
353 $result = BusinessValueTable::getList(array(
354 'select' => array('CODE_KEY', 'CONSUMER_KEY', 'PERSON_TYPE_ID', 'PROVIDER_KEY', 'PROVIDER_VALUE'),
355 // !!! if filter is set, than setMapping must evaluate personTypeId's for codes, otherwise it will throw duplicate error!!!
356 //'filter' => array('=PERSON_TYPE_ID' => array_merge(array(BusinessValueTable::COMMON_PERSON_TYPE_ID), array_keys(self::getPersonTypes())),
357 ));
358
359 while ($row = $result->fetch())
360 {
361 $row['CODE_KEY'] = ToUpper($row['CODE_KEY']);
362 // TODO delete mappings for non existent code:consumer:person from db
363 // TODO optimize memory usage $consumerCodePersonMapping >> $personConsumerCodeMapping
364 self::$consumerCodePersonMapping[$row['CONSUMER_KEY']][$row['CODE_KEY']][$row['PERSON_TYPE_ID']] = array(
365 'PROVIDER_KEY' => $row['PROVIDER_KEY'],
366 'PROVIDER_VALUE' => $row['PROVIDER_VALUE'],
367 );
368 }
369 }
370
371 return self::$consumerCodePersonMapping;
372 }
373
379 public static function getProviders()
380 {
381 $providers = BusinessValueHandlers::getProviders();
382
383 foreach (EventManager::getInstance()->findEventHandlers('sale', 'OnGetBusinessValueProviders') as $handler)
384 {
385 $result = ExecuteModuleEventEx($handler);
386
387 if (is_array($result))
388 {
389 foreach ($result as $providerKey => $provider)
390 {
391 if ($providerKey && is_array($provider))
392 {
393 if (isset($providers[$providerKey]))
394 throw new SystemException('duplicate provider key '.$providerKey);
395
396 $providers[$providerKey] = $provider;
397 }
398 }
399 }
400 }
401
402 self::sortArray($providers);
403 return $providers;
404 }
405
406 private static $commonConsumer = array();
407
412 public static function getConsumers()
413 {
414 if (! self::$consumers)
415 {
416 self::$commonConsumer = array(
417 'NAME' => Loc::getMessage('BIZVAL_CONSUMER_COMMON'),
418 'SORT' => 0,
419 'CODES' => array(),
420 );
421
422 self::$consumers[''] =& self::$commonConsumer;
423
424 foreach (EventManager::getInstance()->findEventHandlers('sale', 'OnGetBusinessValueConsumers') as $handler)
425 {
426 $result = ExecuteModuleEventEx($handler); // TODO modern api
427
428 if (is_array($result))
429 {
430 foreach ($result as $consumerKey => &$consumer) // by ref!
431 {
432 if ($consumerKey && is_array($consumer))
433 {
434 self::addConsumerByRef($consumerKey, $consumer);
435 }
436 }
437 }
438 }
439 self::sortArray(self::$commonConsumer['CODES']);
440
441 self::sortArray(self::$consumers);
442
443// if (self::$commonConsumer['CODES'] &&
444// (self::$commonConsumer['CODES'] = array_filter(
445// self::$commonConsumer['CODES'],
446// function ($i) {return count($i['CONSUMERS']) > 1 && (! is_array($i['DOMAINS']) || $i['DOMAINS']);}
447// )))
448// {}
449 }
450
451 return self::$consumers;
452 }
453
454 private static function addConsumerByRef($consumerKey, array &$consumer) // by ref!
455 {
456 if (isset(self::$consumers[$consumerKey]) || $consumerKey == BusinessValueTable::COMMON_CONSUMER_KEY)
457 throw new SystemException('duplicate consumer key '.$consumerKey);
458
459// // TODO remove hack to paysytem
460// if (substr($consumerKey, 0, 10) == 'PAYSYSTEM_')
461// $consumer['PROVIDERS'] = array('VALUE', 'COMPANY', 'ORDER', 'USER', 'PROPERTY', 'PAYMENT');
462
463 if (isset($consumer['CODES']) && is_array($consumer['CODES']))
464 {
465 $codes =& self::$commonConsumer['CODES'];
466
467 foreach ($consumer['CODES'] as $codeKey => $code)
468 {
469 $isCommon = !($code['NOT_COMMON'] ?? false);
470 if ($codeKey && is_array($code) && $isCommon)
471 {
472 $codeName = ($code['NAME'] ?? null) ?: $codeKey;
473
474 if ($c =& $codes[$codeKey])
475 {
476 if (empty($c['GROUP']) && !empty($code['GROUP']))
477 {
478 $c['GROUP'] = $code['GROUP'];
479 }
480
481 if (!isset($c['SORT']) && isset($code['SORT']))
482 {
483 $c['SORT'] = $code['SORT'];
484 }
485
486 if (
487 isset($c['DEFAULT'], $code['DEFAULT'])
488 && !is_array($c['DEFAULT'])
489 && is_array($code['DEFAULT'])
490 )
491 {
492 $c['DEFAULT'] = $code['DEFAULT'];
493 }
494
495 if (isset($code['DOMAINS']) && is_array($code['DOMAINS']))
496 {
497 $c['DOMAINS'] = isset($c['DOMAINS']) && is_array($c['DOMAINS'])
498 ? array_intersect($c['DOMAINS'], $code['DOMAINS'])
499 : $code['DOMAINS']
500 ;
501 }
502
503 if (!isset($code['PROVIDERS']) || !is_array($code['PROVIDERS']))
504 {
505 $code['PROVIDERS'] = $consumer['PROVIDERS'] ?? [];
506 }
507
508 if (is_array($code['PROVIDERS']))
509 {
510 $c['PROVIDERS'] = isset($c['PROVIDERS']) && is_array($c['PROVIDERS'])
511 ? array_intersect($c['PROVIDERS'], $code['PROVIDERS'])
512 : $code['PROVIDERS']
513 ;
514 }
515
516 $c['NAMES'][$codeName] = 1;
517
518// if (! $c['NAME'] && $code['NAME'])
519// $c['NAME'] = $code['NAME'];
520 }
521 else
522 {
523 $c = $code;
524 $c['PROVIDERS'] = $code['PROVIDERS'] ?? $consumer['PROVIDERS'] ?? [];
525 $c['NAMES'][$codeName] = 1;
526 }
527
528 $c['CONSUMERS'] []= !empty($consumer['NAME']) ? $consumer['NAME'] : $consumerKey;
529 }
530 }
531
532 self::sortArray($consumer['CODES']);
533 }
534
535 self::$consumers[$consumerKey] = & $consumer; // by ref!
536 }
537
538 public static function addConsumer($consumerKey, array $consumer)
539 {
540 if (! $consumerKey)
541 throw new SystemException('Empty consumer key!');
542
543 if (! is_array($consumer))
544 throw new SystemException('Invalid consumer!');
545
546 self::getConsumers(); // preload consumers
547 self::addConsumerByRef($consumerKey, $consumer);
548 }
549
550 public static function changeConsumer($consumerKey, array $consumer)
551 {
552 if (! $consumerKey)
553 throw new SystemException('Empty consumer key!');
554
555 self::getConsumers(); // preload consumers
556
557 if (! isset(self::$consumers[$consumerKey]))
558 throw new SystemException('There is no consumer "'.$consumerKey.'" to change!');
559
560 self::$consumers[$consumerKey] = $consumer;
561 }
562
563 public static function changeConsumerKey($fromConsumerKey, $toConsumerKey)
564 {
565 if (! ($fromConsumerKey && $toConsumerKey))
566 throw new SystemException('Empty consumer key!');
567
568 if ($fromConsumerKey == $toConsumerKey)
569 return;
570
571 self::getConsumers(); // preload consumers
572
573 if (! isset(self::$consumers[$fromConsumerKey]))
574 throw new SystemException('There is no consumer "'.$fromConsumerKey.'" to change key!');
575
576 if (isset(self::$consumers[$toConsumerKey]))
577 throw new SystemException('Cannot change to consumer key "'.$toConsumerKey.'", as it already exists!');
578
579 // TODO change common consumer code['CONSUMERS']
580 self::$consumers[$toConsumerKey] = & self::$consumers[$fromConsumerKey]; // by ref!
581 unset(self::$consumers[$fromConsumerKey]);
582 }
583
584 public static function getGroups()
585 {
586 static $groups = array();
587
588 if (! $groups)
589 {
590 foreach (EventManager::getInstance()->findEventHandlers('sale', 'OnGetBusinessValueGroups') as $handler)
591 {
592 $result = ExecuteModuleEventEx($handler); // TODO modern api
593
594 if (is_array($result))
595 {
596 foreach ($result as $groupKey => $group)
597 {
598 if ($groupKey && is_array($group) && !isset($groups[$groupKey]))
599 {
600 $groups[$groupKey] = $group;
601 }
602 }
603 }
604 }
605
606 self::sortArray($groups);
607 }
608
609 return $groups;
610 }
611
613 private static function sortArray(array &$data)
614 {
615 @uasort( // @ because https://bugs.php.net/bug.php?id=50688
616 $data,
617 function ($aa, $bb) // $aa & $bb - because php before 5.4 passes by reference
618 {
619 $a = is_array($aa) ? (int)($aa['SORT'] ?? 0) : 0;
620 $b = is_array($bb) ? (int)($bb['SORT'] ?? 0) : 0;
621 return $a < $b ? -1 : ($a > $b ? 1 : 0);
622 }
623 );
624 }
625
627 public static function getPersonTypes($all = false, array $resetAllPersonTypes = null)
628 {
629 static $allPersonTypes = array(), $personTypes = array();
630
631 if ($resetAllPersonTypes !== null)
632 {
633 $allPersonTypes = $resetAllPersonTypes;
634 $personTypes = array_filter(
635 $allPersonTypes,
636 function ($i)
637 {
638 return $i['DOMAIN'];
639 }
640 );
641 }
642 elseif (!$allPersonTypes)
643 {
644 $dbRes = Internals\PersonTypeTable::getList([
645 'select' => [
646 'ID', 'NAME', 'LID', 'ACTIVE', 'ENTITY_REGISTRY_TYPE',
647 'DOMAIN' => 'BIZVAL.DOMAIN',
648 'PT_SITE' => 'PERSON_TYPE_SITE.SITE_ID'
649 ],
650 'order' => ['SORT', 'NAME'],
651 'runtime' => [
652 new \Bitrix\Main\Entity\ReferenceField(
653 'BIZVAL',
654 'Bitrix\Sale\Internals\BusinessValuePersonDomainTable',
655 ['=this.ID' => 'ref.PERSON_TYPE_ID'],
656 ['join_type' => 'LEFT']
657 ),
658 ],
659 ]);
660
661 $result = array();
662 while ($row = $dbRes->fetch())
663 {
664 if (!isset($result[$row['ID']]))
665 {
666 $row['PT_SITE'] = array($row['PT_SITE']);
667 $result[$row['ID']] = $row;
668 }
669 else
670 {
671 $result[$row['ID']]['PT_SITE'][] = $row['PT_SITE'];
672 }
673 }
674
675 foreach ($result as $item)
676 {
677 $item['TITLE'] = $item['NAME'].' ('.implode(', ' , $item['PT_SITE']).')';
678
679 $allPersonTypes[$item['ID']] = $item;
680
681 if ($item['DOMAIN'])
682 $personTypes[$item['ID']] = $item;
683 }
684 }
685
686 return $all ? $allPersonTypes : $personTypes;
687 }
688
700 public static function updateMapping($codeKey, array $oldMapping, array $newMapping, $consumerKey = null, $personTypeId = null): \Bitrix\Main\Entity\Result
701 {
702 if (!isset($oldMapping['PROVIDER_KEY'], $oldMapping['PROVIDER_VALUE']))
703 {
704 throw new \Bitrix\Main\ArgumentException("Parameters \$oldMapping['PROVIDER_KEY'] and \$oldMapping['PROVIDER_VALUE'] are required.", 'oldMapping');
705 }
706
707 $result = new \Bitrix\Main\Entity\Result();
708
709 $filter = [
710 'CODE_KEY' => $codeKey,
711 'PROVIDER_KEY' => $oldMapping['PROVIDER_KEY'],
712 'PROVIDER_VALUE' => $oldMapping['PROVIDER_VALUE'],
713 ];
714
715 if ($consumerKey)
716 {
717 $filter['CONSUMER_KEY'] = $consumerKey;
718 }
719
720 if ($personTypeId)
721 {
722 $filter['PERSON_TYPE_ID'] = $personTypeId;
723 }
724
725 $businessValueResult = BusinessValueTable::getList(array(
726 'select' => ['CONSUMER_KEY', 'PERSON_TYPE_ID'],
727 'filter' => $filter,
728 ));
729
730 while ($item = $businessValueResult->fetch())
731 {
732 $setMappingResult = self::setMapping($codeKey, $item['CONSUMER_KEY'], $item['PERSON_TYPE_ID'], $newMapping);
733 if (!$setMappingResult->isSuccess())
734 {
735 $result->addErrors($setMappingResult->getErrors());
736 }
737 }
738
739 return $result;
740 }
741
747 public static function getValuesByCode(string $consumerName, string $code)
748 {
749 $result = [];
750
751 $consumerCodePersonMapping = self::getConsumerCodePersonMapping();
752 if (isset($consumerCodePersonMapping[''][$code]))
753 {
754 $consumerValues = $consumerCodePersonMapping[''][$code];
755 foreach ($consumerValues as $values)
756 {
757 if ($values['PROVIDER_KEY'] && $values['PROVIDER_VALUE'])
758 {
759 $result[] = self::getValueFromMapping($values);
760 }
761 }
762 }
763
764 $consumerValues = $consumerCodePersonMapping[$consumerName][$code] ?? [];
765 foreach ($consumerValues as $values)
766 {
767 if ($values['PROVIDER_KEY'] && $values['PROVIDER_VALUE'])
768 {
769 $result[] = self::getValueFromMapping($values);
770 }
771 }
772
773 return array_unique($result);
774 }
775
776 private static function getValueFromMapping(array $mapping, $providerInstance = null, $personTypeId = null)
777 {
778 $value = null;
779
780 switch ($mapping['PROVIDER_KEY'])
781 {
782 case 'VALUE':
783 case 'INPUT':
784 $value = $mapping['PROVIDER_VALUE'];
785 break;
786
787 default:
788 if (
789 ($providers = self::getProviders())
790 && ($provider = $providers[$mapping['PROVIDER_KEY']])
791 && \is_array($provider)
792 && \is_callable($provider['GET_INSTANCE_VALUE'])
793 && ($v = \call_user_func($provider['GET_INSTANCE_VALUE'], $providerInstance, $mapping['PROVIDER_VALUE'], $personTypeId))
794 )
795 {
796 $value = $v;
797 }
798 }
799
800 return $value;
801 }
802
803 // DEPRECATED API //////////////////////////////////////////////////////////////////////////////////////////////////
804
806 public static function install($parentName, $translationSource, $data)
807 {
808 }
809
811 const COMMON_PERSON_ID = 0; // person type id
813 const COMMON_DOMAIN = 'C';
814}
815
818{
819 public static function getProviders()
820 {
821 static $providers = array();
822 if ($providers)
823 return $providers;
824
825 $providers = array(
826 'VALUE' => array(
827 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_VALUE'),
828 'SORT' => 100,
829 ),
830 'COMPANY' => array(
831 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_COMPANY'),
832 'SORT' => 600,
833 'FIELDS' => call_user_func(function ()
834 {
835 $data = array(
836 'ID' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_COMPANY_ID')),
837 'NAME' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_NAME' )),
838 'LOCATION_ID' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_LOCATION' )),
839 'CODE' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_CODE' )),
840 'XML_ID' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_XML_ID' )),
841 'ACTIVE' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_ACTIVE' )),
842 'DATE_CREATE' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_DATE_CREATE')),
843 'DATE_MODIFY' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_DATE_MODIFY')),
844 'CREATED_BY' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_CREATED_BY' )),
845 'MODIFIED_BY' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_MODIFIED_BY')),
846 'ADDRESS' => array('GROUP' => 'ENTITY', 'NAME' => Loc::getMessage('BIZVAL_CODE_ADDRESS' )),
847 );
848
849 global $USER_FIELD_MANAGER;
850 $result = $USER_FIELD_MANAGER->GetUserFields(Internals\CompanyTable::getUfId(), null, LANGUAGE_ID);
851
852 foreach ($result as $name => $row)
853 $data[$name] = array('NAME' => $row['EDIT_FORM_LABEL'] ?: $name, 'GROUP' => 'UF');
854
855 return $data;
856 }),
857 'FIELDS_GROUPS' => array(
858 'ENTITY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CODE_COMPANY_ENTITY')),
859 'UF' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CODE_COMPANY_UF' )),
860 ),
861 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId)
862 {
863 $value = null;
864
865 if ($providerInstance === null)
866 return $value;
867
868 if (mb_substr($providerValue, 0, 3) == 'UF_')
869 {
870 global $USER_FIELD_MANAGER;
871 $value = $USER_FIELD_MANAGER->GetUserFieldValue(Internals\CompanyTable::getUfId(), $providerValue, $providerInstance) ?: null;
872 }
873 elseif ($company = Internals\CompanyTable::getById($providerInstance)->fetch())
874 {
875 $value = $company[$providerValue];
876 }
877
878 return $value;
879 },
880 ),
881 'ORDER' => call_user_func(
882 function ()
883 {
884 $result = array(
885 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_ORDER'),
886 'SORT' => 200,
887 'FIELDS' => array(
888 'ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_ID')),
889 'LID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SITE_ID')),
890 'ACCOUNT_NUMBER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_ACCOUNT_NUMBER')),
891 'TRACKING_NUMBER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_TRACKING_NUMBER')),
892 'DATE_INSERT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_DATE_CREATE')),
893 'DATE_INSERT_DATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_DATE_CREATE_DATE')),
894 'DATE_UPDATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_DATE_MODIFY')),
895 'PERSON_TYPE_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PERSON_TYPE_ID')),
896 'USER_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_USER_ID')),
897 'PAYED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAID')),
898 'DATE_PAY_BEFORE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_PAY_BEFORE')),
899 'SHOULD_PAY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_PRICE')),
900 'CURRENCY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_CURRENCY')),
901 'PRICE_DELIVERY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_PRICE_DELIV')),
902 'DISCOUNT_VALUE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_DESCOUNT')),
903 'PAY_SYSTEM_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_PAY_SYSTEM_ID')),
904 'DELIVERY_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_DELIVERY_ID')),
905 'TAX_VALUE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_TAX')),
906 'COMMENTS' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_COMMENTS')),
907 'USER_DESCRIPTION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_USER_DESCRIPTION')),
908 ),
909 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId)
910 {
911 $value = null;
912
913 if ($providerInstance instanceof Order)
914 {
915 if ($providerValue == 'DATE_INSERT_DATE')
916 {
917 $value = new Date($providerInstance->getField('DATE_INSERT'));
918 }
919 else if ($providerValue == 'DATE_BILL_DATE') // for crm compatibility
920 {
921 $value = new Date($providerInstance->getField('DATE_BILL'));
922 }
923 else
924 {
925 $value = $providerInstance->getField($providerValue);
926 }
927 }
928
929 return $value;
930 },
931 );
932
933 if (IsModuleInstalled('crm'))
934 {
935 $result['FIELDS']['ORDER_TOPIC'] = ['NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_ORDER_TOPIC')];
936 $result['FIELDS']['PRICE'] = ['NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_PRICE')];
937 $result['FIELDS']['DATE_BILL'] = ['NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_DATE_BILL')];
938 $result['FIELDS']['DATE_BILL_DATE'] = ['NAME' => Loc::getMessage('BIZVAL_CODE_ORDER_DATE_BILL_DATE')];
939 }
940
941 return $result;
942 }
943 ),
944 'USER' => array(
945 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_USER'),
946 'SORT' => 110,
947 'FIELDS' => array(
948 'ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_USER_ID'), 'GROUP' => 'CLIENT'),
949 'LOGIN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_LOGIN'), 'GROUP' => 'CLIENT'),
950 'NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_FIRST_NAME'), 'GROUP' => 'CLIENT'),
951 'SECOND_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SECOND_NAME'), 'GROUP' => 'CLIENT'),
952 'LAST_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_LAST_NAME'), 'GROUP' => 'CLIENT'),
953 'EMAIL' => array('NAME' => Loc::getMessage('BIZVAL_CODE_EMAIL'), 'GROUP' => 'CLIENT'),
954 'LID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SITE_ID'), 'GROUP' => 'CLIENT'),
955 'PERSONAL_PROFESSION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_JOB_PROFESSION'), 'GROUP' => 'CLIENT'),
956 'PERSONAL_WWW' => array('NAME' => Loc::getMessage('BIZVAL_CODE_WEBSITE'), 'GROUP' => 'CLIENT'),
957 'PERSONAL_ICQ' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ICQ'), 'GROUP' => 'CLIENT'),
958 'PERSONAL_GENDER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_GENDER'), 'GROUP' => 'CLIENT'),
959 'PERSONAL_FAX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_FAX'), 'GROUP' => 'CLIENT'),
960 'PERSONAL_MOBILE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PHONE'), 'GROUP' => 'CLIENT'),
961 'PERSONAL_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ADDRESS'), 'GROUP' => 'CLIENT'),
962 'PERSONAL_MAILBOX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_POSTAL_ADDRESS'), 'GROUP' => 'CLIENT'),
963 'PERSONAL_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CITY'), 'GROUP' => 'CLIENT'),
964 'PERSONAL_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_REGION'), 'GROUP' => 'CLIENT'),
965 'PERSONAL_ZIP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ZIP'), 'GROUP' => 'CLIENT'),
966 'PERSONAL_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_COUNTRY'), 'GROUP' => 'CLIENT'),
967 'PERSONAL_NOTES' => array('NAME' => Loc::getMessage('BIZVAL_CODE_NOTES'), 'GROUP' => 'CLIENT'),
968 'WORK_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_NAME'), 'GROUP' => 'CLIENT_COMPANY'),
969 'WORK_DEPARTMENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_JOB_DEPARTMENT'), 'GROUP' => 'CLIENT_COMPANY'),
970 'WORK_POSITION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_JOB_POSITION' ), 'GROUP' => 'CLIENT_COMPANY'),
971 'WORK_WWW' => array('NAME' => Loc::getMessage('BIZVAL_CODE_WEBSITE'), 'GROUP' => 'CLIENT_COMPANY'),
972 'WORK_PHONE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PHONE'), 'GROUP' => 'CLIENT_COMPANY'),
973 'WORK_FAX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_FAX'), 'GROUP' => 'CLIENT_COMPANY'),
974 'WORK_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ADDRESS'), 'GROUP' => 'CLIENT_COMPANY'),
975 'WORK_MAILBOX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_POSTAL_ADDRESS'), 'GROUP' => 'CLIENT_COMPANY'),
976 'WORK_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CITY'), 'GROUP' => 'CLIENT_COMPANY'),
977 'WORK_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_REGION'), 'GROUP' => 'CLIENT_COMPANY'),
978 'WORK_ZIP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ZIP'), 'GROUP' => 'CLIENT_COMPANY'),
979 'WORK_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_COUNTRY'), 'GROUP' => 'CLIENT_COMPANY'),
980 ),
981 'FIELDS_GROUPS' => array(
982 'CLIENT' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CODE_CLIENT' )),
983 'CLIENT_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CODE_CLIENT_COMPANY')),
984 ),
985 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId)
986 {
987 $value = null;
988
989 global $USER;
990
991 if ($user = $USER->GetByID($providerInstance)->Fetch())
992 {
993 $value = $user[$providerValue];
994 }
995 return $value;
996 },
997 ),
998 'PAYMENT' => array(
999 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_PAYMENT'),
1000 'SORT' => 400,
1001 'FIELDS' => array(
1002 'ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_ID')),
1003 'ACCOUNT_NUMBER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_ACCOUNT_NUMBER')),
1004 'PAID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAID')),
1005 'DATE_PAID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_DATE_PAID')),
1006 'PAY_SYSTEM_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_SYSTEM_ID')),
1007 'PAY_VOUCHER_NUM' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_VOUCHER_NUM')),
1008 'PAY_VOUCHER_DATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_VOUCHER_DATE')),
1009 'DATE_PAY_BEFORE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_DATE_PAY_BEFORE')),
1010 'DATE_BILL' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_DATE_BILL')),
1011 'DATE_BILL_DATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_DATE_BILL_DATE')),
1012 'XML_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_XML_ID')),
1013 'SUM' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_SUM')),
1014 'CURRENCY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_CURRENCY')),
1015 'PAY_SYSTEM_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_SYSTEM_NAME')),
1016 'COMPANY_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_COMPANY_ID')),
1017 'PAY_RETURN_NUM' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_RETURN_NUM')),
1018 'PAY_RETURN_DATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_RETURN_DATE')),
1019 'PAY_RETURN_COMMENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PAYMENT_PAY_RETURN_COMMENT')),
1020 ),
1021 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId)
1022 {
1023 $value = null;
1024
1025 if ($providerInstance instanceof Payment)
1026 {
1027 if ($providerValue == 'DATE_BILL_DATE')
1028 $value = new Date($providerInstance->getField('DATE_BILL'));
1029 else
1030 $value = $providerInstance->getField($providerValue);
1031 }
1032 return $value;
1033 },
1034 ),
1035 'SHIPMENT' => array(
1036 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_SHIPMENT'),
1037 'SORT' => 500,
1038 'FIELDS' => array(
1039 'STATUS_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_STATUS_ID')),
1040 'PRICE_DELIVERY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_PRICE_DELIVERY')),
1041 'ALLOW_DELIVERY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_ALLOW_DELIVERY')),
1042 'DATE_ALLOW_DELIVERY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DATE_ALLOW_DELIVERY')),
1043 'EMP_ALLOW_DELIVERY_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_EMP_ALLOW_DELIVERY_ID')),
1044 'DEDUCTED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DEDUCTED')),
1045 'DATE_DEDUCTED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DATE_DEDUCTED')),
1046 'EMP_DEDUCTED_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_EMP_DEDUCTED_ID')),
1047 'REASON_UNDO_DEDUCTED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_REASON_UNDO_DEDUCTED')),
1048 'DELIVERY_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DELIVERY_ID')),
1049 'DELIVERY_DOC_NUM' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DELIVERY_DOC_NUM')),
1050 'DELIVERY_DOC_DATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DELIVERY_DOC_DATE')),
1051 'TRACKING_NUMBER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_TRACKING_NUMBER')),
1052 'XML_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_XML_ID')),
1053 'PARAMETERS' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_PARAMETERS')),
1054 'DELIVERY_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DELIVERY_NAME')),
1055 'COMPANY_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_COMPANY_ID')),
1056 'MARKED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_MARKED')),
1057 'DATE_MARKED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DATE_MARKED')),
1058 'EMP_MARKED_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_EMP_MARKED_ID')),
1059 'REASON_MARKED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_REASON_MARKED')),
1060 'CANCELED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_CANCELED')),
1061 'DATE_CANCELED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_DATE_CANCELED')),
1062 'EMP_CANCELED_ID' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SHIPMENT_EMP_CANCELED_ID')),
1063 ),
1064 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId)
1065 {
1066 $value = null;
1067
1068 if ($providerInstance instanceof Shipment)
1069 $value = $providerInstance->getField($providerValue);
1070
1071 return $value;
1072 },
1073 ),
1074 'PROPERTY' => call_user_func(
1075 function ()
1076 {
1077 $getFields = function ($registryType)
1078 {
1079 static $fields = array();
1080
1081 if (isset($fields[$registryType]))
1082 {
1083 return $fields[$registryType];
1084 }
1085
1086 $fields[$registryType] = [];
1087
1088 $result = Internals\OrderPropsTable::getList(array(
1089 'select' => array('ID', 'NAME', 'PERSON_TYPE_ID', 'TYPE', 'CODE'),
1090 'filter' => array(
1091 '=PERSON_TYPE_ID' => array_keys(BusinessValue::getPersonTypes()),
1092 '=ENTITY_REGISTRY_TYPE' => $registryType
1093 ),
1094 'order' => array('PERSON_TYPE_ID', 'SORT'),
1095 ));
1096
1097 while ($row = $result->fetch())
1098 {
1099 $id = $row['ID'];
1100 $name = $row['NAME'];
1101 $field = array(
1102 'NAME' => $name,
1103 'CODE' => $row['CODE'],
1104 'GROUP' => $row['PERSON_TYPE_ID'],
1105 'PERSON_TYPE_ID' => $row['PERSON_TYPE_ID']
1106 );
1107
1108 $fields[$registryType][$id] = $field;
1109
1110 if ($row['TYPE'] == 'LOCATION')
1111 {
1112 $field['NAME'] = $name.' ('.Loc::getMessage('BIZVAL_CODE_COUNTRY').')';
1113 $fields[$registryType][$id.'_COUNTRY'] = $field;
1114
1115 $field['NAME'] = $name.' ('.Loc::getMessage('BIZVAL_CODE_REGION').')';
1116 $fields[$registryType][$id.'_REGION'] = $field;
1117
1118 $field['NAME'] = $name.' ('.Loc::getMessage('BIZVAL_CODE_CITY').')';
1119 $fields[$registryType][$id.'_CITY'] = $field;
1120 }
1121 }
1122
1123 return $fields[$registryType];
1124 };
1125
1126 $parseId = function ($propertyId)
1127 {
1128 $propertyCode = null;
1129 $locationField = null;
1130
1131 if (! is_numeric($propertyId)) // TODO is_int
1132 {
1133 if (($a = explode('_', $propertyId, 2))
1134 && is_numeric($a[0])
1135 && in_array($a[1], array('CITY', 'COUNTRY', 'REGION'), true))
1136 {
1137 $propertyId = $a[0];
1138 $locationField = $a[1];
1139 }
1140 else
1141 {
1142 $propertyCode = $propertyId;
1143 $propertyId = null;
1144 }
1145 }
1146
1147 return array($propertyCode, $propertyId, $locationField);
1148 };
1149
1150 return array(
1151 'NAME' => Loc::getMessage('BIZVAL_PROVIDER_PROPERTY'),
1152 'SORT' => 300,
1153 'FIELDS' => call_user_func($getFields, Registry::REGISTRY_TYPE_ORDER),
1154 'FIELDS_GROUPS' => array_map(
1155 function ($i)
1156 {
1157 return array('NAME' => $i['TITLE']);
1158 },
1160 ),
1161 'GET_VALUE' => function ($providerValue, $personTypeId, $options, $provider) use ($parseId, $getFields)
1162 {
1163 list ($propertyCode, $propertyId, $locationField) = call_user_func($parseId, $providerValue);
1164
1165 // for crm invoice compatibility
1166 if ($provider && method_exists($provider, 'getRegistryType'))
1167 {
1168 $registry = $provider::getRegistryType();
1169 }
1170 else
1171 {
1173 }
1174
1175 $fields = $getFields($registry);
1176
1177 if ($propertyCode)
1178 {
1179 if ($options == 'BY_ID')
1180 {
1181 foreach ($fields as $propertyId => $property)
1182 {
1183 if ($property['PERSON_TYPE_ID'] == $personTypeId && $property['CODE'] == $propertyCode)
1184 {
1185 $providerValue = $propertyId;
1186 break;
1187 }
1188 }
1189 }
1190 }
1191 else
1192 {
1193 if ($options != 'BY_ID' && ($property = $fields[$propertyId]))
1194 {
1195 $providerValue = $property['CODE'];
1196 }
1197 }
1198
1199 return $providerValue;
1200 },
1201 'GET_INSTANCE_VALUE' => function ($providerInstance, $providerValue, $personTypeId) use ($parseId)
1202 {
1203 $value = null;
1204
1205 if ($providerInstance instanceof Order)
1206 {
1207 list ($propertyCode, $propertyId, $locationField) = call_user_func($parseId, $providerValue);
1208
1210 foreach ($providerInstance->getPropertyCollection() as $property)
1211 {
1212 if ($property->getPersonTypeId() == $personTypeId)
1213 {
1214 if ($propertyCode)
1215 {
1216 if ($property->getField('CODE') == $propertyCode)
1217 {
1218 $value = $property->getValue();
1219 break;
1220 }
1221 }
1222 elseif ($property->getPropertyId() == $propertyId)
1223 {
1224 if (($value = $property->getValue())
1225 && ($propertyField = $property->getProperty())
1226 )
1227 {
1228 if($propertyField['TYPE'] == "LOCATION")
1229 {
1230 $limit = -1;
1231 $filter['=CODE'] = $value;
1232 $filter['=PARENTS.NAME.LANGUAGE_ID'] = LANGUAGE_ID;
1233 if(is_set($locationField))
1234 {
1235 $filter['=PARENTS.TYPE.CODE'] = $locationField;
1236 $limit = 1;
1237 }
1238 $row = \Bitrix\Sale\Location\LocationTable::getList(array(
1239 'select' => array('LOCATION_NAME' => 'PARENTS.NAME.NAME'),
1240 'filter' => $filter,
1241 'limit' => $limit,
1242 ));
1243 $locations = array();
1244 while($location = $row->fetch())
1245 $locations[] = $location['LOCATION_NAME'];
1246
1247 $value = (count($locations) > 0) ? implode('-', $locations) : null;
1248 }
1249 elseif($propertyField['TYPE'] == "ENUM")
1250 {
1251 $multipleValues = $property->getValue();
1252
1253 if(is_array($multipleValues) && count($multipleValues)>0)
1254 {
1255 $value = [];
1256 foreach($multipleValues as $v)
1257 {
1258 if(isset($propertyField['OPTIONS'][$v]))
1259 $value[] = $propertyField['OPTIONS'][$v];
1260 }
1261 }
1262 else
1263 $value = $propertyField['OPTIONS'][$property->getValue()];
1264 }
1265 }
1266 break;
1267 }
1268 }
1269 }
1270 }
1271
1272 return $value;
1273 },
1274 );
1275 }
1276 ),
1277 );
1278
1279 return $providers;
1280 }
1281
1282 public static function getConsumers()
1283 {
1284 return array(
1285 /*'DEF' => array(
1286 'SORT' => 100,
1287 'CODES' => array(
1288// 'BASIC_INDIVIDUAL' => array('GROUP' => 'BASIC', 'SORT' => 10, 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1289// 'BASIC_ENTITY' => array('GROUP' => 'BASIC', 'SORT' => 20, 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1290 'DEFA' => array('GROUP' => 'BASIC', 'SORT' => 5),
1291 'DEF0' => array('GROUP' => 'BASIC', 'SORT' => 10, 'DEFAULT' => array('PROVIDER_KEY' => 'VALUE' , 'PROVIDER_VALUE' => 'default')),
1292 'DEF1' => array('GROUP' => 'BASIC', 'SORT' => 20, 'DEFAULT' => array('PROVIDER_KEY' => 'PROPERTY', 'PROVIDER_VALUE' => '3' )),
1293 'DEF2' => array('GROUP' => 'BASIC', 'SORT' => 30, 'DEFAULT' => array('PROVIDER_KEY' => 'PROPERTY', 'PROVIDER_VALUE' => 'EMAIL' )),
1294 'DEFF' => array('GROUP' => 'BASIC', 'SORT' => 50, 'INPUT' => array('TYPE' => 'FILE')),
1295 ),
1296 ),
1297 'OTHER' => array(
1298 'SORT' => 200,
1299 'CODES' => array(
1300 'OTHER' => array(
1301 'SORT' => 10,
1302 'DESCRIPTION' => 'If some value has a type of, say, IO String, that means that it\'s an I/O action that, when performed, will go out into the real world and get some string for us, which it will yield as a result.',
1303 //'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN),
1304 ),
1305 'OTHER_INPUT' => array(
1306 'SORT' => 20,
1307 //'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN),
1308 //'DEFAULT' => array('PROVIDER_KEY' => 'INPUT', 'PROVIDER_VALUE' => '33'),
1309 'INPUT' => array('TYPE' => 'ENUM', 'OPTIONS' => array('a' => 'AAA', 'b' => 'BBB', 'c' => 'CCC', 'd' => 'DDD', 'e' => 'EEE'),
1310 )),
1311 'OTR_YN' => array('SORT' => 40, 'INPUT' => array('TYPE' => 'Y/N'))
1312 ),
1313 'PROVIDERS' => array('VALUE', 'COMPANY', 'PROPERTY'),
1314 ),/*
1315 'BOTH' => array(
1316 'SORT' => 300,
1317 'CODES' => array(
1318 'BASIC_COMMON' => array(),
1319 'OTHER' => array(),//'DEFAULT' => array('PROVIDER_KEY' => 'VALUE', 'PROVIDER_VALUE' => 'def123')),
1320 'OTHER_INPUT' => array(),
1321 'BASIC_ENTITY' => array('DOMAINS' => array(BusinessValue::ENTITY_DOMAIN)),
1322 'BOTH_ENTITY' => array('DOMAINS' => array(BusinessValue::ENTITY_DOMAIN), 'INPUT' => array(
1323 'TYPE' => 'ENUM',
1324 'OPTIONS' => array(
1325 'one',
1326 'two',
1327 'three',
1328 ),
1329 )),
1330 ),
1331 ),*/
1332 );
1333 }
1334
1335 public static function getGroups()
1336 {
1337 return array(
1338 'CONSUMER_PAYSYS' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CONSUMER_PAYSYS'), 'SORT' => 100),
1339 'CODE_PAYSYS' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_CONSUMER_PAYSYS'), 'SORT' => 100),
1340 'BUYER_PERSON' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_BUYER_PERSON'), 'SORT' => 300),
1341 'BUYER_PERSON_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_BUYER_PERSON_COMPANY'), 'SORT' => 305),
1342 'BUYER_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_BUYER_COMPANY'), 'SORT' => 310),
1343 '1C_REKV' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_1C_REKV'), 'SORT' => 320),
1344 'SELLER_PERSON' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_SELLER_PERSON'), 'SORT' => 400),
1345 'SELLER_PERSON_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_SELLER_PERSON_COMPANY'), 'SORT' => 405),
1346 'SELLER_COMPANY' => array('NAME' => Loc::getMessage('BIZVAL_GROUP_SELLER_PERSON'), 'SORT' => 410),
1347 );
1348 }
1349}
1350
1351call_user_func(
1352 function ()
1353 {
1354 $eventManager = EventManager::getInstance();
1355// $eventManager->addEventHandler('sale', 'OnGetBusinessValueProviders', array(__NAMESPACE__.'\BusinessValueHandlers', 'getProviders'));
1356 $eventManager->addEventHandler('sale', 'OnGetBusinessValueConsumers', array(__NAMESPACE__.'\BusinessValueHandlers', 'getConsumers'));
1357 $eventManager->addEventHandler('sale', 'OnGetBusinessValueGroups' , array(__NAMESPACE__.'\BusinessValueHandlers', 'getGroups' ));
1358
1359 $eventManager->addEventHandler('sale', 'OnGetBusinessValueConsumers', array(__NAMESPACE__.'\BusinessValueConsumer1C', 'getConsumers'));
1360 }
1361);
1362
1363use \Bitrix\Sale\Helpers\Admin\BusinessValueControl;
1364
1367{
1368 const CONSUMER_KEY = '1C';
1369
1370 private static $codes, $personMaxIndex;
1371 private static $systemProviderKeys = array('', 'VALUE', 'USER', 'ORDER', 'PROPERTY', 'COMPANY', 'PAYMENT', 'SHIPMENT');
1372 private static $rekvProviderKeys = array('VALUE', 'USER', 'ORDER', 'PROPERTY', 'COMPANY', 'PAYMENT', 'SHIPMENT');
1373
1374 private static $codeNameInput = array('TYPE' => 'STRING', 'SIZE' => 40, 'MAXLENGTH' => 50);
1375
1376 public static function &getConsumers() // by ref!
1377 {
1378 static $consumers;
1379 if (! $consumers)
1380 {
1381 self::$codes = array(
1382 // !!! Make sure these codes are in sync with system codes in CSaleExport !!!
1383 'BUYER_PERSON_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_NAME' ), 'SORT' => 100, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1384 'BUYER_PERSON_NAME_FIRST' => array('NAME' => Loc::getMessage('BIZVAL_CODE_FIRST_NAME' ), 'SORT' => 200, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1385 'BUYER_PERSON_NAME_SECOND' => array('NAME' => Loc::getMessage('BIZVAL_CODE_SECOND_NAME' ), 'SORT' => 300, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1386 'BUYER_PERSON_NAME_LAST' => array('NAME' => Loc::getMessage('BIZVAL_CODE_LAST_NAME' ), 'SORT' => 400, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1387 'BUYER_PERSON_NAME_AGENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_AGENT_NAME' ), 'SORT' => 500, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1388 'BUYER_PERSON_NAME_CONTACT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CONTACT_NAME'), 'SORT' => 600, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1389 'BUYER_PERSON_BIRTHDAY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BIRTHDAY' ), 'SORT' => 700, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1390 'BUYER_PERSON_GENDER' => array('NAME' => Loc::getMessage('BIZVAL_CODE_GENDER' ), 'SORT' => 800, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1391 'BUYER_PERSON_INN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_INN' ), 'SORT' => 900, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1392 'BUYER_PERSON_KPP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_KPP' ), 'SORT' => 1000, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1393 'BUYER_PERSON_ADDRESS' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ADDRESS' ), 'SORT' => 1100, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1394 'BUYER_PERSON_ZIP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ZIP' ), 'SORT' => 1200, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1395 'BUYER_PERSON_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_COUNTRY' ), 'SORT' => 1300, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1396 'BUYER_PERSON_REGION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_REGION' ), 'SORT' => 1400, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1397 'BUYER_PERSON_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_DISTRICT' ), 'SORT' => 1500, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1398 'BUYER_PERSON_TOWN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_LOCALITY' ), 'SORT' => 1600, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1399 'BUYER_PERSON_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CITY' ), 'SORT' => 1700, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1400 'BUYER_PERSON_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_STREET' ), 'SORT' => 1800, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1401 'BUYER_PERSON_HOUSING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_HOUSING' ), 'SORT' => 1900, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1402 'BUYER_PERSON_BUILDING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BUILDING' ), 'SORT' => 2000, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1403 'BUYER_PERSON_APARTMENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_APARTMENT' ), 'SORT' => 2100, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1404 'BUYER_PERSON_PHONE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PHONE' ), 'SORT' => 2200, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1405 'BUYER_PERSON_NOTES' => array('NAME' => Loc::getMessage('BIZVAL_CODE_NOTES' ), 'SORT' => 2250, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1406 'BUYER_PERSON_EMAIL' => array('NAME' => Loc::getMessage('BIZVAL_CODE_EMAIL' ), 'SORT' => 2300, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1407 'BUYER_PERSON_F_ADDRESS_FULL'=> array('NAME' => Loc::getMessage('BIZVAL_CODE_F_ADDRESS_FULL'), 'SORT' => 2400, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1408 'BUYER_PERSON_F_INDEX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_INDEX' ), 'SORT' => 2500, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1409 'BUYER_PERSON_F_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_COUNTRY' ), 'SORT' => 2600, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1410 'BUYER_PERSON_F_REGION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_REGION' ), 'SORT' => 2700, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1411 'BUYER_PERSON_F_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_STATE' ), 'SORT' => 2800, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1412 'BUYER_PERSON_F_TOWN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_TOWN' ), 'SORT' => 2900, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1413 'BUYER_PERSON_F_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_CITY' ), 'SORT' => 3000, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1414 'BUYER_PERSON_F_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_STREET' ), 'SORT' => 3100, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1415 'BUYER_PERSON_F_BUILDING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_BUILDING' ), 'SORT' => 3200, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1416 'BUYER_PERSON_F_HOUSE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_HOUSE' ), 'SORT' => 3300, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1417 'BUYER_PERSON_F_FLAT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_FLAT' ), 'SORT' => 3400, 'GROUP' => 'BUYER_PERSON' , 'DOMAINS' => array(BusinessValue::INDIVIDUAL_DOMAIN)),
1418 'BUYER_COMPANY_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_NAME' ), 'SORT' => 100, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1419 'BUYER_COMPANY_NAME_AGENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_AGENT_NAME' ), 'SORT' => 200, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1420 'BUYER_COMPANY_NAME_CONTACT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CONTACT_NAME'), 'SORT' => 300, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1421 'BUYER_COMPANY_INN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_INN' ), 'SORT' => 400, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1422 'BUYER_COMPANY_KPP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_KPP' ), 'SORT' => 500, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1423 'BUYER_COMPANY_EGRPO' => array('NAME' => Loc::getMessage('BIZVAL_CODE_EGRPO' ), 'SORT' => 600, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1424 'BUYER_COMPANY_OKVED' => array('NAME' => Loc::getMessage('BIZVAL_CODE_OKVED' ), 'SORT' => 700, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1425 'BUYER_COMPANY_OKDP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_OKDP' ), 'SORT' => 800, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1426 'BUYER_COMPANY_OKOPF' => array('NAME' => Loc::getMessage('BIZVAL_CODE_OKOPF' ), 'SORT' => 900, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1427 'BUYER_COMPANY_OKFC' => array('NAME' => Loc::getMessage('BIZVAL_CODE_OKFC' ), 'SORT' => 1000, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1428 'BUYER_COMPANY_OKPO' => array('NAME' => Loc::getMessage('BIZVAL_CODE_OKPO' ), 'SORT' => 1100, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1429 'BUYER_COMPANY_BANK_ACCOUNT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_ACCOUNT'), 'SORT' => 1200, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1430
1431 'BUYER_COMPANY_BANK_NAME' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_NAME' ), 'SORT' => 1201, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1432 'BUYER_COMPANY_BANK_BIK' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_BIK' ), 'SORT' => 1202, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1433 'BUYER_COMPANY_BANK_ADDRESS_FULL'=> array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_ADDRESS_FULL'), 'SORT' => 1203, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1434 'BUYER_COMPANY_BANK_INDEX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_INDEX' ), 'SORT' => 1204, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1435 'BUYER_COMPANY_BANK_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_COUNTRY'), 'SORT' => 1205, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1436 'BUYER_COMPANY_BANK_REGION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_REGION' ), 'SORT' => 1206, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1437 'BUYER_COMPANY_BANK_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_STATE' ), 'SORT' => 1207, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1438 'BUYER_COMPANY_BANK_TOWN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_TOWN' ), 'SORT' => 1208, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1439 'BUYER_COMPANY_BANK_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_CITY' ), 'SORT' => 1209, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1440 'BUYER_COMPANY_BANK_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_STREET' ), 'SORT' => 1210, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1441 'BUYER_COMPANY_BANK_BUILDING'=> array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_BUILDING'), 'SORT' => 1211, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1442 'BUYER_COMPANY_BANK_HOUSE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BANK_HOUSE' ), 'SORT' => 1212, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1443
1444 'BUYER_COMPANY_ADDRESS' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ADDRESS' ), 'SORT' => 1300, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1445 'BUYER_COMPANY_ZIP' => array('NAME' => Loc::getMessage('BIZVAL_CODE_ZIP' ), 'SORT' => 1400, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1446 'BUYER_COMPANY_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_COUNTRY' ), 'SORT' => 1500, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1447 'BUYER_COMPANY_REGION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_REGION' ), 'SORT' => 1600, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1448 'BUYER_COMPANY_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_DISTRICT' ), 'SORT' => 1700, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1449 'BUYER_COMPANY_TOWN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_LOCALITY' ), 'SORT' => 1800, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1450 'BUYER_COMPANY_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_CITY' ), 'SORT' => 1900, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1451 'BUYER_COMPANY_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_STREET' ), 'SORT' => 2000, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1452 'BUYER_COMPANY_HOUSING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_HOUSING' ), 'SORT' => 2100, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1453 'BUYER_COMPANY_BUILDING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_BUILDING' ), 'SORT' => 2200, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1454 'BUYER_COMPANY_APARTMENT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_APARTMENT' ), 'SORT' => 2300, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1455 'BUYER_COMPANY_PHONE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_PHONE' ), 'SORT' => 2400, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1456 'BUYER_COMPANY_EMAIL' => array('NAME' => Loc::getMessage('BIZVAL_CODE_EMAIL' ), 'SORT' => 2500, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1457 'BUYER_COMPANY_F_ADDRESS_FULL'=> array('NAME' => Loc::getMessage('BIZVAL_CODE_F_ADDRESS_FULL'), 'SORT' => 2600, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1458 'BUYER_COMPANY_F_INDEX' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_INDEX' ), 'SORT' => 2700, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1459 'BUYER_COMPANY_F_COUNTRY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_COUNTRY' ), 'SORT' => 2800, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1460 'BUYER_COMPANY_F_REGION' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_REGION' ), 'SORT' => 2900, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1461 'BUYER_COMPANY_F_STATE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_STATE' ), 'SORT' => 3000, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1462 'BUYER_COMPANY_F_TOWN' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_TOWN' ), 'SORT' => 3100, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1463 'BUYER_COMPANY_F_CITY' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_CITY' ), 'SORT' => 3200, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1464 'BUYER_COMPANY_F_STREET' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_STREET' ), 'SORT' => 3300, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1465 'BUYER_COMPANY_F_BUILDING' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_BUILDING' ), 'SORT' => 3400, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1466 'BUYER_COMPANY_F_HOUSE' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_HOUSE' ), 'SORT' => 3500, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1467 'BUYER_COMPANY_F_FLAT' => array('NAME' => Loc::getMessage('BIZVAL_CODE_F_FLAT' ), 'SORT' => 3600, 'GROUP' => 'BUYER_COMPANY', 'DOMAINS' => array(BusinessValue::ENTITY_DOMAIN )),
1468
1469 );
1470
1471 self::$personMaxIndex = array();
1472
1473 $result = Internals\BusinessValueCode1CTable::getList(array('order' => array('CODE_INDEX')));
1474
1475 while ($row = $result->fetch())
1476 {
1477 $personTypeId = $row['PERSON_TYPE_ID'];
1478 $codeIndex = $row['CODE_INDEX'];
1479
1480 if (self::$personMaxIndex[$personTypeId] < $codeIndex)
1481 self::$personMaxIndex[$personTypeId] = $codeIndex;
1482
1483 self::$codes[self::getRekvCodeKey($personTypeId, $codeIndex)] = self::getRekvCode($personTypeId, $codeIndex, $row['NAME']);
1484 }
1485
1486 self::$codes['1C_REKV_##PERSON_TYPE_ID##_##CODE_INDEX##'] = array(
1487 'SORT' => 100000,
1488 'GROUP' => '1C_REKV',
1490 'NOT_COMMON' => true,
1491 );
1492
1493 $consumers = array(
1494 self::CONSUMER_KEY => array(
1495 'NAME' => Loc::getMessage('BIZVAL_CONSUMER_1C'),
1496 'SORT' => 400,
1497 'CODES' => &self::$codes, // by ref!
1498 'SKIP_NEW_CODE_SANITATION' => true,
1499 'SANITIZE_MAPPING' => array(__CLASS__, 'sanitizeMapping'),
1500 'SET_MAPPING' => array(__CLASS__, 'setMapping'),
1501 'RENDER_COLUMNS' => array(__CLASS__, 'renderColumns'),
1502 'GET_JAVASCRIPT' => array(__CLASS__, 'getJavaScript'),
1503 ),
1504 );
1505 }
1506
1507 return $consumers;
1508 }
1509
1510 public static function getRekvCodeKey($personTypeId, $codeIndex)
1511 {
1512 return '1C_REKV_'.$personTypeId.'_'.$codeIndex;
1513 }
1514
1515 private static function getRekvCode($personTypeId, $codeIndex, $name)
1516 {
1517 return array(
1518 'NAME' => $name,
1519 'SORT' => $codeIndex + 10000,
1520 'GROUP' => '1C_REKV',
1521 'PERSON_TYPE_ID' => $personTypeId,
1522 'NOT_COMMON' => true,
1523 'CODE_INDEX' => $codeIndex, // Used internally!
1524 );
1525 }
1526
1527 public static function sanitizeMapping($codeKey, $personTypeId, array &$mapping)
1528 {
1529 $error = array();
1530
1531 if (! ($code = self::$codes[$codeKey]) || isset($code['CODE_INDEX']))
1532 {
1533 if ($e = Internals\Input\Manager::getError(self::$codeNameInput, $mapping['NAME']))
1534 $error['NAME'] = $e;
1535 else
1536 $mapping['NAME'] = Internals\Input\Manager::getValue(self::$codeNameInput, $mapping['NAME']);
1537
1538 $providerKeys = self::$rekvProviderKeys;
1539 }
1540 else
1541 {
1542 $providerKeys = self::$systemProviderKeys;
1543 }
1544
1545 $error += BusinessValueControl::sanitizeMapping($personTypeId, $mapping, $providerKeys);
1546
1547 return $error;
1548 }
1549
1550 public static function setMapping($codeKey, $personTypeId, array $mapping)
1551 {
1552 $systemCode = true;
1553
1554 if (! ($code = & self::$codes[$codeKey]) || isset($code['CODE_INDEX']))
1555 {
1556 $systemCode = false;
1557
1558 if ($code)
1559 {
1560 if ($mapping && $mapping['NAME'])
1561 {
1562 if ($mapping['NAME'] != $code['NAME'])
1563 {
1564 $result = Internals\BusinessValueCode1CTable::update(
1565 array(
1566 'PERSON_TYPE_ID' => $code['PERSON_TYPE_ID'],
1567 'CODE_INDEX' => $code['CODE_INDEX'],
1568 ),
1569 array(
1570 'NAME' => $mapping['NAME']
1571 )
1572 );
1573
1574 if ($result->isSuccess())
1575 $code['NAME'] = $mapping['NAME'];
1576 }
1577 }
1578 else
1579 {
1580 $result = Internals\BusinessValueCode1CTable::delete(array(
1581 'PERSON_TYPE_ID' => $code['PERSON_TYPE_ID'],
1582 'CODE_INDEX' => $code['CODE_INDEX'],
1583 ));
1584
1585 if ($result->isSuccess())
1586 {
1587 unset(self::$codes[$codeKey]);
1588 $mapping = array(); // delete
1589 }
1590 }
1591 }
1592 else
1593 {
1594 $codeIndex = isset(self::$personMaxIndex[$personTypeId]) ? self::$personMaxIndex[$personTypeId] + 1 : 0;
1595
1596 if ($mapping && $mapping['NAME'] && $codeKey == self::getRekvCodeKey($personTypeId, $codeIndex))
1597 {
1598 $result = Internals\BusinessValueCode1CTable::add(array(
1599 'PERSON_TYPE_ID' => $personTypeId,
1600 'CODE_INDEX' => $codeIndex,
1601 'NAME' => $mapping['NAME'],
1602 ));
1603
1604 if ($result->isSuccess())
1605 {
1606 self::$personMaxIndex[$personTypeId] = $codeIndex;
1607 $code = self::getRekvCode($personTypeId, $codeIndex, $mapping['NAME']);
1608
1609 // move adding button to the end
1610 $addCode = self::$codes['1C_REKV_##PERSON_TYPE_ID##_##CODE_INDEX##'];
1611 unset(self::$codes['1C_REKV_##PERSON_TYPE_ID##_##CODE_INDEX##']);
1612 self::$codes['1C_REKV_##PERSON_TYPE_ID##_##CODE_INDEX##'] = $addCode;
1613 }
1614 }
1615 else
1616 {
1617 return new \Bitrix\Main\Result();
1618 }
1619 }
1620 }
1621
1622 $mapping = $mapping['PROVIDER_KEY']
1623 ? array(
1624 'PROVIDER_KEY' => $mapping['PROVIDER_KEY' ],
1625 'PROVIDER_VALUE' => $mapping['PROVIDER_VALUE'],
1626 )
1627 : array();
1628
1629
1630 return isset($result) && ! $result->isSuccess()
1631 ? $result
1632 : BusinessValue::setMapping($codeKey, self::CONSUMER_KEY, $personTypeId, $mapping, $systemCode);
1633 }
1634
1635 public static function renderColumns($codeKey, $personTypeId, array $mappings, $inputNamePrefix)
1636 {
1637 $hideCode = false;
1638
1639 if ($code = self::$codes[$codeKey])
1640 {
1641 $providerValueInput = BusinessValueControl::getValueInput($personTypeId);
1642 $commonProviderInput = null;
1643 $commonProviderValueInput = BusinessValueControl::getValueInput('');
1644
1645 if ($codeKey == '1C_REKV_##PERSON_TYPE_ID##_##CODE_INDEX##') // new code
1646 {
1647 $inputNamePrefix = str_replace('##PERSON_TYPE_ID##', $personTypeId, $inputNamePrefix);
1648
1649 ob_start();
1650
1651 ?>
1652 <td>
1653 <?=Internals\Input\Manager::getEditHtml($inputNamePrefix.'[NAME]', self::$codeNameInput)?>
1654 </td>
1655 <td>
1656 <?
1657
1658 BusinessValueControl::renderMapping(
1659 array(),
1660 $inputNamePrefix,
1661 BusinessValueControl::getProviderInput($personTypeId, self::$rekvProviderKeys),
1662 $providerValueInput
1663 );
1664
1665 ?>
1666 </td>
1667 <?
1668
1669 $newMappingHTML = ob_get_clean();
1670
1671 ?>
1672 <td>
1673 <a href="#" onclick="bizvalAdd1CRekv(this, '<?=$personTypeId?>', '<?=\CUtil::JSEscape(htmlspecialcharsbx($newMappingHTML))?>'); return false;">
1674 <?=Loc::getMessage('BIZVAL_CODE_1C_REKV_ADD')?>
1675 </a>
1676 </td>
1677 <td></td>
1678 <?
1679 }
1680 elseif (isset($code['CODE_INDEX'])) // rekv code
1681 {
1682 ?>
1683 <td><?=Internals\Input\Manager::getEditHtml($inputNamePrefix.'[NAME]', self::$codeNameInput, $code['NAME'] ?: $codeKey)?></td>
1684 <td>
1685 <?
1686
1687 if ($personTypeId)
1688 $commonProviderInput = BusinessValueControl::getProviderInput('', self::$rekvProviderKeys);
1689
1690 BusinessValueControl::renderMapping(
1691 $mappings,
1692 $inputNamePrefix,
1693 BusinessValueControl::getProviderInput($personTypeId, self::$rekvProviderKeys),
1694 $providerValueInput,
1695 $commonProviderInput,
1696 $commonProviderValueInput
1697 );
1698
1699 ?>
1700 </td>
1701 <?
1702 }
1703 else // system code
1704 {
1705 ?>
1706 <td><?=htmlspecialcharsbx($code['NAME'] ?: $codeKey)?></td>
1707 <td>
1708 <?
1709
1710 if ($personTypeId)
1711 $commonProviderInput = BusinessValueControl::getProviderInput('', self::$systemProviderKeys);
1712
1713 $hideCode = BusinessValueControl::renderMapping(
1714 $mappings,
1715 $inputNamePrefix,
1716 BusinessValueControl::getProviderInput($personTypeId, self::$systemProviderKeys),
1717 $providerValueInput,
1718 $commonProviderInput,
1719 $commonProviderValueInput
1720 );
1721
1722// BusinessValueControl::renderDeletor($inputNamePrefix, $mapping, $commonMapping, $defaultMapping);
1723
1724 ?>
1725 </td>
1726 <?
1727 }
1728 }
1729 else
1730 {
1731 ?>
1732 <td><?=self::CONSUMER_KEY?></td>
1733 <td><?=$codeKey?></td>
1734 <?
1735 }
1736
1737 return $hideCode;
1738 }
1739
1740 public static function getJavaScript()
1741 {
1742 return '
1743 var bizvalAdd1CRekv = (function (personMaxIndex) {
1744 "use strict";
1745 return function(anchor, personTypeId, sample)
1746 {
1747 var myRow = anchor.parentNode.parentNode,
1748 table = myRow.parentNode,
1749 newRow = document.createElement("tr");
1750
1751 if (typeof personMaxIndex[personTypeId] === "undefined")
1752 personMaxIndex[personTypeId] = 0;
1753 else
1754 personMaxIndex[personTypeId] ++;
1755
1756 newRow.innerHTML = sample.replace(/##CODE_INDEX##/g, personMaxIndex[personTypeId]);
1757
1758 table.insertBefore(newRow, myRow);
1759 };
1760 })('.(self::$personMaxIndex ? \CUtil::PhpToJSObject(self::$personMaxIndex) : '{}').');
1761 ';
1762 }
1763}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getRekvCodeKey($personTypeId, $codeIndex)
static renderColumns($codeKey, $personTypeId, array $mappings, $inputNamePrefix)
static sanitizeMapping($codeKey, $personTypeId, array &$mapping)
static setMapping($codeKey, $personTypeId, array $mapping)
static setMapping($codeKey, $consumerKey, $personTypeId, array $mapping, $withCommon=false)
static getMapping($codeKey, $consumerKey=null, $personTypeId=null, array $options=array())
static changeConsumerKey($fromConsumerKey, $toConsumerKey)
static addConsumer($consumerKey, array $consumer)
static updateMapping($codeKey, array $oldMapping, array $newMapping, $consumerKey=null, $personTypeId=null)
static changeConsumer($consumerKey, array $consumer)
static getValuesByCode(string $consumerName, string $code)
static getPersonTypes($all=false, array $resetAllPersonTypes=null)
static get($codeKey, $consumerKey=null, $personTypeId=null, $providerInstance=null)
static install($parentName, $translationSource, $data)
static isSetMapping($codeKey, $consumerKey=null, $personTypeId=null)
static redefineProviderField(array $fields)
static getValueFromProvider(IBusinessValueProvider $provider=null, $codeKey, $consumerKey)