Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
roundingrule.php
1<?php
2
3
5
6
13
14final class RoundingRule extends Controller implements EventBindInterface
15{
16 use PriceTypeRights;
17
18 //region Actions
19
24 public function addAction(array $fields): ?array
25 {
26 $checkFieldsResult = $this->checkFields($fields);
27 if (!$checkFieldsResult->isSuccess())
28 {
29 $this->addErrors($checkFieldsResult->getErrors());
30 return null;
31 }
32
33 $addResult = RoundingTable::add($fields);
34 if (!$addResult->isSuccess())
35 {
36 $this->addErrors($addResult->getErrors());
37 return null;
38 }
39
40 return ['ROUNDING_RULE' => $this->get($addResult->getId())];
41 }
42
48 public function updateAction(int $id, array $fields): ?array
49 {
50 $existsResult = $this->exists($id);
51 if (!$existsResult->isSuccess())
52 {
53 $this->addErrors($existsResult->getErrors());
54 return null;
55 }
56
57 $checkFieldsResult = $this->checkFields($fields);
58 if (!$checkFieldsResult->isSuccess())
59 {
60 $this->addErrors($checkFieldsResult->getErrors());
61 return null;
62 }
63
64 $updateResult = RoundingTable::update($id, $fields);
65 if (!$updateResult)
66 {
67 $this->addErrors($updateResult->getErrors());
68 return null;
69 }
70
71 return ['ROUNDING_RULE' => $this->get($id)];
72 }
73
78 public function deleteAction(int $id): ?bool
79 {
80 $existsResult = $this->exists($id);
81 if (!$existsResult->isSuccess())
82 {
83 $this->addErrors($existsResult->getErrors());
84 return null;
85 }
86
87 $deleteResult = RoundingTable::delete($id);
88 if (!$deleteResult)
89 {
90 $this->addErrors($deleteResult->getErrors());
91 return null;
92 }
93
94 return true;
95 }
96
100 public function getFieldsAction(): array
101 {
102 return ['ROUNDING_RULE' => $this->getViewFields()];
103 }
104
112 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
113 {
114 return new Page(
115 'ROUNDING_RULES',
116 $this->getList($select, $filter, $order, $pageNavigation),
117 $this->count($filter)
118 );
119 }
120
125 public function getAction($id): ?array
126 {
127 $r = $this->exists($id);
128 if ($r->isSuccess())
129 {
130 return ['ROUNDING_RULE' => $this->get($id)];
131 }
132 else
133 {
134 $this->addErrors($r->getErrors());
135 return null;
136 }
137 }
138 //endregion
139
143 protected function exists($id)
144 {
145 $r = new Result();
146 if (isset($this->get($id)['ID']) == false)
147 {
148 $r->addError(new Error('Rounding does not exist'));
149 }
150
151 return $r;
152 }
153
157 protected function getEntityTable()
158 {
159 return new RoundingTable();
160 }
161
166 private function checkFields(array $fields): Result
167 {
168 $result = new Result();
169
170 if (array_key_exists('ROUND_TYPE', $fields))
171 {
172 $roundTypes = RoundingTable::getRoundTypes();
173 if (!in_array($fields['ROUND_TYPE'], $roundTypes))
174 {
175 $result->addError(
176 new Error(
177 'Invalid rounding type provided. The available values are: '
178 . implode(', ', $roundTypes)
179 )
180 );
181 }
182 unset($roundTypes);
183 }
184
185 if (array_key_exists('ROUND_PRECISION', $fields))
186 {
188 if (!in_array($fields['ROUND_PRECISION'], $precisionList))
189 {
190 $result->addError(
191 new Error(
192 'Invalid rounding precision provided. The available values are: '
193 . implode(', ', $precisionList)
194 )
195 );
196 }
197 unset($precisionList);
198 }
199
200 return $result;
201 }
202}
updateAction(int $id, array $fields)
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
static getRoundTypes(bool $full=false)
Definition rounding.php:443
static update($primary, array $data)