Skip to content

Commit

Permalink
Fix update stock
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienPapet committed Jul 25, 2022
1 parent fd4448c commit c496c9b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
Expand Up @@ -97,7 +97,8 @@ public function handle(UpdateCombinationStockCommand $command): void
$command->getLocation(),
$command->getLowStockThreshold(),
$command->getLowStockAlertEnabled(),
$command->getAvailableDate()
$command->getAvailableDate(),
$command->getOutOfStockType()
);

$this->combinationStockUpdater->update($command->getCombinationId(), $properties);
Expand Down
Expand Up @@ -38,7 +38,9 @@
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\Exception\CannotDeleteCombinationException;
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\Exception\CombinationNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\ValueObject\CombinationId;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\OutOfStockType;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductId;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
use PrestaShop\PrestaShop\Core\Grid\Query\ProductCombinationQueryBuilder;
use PrestaShop\PrestaShop\Core\Repository\AbstractObjectModelRepository;
Expand Down Expand Up @@ -366,4 +368,19 @@ public function getCombinationIdsByAttributes(ProductId $productId, array $attri
return new CombinationId((int) $combination['id_product_attribute']);
}, $result);
}

public function updateCombinationStock(ProductId $productId, OutOfStockType $outOfStockType, ShopConstraint $shopConstraint): void
{
$qb = $this->connection->createQueryBuilder();
$qb
->update(sprintf('%sstock_available', $this->dbPrefix), 'ps')
->set('ps.out_of_stock', (string) $outOfStockType->getValue())
->where('ps.id_product = :productId')
->andWhere('id_attribute != 0')
->andWhere('id_shop = :shop')
->setParameter('productId', $productId->getValue())
->setParameter('shop', $shopConstraint->getShopId())
->execute()
;
}
}
Expand Up @@ -28,6 +28,7 @@
namespace PrestaShop\PrestaShop\Adapter\Product\Combination\Update;

use DateTimeInterface;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\OutOfStockType;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\StockModification;

class CombinationStockProperties
Expand Down Expand Up @@ -62,6 +63,11 @@ class CombinationStockProperties
*/
private $availableDate;

/**
* @var OutOfStockType|null
*/
private $outOfStockType;

/**
* @param StockModification|null $stockModification
* @param int|null $minimalQuantity
Expand All @@ -76,15 +82,16 @@ public function __construct(
?string $location = null,
?int $lowStockThreshold = null,
?bool $lowStockAlertEnabled = null,
?DateTimeInterface $availableDate = null
?DateTimeInterface $availableDate = null,
?OutOfStockType $outOfStockType = null
) {
$this->stockModification = $stockModification;
$this->minimalQuantity = $minimalQuantity;
$this->location = $location;
$this->lowStockThreshold = $lowStockThreshold;
$this->lowStockAlertEnabled = $lowStockAlertEnabled;
$this->availableDate = $availableDate;
$this->stockModification = $stockModification;
$this->outOfStockType = $outOfStockType;
}

/**
Expand Down Expand Up @@ -134,4 +141,12 @@ public function getAvailableDate(): ?DateTimeInterface
{
return $this->availableDate;
}

/**
* @return OutOfStockType|null
*/
public function getOutOfStockType(): ?OutOfStockType
{
return $this->outOfStockType;
}
}
Expand Up @@ -124,6 +124,11 @@ private function fillUpdatableProperties(Combination $combination, CombinationSt
$updatableProperties[] = 'location';
}

if (null !== $properties->getOutOfStockType()) {
$combination->location = $properties->getOutOfStockType()->getValue();
$updatableProperties[] = 'out_of_stock';
}

return $updatableProperties;
}

Expand Down
Expand Up @@ -28,6 +28,7 @@

namespace PrestaShop\PrestaShop\Adapter\Product\Stock\CommandHandler;

use PrestaShop\PrestaShop\Adapter\Product\Combination\Repository\CombinationRepository;
use PrestaShop\PrestaShop\Adapter\Product\Stock\Repository\MovementReasonRepository;
use PrestaShop\PrestaShop\Adapter\Product\Stock\Update\ProductStockProperties;
use PrestaShop\PrestaShop\Adapter\Product\Stock\Update\ProductStockUpdater;
Expand All @@ -49,17 +50,23 @@ final class UpdateProductStockInformationHandler implements UpdateProductStockIn
* @var MovementReasonRepository
*/
private $movementReasonRepository;
/**
* @var CombinationRepository
*/
private $combinationRepository;

/**
* @param ProductStockUpdater $productStockUpdater
* @param MovementReasonRepository $movementReasonRepository
*/
public function __construct(
ProductStockUpdater $productStockUpdater,
MovementReasonRepository $movementReasonRepository
MovementReasonRepository $movementReasonRepository,
CombinationRepository $combinationRepository
) {
$this->productStockUpdater = $productStockUpdater;
$this->movementReasonRepository = $movementReasonRepository;
$this->combinationRepository = $combinationRepository;
}

/**
Expand Down Expand Up @@ -91,5 +98,9 @@ public function handle(UpdateProductStockInformationCommand $command): void
),
$command->getShopConstraint()
);

if (null !== $command->getOutOfStockType()) {
$this->combinationRepository->updateCombinationStock($command->getProductId(), $command->getOutOfStockType(), $command->getShopConstraint());
}
}
}
Expand Up @@ -31,6 +31,7 @@
use DateTimeInterface;
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\ValueObject\CombinationId;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\Exception\ProductStockConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\OutOfStockType;

/**
* Updates combination stock information
Expand Down Expand Up @@ -77,6 +78,11 @@ class UpdateCombinationStockCommand
*/
private $availableDate;

/**
* @var OutOfStockType|null
*/
private $outOfStockType;

/**
* @param int $combinationId
*/
Expand Down Expand Up @@ -249,4 +255,19 @@ public function getLowStockAlertEnabled(): ?bool
{
return $this->lowStockAlertEnabled;
}

public function setOutOfStockType(?OutOfStockType $outOfStockType): UpdateCombinationStockCommand
{
$this->outOfStockType = $outOfStockType;

return $this;
}

/**
* @return OutOfStockType|null
*/
public function getOutOfStockType(): ?OutOfStockType
{
return $this->outOfStockType;
}
}
Expand Up @@ -35,6 +35,7 @@ services:
arguments:
- '@prestashop.adapter.product.stock.update.product_stock_updater'
- '@prestashop.adapter.product.stock.repository.movement_reason_repository'
- '@prestashop.adapter.product.combination.repository.combination_repository'
tags:
- name: tactician.handler
command: PrestaShop\PrestaShop\Core\Domain\Product\Stock\Command\UpdateProductStockInformationCommand
Expand Down

0 comments on commit c496c9b

Please sign in to comment.