Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option stockMovements for the command prestashop:product-creator (with combinations) #18

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading