Skip to content

Commit

Permalink
Merge pull request #220 from magmodules/release/1.18.0
Browse files Browse the repository at this point in the history
Release/1.18.0
  • Loading branch information
Marvin-Magmodules authored Mar 25, 2024
2 parents d9665a8 + f124f5b commit c7178e3
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Api/Config/System/ReturnsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ReturnsInterface extends ItemupdateInterface
public const XML_PATH_RETURNS_ENABLE = 'magmodules_channable_marketplace/returns/enable';
public const XML_PATH_RETURNS_CREDITMEMO = 'magmodules_channable_marketplace/returns/show_on_creditmemo';
public const XML_PATH_RETURNS_AUTO_MATCH = 'magmodules_channable_marketplace/returns/auto_update';
public const XML_PATH_GTIN_ATTRIBUTE = 'magmodules_channable/data/ean_attribute';

/**
* Check whether returns are enabled
Expand Down Expand Up @@ -55,4 +56,12 @@ public function getReturnsWebhookUrl(int $storeId): string;
* @return bool
*/
public function autoUpdateReturnsOnCreditmemo(int $storeId = null): bool;

/**
* Returns attribute set as GTIN
*
* @param int|null $storeId
* @return string
*/
public function getGtinAttribute(int $storeId = null): string;
}
4 changes: 2 additions & 2 deletions Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,8 @@ public function getPriceCollection($config, $product)
$finalPrice = $product->getFinalPrice();
$specialPrice = $product->getSpecialPrice();
} else {
$finalPrice = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
$price = $product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
$finalPrice = $product->getPriceInfo()->getPrice('final_price')->getValue();
$price = $product->getPriceInfo()->getPrice('regular_price')->getValue();
$product['min_price'] = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getBaseAmount();
$product['max_price'] = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getBaseAmount();
}
Expand Down
10 changes: 10 additions & 0 deletions Helper/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Source extends AbstractHelper
const XPATH_INVENTORY = 'magmodules_channable/advanced/inventory';
const XPATH_INVENTORY_DATA = 'magmodules_channable/advanced/inventory_fields';
const XPATH_FORCE_NON_MSI = 'magmodules_channable/advanced/force_non_msi';
const XPATH_INVENTORY_SOURCE_ITEMS = 'magmodules_channable/advanced/inventory_source_items';
const XPATH_TAX = 'magmodules_channable/advanced/tax';
const XPATH_TAX_INCLUDE_BOTH = 'magmodules_channable/advanced/tax_include_both';
const XPATH_MANAGE_STOCK = 'cataloginventory/item_options/manage_stock';
Expand Down Expand Up @@ -612,6 +613,14 @@ public function getAttributes($type, $filters = [], $storeId = null)
'source' => 'backorders'
];
}

if ($this->getStoreValue(self::XPATH_INVENTORY_SOURCE_ITEMS)) {
$attributes['inventory_source_items'] = [
'label' => 'inventory_source_items',
'source' => 'inventory_source_items'
];
}

}
$attributes['weight'] = [
'label' => 'shipping_weight',
Expand Down Expand Up @@ -791,6 +800,7 @@ public function getInventoryData($type)
$invAtt['stock_id'] = null;
} else {
$invAtt['stock_id'] = $this->inventorySource->execute($websiteCode);
$invAtt['inventory_source_items'] = (bool)$this->getStoreValue(self::XPATH_INVENTORY_SOURCE_ITEMS);
}

return $invAtt;
Expand Down
9 changes: 9 additions & 0 deletions Model/Config/System/ReturnsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ public function showOnCreditmemoCreation(int $storeId = null): bool
return $this->getStoreValue(self::XML_PATH_RETURNS_CREDITMEMO, $storeId)
&& $this->isReturnsEnabled($storeId);
}

/**
* @param int|null $storeId
* @return string
*/
public function getGtinAttribute(int $storeId = null): string
{
return $this->getStoreValue(self::XML_PATH_GTIN_ATTRIBUTE, (int)$storeId) ?: 'sku';
}
}
4 changes: 2 additions & 2 deletions Model/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public function generateByStore(

$config = $this->sourceHelper->getConfig($storeId, $type, $currency);
$productCollection = $this->productModel->getCollection($config, $page, $productIds);
$size = $this->productModel->getCollectionCountWithFilters($productCollection);
$size = $productCollection->getSize();

if (($config['filters']['limit'] > 0) && empty($productIds)) {
$productCollection->setPage($page, $config['filters']['limit'])->getCurPage();
$productCollection->setPage($page, $config['filters']['limit']);
$pages = ceil($size / $config['filters']['limit']);
}

Expand Down
24 changes: 20 additions & 4 deletions Service/Order/Shipping/CalculatePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ public function __construct(
*/
public function execute(Quote $quote, array $orderData, StoreInterface $store): float
{
$taxCalculation = $this->configProvider->getNeedsTaxCalulcation('shipping', (int)$store->getId());

$amount = (float)$orderData['price']['shipping'];
$baseCurrency = $this->storeManager->getStore($quote->getStoreId())->getBaseCurrencyCode();
if ($baseCurrency != $orderData['price']['currency']) {
if ($amount == 0) {
return $amount;
}

$baseCurrency = $this->getBaseCurrency($quote);
if ($baseCurrency && $baseCurrency != $orderData['price']['currency']) {
$rate = $this->priceManager->convert($amount, $quote->getStoreId()) / $amount;
$amount = $amount / $rate;
}

$taxCalculation = $this->configProvider->getNeedsTaxCalulcation('shipping', (int)$store->getId());
if (empty($taxCalculation)) {
$shippingAddress = $quote->getShippingAddress();
$billingAddress = $quote->getBillingAddress();
Expand All @@ -87,4 +90,17 @@ public function execute(Quote $quote, array $orderData, StoreInterface $store):

return $amount;
}

/**
* @param Quote $quote
* @return string|null
*/
private function getBaseCurrency(Quote $quote): ?string
{
try {
return $this->storeManager->getStore($quote->getStoreId())->getBaseCurrencyCode();
} catch (\Exception $exception) {
return null;
}
}
}
38 changes: 35 additions & 3 deletions Service/Product/InventoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class InventoryData
* @var array
*/
private $inventory;

/**
* @var array
*/
private $inventorySourceItems;
/**
* @var array
*/
Expand All @@ -45,7 +48,6 @@ public function __construct(
*
* @param array $skus
* @param int $stockId
*
* @return void
*/
private function getInventoryData(array $skus, int $stockId): void
Expand All @@ -67,6 +69,31 @@ private function getInventoryData(array $skus, int $stockId): void
}
}

/**
* Get Inventory Data by SKU and StockID
*
* @param array $skus
* @return void
*/
private function getInventorySourceItems(array $skus): void
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('inventory_source_item');

if (!$connection->isTableExists($tableName)) {
return;
}

$select = $connection->select()
->from($tableName)
->where('sku IN (?)', $skus);

$inventoryData = $connection->fetchAll($select);
foreach ($inventoryData as $data) {
$this->inventorySourceItems[$data['sku']][$data['source_code']] = $data['quantity'];
}
}

/**
* Returns number of reservations by SKU & StockId
*
Expand Down Expand Up @@ -108,6 +135,9 @@ public function load(array $skus, array $config): void
if (isset($config['inventory']['stock_id'])) {
$this->getInventoryData($skus, (int)$config['inventory']['stock_id']);
$this->getReservations($skus, (int)$config['inventory']['stock_id']);
if (!empty($config['inventory']['inventory_source_items'])) {
$this->getInventorySourceItems($skus);
}
}
}

Expand All @@ -129,12 +159,14 @@ public function addDataToProduct(Product $product, array $config): Product

$inventoryData = $this->inventory[$config['inventory']['stock_id']][$product->getSku()] ?? [];
$reservations = $this->reservation[$config['inventory']['stock_id']][$product->getSku()] ?? 0;
$sourceItems = $this->inventorySourceItems[$product->getSku()] ?? [];

$qty = isset($inventoryData['quantity']) ? $inventoryData['quantity'] - $reservations : 0;
$isSalable = $inventoryData['is_salable'] ?? 0;

return $product->setQty($qty)
->setIsSalable($isSalable)
->setIsInStock($isSalable);
->setIsInStock($isSalable)
->setInventorySourceItems($sourceItems);
}
}
79 changes: 75 additions & 4 deletions Service/Returns/GetByOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,60 @@

namespace Magmodules\Channable\Service\Returns;

use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Sales\Model\Order;
use Magmodules\Channable\Api\Config\RepositoryInterface as ConfigProvider;
use Magmodules\Channable\Api\Log\RepositoryInterface as LogRepository;
use Magmodules\Channable\Api\Returns\Data\DataInterface as ReturnsDataInterface;
use Magmodules\Channable\Api\Returns\RepositoryInterface as ReturnsRepository;

class GetByOrder
{

/**
* @var string
*/
private $gtinAttribute;
/**
* @var ReturnsRepository
*/
private $returnsRepository;
/**
* @var ProductFactory
*/
private $productFactory;
/**
* @var ConfigProvider
*/
private $configProvider;
/**
* @var LogRepository
*/
private $logRepository;
/**
* @var Json
*/
private $json;

/**
* @param ReturnsRepository $returnsRepository
* @param ProductFactory $productFactory
* @param ConfigProvider $configProvider
* @param LogRepository $logRepository
* @param Json $json
*/
public function __construct(
ReturnsRepository $returnsRepository,
ProductFactory $productFactory,
ConfigProvider $configProvider,
LogRepository $logRepository,
Json $json
) {
$this->returnsRepository = $returnsRepository;
$this->productFactory = $productFactory;
$this->configProvider = $configProvider;
$this->logRepository = $logRepository;
$this->json = $json;
}

Expand All @@ -54,8 +82,9 @@ public function execute(Order $order): ?array
}

