This commit is contained in:
@@ -58,13 +58,21 @@ class HiltesImportCommand extends Command
|
||||
* @var HiltesImport
|
||||
*/
|
||||
$hiltesImport = new HiltesImport($this->productRepository,$this->logger);
|
||||
$hiltesImport->startImport();
|
||||
|
||||
|
||||
$r = $hiltesImport->startImport();
|
||||
|
||||
if($r['error']){
|
||||
$io->error($r['text']);
|
||||
$this->logger->error($r['text']);
|
||||
return Command::FAILURE;
|
||||
}else{
|
||||
|
||||
$io->success('Done.');
|
||||
$pushStock = new SwPushStockCommand($this->stockRepository,$this->warehouseRepository,$this->logger);
|
||||
$pushStock->execute($input,$output);
|
||||
|
||||
return Command::SUCCESS;
|
||||
$io->success('Done.');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,11 @@
|
||||
namespace App\Command;
|
||||
|
||||
use App\Helper\Shopware;
|
||||
use App\Repository\StockRepository;
|
||||
use App\Repository\ProductRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
@@ -20,15 +18,15 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
class SwPushStockCommand extends Command
|
||||
{
|
||||
|
||||
private $stockRepository;
|
||||
private $productRepository;
|
||||
private $logger;
|
||||
|
||||
// public function __construct(StockRepository $stockRepository, LoggerInterface $logger)
|
||||
// {
|
||||
// $this->stockRepository = $stockRepository;
|
||||
// $this->logger = $logger;
|
||||
// parent::__construct();
|
||||
// }
|
||||
public function __construct(ProductRepository $productRepository, LoggerInterface $logger)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
$this->logger = $logger;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
protected function configure(): void
|
||||
@@ -48,15 +46,66 @@ class SwPushStockCommand extends Command
|
||||
// // ...
|
||||
// }
|
||||
|
||||
$shopware = new Shopware();
|
||||
$shopware = new Shopware($this->logger);
|
||||
|
||||
$shopware->pushStock();
|
||||
|
||||
$products = $this->getProducts();
|
||||
|
||||
if (count($products) > 0) {
|
||||
|
||||
foreach ($products as $product) {
|
||||
|
||||
//prüfen ob Shopware ID vorhanden
|
||||
if($product->getShopwareId() == null) {
|
||||
|
||||
$swID = $this->getShopwareId($product);
|
||||
|
||||
if($swID != null) {
|
||||
$product->setShopwareId($swID->id);
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$shopware->setProduct($product);
|
||||
}
|
||||
}
|
||||
|
||||
$io->success('Done.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
protected function getProducts()
|
||||
{
|
||||
$products = $this->productRepository->findAll();
|
||||
$arrProducts = array();
|
||||
foreach ($products as $product) {
|
||||
$arrProducts[$product->getGtin()] = $product;
|
||||
}
|
||||
|
||||
return $arrProducts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return int|null
|
||||
*/
|
||||
protected function getShopwareId($product)
|
||||
{
|
||||
$shopware = new Shopware($this->logger);
|
||||
$shopwareProduct = $shopware->getShopwareIdbyGtin($product->getGtin());
|
||||
if($shopwareProduct != null) {
|
||||
dump($shopwareProduct);
|
||||
$product->setShopwareId($shopwareProduct->id);
|
||||
$this->productRepository->save($product, true);
|
||||
}
|
||||
|
||||
return $shopwareProduct;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,16 +7,12 @@ use App\Entity\Product;
|
||||
use App\Entity\Stock;
|
||||
use App\Entity\Warehouse;
|
||||
use App\Repository\ProductRepository;
|
||||
use App\Repository\StockRepository;
|
||||
use App\Repository\WarehouseRepository;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class HiltesImport
|
||||
{
|
||||
private $productRepository;
|
||||
private $stockRepository;
|
||||
private $warehouseRepository;
|
||||
private $logger;
|
||||
protected $currentDirPath;
|
||||
@@ -24,9 +20,6 @@ class HiltesImport
|
||||
|
||||
public function __construct(ProductRepository $productRepository, LoggerInterface $logger)
|
||||
{
|
||||
// $this->stockRepository = $stockRepository;
|
||||
// $this->warehouseRepository = $warehouseRepository;
|
||||
|
||||
$this->productRepository = $productRepository;
|
||||
$this->logger = $logger;
|
||||
|
||||
@@ -34,7 +27,9 @@ class HiltesImport
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return array|void
|
||||
*/
|
||||
public function startImport()
|
||||
{
|
||||
#*** Holt alle Dateien
|
||||
@@ -44,6 +39,7 @@ class HiltesImport
|
||||
if(is_file($file['realPath'])){
|
||||
$this->loadFiles($file['realPath'],$file['fileSize']);
|
||||
}else{
|
||||
|
||||
return array('error'=>1,'text'=>'Error is_file('.$file['realPath'].')');
|
||||
}
|
||||
}
|
||||
@@ -52,6 +48,10 @@ class HiltesImport
|
||||
}else
|
||||
return array('error'=>1,'text'=>'Error in getFiles');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function getFiles()
|
||||
{
|
||||
$finder = Finder::create();
|
||||
@@ -62,6 +62,8 @@ class HiltesImport
|
||||
# return $file->isDir() || \preg_match('/\.(php|json)$/', $file->getPathname());
|
||||
#});
|
||||
if ($finder->hasResults()) {
|
||||
|
||||
dump($finder);
|
||||
foreach ($finder as $file) {
|
||||
$this->arrData['orgFiles']['data'][] = array(
|
||||
'realPath' => $file->getRealPath(),
|
||||
@@ -79,9 +81,8 @@ class HiltesImport
|
||||
{
|
||||
|
||||
$file = new \SplFileObject($srcFile);
|
||||
|
||||
|
||||
dump($file->getRealPath());
|
||||
|
||||
$this->logger->info('Starte Import von ' . $file->getRealPath());
|
||||
|
||||
$c = 0;
|
||||
@@ -98,6 +99,9 @@ class HiltesImport
|
||||
$this->switchSaveData($this->splitLine($data));
|
||||
}
|
||||
|
||||
// $em = $this->productRepository->getEntityManager();
|
||||
// $em->flush();
|
||||
|
||||
dump($c . ' Datensätze importiert');
|
||||
}
|
||||
protected function splitLine($str){
|
||||
@@ -190,8 +194,8 @@ class HiltesImport
|
||||
#menge;
|
||||
$prod->setStock($stock);
|
||||
|
||||
#gtin;
|
||||
$prod->setGtin($arr[1]);
|
||||
#eanzuIdent;
|
||||
$prod->setGtin($arr[13]);
|
||||
|
||||
$prod->setShopwareId("");
|
||||
|
||||
|
||||
@@ -2,14 +2,17 @@
|
||||
|
||||
namespace App\Helper;
|
||||
|
||||
use App\Entity\Products;
|
||||
use App\Repository\ProductsRepository;
|
||||
|
||||
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use Vin\ShopwareSdk\Client\AdminAuthenticator;
|
||||
use Vin\ShopwareSdk\Client\GrantType\ClientCredentialsGrantType;
|
||||
use Vin\ShopwareSdk\Data\Context;
|
||||
use Vin\ShopwareSdk\Data\Criteria;
|
||||
use Vin\ShopwareSdk\Data\Entity\Order\OrderDefinition;
|
||||
use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
|
||||
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
|
||||
use Vin\ShopwareSdk\Factory\RepositoryFactory;
|
||||
|
||||
@@ -34,8 +37,12 @@ class Shopware
|
||||
try {
|
||||
$grantType = new ClientCredentialsGrantType($_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']);
|
||||
$adminClient = new AdminAuthenticator($grantType, $_ENV['SHOPWARE_API_URL']);
|
||||
|
||||
//dump([$grantType, $adminClient,$_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']]);
|
||||
|
||||
return $adminClient->fetchAccessToken();
|
||||
} catch (\Exception $e) {
|
||||
dump($e->getMessage());
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -70,30 +77,58 @@ class Shopware
|
||||
return $orders->getEntities();
|
||||
}
|
||||
|
||||
public function setProduct(Products $product){
|
||||
/**
|
||||
* @param $prod
|
||||
*/
|
||||
public function setProduct($prod): void
|
||||
{
|
||||
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
|
||||
|
||||
//prüfen ob Shopware Produktid schon bekannt ist
|
||||
if($product->getProductId() == null){
|
||||
if($prod->getShopwareId() == null){
|
||||
$this->logger->error('Shopware Produkt ID ist nicht bekannt');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
|
||||
$productRepository->update($product, $context);
|
||||
}
|
||||
|
||||
public function pushStock()
|
||||
{
|
||||
$productRepository = new ProductsRepository();
|
||||
$products = $productRepository->findAll();
|
||||
dump($prod->getShopwareId());
|
||||
|
||||
|
||||
|
||||
|
||||
foreach ($products as $product) {
|
||||
$this->setProduct($product);
|
||||
try {
|
||||
$productRepository->update([
|
||||
'id' => $prod->getShopwareId(),
|
||||
'stock' => $prod->getStock()->getInStock()
|
||||
], $context);
|
||||
} catch (\ShopwareResponseException $e) {
|
||||
$this->logger->error($e->getResponse());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getShopwareIdbyGtin($gtin)
|
||||
{
|
||||
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
|
||||
|
||||
// Create the repository for the entity
|
||||
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
|
||||
|
||||
// Create the criteria
|
||||
$criteria = new Criteria();
|
||||
$criteria->addFilter(new EqualsFilter('ean', $gtin));
|
||||
|
||||
$products = $productRepository->search($criteria, $context);
|
||||
|
||||
//dump($products->getEntities()->first());
|
||||
|
||||
if($products->count() > 0){
|
||||
$productCollection = $products->getEntities();
|
||||
return $productCollection->first();
|
||||
//$prod->setShopwareId($products->getEntities()->first()->getId());
|
||||
//return $products->getEntities()->first();
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user