From 35c87ba45cdd6cf83d95823a24c41422a0e5c915 Mon Sep 17 00:00:00 2001 From: Stephan Altmann Date: Wed, 18 Dec 2019 19:33:19 +0100 Subject: [PATCH] 0057389: implemented cron for stock update --- Commands/UpdateStock.php | 83 +++++++++++++++++++ Resources/cronjob.xml | 7 ++ Resources/services.xml | 24 ++++++ .../ReadData/External/ReadStockService.php | 80 ++++++++++++++++++ .../WriteData/Internal/WriteStockService.php | 79 ++++++++++++++++++ Subscriber/Cron.php | 30 ++++++- ValueObjects/Stock.php | 39 +++++++++ 7 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 Commands/UpdateStock.php create mode 100644 Services/ReadData/External/ReadStockService.php create mode 100644 Services/WriteData/Internal/WriteStockService.php create mode 100644 ValueObjects/Stock.php diff --git a/Commands/UpdateStock.php b/Commands/UpdateStock.php new file mode 100644 index 0000000..026cc38 --- /dev/null +++ b/Commands/UpdateStock.php @@ -0,0 +1,83 @@ +config = $configReader->getByPluginName($pluginName); + + $this->readProductsService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_stock_service'); + $this->writeProductsService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_stock_service'); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('Afterbuy:Update:Stock') + ->setDescription('Receive stock from Afterbuy') + ->setHelp(<<%command.name% implements a command. +EOF + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + /** + * Structure for receiving and writing data + * Should look everywhere the same. + * Dependencies are handled via services.xml + */ + + /** + * filter array is unused yet but can be implemented + */ + $filter = array( + 'categories' => array(), + 'products' => array( + 'submitAll' => false + ) + ); + + if((int)$this->config['mainSystem'] == 2) { + $products = $this->readProductsService->get($filter['products']); + $output->writeln('Got Products: ' . count($products)); + $this->writeProductsService->put($products); + } + } +} diff --git a/Resources/cronjob.xml b/Resources/cronjob.xml index 964b2fe..a1161e3 100644 --- a/Resources/cronjob.xml +++ b/Resources/cronjob.xml @@ -16,5 +16,12 @@ 86400 false + + Sync Stock + Shopware_CronJob_AfterbuyUpdateStock + false + 3600 + false + diff --git a/Resources/services.xml b/Resources/services.xml index 1dd89ea..a169455 100755 --- a/Resources/services.xml +++ b/Resources/services.xml @@ -97,6 +97,12 @@ %viaeb_shopware_afterbuy.plugin_name% + + + + %viaeb_shopware_afterbuy.plugin_name% + + @@ -119,6 +125,24 @@ + + + + + + viaebShopwareAfterbuy\ValueObjects\Stock + + + + + + + + number + false + + + diff --git a/Services/ReadData/External/ReadStockService.php b/Services/ReadData/External/ReadStockService.php new file mode 100644 index 0000000..31b8b4e --- /dev/null +++ b/Services/ReadData/External/ReadStockService.php @@ -0,0 +1,80 @@ +read($filter); + + return $this->transform($data); + } + + /** + * transforms api input into ValueArticle (targetEntity) + * + * @param array $products + * + * @return ValueArticle[] + */ + public function transform(array $products) + { + $this->logger->debug('Receiving stock from afterbuy', $products); + + if ($this->targetEntity === null) { + return array(); + } + + /** @var ValueArticle[] $valueArticles */ + $valueArticles = array(); + + foreach ($products as $product) { + + if (empty($product)) { + continue; + } + + $valueArticles[] = new $this->targetEntity($product["ProductID"], intval($product["Quantity"])); + } + + return $valueArticles; + } + + + /** + * provides api data. dummy data as used here can be used in tests + * + * @param array $filter + * + * @return array + */ + public function read(array $filter) + { + $resource = new ApiClient($this->apiConfig, $this->logger); + $data = $resource->getAllShopProductsFromAfterbuy($filter); + + if ( ! $data || empty($data)) { + return array(); + } + + return $data; + } +} \ No newline at end of file diff --git a/Services/WriteData/Internal/WriteStockService.php b/Services/WriteData/Internal/WriteStockService.php new file mode 100644 index 0000000..1b96faf --- /dev/null +++ b/Services/WriteData/Internal/WriteStockService.php @@ -0,0 +1,79 @@ +transform($data); + + return $this->send($data); + } + + /** + * transforms valueObject into final structure for storage + * could may be moved into separate helper + * + * @param Stock[] $valueArticles + * @return array|Stock[] + */ + public function transform(array $valueArticles) + { + $this->logger->debug('Importing stock', $valueArticles); + + foreach ($valueArticles as $article) { + + $detail = $this->helper->getArticleByExternalIdentifier($article->getIdentifyer()); + + if($detail === null || !is_int($article->getStock())) { + continue; + } + + $detail->setInStock($article->getStock()); + + try { + $this->entityManager->persist($detail); + } catch (ORMException $e) { + $this->logger->error('Error storing stock', $article); + } + + } + + return $valueArticles; + } + + + /** + * @param array $targetData + * + * @return array + */ + public function send($targetData) + { + try { + $this->entityManager->flush(); + } catch (OptimisticLockException | ORMException $e) { + $this->logger->error('Error storing stock', $targetData); + exit('Error storing stock'); + } + + return $targetData; + } +} diff --git a/Subscriber/Cron.php b/Subscriber/Cron.php index 1e9dfac..7259784 100644 --- a/Subscriber/Cron.php +++ b/Subscriber/Cron.php @@ -51,6 +51,16 @@ class Cron implements SubscriberInterface * @var WriteDataInterface */ protected $writeProductsService; + /** + * @var ReadDataInterface + */ + protected $readStockService; + + /** + * @var WriteDataInterface + */ + protected $writeStockService; + public function __construct(CachedConfigReader $configReader, string $pluginName) { @@ -69,6 +79,9 @@ public function __construct(CachedConfigReader $configReader, string $pluginName $this->readOrderStatusService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_orders_service'); $this->writeOrderStatusService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_status_service'); + + $this->readStockService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_stock_service'); + $this->writeStockService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_stock_service'); } //shopware is data carrying system otherwise else { @@ -93,10 +106,25 @@ public static function getSubscribedEvents() { return array( 'Shopware_CronJob_AfterbuyUpdateProducts' => 'updateProducts', - 'Shopware_CronJob_AfterbuyUpdateOrders' => 'updateOrders' + 'Shopware_CronJob_AfterbuyUpdateOrders' => 'updateOrders', + 'Shopware_CronJob_AfterbuyUpdateStock' => 'updateStock' ); } + public function updateStock() { + $filter = array( + 'categories' => array(), + 'products' => array( + 'submitAll' => false + ) + ); + + if(!empty($this->readStockService)) { + $products = $this->readStockService->get($filter['products']); + $this->writeStockService->put($products); + } + } + /** @noinspection PhpUnused */ public function updateProducts( /** @noinspection PhpUnusedParameterInspection */ Shopware_Components_Cron_CronJob $job diff --git a/ValueObjects/Stock.php b/ValueObjects/Stock.php new file mode 100644 index 0000000..7daf429 --- /dev/null +++ b/ValueObjects/Stock.php @@ -0,0 +1,39 @@ +stock = $stock; + $this->identifyer = $identifyer; + } + + /** + * @return string + */ + public function getIdentifyer(): string + { + return $this->identifyer; + } + + /** + * @return int + */ + public function getStock() + { + return $this->stock; + } +}