src/App/EventListener/Client/Store/StoreGroupListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Client\Store;
  3. use App\Entity\Client\Store\StoreGroup;
  4. use App\Entity\Client\Store\StoreStock;
  5. use App\EventListener\GenericEvent;
  6. use App\Service\AppManager;
  7. use App\Service\Aura\AuraManager;
  8. use App\Utils\SQLUtils;
  9. use Aura\SqlQuery\QueryFactory;
  10. use Doctrine\DBAL\FetchMode;
  11. class StoreGroupListener
  12. {
  13.     private AppManager $appManager;
  14.     public function __construct(AppManager $appManager)
  15.     {
  16.         $this->appManager $appManager;
  17.     }
  18.     public function copyAssortment(GenericEvent $genericEvent)
  19.     {
  20.         $storeGroup $genericEvent->getSubject();
  21.         if (!$storeGroup instanceof StoreGroup) {
  22.             return;
  23.         }
  24.         if ($storeGroup->getCopyFromStores()->count() === 0) {
  25.             return;
  26.         }
  27.         $copyStoreIDs = [];
  28.         foreach ($storeGroup->getCopyFromStores() as $copyFromStore) {
  29.             $copyStoreIDs[] = $copyFromStore->getId();
  30.         }
  31.         $now = (new \DateTime())->format('Y-m-d H:i:s');
  32.         $connection $this->appManager->getConnection();
  33.         $queryFactory = new QueryFactory('pgsql');
  34.         $storeStockQuery $queryFactory->newSelect()
  35.             ->cols(['ss.product_id'])
  36.             ->from('client_' $this->appManager->getClientId() . '.store_stock ss')
  37.             ->where('ss.store_id IN (' implode(','$copyStoreIDs) . ')')
  38.             ->where("ss.assortment = '".StoreStock::ASSORTMENT_IN."'")
  39.             ->groupBy(['ss.product_id'])
  40.             ->orderBy(['ss.product_id ASC']);
  41.         $storeStocks AuraManager::fetchAll($connection$storeStockQueryFetchMode::COLUMN);
  42.         if (count($storeStocks) === 0) {
  43.             return;
  44.         }
  45.         foreach ($storeStocks as $productID) {
  46.             $updateQuery $queryFactory->newInsert()
  47.                 ->into('client_' $this->appManager->getClientId() . '.store_stock_group')
  48.                 ->set('product_id'$productID)
  49.                 ->set('created_at'SQLUtils::toSQLString($now))
  50.                 ->set('assortment'"'" .StoreStock::ASSORTMENT_IN "'")
  51.                 ->set('store_group_id'$storeGroup->getId());
  52.             try {
  53.                 $stmt $connection->prepare($updateQuery->getStatement());
  54.                 $stmt->execute($updateQuery->getBindValues());
  55.                 echo '.';
  56.             } catch (\Exception $exception) {
  57.                 echo '_';
  58.             }
  59.         }
  60.         return 0;
  61.     }
  62. }