An open API service indexing awesome lists of open source software.

https://github.com/bdcrops/module-minventory


https://github.com/bdcrops/module-minventory

Last synced: 3 days ago
JSON representation

Awesome Lists containing this project

README

        

# BDC_Minventory

This module is used as a resupply for stock BDCrops Magento 2 extensions.
Magento 2 Stock / Inventory Management, demonstrating UI grid.

## Goal
- Quick Stock / Inventory update from admin
- Easily bump up product stock in certain increments without ever getting access to other areas of the Magento admin.
- Better understanding of the adminhtml developing for the adminhtml area
- Using the listing & form component

## How to install & upgrade BDC_Minventory

### 1.1 Copy and paste

If you don't want to install via composer, you can use this way.

- Download [the latest version here](https://github.com/bdcrops/module-minventory/archive/master.zip)
- Extract `master.zip` file to `app/code/BDC/Core` ; You should create a folder path `app/code/BDC/Core` if not exist.
- Go to Magento root folder and run upgrade command line to install `BDC_Minventory`:

### 1.2 Install via composer

We recommend you to install BDC_Minventory module via composer. It is easy to install, update and maintaince.Run the following command in Magento 2 root folder.

```
composer config repositories.module-minventory git
https://github.com/bdcrops/module-minventory.git

composer require bdc/module-minventory:~1.0.0
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
```

#### 1.2 Upgrade

```
composer update bdcrops/module-minventory
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
```

Run compile if your store in Product mode:

```
php bin/magento setup:di:compile

```
## Tutorial Module Step By Step (BDCrops)

- create app/code/BDC/Minventory/registration.php
```

```
- create a app/code/BDC/Minventory/etc/adminhtml/routes.xml
```







```
- create a app/code/BDC/Minventory/Controller/Adminhtml/Product.php
```
resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->getConfig()->getTitle()->prepend((__('Micro Inventory')));
return $resultPage;
}
}
```
- create a app/code/BDC/Minventory/Controller/Adminhtml/Product/MassResupply.php
```
filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->resupply = $resupply;
}

public function execute(){
$redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$qty = $this->getRequest()->getParam('qty');
$collection = $this->filter->getCollection($this->collectionFactory->create());
$productResupplied = 0;
foreach ($collection->getItems() as $product) {
$this->resupply->resupply($product->getId(), $qty);
$productResupplied++;
}

$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been resupplied.', $productResupplied));
return $redirectResult->setPath('minventory/product/index');
}
}

```
- create a app/code/BDC/Minventory/Controller/Adminhtml/Product/Resupply.php
```
productRepository = $productRepository;
$this->stockRegistry = $stockRegistry;
$this->resupply = $resupply;
}

public function execute() {
if ($this->getRequest()->isPost()) {
$this->resupply->resupply(
$this->getRequest()->getParam('id'),
$_POST['minventory_product']['qty']
);
$this->messageManager->addSuccessMessage(__('Successfully resupplied'));
$redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $redirectResult->setPath('minventory/product/index');
} else {
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->getConfig()->getTitle()->prepend((__('Stock Resupply')));
return $resultPage;
}
}
}

```
- create a app/code/BDC/Minventory/Model/Resupply.php
```
productRepository = $productRepository;
$this->collectionFactory = $collectionFactory;
$this->stockRegistry = $stockRegistry;
}

public function resupply($productId, $qty) {
$product = $this->productRepository->getById($productId);
$stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
$stockItem->setQty($stockItem->getQty() + $qty);
$stockItem->setIsInStock((bool)$stockItem->getQty());
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
}
}

```
- create a app/code/BDC/Minventory/etc/adminhtml/menu.xml
```



```
![](docs/menuDash.png)

- create a app/code/BDC/Minventory/etc/acl.xml
```











```
![](docs/aclMenu.png)

- create a app/code/BDC/Minventory/view/adminhtml/layout/minventory_product_index.xml
```






```
- create a app/code/BDC/Minventory/view/adminhtml/layout/minventory_product_resupply.xml
```






```
- create a app/code/BDC/Minventory/view/adminhtml/ui_component/minventory_listing.xml
```



minventory_listing.minventory_listing_data_source



minventory_columns

minventory_listing.minventory_listing_data_source





entity_id





id
entity_id











All Store Views
Store View
store_id






resupply
Resupply


resupply_10
Resupply +10

10



resupply_50
Resupply +50

50












minventory_listing.minventory_listing.minventory_columns.actions
applyAction

resupply
${ $.$data.rowIndex }






entity_id




textRange
ID
asc




text
SKU




text
SKU




true

select
select
Status




true
textRange
Quantity




entity_id


```
![](docs/UiComponent.png)

- create a app/code/BDC/Minventory/view/adminhtml/ui_component/minventory_resupply_form.xml

```



minventory_resupply_form.minventory_resupply_form_data_source
minventory_resupply_form.minventory_resupply_form_data_source


tabs











Magento_Ui/js/form/provider




id
entity_id






General





Stock
true
text
input
true






+Qty
true
text
input
true

true
true




```

![](docs/resupplyStock.png)

- create a app/code/BDC/Minventory/Block/Adminhtml/Product/Edit/Button/Back.php
```
__('Back'),
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
'class' => 'back',
'sort_order' => 10
];
}

public function getBackUrl() {
return $this->getUrl('*/*/');
}
}

```
- create a app/code/BDC/Minventory/Block/Adminhtml/Product/Edit/Button/Save.php
```
__('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 20,
];
}
}

```
- create a app/code/BDC/Minventory/Ui/Component/Listing/Columns/Resupply.php
```
urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$storeId = $this->context->getFilterParam('store_id');

foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['resupply'] = [
'href' => $this->urlBuilder->getUrl(
'minventory/product/resupply',
['id' => $item['entity_id'], 'store' => $storeId]
),
'label' => __('Resupply'),
'hidden' => false,
];
}
}

return $dataSource;
}
}

```
- create a app/code/BDC/Minventory/Ui/DataProvider/Product/Form/ProductDataProvider.php
```
collection = $collectionFactory->create();
$this->productRepository = $productRepository;
$this->stockRegistry = $stockRegistry;
$this->request = $request;
}

public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

$id = $this->request->getParam('id');
$product = $this->productRepository->getById($id);
$stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());

$this->loadedData[$product->getId()]['minventory_product'] = [
'stock' => __('%1 | %2', $product->getSku(), $stockItem->getQty()),
'qty' => 10
];

return $this->loadedData;
}
}

```
- create a app/code/BDC/Minventory/Ui/DataProvider/Product/ProductDataProvider.php

```
collection = $collectionFactory->create();

$this->joinQty();
}

public function getData()
{
if (!$this->getCollection()->isLoaded()) {
$this->getCollection()->load();
}

// By default parent's getData already does $this->getCollection()->toArray();
// However, this is not enough for product collection as we need the "totalRecords and items" structure
$items = $this->getCollection()->toArray();

return [
'totalRecords' => $this->getCollection()->getSize(),
'items' => array_values($items),
];
}

protected function joinQty()
{
if ($this->getCollection()) {
$this->getCollection()->joinField(
'qty',
'cataloginventory_stock_item',
'qty',
'product_id=entity_id'
);
}
}
}

```

- Output

![](docs/minventoryList.png)
![](docs/resupplyStock.png)
![](docs/resupplyaddAfter.png)
![](docs/massResuply.png)

### Ref