-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathSelectBuilder.php
More file actions
142 lines (127 loc) · 4.67 KB
/
SelectBuilder.php
File metadata and controls
142 lines (127 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryBundleProductIndexer\Indexer;
use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\InventoryIndexer\Indexer\IndexStructure;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryMultiDimensionalIndexerApi\Model\Alias;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameBuilder;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameResolverInterface;
/**
* Get bundle product for given stock select builder.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SelectBuilder
{
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var IndexNameBuilder
*/
private $indexNameBuilder;
/**
* @var IndexNameResolverInterface
*/
private $indexNameResolver;
/**
* @var MetadataPool
*/
private $metadataPool;
/**
* @var Config
*/
private $eavConfig;
/**
* @param ResourceConnection $resourceConnection
* @param IndexNameBuilder $indexNameBuilder
* @param IndexNameResolverInterface $indexNameResolver
* @param MetadataPool $metadataPool
* @param Config $eavConfig
*/
public function __construct(
ResourceConnection $resourceConnection,
IndexNameBuilder $indexNameBuilder,
IndexNameResolverInterface $indexNameResolver,
MetadataPool $metadataPool,
Config $eavConfig
) {
$this->resourceConnection = $resourceConnection;
$this->indexNameBuilder = $indexNameBuilder;
$this->indexNameResolver = $indexNameResolver;
$this->metadataPool = $metadataPool;
$this->eavConfig = $eavConfig;
}
/**
* Prepare select for getting bundle products on given stock.
*
* @param int $stockId
* @return Select
* @throws Exception
*/
public function execute(int $stockId): Select
{
$connection = $this->resourceConnection->getConnection();
$indexName = $this->indexNameBuilder
->setIndexId(InventoryIndexer::INDEXER_ID)
->addDimension('stock_', (string)$stockId)
->setAlias(Alias::ALIAS_MAIN)
->build();
$indexTableName = $this->indexNameResolver->resolveName($indexName);
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
$linkField = $metadata->getLinkField();
$statusAttributeId = $this->getAttribute(ProductInterface::STATUS)->getId();
$select = $connection->select()
->from(
['stock' => $indexTableName],
[
IndexStructure::SKU => 'parent_product_entity.sku',
IndexStructure::QUANTITY => 'SUM(stock.quantity)',
IndexStructure::IS_SALABLE => 'MAX(stock.is_salable)',
]
)->joinInner(
['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
'product_entity.sku = stock.sku',
[]
)->joinInner(
['parent_link' => $this->resourceConnection->getTableName('catalog_product_bundle_selection')],
'parent_link.product_id = product_entity.entity_id',
[]
)->joinInner(
['parent_product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
'parent_product_entity.' . $linkField . ' = parent_link.parent_product_id',
[]
)->joinInner(
['product_status' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
"product_entity.$linkField = product_status.$linkField"
. " AND product_status.attribute_id = $statusAttributeId"
. ' AND product_status.value = ' . ProductStatus::STATUS_ENABLED,
[]
)->group(['parent_product_entity.sku']);
return $select;
}
/**
* Retrieve catalog_product attribute instance by attribute code
*
* @param string $attributeCode
* @return Attribute
*/
private function getAttribute($attributeCode): Attribute
{
return $this->eavConfig->getAttribute(Product::ENTITY, $attributeCode);
}
}