115 private $queryTemplates;
122 private $resourceConnections;
127 private $storeManager;
132 private $productCollectionFactory;
137 private $productRepository;
142 private $optionRepository;
147 private $linkManagement;
159 private $orderQuotesEnable =
true;
180 $this->productCollectionFactory = $productCollectionFactory;
182 $this->optionRepository = $optionRepository;
199 $orderSimpleCountFrom = (int)$this->fixtureModel->getValue(
200 'order_simple_product_count_from',
201 self::ORDER_SIMPLE_PRODUCT_COUNT_FROM
203 $orderSimpleCountTo = (int)$this->fixtureModel->getValue(
204 'order_simple_product_count_to',
205 self::ORDER_SIMPLE_PRODUCT_COUNT_TO
207 $orderConfigurableCountFrom = (int)$this->fixtureModel->getValue(
208 'order_configurable_product_count_from',
209 self::ORDER_CONFIGURABLE_PRODUCT_COUNT_FROM
211 $orderConfigurableCountTo = (int)$this->fixtureModel->getValue(
212 'order_configurable_product_count_to',
213 self::ORDER_CONFIGURABLE_PRODUCT_COUNT_TO
215 $orderBigConfigurableCountFrom = (int)$this->fixtureModel->getValue(
216 'order_big_configurable_product_count_from',
217 self::ORDER_BIG_CONFIGURABLE_PRODUCT_COUNT_FROM
219 $orderBigConfigurableCountTo = (int)$this->fixtureModel->getValue(
220 'order_big_configurable_product_count_to',
221 self::ORDER_BIG_CONFIGURABLE_PRODUCT_COUNT_TO
223 $this->orderQuotesEnable = (bool)$this->fixtureModel->getValue(
'order_quotes_enable',
true);
225 $entityId = $this->getMaxEntityId(
230 $requestedOrders = (int)$this->fixtureModel->getValue(
'orders', 0);
231 if ($requestedOrders - $entityId < 1) {
235 $ruleId = $this->getMaxEntityId(
241 $maxItemId = $this->getMaxEntityId(
246 $maxItemsPerOrder = $orderSimpleCountTo + ($orderConfigurableCountTo + $orderBigConfigurableCountTo) * 2;
249 $itemIdSequence = $this->getItemIdSequence($maxItemId, $requestedOrders, $maxItemsPerOrder);
251 $this->prepareQueryTemplates();
254 foreach ($this->storeManager->getStores() as
$store) {
255 $productsResult = [];
256 $this->storeManager->setCurrentStore(
$store->getId());
258 if ($orderSimpleCountTo > 0) {
259 $productsResult[Type::TYPE_SIMPLE] = $this->prepareSimpleProducts(
260 $this->getProductIds(
$store, Type::TYPE_SIMPLE, $orderSimpleCountTo)
263 if ($orderConfigurableCountTo > 0) {
268 if ($orderBigConfigurableCountTo > 0) {
270 $this->getProductIds(
$store, self::BIG_CONFIGURABLE_TYPE, $orderBigConfigurableCountTo)
277 $this->storeManager->getWebsite(
$store->getWebsiteId())->getName(),
278 $this->storeManager->getGroup(
$store->getStoreGroupId())->getName(),
311 '%firstName%' =>
'First Name',
312 '%lastName%' =>
'Last Name',
313 '%company%' =>
'Company',
314 '%address%' =>
'Address',
316 '%state%' =>
'Alabama',
324 while ($entityId <= $requestedOrders) {
327 Type::TYPE_SIMPLE => mt_rand($orderSimpleCountFrom, $orderSimpleCountTo),
329 self::BIG_CONFIGURABLE_TYPE => mt_rand($orderBigConfigurableCountFrom, $orderBigConfigurableCountTo)
332 '%itemsPerOrder%' => array_sum($productCount),
333 '%orderNumber%' => 100000000 * $productStoreId($entityId) + $entityId,
334 '%email%' =>
"order_{$entityId}@example.com",
335 '%time%' => date(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
336 '%productStoreId%' => $productStoreId($entityId),
337 '%productStoreName%' => $productStoreName($entityId),
338 '%entityId%' => $entityId,
341 $shippingAddress = [
'%orderAddressId%' => $entityId * 2 - 1,
'%addressType%' =>
'shipping'];
342 $billingAddress = [
'%orderAddressId%' => $entityId * 2,
'%addressType%' =>
'billing'];
357 $this->
query(
'sales_order_status_history',
$order);
359 for (
$i = 0;
$i < $productCount[Type::TYPE_SIMPLE];
$i++) {
361 '%productId%' =>
$productId($entityId,
$i, Type::TYPE_SIMPLE),
362 '%sku%' => $productSku($entityId,
$i, Type::TYPE_SIMPLE),
363 '%name%' => $productName($entityId,
$i, Type::TYPE_SIMPLE),
364 '%itemId%' => $itemIdSequence->current(),
365 '%productType%' => Type::TYPE_SIMPLE,
366 '%productOptions%' => $productBuyRequest($entityId,
$i, Type::TYPE_SIMPLE),
367 '%parentItemId%' =>
'null',
369 $this->
query(
'sales_order_item',
$order, $itemData);
371 $this->
query(
'quote_item_option',
$order, $itemData, [
372 '%code%' =>
'info_buyRequest',
373 '%value%' => $this->serializer->serialize([
374 'product' =>
$productId($entityId,
$i, Type::TYPE_SIMPLE),
376 'uenc' =>
'aHR0cDovL21hZ2UyLmNvbS9jYXRlZ29yeS0xLmh0bWw' 379 $itemIdSequence->next();
385 $parentItemId = $itemIdSequence->current();
388 '%sku%' => $productSku($entityId,
$i,
$type),
389 '%name%' => $productName($entityId,
$i,
$type),
390 '%productOptions%' => $productBuyRequest($entityId,
$i,
$type)[
'order'],
391 '%itemId%' => $parentItemId,
392 '%parentItemId%' =>
'null',
395 $this->
query(
'sales_order_item',
$order, $itemData);
397 $this->
query(
'quote_item_option',
$order, $itemData, [
398 '%code%' =>
'info_buyRequest',
399 '%value%' => $productBuyRequest($entityId,
$i,
$type)[
'quote']
401 $this->
query(
'quote_item_option',
$order, $itemData, [
402 '%code%' =>
'attributes',
403 '%value%' => $productBuyRequest($entityId,
$i,
$type)[
'super_attribute']
405 $itemData[
'%productId%'] = $productChildId($entityId,
$i,
$type);
406 $this->
query(
'quote_item_option', $itemData, [
407 '%code%' =>
"product_qty_" . $productChildId($entityId,
$i,
$type),
410 $this->
query(
'quote_item_option', $itemData, [
411 '%code%' =>
"simple_product",
412 '%value%' => $productChildId($entityId,
$i,
$type)
414 $itemIdSequence->next();
418 '%productId%' => $productChildId($entityId,
$i,
$type),
419 '%sku%' => $productSku($entityId,
$i,
$type),
420 '%name%' => $productName($entityId,
$i,
$type),
421 '%productOptions%' => $productChildBuyRequest($entityId,
$i,
$type)[
'order'],
422 '%itemId%' => $itemIdSequence->current(),
423 '%parentItemId%' => $parentItemId,
424 '%productType%' => Type::TYPE_SIMPLE
427 $this->
query(
'sales_order_item',
$order, $itemData);
429 $this->
query(
'quote_item_option', $itemData, [
430 '%code%' =>
"info_buyRequest",
431 '%value%' => $productChildBuyRequest($entityId,
$i,
$type)[
'quote']
433 $this->
query(
'quote_item_option', $itemData, [
434 '%code%' =>
"parent_product_id",
437 $itemIdSequence->next();
440 }
catch (\Exception $lastException) {
441 foreach ($this->resourceConnections as
$connection) {
446 throw $lastException;
449 if ($batchNumber >= self::BATCH_SIZE) {
450 $this->commitBatch();
456 foreach ($this->resourceConnections as
$connection) {
473 private function prepareQueryTemplates()
475 $fileName =
__DIR__ . DIRECTORY_SEPARATOR .
"_files" . DIRECTORY_SEPARATOR .
"orders_fixture_data.json";
492 .
"\\Model\\ResourceModel\\" 500 $querySuffix =
$template[
'_query_suffix'];
514 $this->queryTemplates[
$table] =
"INSERT INTO `{$tableName}` ({$fields}) VALUES ({$values}){$querySuffix};";
531 if (!$this->orderQuotesEnable && strpos(
$table,
"quote") !==
false) {
535 foreach ($replacements as
$data) {
553 private function getMaxEntityId(
$tableName, $resourceName, $column =
'entity_id')
561 $resource = $this->fixtureModel->getObjectManager()->get($resourceName);
563 return (
int)
$connection->query(
"SELECT MAX(`{$column}`) FROM `{$tableName}`;")->fetchColumn(0);
575 private function getProductIds(\
Magento\Store\Api\Data\StoreInterface
$store, $typeId, $limit =
null)
584 if ($typeId === self::BIG_CONFIGURABLE_TYPE) {
592 if ($limit && count($ids) < $limit) {
593 throw new \Exception(
'Not enough products of type: ' . $typeId);
606 private function prepareSimpleProducts(array
$productIds = [])
608 $productsResult = [];
615 $productsResult[$key][
'buyRequest'] = $this->serializer->serialize([
616 "info_buyRequest" => [
617 "uenc" =>
"aHR0cDovL21hZ2VudG8uZGV2L2NvbmZpZ3VyYWJsZS1wcm9kdWN0LTEuaHRtbA,,",
623 return $productsResult;
634 private function prepareConfigurableProducts(array
$productIds = [])
636 $productsResult = [];
641 $configurableChild = reset($configurableChild);
642 $simpleSku = $configurableChild->getSku();
643 $simpleId = $this->productRepository->get($simpleSku)->getId();
645 $attributesInfo = [];
646 $superAttribute = [];
648 $attributesInfo[] = [
649 "label" =>
$option->getLabel(),
650 "value" =>
$option[
'options'][
'0'][
'label'],
651 "option_id" =>
$option->getAttributeId(),
652 "option_value" =>
$option->getValues()[0]->getValueIndex()
654 $superAttribute[
$option->getAttributeId()] =
$option->getValues()[0]->getValueIndex();
657 $configurableBuyRequest = [
658 "info_buyRequest" => [
659 "uenc" =>
"aHR0cDovL21hZ2UyLmNvbS9jYXRlZ29yeS0xLmh0bWw",
660 "product" => $configurableId,
661 "selected_configurable_option" =>
$simpleId,
662 "related_product" =>
"",
663 "super_attribute" => $superAttribute,
666 "attributes_info" => $attributesInfo,
667 "simple_name" => $configurableChild->getName(),
668 "simple_sku" => $configurableChild->getSku(),
670 $simpleBuyRequest = [
671 "info_buyRequest" => [
672 "uenc" =>
"aHR0cDovL21hZ2VudG8uZGV2L2NvbmZpZ3VyYWJsZS1wcm9kdWN0LTEuaHRtbA,,",
673 "product" => $configurableId,
674 "selected_configurable_option" =>
$simpleId,
675 "related_product" =>
"",
676 "super_attribute" => $superAttribute,
681 $quoteConfigurableBuyRequest = $configurableBuyRequest[
'info_buyRequest'];
682 $quoteSimpleBuyRequest = $simpleBuyRequest[
'info_buyRequest'];
683 unset($quoteConfigurableBuyRequest[
'selected_configurable_option']);
684 unset($quoteSimpleBuyRequest[
'selected_configurable_option']);
686 $productsResult[$key][
'id'] = $configurableId;
687 $productsResult[$key][
'sku'] = $simpleSku;
689 $productsResult[$key][
'childId'] =
$simpleId;
690 $productsResult[$key][
'buyRequest'] = [
691 'order' => $this->serializer->serialize($configurableBuyRequest),
692 'quote' => $this->serializer->serialize($quoteConfigurableBuyRequest),
693 'super_attribute' => $this->serializer->serialize($superAttribute)
695 $productsResult[$key][
'childBuyRequest'] = [
696 'order' => $this->serializer->serialize($simpleBuyRequest),
697 'quote' => $this->serializer->serialize($quoteSimpleBuyRequest),
700 return $productsResult;
710 private function commitBatch()
712 foreach ($this->resourceConnections as
$connection) {
725 return 'Generating orders';
746 public function getTableName(
$tableName, $resourceName)
749 $resource = $this->fixtureModel->getObjectManager()->get($resourceName);
761 private function getItemIdSequence($maxItemId, $requestedOrders, $maxItemsPerOrder)
763 $requestedItems = $requestedOrders * $maxItemsPerOrder;
764 for (
$i = $maxItemId + 1;
$i <= $requestedItems;
$i++) {
const ORDER_BIG_CONFIGURABLE_PRODUCT_COUNT_FROM
defined('TESTS_BP')||define('TESTS_BP' __DIR__
const ORDER_CONFIGURABLE_PRODUCT_COUNT_FROM
const ORDER_BIG_CONFIGURABLE_PRODUCT_COUNT_TO
const ORDER_SIMPLE_PRODUCT_COUNT_TO
const BIG_CONFIGURABLE_TYPE
const ORDER_SIMPLE_PRODUCT_COUNT_FROM
query($table,... $replacements)
const ORDER_CONFIGURABLE_PRODUCT_COUNT_TO
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository, \Magento\ConfigurableProduct\Api\LinkManagementInterface $linkManagement, \Magento\Framework\Serialize\SerializerInterface $serializer, FixtureModel $fixtureModel)