<?php
namespace App\Repository\Idempiere;
use App\Entity\Idempiere\MProductprice;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query\Parameter;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method MProductprice|null find($id, $lockMode = null, $lockVersion = null)
* @method MProductprice|null findOneBy(array $criteria, array $orderBy = null)
* @method MProductprice[] findAll()
* @method MProductprice[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MProductpriceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, MProductprice::class);
}
/**
* Busca el ultimo precio de un producto
*
* @param int $ad_org_id Identificador de la organizacion
* @param int $m_pricelist_id Identificador de la lista de precios
* @param string $m_product_id Identificador del producto
*
* @return null|MProductprice Precio
*/
public function findPrice(Int $ad_org_id = 0, Int $m_pricelist_id = 0, Int $m_product_id):? MProductprice
{
$qb = $this->createQueryBuilder('mpp');
$qb
->join('mpp.m_pricelist_version', 'mpv')
->join('mpv.m_pricelist', 'mpl')
->join('mpp.m_product', 'mp')
->where(
$qb->expr()->andX(
$qb->expr()->eq('mpp.isactive', "'Y'"),
$qb->expr()->eq('mpv.isactive', "'Y'"),
$qb->expr()->eq('mpl.issopricelist', "'Y'"),
$qb->expr()->eq('mp.m_product_id', ':product_id')
)
)
->setParameter('product_id', $m_product_id);
if ($ad_org_id > 0) {
$qb
->andWhere($qb->expr()->eq('mpp.ad_org_id', ':org_id'))
->setParameter('org_id', $ad_org_id);
}
if ($m_pricelist_id > 0) {
$qb
->andWhere($qb->expr()->eq('mpv.m_pricelist_id', ':pricelist_id'))
->setParameter('pricelist_id', $m_pricelist_id);
}
return $qb
->orderBy(
$qb->expr()->desc('mpv.updated')
)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* Busca los precios de los productos
*
* @param int $ad_org_id Identificador de la organizacion
* @param int $m_pricelist_id Identificador de la lista de precios
* @param string $value Codigo o Nombre del producto
*
* @return null|MProductprice[] Productos con precios
*/
public function findProductPrices(Int $m_pricelist_id, String $value = '')
{
$qb = $this->createQueryBuilder('mpp');
$query = $qb
->distinct()
->join('mpp.m_product', 'mp')
->join('mp.sm_precios_estimadolines', 'spel')
->join('spel.sm_precio_estimado', 'spe')
->join('spe.m_pricelist_version', 'mpv', 'with', 'mpv.m_pricelist_version_id = mpp.m_pricelist_version_id')
->leftjoin('mp.m_productdownload', 'mpd', 'with', "mpd.iscover = 'Y'")
->where(
$qb->expr()->andX(
// Product Price
$qb->expr()->eq('mpp.isactive', "'Y'"),
// Product
$qb->expr()->eq('mp.isactive', "'Y'"),
$qb->expr()->eq('mp.issold', "'Y'"),
$qb->expr()->eq('mp.issummary', "'N'"),
$qb->expr()->orX(
$qb->expr()->like('UPPER(mp.name)', 'UPPER(:value)'),
$qb->expr()->like('UPPER(mp.sku)', 'UPPER(:value)'),
$qb->expr()->like('UPPER(mp.value)','UPPER(:value)'),
),
// Estimacion
// $qb->expr()->eq('spel.isactive', "'Y'"),
$qb->expr()->eq('spel.listo', "'Y'"),
$qb->expr()->eq('spe.docstatus', "'Y'"),
$qb->expr()->eq('spe.sm_isbreak', "'Y'"),
// Version
$qb->expr()->eq('mpv.m_pricelist_id', ':pricelist_id'),
$qb->expr()->eq('mpv.isactive', "'Y'")
)
)
->setParameters(
new ArrayCollection([
new Parameter('value', $value ?: "%%"),
new Parameter('pricelist_id', $m_pricelist_id)
])
)
->getQuery();
return $query->getResult();
}
}