This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Helper;
|
||||
|
||||
use App\Entity\Warehouse;
|
||||
use App\Repository\ProductRepository;
|
||||
use App\Repository\StockRepository;
|
||||
use App\Repository\WarehouseRepository;
|
||||
use ArrayIterator;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Exception;
|
||||
use League\Csv\Writer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@@ -17,12 +19,13 @@ class Jtl
|
||||
private $stockRepository;
|
||||
private $warehouseRepository;
|
||||
private $logger;
|
||||
private $rootPath;
|
||||
|
||||
/**
|
||||
* @var string[] $arrLager
|
||||
* Lagernamen aus der JTL Wawi
|
||||
*/
|
||||
private $arrLager = array(
|
||||
private array $arrLager = array(
|
||||
1 => 'Lager 1 - Standardlager',
|
||||
3 => 'Lager 3 - DLX',
|
||||
5 => 'Lager 5 - Verkaufsfilale',
|
||||
@@ -30,74 +33,123 @@ class Jtl
|
||||
10 => 'Lager 10 - OJ EF',
|
||||
);
|
||||
|
||||
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger)
|
||||
/**
|
||||
* @param ProductRepository $productRepository
|
||||
* @param WarehouseRepository $warehouseRepository
|
||||
* @param StockRepository $stockRepository
|
||||
* @param LoggerInterface $logger
|
||||
* @param string $rootPath
|
||||
*/
|
||||
public function __construct(
|
||||
ProductRepository $productRepository,
|
||||
WarehouseRepository $warehouseRepository,
|
||||
StockRepository $stockRepository,
|
||||
LoggerInterface $logger,
|
||||
string $rootPath)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
$this->warehouseRepository = $warehouseRepository;
|
||||
$this->stockRepository = $stockRepository;
|
||||
$this->logger = $logger;
|
||||
$this->rootPath = $rootPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt alle Produkte und deren Lagerbestände
|
||||
* @param array $warehousesName
|
||||
* @return array
|
||||
*/
|
||||
public function getProducts(int $warehouseId): array
|
||||
public function getProducts(array $warehousesName): array
|
||||
{
|
||||
|
||||
$r = $this->productRepository->findAll();
|
||||
$data = array();
|
||||
$products = $this->productRepository->findAll();
|
||||
|
||||
foreach ($r as $product) {
|
||||
foreach ($warehousesName as $wn) {
|
||||
$warehouse = $this->warehouseRepository->findByWarehouseByName($wn);
|
||||
}
|
||||
|
||||
if ($warehouseId != 0) {
|
||||
$stock = $this->stockRepository->findBy(['product_id' => $product->getId(), 'warehouse_id' => $warehouseId]);
|
||||
} else {
|
||||
$stock = $this->stockRepository->findBy(['product_id' => $product->getId()]);
|
||||
}
|
||||
if ($warehouse) {
|
||||
foreach ($warehouse as $w) {
|
||||
|
||||
if ($stock) {
|
||||
foreach ($stock as $s) {
|
||||
$warehouse = $s->getWarehouse();
|
||||
$warehouseName = $warehouse->getName();
|
||||
$stock = $this->stockRepository->findBy(['warehouse' => $w]);
|
||||
|
||||
$data[$product->getId() . $warehouseName] = [
|
||||
'gtin' => $product->getGtin(),
|
||||
'stock' => $s->getInstock(),
|
||||
'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName
|
||||
];
|
||||
if ($stock) {
|
||||
foreach ($stock as $s) {
|
||||
$warehouse = $s->getWarehouse();
|
||||
$warehouseName = $warehouse->getName();
|
||||
|
||||
$data[$s->getProductId() . $warehouseName] = [
|
||||
'gtin' => $products[$s->getProductId()]->getGtin(),
|
||||
'stock' => $s->getInstock(),
|
||||
'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName
|
||||
];
|
||||
|
||||
}
|
||||
} else {
|
||||
//Wenn Product nicht gefunden werden kann, dann setzte Bestand auf Null
|
||||
// $data[] = [
|
||||
// 'gtin' => $products[$s->getProductId()]->getGtin(),
|
||||
// 'stock' => 0,
|
||||
// 'warehouse' => 0
|
||||
// ];
|
||||
// $this->logger->info('No stock for product ' . s->getProductId()->getId());
|
||||
}
|
||||
} else {
|
||||
$data[$product->getId()] = [
|
||||
'gtin' => $product->getGtin(),
|
||||
'stock' => 0,
|
||||
'warehouse' => 0
|
||||
];
|
||||
|
||||
$this->logger->info('No stock for product ' . $product->getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// foreach ($r as $product) {
|
||||
//
|
||||
// $stock = $this->stockRepository->findBy(['product_id' => $product->getId()]);
|
||||
//
|
||||
// if ($stock) {
|
||||
// foreach ($stock as $s) {
|
||||
// $warehouse = $s->getWarehouse();
|
||||
// $warehouseName = $warehouse->getName();
|
||||
//
|
||||
// $data[$product->getId() . $warehouseName] = [
|
||||
// 'gtin' => $product->getGtin(),
|
||||
// 'stock' => $s->getInstock(),
|
||||
// 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName
|
||||
// ];
|
||||
//
|
||||
// }
|
||||
// } else {
|
||||
// $data[$product->getId()] = [
|
||||
// 'gtin' => $product->getGtin(),
|
||||
// 'stock' => 0,
|
||||
// 'warehouse' => 0
|
||||
// ];
|
||||
//
|
||||
// $this->logger->info('No stock for product ' . $product->getId());
|
||||
// }
|
||||
// }
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param array $data
|
||||
* @param string $warehouse
|
||||
* @return void
|
||||
*/
|
||||
public function createExportFile($data, $warehouse): void
|
||||
public function createExportFile(array $data, string $warehouse): void
|
||||
{
|
||||
try {
|
||||
$writer = Writer::createFromPath(getcwd() . '/www/jtl/' . $warehouse . '.csv', 'w+');
|
||||
|
||||
$file = $this->rootPath . '/jtl/' . $warehouse . '.csv';
|
||||
|
||||
dump($file);
|
||||
|
||||
$writer = Writer::createFromPath($file, 'w+');
|
||||
$bytes = $writer->insertAll(new ArrayIterator($data));
|
||||
|
||||
if ($bytes) {
|
||||
$this->logger->info('Exported ' . $bytes . ' bytes');
|
||||
|
||||
$FTP = new Ftp();
|
||||
$FTP->uploadFile(getcwd() . '/www/jtl/' . $warehouse . '.csv');
|
||||
$FTP->uploadFile($file, $warehouse);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user