Bitrix-D7  20.0.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(array('filter' => array("ACCOUNT_NUMBER" => $accountNumber)));
37  if ($dbRes->fetch())
38  {
39  $accountNumber = null;
40  }
41  }
42  else
43  {
44  $accountNumber = static::generateBySettings($order);
45  }
46 
47  if (!$accountNumber) // if no special template is used or error occured
48  {
49  $accountNumber = static::generateById($order);
50  }
51 
52  return $accountNumber;
53  }
54 
55  /**
56  * @param Sale\OrderBase $order
57  * @return null|string|int
58  * @throws Main\ArgumentException
59  * @throws Main\NotImplementedException
60  * @throws Main\ObjectPropertyException
61  * @throws Main\SystemException
62  */
63  private static function generateBySettings(Sale\OrderBase $order)
64  {
65  $accountNumber = static::getNextNumber($order);
66  if ($accountNumber)
67  {
68  $dbRes = $order::getList(['filter' => ["=ACCOUNT_NUMBER" => $accountNumber]]);
69  if ($dbRes->fetch())
70  {
71  return null;
72  }
73  }
74 
75  return $accountNumber;
76  }
77 
78  /**
79  * @param Sale\OrderBase $order
80  * @return int|string
81  * @throws Main\NotImplementedException
82  */
83  private static function generateById(Sale\OrderBase $order)
84  {
85  $accountNumber = $order->getId();
86  for ($i = 1; $i <= 10; $i++)
87  {
88  $dbRes = $order::getList(array('filter' => array("ACCOUNT_NUMBER" => $accountNumber)));
89  if ($dbRes->fetch())
90  {
91  $accountNumber = $order->getId()."-".$i;
92  }
93  else
94  {
95  break;
96  }
97  }
98 
99  return $accountNumber;
100  }
101 
102  /**
103  * @param Sale\OrderBase $order
104  * @return mixed
105  * @throws Main\ArgumentNullException
106  * @throws Main\ArgumentOutOfRangeException
107  */
108  private static function generateCustom(Sale\OrderBase $order)
109  {
110  $type = Main\Config\Option::get("sale", "account_number_template", "");
111 
112  foreach(GetModuleEvents("sale", "OnBeforeOrderAccountNumberSet", true) as $arEvent)
113  {
114  $tmpRes = ExecuteModuleEventEx($arEvent, array($order->getId(), $type));
115  if ($tmpRes !== false)
116  {
117  return $tmpRes;
118  }
119  }
120 
121  return null;
122  }
123 
124  /**
125  * @param CollectableEntity $item
126  * @return null
127  * @throws Main\NotSupportedException
128  * @throws Main\ObjectNotFoundException
129  */
130  public static function generateForPayment(CollectableEntity $item)
131  {
132  return static::generate($item);
133  }
134 
135  /**
136  * @param CollectableEntity $item
137  * @return null
138  * @throws Main\NotSupportedException
139  * @throws Main\ObjectNotFoundException
140  */
141  public static function generateForShipment(CollectableEntity $item)
142  {
143  return static::generate($item);
144  }
145 
146  /**
147  * @param CollectableEntity $item
148  *
149  * @return null
150  * @throws Main\NotSupportedException
151  * @throws Main\ObjectNotFoundException
152  */
153  private static function generate(CollectableEntity $item)
154  {
155  $accountNumber = null;
156  /** @var EntityCollection $collection */
157  if (!$collection = $item->getCollection())
158  {
159  throw new Main\ObjectNotFoundException('Entity "Collection" not found');
160  }
161 
162  if (!method_exists($collection, "getOrder"))
163  {
164  throw new Main\NotSupportedException();
165  }
166 
167  /** @var Sale\OrderBase $order */
168  if (!$order = $collection->getOrder())
169  {
170  throw new Main\ObjectNotFoundException('Entity "Order" not found');
171  }
172 
173  $accountNumber = $order->getField('ACCOUNT_NUMBER').static::ACCOUNT_NUMBER_SEPARATOR;
174 
175  $count = 1;
176  /** @var CollectableEntity $itemCollection */
177  foreach ($collection as $itemCollection)
178  {
179  if (strval($itemCollection->getField("ACCOUNT_NUMBER")) != "")
180  {
181  $accountNumberIdList = explode(static::ACCOUNT_NUMBER_SEPARATOR, $itemCollection->getField("ACCOUNT_NUMBER"));
182 
183  $itemAccountNumber = trim(end($accountNumberIdList));
184 
185  if ($count <= $itemAccountNumber)
186  $count = $itemAccountNumber + 1;
187  }
188  }
189 
190  return $accountNumber.$count;
191  }
192 
193  /**
194  * Generates next account number according to the scheme selected in the module options
195  *
196  * @param Sale\OrderBase $order
197  * @return null|int|string
198  * @throws Main\ArgumentException
199  * @throws Main\ObjectPropertyException
200  * @throws Main\SystemException
201  */
202  private static function getNextNumber(Sale\OrderBase $order)
203  {
204  $accountNumber = null;
205 
206  $numeratorForOrderSettings = Main\Numerator\Numerator::getOneByType($order::getRegistryType());
207  $numerator = null;
208  if ($numeratorForOrderSettings)
209  {
210  $numerator = Main\Numerator\Numerator::load(
211  $numeratorForOrderSettings['id'],
212  [
213  'ORDER_ID' => $order->getId()
214  ]);
215  }
216  if ($numerator)
217  {
218  $accountNumber = $numerator->getNext();
219  }
220 
221  return $accountNumber;
222  }
223 }
Bitrix\Sale\Internals\CollectableEntity
Definition: collectableentity.php:12
Bitrix\Sale\Internals\AccountNumberGenerator
Definition: accountnumber.php:13
Bitrix\Sale\Internals\CollectableEntity\getCollection
getCollection()
Definition: collectableentity.php:46
Bitrix\Main
Bitrix\Main\Numerator\Numerator\load
static load($numeratorId, $source=null)
Load numerator by id.
Definition: numerator/numerator.php:367
Bitrix\Main\ObjectNotFoundException
Exception is thrown when an object is not present.
Definition: main/lib/exception.php:202
Bitrix\Sale\Internals\AccountNumberGenerator\generateForPayment
static generateForPayment(CollectableEntity $item)
Definition: accountnumber.php:130
Bitrix\Sale\Internals\AccountNumberGenerator\generateForOrder
static generateForOrder(Sale\OrderBase $order)
Definition: accountnumber.php:25
Bitrix\Main\Config\Option\get
static get($moduleId, $name, $default="", $siteId=false)
Returns a value of an option.
Definition: main/lib/config/option.php:29
Bitrix\Sale\Internals
Definition: accountnumber.php:3
Bitrix\Sale
Created by PhpStorm.
Bitrix\Main\NotSupportedException
Exception is thrown when operation is not supported.
Definition: main/lib/exception.php:158
Bitrix\Sale\OrderBase
Definition: orderbase.php:22
Sale
Class RestHandler.
Bitrix\Sale\Internals\AccountNumberGenerator\ACCOUNT_NUMBER_SEPARATOR
const ACCOUNT_NUMBER_SEPARATOR
Definition: accountnumber.php:15
Bitrix\Main\Numerator\Numerator\getOneByType
static getOneByType($type=null)
Returns numerator related fields from db by its type (use it in case of only single one exists for th...
Definition: numerator/numerator.php:173
Bitrix\Sale\Internals\AccountNumberGenerator\generateForShipment
static generateForShipment(CollectableEntity $item)
Definition: accountnumber.php:141