Skip to content

Commit 13025f2

Browse files
authored
Merge pull request #15 from Progi1984/stockMovements
Added option `stockMovements` for the command `prestashop:product-creator`
2 parents fe64a18 + 8c96e22 commit 13025f2

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

config/services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ services:
2020
arguments:
2121
$langRepository: '@prestashop.core.admin.lang.repository'
2222
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
23+
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
2324
$connection: '@doctrine.dbal.default_connection'
2425
$dbPrefix: '%database_prefix%'
2526
$faker: '@Faker\Generator'
@@ -47,6 +48,7 @@ services:
4748
$attributeCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\AttributeCreator'
4849
$langRepository: '@prestashop.core.admin.lang.repository'
4950
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
51+
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
5052
$connection: '@doctrine.dbal.default_connection'
5153
$dbPrefix: '%database_prefix%'
5254
$faker: '@Faker\Generator'
@@ -64,6 +66,11 @@ services:
6466
tags:
6567
- { name: 'console.command' }
6668

69+
PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator:
70+
arguments:
71+
$stockManager: '@prestashop.adapter.stock_manager'
72+
$stockMvtRepository: '@prestashop.core.api.stock_movement.repository'
73+
6774
PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand:
6875
class: PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand
6976
arguments:

src/Command/ProductCreatorCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected function configure(): void
6767
->addOption('attributes', null, InputOption::VALUE_OPTIONAL, 'Number of attributes per attribute group', 5)
6868
->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features per product', 2)
6969
->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 5)
70+
->addOption('stockMovements', null, InputOption::VALUE_OPTIONAL, 'Number of stock movements per product', 0)
7071
;
7172
}
7273

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

8587
// create products
8688
if (!empty($numberOfProducts)) {
87-
$this->productCreator->generate($numberOfProducts, $numberOfFeatures, $numberOfFeatureValues, $shopId);
89+
$this->productCreator->generate(
90+
$numberOfProducts,
91+
$numberOfFeatures,
92+
$numberOfFeatureValues,
93+
$numberOfStockMovements,
94+
$shopId
95+
);
8896
$output->writeln(sprintf('%s product(s) created', $numberOfProducts));
8997
}
9098

src/Creator/AbstractProductCreator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@
3333
abstract class AbstractProductCreator
3434
{
3535
protected FeatureCreator $featureCreator;
36+
protected StockMovementCreator $stockMovementCreator;
3637
protected Connection $connection;
3738
protected string $dbPrefix;
3839

3940
public function __construct(
4041
FeatureCreator $featureCreator,
42+
StockMovementCreator $stockMovementCreator,
4143
Connection $connection,
4244
string $dbPrefix
4345
) {
4446
$this->featureCreator = $featureCreator;
47+
$this->stockMovementCreator = $stockMovementCreator;
4548
$this->connection = $connection;
4649
$this->dbPrefix = $dbPrefix;
4750
}
@@ -75,6 +78,15 @@ protected function associateFeatureValues(int $productId, int $featureId, array
7578
}
7679
}
7780

81+
protected function associateStockMovements(int $productId, int $numberOfStockMovements): void
82+
{
83+
if ($numberOfStockMovements <= 0) {
84+
return;
85+
}
86+
87+
$this->stockMovementCreator->generate($numberOfStockMovements, $productId);
88+
}
89+
7890
protected function getRandomValues(int $featureId, int $numberOfFeatureValues): array
7991
{
8092
$featureValueIds = $this->connection->createQueryBuilder()

src/Creator/ProductCombinationCreator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public function __construct(
3737
FeatureCreator $featureCreator,
3838
Connection $connection,
3939
string $dbPrefix,
40-
Generator $faker
40+
Generator $faker,
41+
StockMovementCreator $stockMovementCreator
4142
) {
42-
parent::__construct($featureCreator, $connection, $dbPrefix);
43+
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
4344
$this->entityManager = $entityManager;
4445
$this->commandBus = $commandBus;
4546
$this->attributeCreator = $attributeCreator;

src/Creator/ProductCreator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ public function __construct(
1818
FeatureCreator $featureCreator,
1919
Connection $connection,
2020
string $dbPrefix,
21-
Faker $faker
21+
Faker $faker,
22+
StockMovementCreator $stockMovementCreator
2223
) {
23-
parent::__construct($featureCreator, $connection, $dbPrefix);
24+
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
2425
$this->langRepository = $langRepository;
2526
$this->faker = $faker;
2627
}
2728

28-
public function generate(int $number, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId): void
29-
{
29+
public function generate(
30+
int $number,
31+
int $numberOfFeatures,
32+
int $numberOfFeatureValues,
33+
int $numberOfStockMovements,
34+
int $shopId
35+
): void {
3036
for ($i = 0; $i < $number; ++$i) {
3137
$productId = $this->createProduct($shopId);
38+
$this->associateStockMovements($productId, $numberOfStockMovements);
3239
$this->associateFeatures($productId, $numberOfFeatures, $numberOfFeatureValues, $shopId);
3340
}
3441
}

src/Creator/StockMovementCreator.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace PrestaShop\Module\PsFixturesCreator\Creator;
4+
5+
use DateTime;
6+
use Employee;
7+
use PrestaShop\PrestaShop\Adapter\StockManager;
8+
use PrestaShopBundle\Entity\Repository\StockMovementRepository;
9+
use PrestaShopBundle\Entity\StockMvt;
10+
use Product;
11+
use StockAvailable;
12+
13+
class StockMovementCreator
14+
{
15+
protected StockManager $stockManager;
16+
protected StockMovementRepository $stockMvtRepository;
17+
protected Employee $employee;
18+
19+
public function __construct(
20+
StockManager $stockManager,
21+
StockMovementRepository $stockMvtRepository
22+
) {
23+
$this->stockManager = $stockManager;
24+
$this->stockMvtRepository = $stockMvtRepository;
25+
$this->employee = new Employee(1);
26+
}
27+
28+
public function generate(int $number, int $productId): void
29+
{
30+
// Start
31+
$qtyProduct = 500;
32+
33+
StockAvailable::setQuantity($productId, 0, $qtyProduct, null, false);
34+
35+
for ($i = 0; $i < $number; ++$i) {
36+
$deltaQuantity = rand(-10, 10);
37+
38+
$qtyProduct += $deltaQuantity;
39+
$this->createStockMovement($productId, $deltaQuantity);
40+
}
41+
42+
StockAvailable::setQuantity($productId, 0, $qtyProduct, null, false);
43+
}
44+
45+
public function createStockMovement(int $productId, int $deltaQuantity): void
46+
{
47+
$stockAvailable = $this->stockManager->getStockAvailableByProduct(new Product($productId));
48+
49+
$stockMvt = new StockMvt();
50+
$stockMvt->setIdStock((int) $stockAvailable->id);
51+
$stockMvt->setIdEmployee($this->employee->id);
52+
$stockMvt->setEmployeeFirstname($this->employee->firstname);
53+
$stockMvt->setEmployeeLastname($this->employee->lastname);
54+
$stockMvt->setSign($deltaQuantity >= 1 ? 1 : -1);
55+
$stockMvt->setPhysicalQuantity(abs($deltaQuantity));
56+
$stockMvt->setDateAdd(new DateTime());
57+
58+
$this->stockMvtRepository->saveStockMvt($stockMvt);
59+
}
60+
}

0 commit comments

Comments
 (0)