Bitrix-D7  20.5.0
accountnumber.php
См. документацию.
1 <?php
2 
4 
5 
6 use Bitrix\Main;
7 use Bitrix\Sale;
8 
9 /**
10  * Class AccountNumberGenerator
11  * @package Bitrix\Sale\Internals
12  */
14 {
16 
17  /**
18  * @param Sale\OrderBase $order
19  * @return mixed
20  * @throws Main\ArgumentNullException
21  * @throws Main\ArgumentOutOfRangeException
22  * @throws Main\NotImplementedException
23  * @throws Main\SystemException
24  */
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 
67  /**
68  * @param Sale\OrderBase $order
69  * @return null|string|int
70  * @throws Main\ArgumentException
71  * @throws Main\NotImplementedException
72  * @throws Main\ObjectPropertyException
73  * @throws Main\SystemException
74  */
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 
90  /**
91  * @param Sale\OrderBase $order
92  * @return int|string
93  * @throws Main\NotImplementedException
94  */
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 
117  /**
118  * @param Sale\OrderBase $order
119  * @return mixed
120  * @throws Main\ArgumentNullException
121  * @throws Main\ArgumentOutOfRangeException
122  */
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 
139  /**
140  * @param CollectableEntity $item
141  * @return null
142  * @throws Main\NotSupportedException
143  * @throws Main\ObjectNotFoundException
144  */
145  public static function generateForPayment(CollectableEntity $item)
146  {
147  return static::generate($item);
148  }
149 
150  /**
151  * @param CollectableEntity $item
152  * @return null
153  * @throws Main\NotSupportedException
154  * @throws Main\ObjectNotFoundException
155  */
156  public static function generateForShipment(CollectableEntity $item)
157  {
158  return static::generate($item);
159  }
160 
161  /**
162  * @param CollectableEntity $item
163  *
164  * @return null
165  * @throws Main\NotSupportedException
166  * @throws Main\ObjectNotFoundException
167  */
168  private static function generate(CollectableEntity $item)
169  {
170  $accountNumber = null;
171  /** @var EntityCollection $collection */
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 
182  /** @var Sale\OrderBase $order */
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;
191  /** @var CollectableEntity $itemCollection */
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 
208  /**
209  * Generates next account number according to the scheme selected in the module options
210  *
211  * @param Sale\OrderBase $order
212  * @return null|int|string
213  * @throws Main\ArgumentException
214  * @throws Main\ObjectPropertyException
215  * @throws Main\SystemException
216  */
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 get($moduleId, $name, $default="", $siteId=false)
Returns a value of an option.
Exception is thrown when operation is not supported.
static getOneByType($type=null)
Returns numerator related fields from db by its type (use it in case of only single one exists for th...
static load($numeratorId, $source=null)
Load numerator by id.
Exception is thrown when an object is not present.
static generateForPayment(CollectableEntity $item)
static generateForShipment(CollectableEntity $item)
static generateForOrder(Sale\OrderBase $order)
Class RestHandler \Handlers\PaySystem.