-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Progi1984/stockMovements
Added option `stockMovements` for the command `prestashop:product-creator`
- Loading branch information
Showing
6 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |