Skip to content

Commit

Permalink
Improve copy of combinations stocks from shop to shop
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Dec 14, 2022
1 parent 2ad2d95 commit 7afbebd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
10 changes: 8 additions & 2 deletions src/Adapter/Product/Stock/Update/ProductStockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ private function fillUpdatableProperties(
$updatableProperties[] = 'pack_stock_type';
}
if (null !== $properties->getStockModification()) {
$product->quantity = $stockAvailable->quantity + $properties->getStockModification()->getDeltaQuantity();
$product->quantity = $properties->getStockModification()->getDeltaQuantity() !== null ?
$stockAvailable->quantity + $properties->getStockModification()->getDeltaQuantity() :
$properties->getStockModification()->getFixedQuantity()
;
$updatableProperties[] = 'quantity';
}
if (null !== $properties->getAvailableDate()) {
Expand Down Expand Up @@ -305,7 +308,10 @@ private function updateStockAvailable(StockAvailable $stockAvailable, ProductSto
}

if ($properties->getStockModification()) {
$stockAvailable->quantity += $properties->getStockModification()->getDeltaQuantity();
$stockAvailable->quantity = $properties->getStockModification()->getDeltaQuantity() !== null ?
$stockAvailable->quantity + $properties->getStockModification()->getDeltaQuantity() :
$properties->getStockModification()->getFixedQuantity()
;
$stockUpdateRequired = true;
}

Expand Down
68 changes: 57 additions & 11 deletions src/Adapter/Product/Update/ProductShopUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

namespace PrestaShop\PrestaShop\Adapter\Product\Update;

use PrestaShop\PrestaShop\Adapter\Product\Combination\Repository\CombinationMultiShopRepository;
use PrestaShop\PrestaShop\Adapter\Product\Combination\Update\CombinationStockProperties;
use PrestaShop\PrestaShop\Adapter\Product\Combination\Update\CombinationStockUpdater;
use PrestaShop\PrestaShop\Adapter\Product\Image\Repository\ProductImageMultiShopRepository;
use PrestaShop\PrestaShop\Adapter\Product\Repository\ProductMultiShopRepository;
use PrestaShop\PrestaShop\Adapter\Product\Stock\Repository\StockAvailableMultiShopRepository;
Expand All @@ -40,6 +43,7 @@
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\OutOfStockType;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\StockModification;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductId;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductType;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId;
use Product;
Expand Down Expand Up @@ -75,24 +79,31 @@ class ProductShopUpdater
private $productStockUpdater;

/**
* @param ProductMultiShopRepository $productRepository
* @param StockAvailableMultiShopRepository $stockAvailableRepository
* @param ShopRepository $shopRepository
* @param ProductImageMultiShopRepository $productImageMultiShopRepository
* @param ProductStockUpdater $productStockUpdater
* @var CombinationMultiShopRepository
*/
private $combinationRepository;

/**
* @var CombinationStockUpdater
*/
private $combinationStockUpdater;

public function __construct(
ProductMultiShopRepository $productRepository,
StockAvailableMultiShopRepository $stockAvailableRepository,
ShopRepository $shopRepository,
ProductImageMultiShopRepository $productImageMultiShopRepository,
ProductStockUpdater $productStockUpdater
ProductStockUpdater $productStockUpdater,
CombinationMultiShopRepository $combinationMultiShopRepository,
CombinationStockUpdater $combinationStockUpdater
) {
$this->productRepository = $productRepository;
$this->stockAvailableRepository = $stockAvailableRepository;
$this->shopRepository = $shopRepository;
$this->productImageMultiShopRepository = $productImageMultiShopRepository;
$this->productStockUpdater = $productStockUpdater;
$this->combinationRepository = $combinationMultiShopRepository;
$this->combinationStockUpdater = $combinationStockUpdater;
}

/**
Expand All @@ -113,15 +124,16 @@ public function copyToShop(ProductId $productId, ShopId $sourceShopId, ShopId $t
CannotUpdateProductException::FAILED_SHOP_COPY
);

$this->copyStockToShop($productId, $sourceShopId, $targetShopId);
$this->copyStockToShop($productId, $sourceShopId, $targetShopId, $sourceProduct->getProductType());
$this->copyCarriersToShop($sourceProduct, $targetShopId);
$this->copyImageAssociations($productId, $sourceShopId, $targetShopId);
}

private function copyStockToShop(ProductId $productId, ShopId $sourceShopId, ShopId $targetShopId): void
private function copyStockToShop(ProductId $productId, ShopId $sourceShopId, ShopId $targetShopId, string $productType): void
{
// First get the source stock
$sourceStock = $this->stockAvailableRepository->getForProduct($productId, $sourceShopId);
$outOfStock = new OutOfStockType((int) $sourceStock->out_of_stock);

// Then try to get the target stock, if it doesn't exist create it
try {
Expand All @@ -131,17 +143,51 @@ private function copyStockToShop(ProductId $productId, ShopId $sourceShopId, Sho
}

$deltaQuantity = (int) $sourceStock->quantity - (int) $targetStock->quantity;
if ($deltaQuantity !== 0) {
$stockModification = StockModification::buildDeltaQuantity($deltaQuantity);
if ($deltaQuantity !== 0 || (int) $sourceStock->out_of_stock !== (int) $targetStock->out_of_stock || $sourceStock->location !== $targetStock->location) {
$stockModification = StockModification::buildFixedQuantity((int) $sourceStock->quantity);
$stockProperties = new ProductStockProperties(
null,
$stockModification,
new OutOfStockType((int) $sourceStock->out_of_stock),
$outOfStock,
null,
$sourceStock->location
);
$this->productStockUpdater->update($productId, $stockProperties, ShopConstraint::shop($targetShopId->getValue()));
}

if ($productType === ProductType::TYPE_COMBINATIONS) {
$this->copyCombinationsStockToShop($productId, $sourceShopId, $targetShopId, $outOfStock);
}
}

private function copyCombinationsStockToShop(ProductId $productId, ShopId $sourceShopId, ShopId $targetShopId, OutOfStockType $outOfStockType): void
{
$sourceCombinations = $this->combinationRepository->getCombinationIds($productId, ShopConstraint::shop($targetShopId->getValue()));
$targetConstraint = ShopConstraint::shop($targetShopId->getValue());

foreach ($sourceCombinations as $combinationId) {
// First get the source stock
$sourceStock = $this->stockAvailableRepository->getForCombination($combinationId, $sourceShopId);

// Then try to get the target stock, if it doesn't exist create it
try {
$targetStock = $this->stockAvailableRepository->getForCombination($combinationId, $targetShopId);
} catch (StockAvailableNotFoundException $e) {
$targetStock = $this->stockAvailableRepository->createStockAvailable($productId, $targetShopId, $combinationId);
}

$deltaQuantity = (int) $sourceStock->quantity - (int) $targetStock->quantity;
if ($deltaQuantity !== 0) {
$stockModification = StockModification::buildDeltaQuantity($deltaQuantity);
$stockProperties = new CombinationStockProperties(
$stockModification,
null,
$sourceStock->location
);
$this->combinationStockUpdater->update($combinationId, $stockProperties, $targetConstraint);
}
}
$this->combinationRepository->updateCombinationOutOfStockType($productId, $outOfStockType, $targetConstraint);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ services:
- '@prestashop.adapter.shop.repository.shop_repository'
- '@PrestaShop\PrestaShop\Adapter\Product\Image\Repository\ProductImageMultiShopRepository'
- '@prestashop.adapter.product.stock.update.product_stock_updater'
- '@PrestaShop\PrestaShop\Adapter\Product\Combination\Repository\CombinationMultiShopRepository'
- '@PrestaShop\PrestaShop\Adapter\Product\Combination\Update\CombinationStockUpdater'

prestashop.adapter.product.command_handler.update_product_handler:
class: PrestaShop\PrestaShop\Adapter\Product\CommandHandler\UpdateProductHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ services:
- '@prestashop.core.stock.stock_manager'
- '@prestashop.adapter.legacy.configuration'

prestashop.adapter.product.combination.update.combination_stock_updater:
alias: PrestaShop\PrestaShop\Adapter\Product\Combination\Update\CombinationStockUpdater
deprecated: ~

prestashop.adapter.product.combination.create.combination_creator:
class: PrestaShop\PrestaShop\Adapter\Product\Combination\Create\CombinationCreator
arguments:
Expand Down

0 comments on commit 7afbebd

Please sign in to comment.