<?php
namespace App\Repository\Idempiere;
use App\Entity\Idempiere\MProduct;
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;
use Monolog\Logger;
/**
* @method MProduct|null find($id, $lockMode = null, $lockVersion = null)
* @method MProduct|null findOneBy(array $criteria, array $orderBy = null)
* @method MProduct[] findAll()
* @method MProduct[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, MProduct::class);
}
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$logger = new Logger("Repositorio de Producto");
$ad_ord_id = 0;
if (key_exists('ad_org_id', $criteria)) {
$ad_ord_id = $criteria['ad_org_id'];
$logger->log(Logger::DEBUG, "Org = " . $ad_ord_id);
unset($criteria['ad_org_id']);
}
$m_pricelist_id = 0;
if (key_exists('m_pricelist_id', $criteria)) {
$m_pricelist_id = $criteria['m_pricelist_id'];
$logger->log(Logger::DEBUG, "Price List = " . $m_pricelist_id);
unset($criteria['m_pricelist_id']);
}
/** @var MProductPriceRepository */
$RPrice = $this->_em->getRepository(MProductprice::class);
/** @var MProduct[] */
$Products = parent::findBy($criteria, $orderBy, $limit, $offset);
foreach ($Products as $p) {
$productPrice = $RPrice->findPrice($ad_ord_id, $m_pricelist_id, $p->getId());
if ($productPrice != null)
$p->setPrice($productPrice->getPricelist());
$logger->log(Logger::DEBUG, "Product Price = [" . $p->getSku() . ", " . $p->getPrice() . "]");
}
return $Products;
}
/**
* Buscar productos con imagenes para el catalogo
*
* @param int $sm_marca_id Identificador de la marca
* @param int $m_product_category_id Identificador de la Categoria
* @return array
*/
public function findToCatalog(Int $sm_marca_id, Int $m_product_category_id = 0): array
{
$qb = $this->createQueryBuilder('mp');
$qb
->join('mp.m_productdownload', 'mpd')
->where(
$qb->expr()->andX(
$qb->expr()->eq('mp.isactive', "'Y'"),
$qb->expr()->eq('mp.issold', "'Y'"),
$qb->expr()->eq('mp.sm_marca_id', ':marca_id'),
$qb->expr()->eq('mpd.isactive', "'Y'"),
$qb->expr()->like('mpd.downloadurl', ':format')
)
);
if ($m_product_category_id > 0)
$qb->andWhere( $qb->expr()->eq('mp.m_product_category_id', ':category_id') );
$qb->setParameters(
new ArrayCollection([
new Parameter('marca_id', $sm_marca_id),
new Parameter('format', '%.png')
])
);
if ($m_product_category_id > 0)
$qb->setParameter('category_id', $m_product_category_id);
$query = $qb
->orderBy('mp.m_product_category_id', 'DESC')
->addOrderBy('mp.m_product_id', 'DESC')
->getQuery();
return $query->getResult();
}
public function findToOrder(Int $sm_marca_id, Array $categories, String $value = null)
{
$qb = $this->createQueryBuilder('mp');
$qb
->where(
$qb->expr()->andX(
$qb->expr()->eq('mp.issold', "'Y'"),
$qb->expr()->eq('mp.isactive', "'Y'"),
$qb->expr()->eq('mp.sm_marca_id', ':marca_id'),
$qb->expr()->in('mp.m_product_category_id', ':categories')
)
)
->setParameters(
new ArrayCollection([
new Parameter('marca_id', $sm_marca_id),
new Parameter('categories', $categories)
])
);
if ( !is_null($value) ) {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->like('mp.value', ':value'),
$qb->expr()->like('mp.sku', ':value'),
$qb->expr()->like('mp.name', ':value')
)
)->setParameter('value', '%'. $value .'%');
}
$query = $qb
->orderBy('mp.m_product_category_id', 'DESC')
->addOrderBy('mp.m_product_id', 'DESC')
->getQuery();
return $query->getResult();
}
/**
* Busca los productos con sus cantidades
* disponibles segun su almacen o ubicacion
*
* @param int $m_product_id Identificador del producto
* @param int $m_warehouse_id Identificador del almacen
* @param int $m_locator_id Identificador de la ubicacion
*
* @return float Productos
*/
public function getQtyOnHandToOrder(Int $m_product_id, Int $m_warehouse_id, Int $m_locator_id = 0): Float
{
$connection = $this->getEntityManager()->getConnection();
$stmt = $connection->prepare("SELECT BOMQtyOnHandToOrder(:product_id, :warehouse_id, :locator_id) AS QtyToOrder");
$resulSet = $stmt->executeQuery([
'product_id' => $m_product_id,
'warehouse_id' => $m_warehouse_id,
'locator_id' => $m_locator_id
]);
return $resulSet->fetchOne();
}
/**
* Busca los productos con sus cantidades
* disponibles segun su almacen o ubicacion
*
* @param int $m_product_id Identificador del producto
* @param int $m_warehouse_id Identificador del almacen
* @param int $m_locator_id Identificador de la ubicacion
*
* @return float Productos
*/
public function findStorage(Int $m_product_id, Int $m_warehouse_id, Int $m_locator_id = 0): Float
{
$connection = $this->getEntityManager()->getConnection();
$stmt = $connection->prepare("SELECT bomqtyonhand(:product_id, :warehouse_id, :locator_id) AS QtyToOrder");
$resulSet = $stmt->executeQuery([
'product_id' => $m_product_id,
'warehouse_id' => $m_warehouse_id,
'locator_id' => $m_locator_id
]);
return $resulSet->fetchOne();
}
}