Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
payableitem.php
1<?php
2
3namespace Bitrix\Sale;
4
6
7Main\Localization\Loc::loadMessages(__FILE__);
8
14{
15 abstract public function getEntityObject();
16
17 abstract public static function getEntityType();
18
20 protected $item;
21
25 public static function getRegistryType()
26 {
28 }
29
33 public static function getAvailableFields()
34 {
35 return [
36 'ORDER_ID', 'PAYMENT_ID', 'ENTITY_ID', 'ENTITY_TYPE',
37 'DATE_INSERT', 'QUANTITY', 'XML_ID'
38 ];
39 }
40
44 protected static function getMeaningfulFields()
45 {
46 return [];
47 }
48
52 protected static function getFieldsMap()
53 {
54 return Internals\PayableItemTable::getMap();
55 }
56
65 public static function create(PayableItemCollection $collection, Internals\CollectableEntity $entity)
66 {
68 $item = static::createPayableItemObject();
69
70 $item->setCollection($collection);
71
72 $item->item = $entity;
73
74 if ($entity->getId() > 0)
75 {
76 $item->setFieldNoDemand('ENTITY_ID', $entity->getId());
77 }
78
79 $item->setFieldNoDemand('ENTITY_TYPE', static::getEntityType());
80 $item->setFieldNoDemand('XML_ID', static::generateXmlId());
81
82 return $item;
83 }
84
85 public function getQuantity() : float
86 {
87 return (float)$this->getField('QUANTITY');
88 }
89
90 abstract public function getPrice() : float;
91
95 protected static function generateXmlId() : string
96 {
97 return uniqid('bx_');
98 }
99
106 private static function createPayableItemObject(array $fields = array())
107 {
108 $registry = Registry::getInstance(static::getRegistryType());
109 $entityClassName = $registry->get(static::getRegistryEntity());
110
111 return new $entityClassName($fields);
112 }
113
121 public static function loadForPayment($id)
122 {
123 if (intval($id) <= 0)
124 {
125 return [];
126 }
127
128 $registry = Registry::getInstance(static::getRegistryType());
129
131 $payableItemCollection = $registry->get(Registry::ENTITY_PAYABLE_ITEM_COLLECTION);
132 $dbRes = $payableItemCollection::getList([
133 'filter' => [
134 '=PAYMENT_ID' => $id,
135 '=ENTITY_TYPE' => static::getEntityType()
136 ]
137 ]);
138
139 $entityList = [];
140 while ($data = $dbRes->fetch())
141 {
142 $entityList[] = static::createPayableItemObject($data);
143 }
144
145 return $entityList;
146 }
147
152 public function save()
153 {
154 $result = new Result();
155
156 if (!$this->isChanged())
157 {
158 return $result;
159 }
160
161 $id = $this->getId();
162
163 if ($id > 0)
164 {
165 $fields = $this->getFields()->getChangedValues();
166
167 $r = $this->updateInternal($id, $fields);
168 }
169 else
170 {
171 $payment = $this->getCollection()->getPayment();
172
173 if ((int)$this->getField('ENTITY_ID') === 0)
174 {
175 $this->setFieldNoDemand('ENTITY_ID', $this->getEntityObject()->getId());
176 }
177
178 $this->setFieldNoDemand('DATE_INSERT', new Main\Type\DateTime());
179 $this->setFieldNoDemand('PAYMENT_ID', $payment->getId());
180
181 $fields = $this->getFields()->getValues();
182 $r = $this->addInternal($fields);
183 if ($r->isSuccess())
184 {
185 $id = $r->getId();
186 $this->setFieldNoDemand('ID', $id);
187 }
188 }
189
190 if (!$r->isSuccess())
191 {
192 return $result->addErrors($r->getErrors());
193 }
194
195 $result->setId($id);
196
197 return $result;
198 }
199
206 protected function updateInternal($primary, array $data)
207 {
208 return Internals\PayableItemTable::update($primary, $data);
209 }
210
216 protected function addInternal(array $data)
217 {
218 return Internals\PayableItemTable::add($data);
219 }
220}
updateInternal($primary, array $data)
static getInstance($type)
Definition registry.php:183
const ENTITY_PAYABLE_ITEM_COLLECTION
Definition registry.php:25