Skip to content

Commit

Permalink
Merge pull request #18 from Progi1984/stockMovents
Browse files Browse the repository at this point in the history
Added option `stockMovements` for the command `prestashop:product-creator` (with combinations)
  • Loading branch information
Progi1984 authored Jan 10, 2025
2 parents 065d6f3 + 13bb082 commit 5a1f894
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Command/ProductCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfFeatures,
$numberOfFeatureValues,
$numberOfImages,
$numberOfStockMovements,
$shopId
);
$output->writeln(sprintf('%s product(s) with combinations created', $productsWithCombinations));
Expand Down
4 changes: 2 additions & 2 deletions src/Creator/AbstractProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ protected function associateFeatureValues(int $productId, int $featureId, array
}
}

protected function associateStockMovements(int $productId, int $numberOfStockMovements): void
protected function associateStockMovements(int $productId, array $combinationsId, int $numberOfStockMovements): void
{
if ($numberOfStockMovements <= 0) {
return;
}

$this->stockMovementCreator->generate($numberOfStockMovements, $productId);
$this->stockMovementCreator->generate($numberOfStockMovements, $productId, $combinationsId);
}

protected function associateImages(int $productId, array $combinationsId, int $numberOfImages): void
Expand Down
2 changes: 2 additions & 0 deletions src/Creator/ProductCombinationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function generate(
int $numberOfFeatures,
int $numberOfFeatureValues,
int $numberOfImages,
int $numberOfStockMovements,
int $shopId
): void {
$attributeGroups = $this->getAttributeGroupWithAtLeast($attributeValuePerGroupNumber);
Expand Down Expand Up @@ -107,6 +108,7 @@ public function generate(
}, $combinationsIds);

$this->associateImages($newProductId->getValue(), $combinationsIds, $numberOfImages);
$this->associateStockMovements($newProductId->getValue(), $combinationsIds, $numberOfStockMovements);
$this->associateFeatures($newProductId->getValue(), $numberOfFeatures, $numberOfFeatureValues, $shopId);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Creator/ProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function generate(
for ($i = 0; $i < $number; ++$i) {
$productId = $this->createProduct($shopId);
$this->associateImages($productId, [], $numberOfImages);
$this->associateStockMovements($productId, $numberOfStockMovements);
$this->associateStockMovements($productId, [null], $numberOfStockMovements);
$this->associateFeatures($productId, $numberOfFeatures, $numberOfFeatureValues, $shopId);
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/Creator/StockMovementCreator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PsFixturesCreator\Creator;

use DateTime;
Expand All @@ -25,26 +27,28 @@ public function __construct(
$this->employee = new Employee(1);
}

public function generate(int $number, int $productId): void
public function generate(int $number, int $productId, array $combinationsId = []): void
{
// Start
$qtyProduct = 500;
foreach ($combinationsId as $combinationId) {
// Start
$qtyProduct = 500;

StockAvailable::setQuantity($productId, 0, $qtyProduct, null, false);
StockAvailable::setQuantity($productId, $combinationId, $qtyProduct, null, false);

for ($i = 0; $i < $number; ++$i) {
$deltaQuantity = rand(-10, 10);
for ($i = 0; $i < $number; ++$i) {
$deltaQuantity = rand(-10, 10);

$qtyProduct += $deltaQuantity;
$this->createStockMovement($productId, $deltaQuantity);
}
$qtyProduct += $deltaQuantity;
$this->createStockMovement($productId, $combinationId, $deltaQuantity);
}

StockAvailable::setQuantity($productId, 0, $qtyProduct, null, false);
StockAvailable::setQuantity($productId, $combinationId, $qtyProduct, null, false);
}
}

public function createStockMovement(int $productId, int $deltaQuantity): void
public function createStockMovement(int $productId, ?int $combinationId, int $deltaQuantity): void
{
$stockAvailable = $this->stockManager->getStockAvailableByProduct(new Product($productId));
$stockAvailable = $this->stockManager->getStockAvailableByProduct(new Product($productId), $combinationId);

$stockMvt = new StockMvt();
$stockMvt->setIdStock((int) $stockAvailable->id);
Expand Down

0 comments on commit 5a1f894

Please sign in to comment.