/**
  * Update product association export date for the given product.
  *
  * @param JobInstance $jobInstance
  * @param string      $identifier
  */
 public function updateProductAssociationExport(JobInstance $jobInstance, $identifier)
 {
     $product = $this->productRepository->findByReference((string) $identifier);
     if ($product) {
         $this->updateExport($product, $jobInstance, $this->tableNameBuilder->getTableName('pim_prestashop_connector.entity.delta_product_association_export.class'));
     }
 }
    /**
     * Update configurable delta export.
     *
     * @param Channel     $channel
     * @param JobInstance $jobInstance
     * @param string      $identifier
     */
    public function setLastExportDate(Channel $channel, JobInstance $jobInstance, $identifier)
    {
        $variantGroup = $this->groupRepository->findOneBy(['code' => $identifier]);
        if ($variantGroup) {
            $deltaConfigurableTable = $this->tableNameBuilder->getTableName('pim_prestashop_connector.entity.delta_configurable_export.class');
            $exportableProducts = $this->productFilter->apply($channel, $variantGroup->getProducts());
            foreach ($exportableProducts as $product) {
                $sql = <<<SQL
                  INSERT INTO {$deltaConfigurableTable} (product_id, job_instance_id, last_export)
                  VALUES (:product_id, :job_instance_id, :last_export)
                  ON DUPLICATE KEY UPDATE last_export = :last_export
SQL;
                $connection = $this->em->getConnection();
                $query = $connection->prepare($sql);
                $now = new \DateTime('now', new \DateTimeZone('UTC'));
                $lastExport = $now->format('Y-m-d H:i:s');
                $productId = $product->getId();
                $jobInstanceId = $jobInstance->getId();
                $query->bindParam(':last_export', $lastExport, PDO::PARAM_STR);
                $query->bindParam(':product_id', $productId, PDO::PARAM_INT);
                $query->bindParam(':job_instance_id', $jobInstanceId, PDO::PARAM_INT);
                $query->execute();
            }
        }
    }
 /**
  * @param array $productIds
  */
 protected function removeCascade(array $productIds)
 {
     $connection = $this->em->getConnection();
     foreach ($this->deltaEntities as $deltaEntity) {
         $deltaTable = $this->tableNameBuilder->getTableName($deltaEntity);
         $connection->executeQuery($this->getDeleteSQLQuery($productIds, $deltaTable));
     }
 }
    /**
     * @param int $channelId
     * @param int $treeId
     * @param int $jobInstanceId
     *
     * @return string
     */
    protected function getSQLQuery($channelId, $treeId, $jobInstanceId)
    {
        $productTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.product.class');
        $completenessesTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.completeness.class');
        $categoryProductTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.product.class', 'categories');
        $groupTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.group.class');
        $groupProductTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.product.class', 'groups');
        $groupTypeTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.group_type.class');
        $categoryTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.category.class');
        $deltaConfigurableTable = $this->tableNameBuilder->getTableName('pim_prestashop_connector.entity.delta_configurable_export.class');
        return <<<SQL
            SELECT p.id FROM {$productTable} p
            INNER JOIN {$completenessesTable} comp
                ON comp.product_id = p.id AND comp.channel_id = {$channelId} AND comp.ratio = 100
            INNER JOIN {$categoryProductTable} cp ON p.id = cp.product_id
            INNER JOIN {$categoryTable} c ON c.id = cp.category_id AND c.root = {$treeId}

            INNER JOIN {$groupProductTable} gp ON gp.product_id = p.id
            INNER JOIN {$groupTable} g ON g.id = gp.group_id
            INNER JOIN {$groupTypeTable} gt ON gt.id = g.type_id AND gt.is_variant = 1

            LEFT JOIN {$deltaConfigurableTable} de ON de.product_id = p.id
            LEFT JOIN akeneo_batch_job_instance j ON j.id = de.job_instance_id AND j.id = {$jobInstanceId}

            WHERE p.updated > de.last_export OR j.id IS NULL
            AND p.is_enabled = 1

            GROUP BY p.id
SQL;
    }
    /**
     * @param int $channelId
     * @param int $treeId
     * @param int $jobInstanceId
     *
     * @return string
     */
    protected function getSQLQuery($channelId, $treeId, $jobInstanceId)
    {
        $productTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.product.class');
        $completenessesTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.completeness.class');
        $categoryProductTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.product.class', 'categories');
        $categoryTable = $this->tableNameBuilder->getTableName('pim_catalog.entity.category.class');
        $deltaProductTable = $this->tableNameBuilder->getTableName('pim_prestashop_connector.entity.delta_product_export.class');
        return <<<SQL
            SELECT cp.id FROM {$productTable} cp

            INNER JOIN {$completenessesTable} comp
                ON comp.product_id = cp.id AND comp.channel_id = {$channelId} AND comp.ratio = 100

            INNER JOIN {$categoryProductTable} ccp ON ccp.product_id = cp.id
            INNER JOIN {$categoryTable} c
                ON c.id = ccp.category_id AND c.root = {$treeId}

            LEFT JOIN {$deltaProductTable} dpe ON dpe.product_id = cp.id
            LEFT JOIN akeneo_batch_job_instance j
                ON j.id = dpe.job_instance_id AND j.id = {$jobInstanceId}

            WHERE (cp.updated > dpe.last_export OR j.id IS NULL) AND cp.is_enabled = 1

            GROUP BY cp.id;
SQL;
    }