<?php

namespace VGM\WOBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * StockRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class StockRepository extends EntityRepository
{
    public function getStockByNo($stockNo)
    {
        if($this->countStockByNo($stockNo) == 1){
            $qb = $this->createQueryBuilder('s')
                ->where('s.code = :stock')
                ->setParameter('stock', $stockNo);

            $query = $qb->getQuery();

            return $query->getSingleResult();
        }
        return null;
    }

    public function countStockByNo($stockNo)
    {
        $qb = $this->createQueryBuilder('s')
            ->select('count(s.id)')
            ->where('s.code = :stock')
            ->setParameter('stock', $stockNo);

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }

    public function getStocksByWONo($woNo)
    {
        $qb = $this->createQueryBuilder('s')
            ->select('s.id')
            ->where('s.wo = :stock')
            ->setParameter('stock', $woNo);

        $query = $qb->getQuery();

        return $query->getResult();
    }
    public function findAll4Update()
    {
        $qb = $this->createQueryBuilder('s')
            ->where('s.asdb_id is NULL');

        $query = $qb->getQuery();

        return $query->getResult();
    }
}
