1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
numerator.php
См. документацию.
1<?php
2
4
19
38{
42 public static function getTableName()
43 {
44 return 'b_numerator';
45 }
46
51 public static function getMap()
52 {
53 return [
54 (new IntegerField('ID'))
55 ->configurePrimary()
56 ->configureAutocomplete()
57 ,
58 (new StringField('NAME'))
59 ->configureRequired()
60 ,
61 (new StringField('TEMPLATE'))
62 ->configureRequired()
63 ,
64 (new StringField('SETTINGS'))
65 ->configureRequired()
66 ,
67 (new StringField('TYPE'))
68 ->configureDefaultValue('DEFAULT')
69 ,
70 new DatetimeField('CREATED_AT'),
71 new IntegerField('CREATED_BY'),
72 new DatetimeField('UPDATED_AT'),
73 new IntegerField('UPDATED_BY'),
74 (new StringField('CODE'))
75 ->configureSize(255)
77 ->configureNullable()
78 ->addValidator(static function($value) {
79 if (is_null($value) || (is_string($value) && !empty($value)))
80 {
81 return true;
82 }
83
84 return 'CODE should be either NULL or non-empty string';
85 })
86 ->addValidator(static function($value, $primary) {
87 if (!is_string($value))
88 {
89 return true;
90 }
91
92 $existingId = self::getIdByCode($value);
93 if (!$existingId)
94 {
95 // no rows with this code exists
96 return true;
97 }
98
99 $id = (int)($primary['ID'] ?? 0);
100 if ($id > 0 && $id === $existingId)
101 {
102 return true;
103 }
104
105 return 'Entry with the same CODE already exists';
106 })
107 ,
108 ];
109 }
110
114 private static function getCurrentUserId()
115 {
116 global $USER;
117 $userId = 0;
118 if ($USER && is_object($USER) && $USER->isAuthorized())
119 {
120 $userId = $USER->getID();
121 }
122 return $userId;
123 }
124
133 public static function getNumeratorList($type, $sort)
134 {
135 $filter = ['=TYPE' => $type];
136 if ($type == 'ALL')
137 {
138 $filter = [];
139 }
140 $params = [
141 'select' => ['*'],
142 'filter' => $filter,
143 'cache' => ['ttl' => 86400],
144 ];
145 if ($sort)
146 {
147 $params['order'] = $sort;
148 }
149 $results = NumeratorTable::getList($params)->fetchAll();
150 foreach ($results as &$numerator)
151 {
152 $numerator['id'] = $numerator['ID'];
153 $numerator['name'] = $numerator['NAME'];
154 $numerator['template'] = $numerator['TEMPLATE'];
155 $numerator['type'] = $numerator['TYPE'];
156 $numerator['code'] = $numerator['CODE'];
157 }
158 return $results;
159 }
160
170 public static function saveNumerator($numeratorId, $numeratorFields)
171 {
172 $fields = [
173 'NAME' => $numeratorFields['NAME'],
174 'TEMPLATE' => $numeratorFields['TEMPLATE'],
175 'TYPE' => $numeratorFields['TYPE'],
176 'SETTINGS' => Json::encode($numeratorFields['SETTINGS']),
177 'UPDATED_AT' => new DateTime(),
178 'UPDATED_BY' => static::getCurrentUserId(),
179 'CODE' => $numeratorFields['CODE'] ?? null,
180 ];
181 if ($numeratorId)
182 {
183 if (!(Numerator::load($numeratorId)))
184 {
185 $result = new UpdateResult();
186 $result->addError(new Error(Loc::getMessage('MAIN_NUMERATOR_EDIT_NUMERATOR_NOT_FOUND_ERROR')));
187 return $result;
188 }
189
190 $updateRes = NumeratorTable::update($numeratorId, $fields);
191
192 if ($updateRes->isSuccess())
193 {
194 $numerator = Numerator::load($numeratorId);
195 $isNewNumSequential = $numerator->hasSequentialNumber();
196 if (!$isNewNumSequential)
197 {
199 }
200 }
201 return $updateRes;
202 }
203 $fields['CREATED_AT'] = new DateTime();
204 $fields['CREATED_BY'] = static::getCurrentUserId();
206 }
207
215 public static function loadSettings($numeratorId)
216 {
217 $numerator = static::getList([
218 'select' => ['*',],
219 'filter' => ['=ID' => $numeratorId],
220 ])->fetch();
221
222 if ($numerator)
223 {
224 $result = [];
226 'idFromDb' => $numerator['ID'],
227 'name' => $numerator['NAME'],
228 'template' => $numerator['TEMPLATE'],
229 'type' => $numerator['TYPE'],
230 'code' => $numerator['CODE'],
231 ];
232 $numeratorGenerators = Json::decode($numerator['SETTINGS']);
233 $numberGeneratorFactory = new NumberGeneratorFactory();
234 foreach ($numeratorGenerators as $generatorType => $numeratorGenerator)
235 {
236 $class = $numberGeneratorFactory->getClassByType($generatorType);
237 if (in_array(Sequenceable::class, class_implements($class)))
238 {
239 $numeratorGenerators[$generatorType]['numeratorId'] = $numeratorId;
240 }
241 }
242 return array_merge($result, $numeratorGenerators);
243 }
244
245 return $numerator;
246 }
247
248 public static function getIdByCode($code): ?int
249 {
250 $code = (string)$code;
251 if (empty($code))
252 {
253 return null;
254 }
255
256 $row =
257 static::query()
258 ->setSelect(['ID'])
259 ->where('CODE', $code)
260 ->setLimit(1)
261 ->setCacheTtl(3600)
262 ->fetch()
263 ;
264
265 if ($row && isset($row['ID']))
266 {
267 return (int)$row['ID'];
268 }
269
270 return null;
271 }
272}
$type
Определения options.php:106
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения error.php:15
static loadSettings($numeratorId)
Определения numerator.php:215
static getIdByCode($code)
Определения numerator.php:248
static getNumeratorList($type, $sort)
Определения numerator.php:133
static saveNumerator($numeratorId, $numeratorFields)
Определения numerator.php:170
static getType()
Определения numerator.php:621
static load($numeratorId, $source=null)
Определения numerator.php:369
static getList(array $parameters=array())
Определения datamanager.php:431
static add(array $data)
Определения datamanager.php:877
static update($primary, array $data)
Определения datamanager.php:1256
configureUnique($value=true)
Определения scalarfield.php:115
Определения json.php:9
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
global $USER
Определения csv_new_run.php:40
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$fields
Определения yandex_run.php:501