src/Repository/Idempiere/MProductRepository.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Idempiere;
  3. use App\Entity\Idempiere\MProduct;
  4. use App\Entity\Idempiere\MProductprice;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\ORM\Query\Parameter;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Monolog\Logger;
  10. /**
  11. * @method MProduct|null find($id, $lockMode = null, $lockVersion = null)
  12. * @method MProduct|null findOneBy(array $criteria, array $orderBy = null)
  13. * @method MProduct[] findAll()
  14. * @method MProduct[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15. */
  16. class MProductRepository extends ServiceEntityRepository
  17. {
  18. public function __construct(ManagerRegistry $registry)
  19. {
  20. parent::__construct($registry, MProduct::class);
  21. }
  22. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  23. {
  24. $logger = new Logger("Repositorio de Producto");
  25. $ad_ord_id = 0;
  26. if (key_exists('ad_org_id', $criteria)) {
  27. $ad_ord_id = $criteria['ad_org_id'];
  28. $logger->log(Logger::DEBUG, "Org = " . $ad_ord_id);
  29. unset($criteria['ad_org_id']);
  30. }
  31. $m_pricelist_id = 0;
  32. if (key_exists('m_pricelist_id', $criteria)) {
  33. $m_pricelist_id = $criteria['m_pricelist_id'];
  34. $logger->log(Logger::DEBUG, "Price List = " . $m_pricelist_id);
  35. unset($criteria['m_pricelist_id']);
  36. }
  37. /** @var MProductPriceRepository */
  38. $RPrice = $this->_em->getRepository(MProductprice::class);
  39. /** @var MProduct[] */
  40. $Products = parent::findBy($criteria, $orderBy, $limit, $offset);
  41. foreach ($Products as $p) {
  42. $productPrice = $RPrice->findPrice($ad_ord_id, $m_pricelist_id, $p->getId());
  43. if ($productPrice != null)
  44. $p->setPrice($productPrice->getPricelist());
  45. $logger->log(Logger::DEBUG, "Product Price = [" . $p->getSku() . ", " . $p->getPrice() . "]");
  46. }
  47. return $Products;
  48. }
  49. /**
  50. * Buscar productos con imagenes para el catalogo
  51. *
  52. * @param int $sm_marca_id Identificador de la marca
  53. * @param int $m_product_category_id Identificador de la Categoria
  54. * @return array
  55. */
  56. public function findToCatalog(Int $sm_marca_id, Int $m_product_category_id = 0): array
  57. {
  58. $qb = $this->createQueryBuilder('mp');
  59. $qb
  60. ->join('mp.m_productdownload', 'mpd')
  61. ->where(
  62. $qb->expr()->andX(
  63. $qb->expr()->eq('mp.isactive', "'Y'"),
  64. $qb->expr()->eq('mp.issold', "'Y'"),
  65. $qb->expr()->eq('mp.sm_marca_id', ':marca_id'),
  66. $qb->expr()->eq('mpd.isactive', "'Y'"),
  67. $qb->expr()->like('mpd.downloadurl', ':format')
  68. )
  69. );
  70. if ($m_product_category_id > 0)
  71. $qb->andWhere( $qb->expr()->eq('mp.m_product_category_id', ':category_id') );
  72. $qb->setParameters(
  73. new ArrayCollection([
  74. new Parameter('marca_id', $sm_marca_id),
  75. new Parameter('format', '%.png')
  76. ])
  77. );
  78. if ($m_product_category_id > 0)
  79. $qb->setParameter('category_id', $m_product_category_id);
  80. $query = $qb
  81. ->orderBy('mp.m_product_category_id', 'DESC')
  82. ->addOrderBy('mp.m_product_id', 'DESC')
  83. ->getQuery();
  84. return $query->getResult();
  85. }
  86. public function findToOrder(Int $sm_marca_id, Array $categories, String $value = null)
  87. {
  88. $qb = $this->createQueryBuilder('mp');
  89. $qb
  90. ->where(
  91. $qb->expr()->andX(
  92. $qb->expr()->eq('mp.issold', "'Y'"),
  93. $qb->expr()->eq('mp.isactive', "'Y'"),
  94. $qb->expr()->eq('mp.sm_marca_id', ':marca_id'),
  95. $qb->expr()->in('mp.m_product_category_id', ':categories')
  96. )
  97. )
  98. ->setParameters(
  99. new ArrayCollection([
  100. new Parameter('marca_id', $sm_marca_id),
  101. new Parameter('categories', $categories)
  102. ])
  103. );
  104. if ( !is_null($value) ) {
  105. $qb->andWhere(
  106. $qb->expr()->orX(
  107. $qb->expr()->like('mp.value', ':value'),
  108. $qb->expr()->like('mp.sku', ':value'),
  109. $qb->expr()->like('mp.name', ':value')
  110. )
  111. )->setParameter('value', '%'. $value .'%');
  112. }
  113. $query = $qb
  114. ->orderBy('mp.m_product_category_id', 'DESC')
  115. ->addOrderBy('mp.m_product_id', 'DESC')
  116. ->getQuery();
  117. return $query->getResult();
  118. }
  119. /**
  120. * Busca los productos con sus cantidades
  121. * disponibles segun su almacen o ubicacion
  122. *
  123. * @param int $m_product_id Identificador del producto
  124. * @param int $m_warehouse_id Identificador del almacen
  125. * @param int $m_locator_id Identificador de la ubicacion
  126. *
  127. * @return float Productos
  128. */
  129. public function getQtyOnHandToOrder(Int $m_product_id, Int $m_warehouse_id, Int $m_locator_id = 0): Float
  130. {
  131. $connection = $this->getEntityManager()->getConnection();
  132. $stmt = $connection->prepare("SELECT BOMQtyOnHandToOrder(:product_id, :warehouse_id, :locator_id) AS QtyToOrder");
  133. $resulSet = $stmt->executeQuery([
  134. 'product_id' => $m_product_id,
  135. 'warehouse_id' => $m_warehouse_id,
  136. 'locator_id' => $m_locator_id
  137. ]);
  138. return $resulSet->fetchOne();
  139. }
  140. /**
  141. * Busca los productos con sus cantidades
  142. * disponibles segun su almacen o ubicacion
  143. *
  144. * @param int $m_product_id Identificador del producto
  145. * @param int $m_warehouse_id Identificador del almacen
  146. * @param int $m_locator_id Identificador de la ubicacion
  147. *
  148. * @return float Productos
  149. */
  150. public function findStorage(Int $m_product_id, Int $m_warehouse_id, Int $m_locator_id = 0): Float
  151. {
  152. $connection = $this->getEntityManager()->getConnection();
  153. $stmt = $connection->prepare("SELECT bomqtyonhand(:product_id, :warehouse_id, :locator_id) AS QtyToOrder");
  154. $resulSet = $stmt->executeQuery([
  155. 'product_id' => $m_product_id,
  156. 'warehouse_id' => $m_warehouse_id,
  157. 'locator_id' => $m_locator_id
  158. ]);
  159. return $resulSet->fetchOne();
  160. }
  161. }