Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accountnumber.php
1<?php
2
4
5
8
14{
16
25 public static function generateForOrder(Sale\OrderBase $order)
26 {
27 $id = (int)$order->getId();
28 if ($id <= 0)
29 {
30 return false;
31 }
32
33 $accountNumber = static::generateCustom($order);
34 if ($accountNumber)
35 {
36 $dbRes = $order::getList([
37 'select' => ['ID'],
38 'filter' => ['=ACCOUNT_NUMBER' => $accountNumber]
39 ]);
40 if ($dbRes->fetch())
41 {
42 $accountNumber = null;
43 }
44 }
45 else
46 {
47 $accountNumber = static::generateBySettings($order);
48 }
49
50 if (!$accountNumber) // if no special template is used or error occured
51 {
52 $accountNumber = static::generateById($order);
53 }
54
55 $dbRes = $order::getList([
56 'select' => ['ID'],
57 'filter' => ['=ACCOUNT_NUMBER' => $accountNumber]
58 ]);
59 if ($dbRes->fetch())
60 {
61 $accountNumber = static::generateForOrder($order);
62 }
63
64 return $accountNumber;
65 }
66
75 private static function generateBySettings(Sale\OrderBase $order)
76 {
77 $accountNumber = static::getNextNumber($order);
78 if ($accountNumber)
79 {
80 $dbRes = $order::getList(['filter' => ["=ACCOUNT_NUMBER" => $accountNumber]]);
81 if ($dbRes->fetch())
82 {
83 return null;
84 }
85 }
86
87 return $accountNumber;
88 }
89
95 private static function generateById(Sale\OrderBase $order)
96 {
97 $accountNumber = $order->getId();
98 for ($i = 1; $i <= 10; $i++)
99 {
100 $dbRes = $order::getList([
101 'select' => ['ID'],
102 'filter' => ['=ACCOUNT_NUMBER' => $accountNumber]
103 ]);
104 if ($dbRes->fetch())
105 {
106 $accountNumber = $order->getId()."-".$i;
107 }
108 else
109 {
110 break;
111 }
112 }
113
114 return $accountNumber;
115 }
116
123 private static function generateCustom(Sale\OrderBase $order)
124 {
125 $type = Main\Config\Option::get("sale", "account_number_template", "");
126
127 foreach(GetModuleEvents("sale", "OnBeforeOrderAccountNumberSet", true) as $arEvent)
128 {
129 $tmpRes = ExecuteModuleEventEx($arEvent, array($order->getId(), $type));
130 if ($tmpRes !== false)
131 {
132 return $tmpRes;
133 }
134 }
135
136 return null;
137 }
138
145 public static function generateForPayment(CollectableEntity $item)
146 {
147 return static::generate($item);
148 }
149
156 public static function generateForShipment(CollectableEntity $item)
157 {
158 return static::generate($item);
159 }
160
168 private static function generate(CollectableEntity $item)
169 {
170 $accountNumber = null;
172 if (!$collection = $item->getCollection())
173 {
174 throw new Main\ObjectNotFoundException('Entity "Collection" not found');
175 }
176
177 if (!method_exists($collection, "getOrder"))
178 {
179 throw new Main\NotSupportedException();
180 }
181
183 if (!$order = $collection->getOrder())
184 {
185 throw new Main\ObjectNotFoundException('Entity "Order" not found');
186 }
187
188 $accountNumber = $order->getField('ACCOUNT_NUMBER').static::ACCOUNT_NUMBER_SEPARATOR;
189
190 $count = 1;
192 foreach ($collection as $itemCollection)
193 {
194 if (strval($itemCollection->getField("ACCOUNT_NUMBER")) != "")
195 {
196 $accountNumberIdList = explode(static::ACCOUNT_NUMBER_SEPARATOR, $itemCollection->getField("ACCOUNT_NUMBER"));
197
198 $itemAccountNumber = trim(end($accountNumberIdList));
199
200 if ($count <= $itemAccountNumber)
201 $count = $itemAccountNumber + 1;
202 }
203 }
204
205 return $accountNumber.$count;
206 }
207
217 private static function getNextNumber(Sale\OrderBase $order)
218 {
219 $accountNumber = null;
220
221 $numeratorForOrderSettings = Main\Numerator\Numerator::getOneByType($order::getRegistryType());
222 $numerator = null;
223 if ($numeratorForOrderSettings)
224 {
225 $numerator = Main\Numerator\Numerator::load(
226 $numeratorForOrderSettings['id'],
227 [
228 'ORDER_ID' => $order->getId()
229 ]);
230 }
231 if ($numerator)
232 {
233 $accountNumber = $numerator->getNext();
234 }
235
236 return $accountNumber;
237 }
238}
static generateForPayment(CollectableEntity $item)
static generateForShipment(CollectableEntity $item)
static generateForOrder(Sale\OrderBase $order)