<commit_before><?php

namespace CudiBundle\Repository\Stock;

use CudiBundle\Entity\Sale\Article,
    CudiBundle\Entity\Stock\Period as PeriodEntity,
    CommonBundle\Component\Doctrine\ORM\EntityRepository;

/**
 * Period
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Period extends EntityRepository
{
    public function findAllQuery()
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('p')
            ->from('CudiBundle\Entity\Stock\Period', 'p')
            ->orderBy('p.startDate', 'DESC')
            ->getQuery();

       return $resultSet;
    }

    public function findOneActive()
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('p')
            ->from('CudiBundle\Entity\Stock\Period', 'p')
            ->where(
                $query->expr()->isNull('p.endDate')
            )
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();

        return $resultSet;
    }

    private function _findAllArticleIds(PeriodEntity $period)
    {
        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Stock\Order\Item', 'i')
            ->innerJoin('i.article', 'a')
            ->innerJoin('i.order', 'o')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('o.dateOrdered', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('o.dateOrdered', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        $articles = array(0);
        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Stock\Delivery', 'd')
            ->innerJoin('d.article', 'a')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('d.timestamp', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('d.timestamp', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Sale\SaleItem', 'i')
            ->innerJoin('i.article', 'a')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('i.timestamp', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('i.timestamp', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        return $articles;
    }

    public function findAllArticlesByPeriod(PeriodEntity $period, $notDelivered = false)
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('a')
            ->from('CudiBundle\Entity\Sale\Article', 'a')
            ->where(
                $query->expr()->in('a.id', $this->_findAllArticleIds($period))
            )
            ->getQuery()
            ->getResult();

        if ($notDelivered) {
            $articles = array();
            for($i = 0 ; $i < count($resultSet) ; $i++) {
                if ($period->getNbOrdered($resultSet[$i]) + $period->getNbVirtualOrdered($resultSet[$i]) - $period->getNbDelivered($resultSet[$i]) > 0)
                    $articles[] = $resultSet[$i];
            }
            return $articles;
        }

        return $resultSet;
    }

    public function findAllArticlesByPeriodAndTitle(PeriodEntity $period, $title, $notDelivered = false)<commit_msg>Fixed a table order<commit_after><?php

namespace CudiBundle\Repository\Stock;

use CudiBundle\Entity\Sale\Article,
    CudiBundle\Entity\Stock\Period as PeriodEntity,
    CommonBundle\Component\Doctrine\ORM\EntityRepository;

/**
 * Period
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Period extends EntityRepository
{
    public function findAllQuery()
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('p')
            ->from('CudiBundle\Entity\Stock\Period', 'p')
            ->orderBy('p.startDate', 'DESC')
            ->getQuery();

       return $resultSet;
    }

    public function findOneActive()
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('p')
            ->from('CudiBundle\Entity\Stock\Period', 'p')
            ->where(
                $query->expr()->isNull('p.endDate')
            )
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();

        return $resultSet;
    }

    private function _findAllArticleIds(PeriodEntity $period)
    {
        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Stock\Order\Item', 'i')
            ->innerJoin('i.article', 'a')
            ->innerJoin('i.order', 'o')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('o.dateOrdered', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('o.dateOrdered', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        $articles = array(0);
        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Stock\Delivery', 'd')
            ->innerJoin('d.article', 'a')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('d.timestamp', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('d.timestamp', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        $query = $this->_em->createQueryBuilder();
        $query->select('a.id')
            ->from('CudiBundle\Entity\Sale\SaleItem', 'i')
            ->innerJoin('i.article', 'a')
            ->where(
                $query->expr()->andX(
                    $query->expr()->gt('i.timestamp', ':startDate'),
                    $period->isOpen() ? '1=1' : $query->expr()->lt('i.timestamp', ':endDate')
                )
            )
            ->groupBy('a.id')
            ->setParameter('startDate', $period->getStartDate());

        if (!$period->isOpen())
            $query->setParameter('endDate', $period->getEndDate());

        $resultSet = $query->getQuery()
            ->getResult();

        foreach ($resultSet as $item)
            $articles[$item['id']] = $item['id'];

        return $articles;
    }

    public function findAllArticlesByPeriod(PeriodEntity $period, $notDelivered = false)
    {
        $query = $this->_em->createQueryBuilder();
        $resultSet = $query->select('a')
            ->from('CudiBundle\Entity\Sale\Article', 'a')
            ->innerJoin('a.mainArticle', 'm')
            ->where(
                $query->expr()->in('a.id', $this->_findAllArticleIds($period))
            )
            ->orderBy('m.title', 'ASC')
            ->getQuery()
            ->getResult();

        if ($notDelivered) {
            $articles = array();
            for($i = 0 ; $i < count($resultSet) ; $i++) {
                if ($period->getNbOrdered($resultSet[$i]) + $period->getNbVirtualOrdered($resultSet[$i]) - $period->getNbDelivered($resultSet[$i]) > 0)
                    $articles[] = $resultSet[$i];
            }
            return $articles;
        }

        return $resultSet;
    }

    public function findAllArticlesByPeriodAndTitle(PeriodEntity $period, $title, $notDelivered = false)
{'fraction_non_alphanumeric': 0.1824295010845987, 'fraction_numerical': 0.0021691973969631237, 'mean_word_length': 1.9217363751584284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 193, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3945317', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2619, 'n_words': 418}