Skip to content

Commit

Permalink
Merge pull request #15 from Progi1984/stockMovements
Browse files Browse the repository at this point in the history
Added option `stockMovements` for the command `prestashop:product-creator`
  • Loading branch information
Progi1984 authored Jan 9, 2025
2 parents fe64a18 + 8c96e22 commit 13025f2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
7 changes: 7 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
arguments:
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
$faker: '@Faker\Generator'
Expand Down Expand Up @@ -47,6 +48,7 @@ services:
$attributeCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\AttributeCreator'
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
$faker: '@Faker\Generator'
Expand All @@ -64,6 +66,11 @@ services:
tags:
- { name: 'console.command' }

PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator:
arguments:
$stockManager: '@prestashop.adapter.stock_manager'
$stockMvtRepository: '@prestashop.core.api.stock_movement.repository'

PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand:
class: PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand
arguments:
Expand Down
10 changes: 9 additions & 1 deletion src/Command/ProductCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function configure(): void
->addOption('attributes', null, InputOption::VALUE_OPTIONAL, 'Number of attributes per attribute group', 5)
->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features per product', 2)
->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 5)
->addOption('stockMovements', null, InputOption::VALUE_OPTIONAL, 'Number of stock movements per product', 0)
;
}

Expand All @@ -80,11 +81,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfAttributes = (int) $input->getOption('attributes');
$numberOfFeatures = (int) $input->getOption('features');
$numberOfFeatureValues = (int) $input->getOption('featureValues');
$numberOfStockMovements = (int) $input->getOption('stockMovements');
$productsWithCombinations = (int) $input->getOption('productsWithCombinations');

// create products
if (!empty($numberOfProducts)) {
$this->productCreator->generate($numberOfProducts, $numberOfFeatures, $numberOfFeatureValues, $shopId);
$this->productCreator->generate(
$numberOfProducts,
$numberOfFeatures,
$numberOfFeatureValues,
$numberOfStockMovements,
$shopId
);
$output->writeln(sprintf('%s product(s) created', $numberOfProducts));
}

Expand Down
12 changes: 12 additions & 0 deletions src/Creator/AbstractProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
abstract class AbstractProductCreator
{
protected FeatureCreator $featureCreator;
protected StockMovementCreator $stockMovementCreator;
protected Connection $connection;
protected string $dbPrefix;

public function __construct(
FeatureCreator $featureCreator,
StockMovementCreator $stockMovementCreator,
Connection $connection,
string $dbPrefix
) {
$this->featureCreator = $featureCreator;
$this->stockMovementCreator = $stockMovementCreator;
$this->connection = $connection;
$this->dbPrefix = $dbPrefix;
}
Expand Down Expand Up @@ -75,6 +78,15 @@ protected function associateFeatureValues(int $productId, int $featureId, array
}
}

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

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

protected function getRandomValues(int $featureId, int $numberOfFeatureValues): array
{
$featureValueIds = $this->connection->createQueryBuilder()
Expand Down
5 changes: 3 additions & 2 deletions src/Creator/ProductCombinationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function __construct(
FeatureCreator $featureCreator,
Connection $connection,
string $dbPrefix,
Generator $faker
Generator $faker,
StockMovementCreator $stockMovementCreator
) {
parent::__construct($featureCreator, $connection, $dbPrefix);
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
$this->entityManager = $entityManager;
$this->commandBus = $commandBus;
$this->attributeCreator = $attributeCreator;
Expand Down
15 changes: 11 additions & 4 deletions src/Creator/ProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ public function __construct(
FeatureCreator $featureCreator,
Connection $connection,
string $dbPrefix,
Faker $faker
Faker $faker,
StockMovementCreator $stockMovementCreator
) {
parent::__construct($featureCreator, $connection, $dbPrefix);
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
$this->langRepository = $langRepository;
$this->faker = $faker;
}

public function generate(int $number, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId): void
{
public function generate(
int $number,
int $numberOfFeatures,
int $numberOfFeatureValues,
int $numberOfStockMovements,
int $shopId
): void {
for ($i = 0; $i < $number; ++$i) {
$productId = $this->createProduct($shopId);
$this->associateStockMovements($productId, $numberOfStockMovements);
$this->associateFeatures($productId, $numberOfFeatures, $numberOfFeatureValues, $shopId);
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/Creator/StockMovementCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace PrestaShop\Module\PsFixturesCreator\Creator;

use DateTime;
use Employee;
use PrestaShop\PrestaShop\Adapter\StockManager;
use PrestaShopBundle\Entity\Repository\StockMovementRepository;
use PrestaShopBundle\Entity\StockMvt;
use Product;
use StockAvailable;

class StockMovementCreator
{
protected StockManager $stockManager;
protected StockMovementRepository $stockMvtRepository;
protected Employee $employee;

public function __construct(
StockManager $stockManager,
StockMovementRepository $stockMvtRepository
) {
$this->stockManager = $stockManager;
$this->stockMvtRepository = $stockMvtRepository;
$this->employee = new Employee(1);
}

public function generate(int $number, int $productId): void
{
// Start
$qtyProduct = 500;

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

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

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

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

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

$stockMvt = new StockMvt();
$stockMvt->setIdStock((int) $stockAvailable->id);
$stockMvt->setIdEmployee($this->employee->id);
$stockMvt->setEmployeeFirstname($this->employee->firstname);
$stockMvt->setEmployeeLastname($this->employee->lastname);
$stockMvt->setSign($deltaQuantity >= 1 ? 1 : -1);
$stockMvt->setPhysicalQuantity(abs($deltaQuantity));
$stockMvt->setDateAdd(new DateTime());

$this->stockMvtRepository->saveStockMvt($stockMvt);
}
}

0 comments on commit 13025f2

Please sign in to comment.