Completed
Push — master ( ec1dbf...510cdb )
by Gabriel
04:59 queued 51s
created

StockbaseStockManagement::getStockbaseEan()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 3
nop 1
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
crap 6.027
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Model\Inventory;
5
6
use Magento\Catalog\Model\ProductFactory;
7
use Magento\CatalogInventory\Api\StockRegistryInterface;
8
use Magento\Framework\ObjectManagerInterface;
9
use Magento\Quote\Model\Quote\Item as QuoteItem;
10
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
11
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource;
12
use Stockbase\Integration\Model\ResourceModel\StockItemReserve\Collection as StockItemReserveCollection;
13
use Stockbase\Integration\Model\StockItemReserve;
14
15
/**
16
 * Class StockbaseStockManagement
17
 */
18
class StockbaseStockManagement
19
{
20
    /**
21
     * @var StockRegistryInterface
22
     */
23
    private $stockRegistry;
24
    /**
25
     * @var StockbaseConfiguration
26
     */
27
    private $config;
28
    /**
29
     * @var ProductFactory
30
     */
31
    private $productFactory;
32
    /**
33
     * @var StockItemResource
34
     */
35
    private $stockItemResource;
36
    /**
37
     * @var ObjectManagerInterface
38
     */
39
    private $objectManager;
40
41
    /**
42
     * StockbaseStockManagement constructor.
43
     * @param StockRegistryInterface $stockRegistry
44
     * @param StockbaseConfiguration $config
45
     * @param ProductFactory         $productFactory
46
     * @param StockItemResource      $stockItemResource
47
     * @param ObjectManagerInterface $objectManager
48
     */
49 9
    public function __construct(
50
        StockRegistryInterface $stockRegistry,
51
        StockbaseConfiguration $config,
52
        ProductFactory $productFactory,
53
        StockItemResource $stockItemResource,
54
        ObjectManagerInterface $objectManager
55
    ) {
56
57 9
        $this->stockRegistry = $stockRegistry;
58 9
        $this->config = $config;
59 9
        $this->productFactory = $productFactory;
60 9
        $this->stockItemResource = $stockItemResource;
61 9
        $this->objectManager = $objectManager;
62 9
    }
63
64
    /**
65
     * Gets the amount of items available in the Stockbase stock.
66
     *
67
     * @param int $productId
68
     * @return float
69
     */
70 1
    public function getStockbaseStockAmount($productId)
71
    {
72 1
        $qty = 0;
73
        
74 1
        $ean = $this->getStockbaseEan($productId);
75 1
        if (!empty($ean)) {
76 1
            $qty += max($this->stockItemResource->getNotReservedStockAmount($ean), 0);
77
        }
78
        
79 1
        return $qty;
80
    }
81
82
    /**
83
     * Gets the Stockbase EAN for given product (if any).
84
     *
85
     * @param int $productId
86
     * @return string|null
87
     */
88 4
    public function getStockbaseEan($productId)
89
    {
90 4
        $stockItem = $this->stockRegistry->getStockItem($productId);
91 4
        if ($this->config->isModuleEnabled() &&
92 4
            $stockItem->getManageStock() &&
93 4
            $stockItem->getBackorders() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
94
        ) {
95 4
            $product = $this->productFactory->create();
96 4
            $product->load($productId);
97 4
            $ean = $product->getData($this->config->getEanFieldName());
98
99 4
            if ($product->getData('stockbase_product') && !empty($ean)) {
100 4
                return $ean;
101
            }
102
        }
103
        
104
        return null;
105
    }
106
107
    /**
108
     * Checks if given product is properly configured to be processed as a Stockbase product.
109
     *
110
     * @param int $productId
111
     * @return bool
112
     */
113 1
    public function isStockbaseProduct($productId)
114
    {
115 1
        return !empty($this->getStockbaseEan($productId));
116
    }
117
118
    /**
119
     * Increments/decrements the amount of items in stock.
120
     *
121
     * @param string $ean
122
     * @param float  $amount
123
     * @param string $operation
124
     */
125 2
    public function updateStockAmount($ean, $amount, $operation = '-')
126
    {
127 2
        $this->stockItemResource->updateStockAmount($ean, $amount, $operation);
128 2
    }
129
130
    /**
131
     * Creates a stock reserve for given item.
132
     *
133
     * @param QuoteItem $quoteItem
134
     * @param float     $stockbaseAmount
135
     * @param float     $magentoStockAmount
136
     * @return StockItemReserve
137
     */
138 1
    public function createReserve(QuoteItem $quoteItem, $stockbaseAmount, $magentoStockAmount)
139
    {
140 1
        $ean = $this->getStockbaseEan($quoteItem->getProductId());
141
        
142
        /** @var StockItemReserve $stockItemReserve */
143 1
        $stockItemReserve = $this->objectManager->create(StockItemReserve::class);
144 1
        $stockItemReserve->setData([
145 1
            'ean' => $ean,
146 1
            'amount' => $stockbaseAmount,
147 1
            'magento_stock_amount' => $magentoStockAmount,
148 1
            'quote_item_id' => $quoteItem->getId(),
149 1
            'product_id' => $quoteItem->getProductId(),
150 1
            'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
151
        ]);
152 1
        $stockItemReserve->save();
153
        
154 1
        return $stockItemReserve;
155
    }
156
    
157
    /**
158
     * Releases the stock reserve.
159
     *
160
     * @param StockItemReserve $reserve
161
     */
162 1
    public function releaseReserve(StockItemReserve $reserve)
163
    {
164 1
        $reserve->delete();
165 1
    }
166
167
    /**
168
     * Gets stock reserve entries for given products.
169
     *
170
     * @param int|int[] $productIds
171
     * @return StockItemReserve[]
172
     */
173 1 View Code Duplication
    public function getReserveForProduct($productIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175 1
        $productIds = is_array($productIds) ? $productIds : [$productIds];
176
177
        /** @var StockItemReserveCollection $reserveCollection */
178 1
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
179 1
        $reserveCollection->addFieldToFilter('product_id', ['in' => $productIds]);
180
181
        /** @var StockItemReserve[] $reservedStockbaseItems */
182 1
        $reservedStockbaseItems = $reserveCollection->getItems();
183
184 1
        return $reservedStockbaseItems;
185
    }
186
187
    /**
188
     * Gets stock reserve entries for given quote items.
189
     *
190
     * @param int|int[] $quoteItemIds
191
     * @return StockItemReserve[]
192
     */
193 1 View Code Duplication
    public function getReserveForQuoteItem($quoteItemIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195 1
        $quoteItemIds = is_array($quoteItemIds) ? $quoteItemIds : [$quoteItemIds];
196
        
197
        /** @var StockItemReserveCollection $reserveCollection */
198 1
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
199 1
        $reserveCollection->addFieldToFilter('quote_item_id', ['in' => $quoteItemIds]);
200
201
        /** @var StockItemReserve[] $reservedStockbaseItems */
202 1
        $reservedStockbaseItems = $reserveCollection->getItems();
203
        
204 1
        return $reservedStockbaseItems;
205
    }
206
}
207