$returnsArray = [];
/** @var ReturnsDataInterface $return */
foreach ($returns as $return) {
if (!$sku = $this->getSkuFromReturnData($return->getItem())) {
if (!$sku = $this->getSkuFromReturnData($return->getItem(), (int)$return->getStoreId())) {
continue;
}
$returnsArray[$sku] = $return;
Expand All @@ -66,19 +95,61 @@ public function execute(Order $order): ?array

/**
* @param $itemData
* @param int $storeId
* @return string|null
*/
private function getSkuFromReturnData($itemData): ?string
private function getSkuFromReturnData($itemData, int $storeId): ?string
{
if (is_array($itemData)) {
return $itemData['gtin'] ?? null;
return $this->getSkuFromGtin($itemData['gtin'] ?? null, $storeId);
}

try {
$itemData = $this->json->unserialize($itemData);
return $itemData['gtin'] ?? null;
return $this->getSkuFromGtin($itemData['gtin'] ?? null, $storeId);
} catch (\Exception $exception) {
return null;
}
}

/**
* @param string $gtin
* @param int $storeId
* @return string|null
*/
private function getSkuFromGtin(string $gtin, int $storeId): ?string
{
$gtinAttribute = $this->getGtinAttributeCode($storeId);
if ($gtinAttribute == 'sku' || $gtinAttribute == null) {
return $gtin;
}

try {
if ($gtinAttribute == 'id') {
if ($product = $this->productFactory->create()->load($gtin)) {
return $product->getSku();
}
}
if ($product = $this->productFactory->create()->loadByAttribute($gtinAttribute, $gtin)) {
return $product->getSku();
}
} catch (\Exception $exception) {
$this->logRepository->addErrorLog('getSkuFromGtin', $exception->getMessage());
}

return $gtin;
}

/**
* @param int $storeId
* @return string
*/
private function getGtinAttributeCode(int $storeId): string
{
if (!$this->gtinAttribute) {
$this->gtinAttribute = $this->configProvider->getGtinAttribute($storeId);
}

return $this->gtinAttribute;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magmodules/magento2-channable",
"description": "Channable integration for Magento 2",
"type": "magento2-module",
"version": "1.17.0",
"version": "1.18.0",
"license": "BSD-2-Clause",
"homepage": "https://github.com/magmodules/magento2-channable",
"require": {
Expand Down
8 changes: 8 additions & 0 deletions etc/adminhtml/system/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@
<field id="inventory">1</field>
</depends>
</field>
<field id="inventory_source_items" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Include Inventory Sources</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>magmodules_channable/advanced/inventory_source_items</config_path>
<depends>
<field id="inventory">1</field>
</depends>
</field>
<field id="heading_delivery" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery Time</label>
<frontend_model>Magmodules\Channable\Block\Adminhtml\Design\Heading</frontend_model>
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<general>
<enable>0</enable>
<limit>250</limit>
<version>v1.17.0</version>
<version>v1.18.0</version>
</general>
<data>
<name_attribute>name</name_attribute>
Expand Down
12 changes: 12 additions & 0 deletions view/adminhtml/layout/sales_order_creditmemo_updateqty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="submit_before">
<block class="Magmodules\Channable\Block\Adminhtml\Order\Creditmemo\View\Returns" name="channable_returns" template="Magmodules_Channable::order/creditmemo/view/returns.phtml"/>
</referenceContainer>
</page>

0 comments on commit c7178e3

Please sign in to comment.