Passed
Push — master ( 878c38...d4077c )
by Luiz Kim
02:25
created

OrderRepository::getPurchasingSuggestion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 32
rs 9.504
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ControleOnline\Repository;
4
5
use ControleOnline\Entity\Order;
6
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ControleOnline\Service\PeopleService;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Doctrine\DBAL\DBALException;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\DBALException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\ORM\Query\ResultSetMapping;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Query\ResultSetMapping was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * @method Order|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method Order|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method Order[]    findAll()
17
 * @method Order[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class OrderRepository extends ServiceEntityRepository
20
{
21
  public function __construct(
22
    private PeopleService $peopleService,
23
24
    ManagerRegistry $registry
25
  ) {
26
    parent::__construct($registry, Order::class);
27
  }
28
29
30
  public function getPurchasingSuggestion(?People $company): array
31
  {
32
    $qb = $this->createQueryBuilder('p')
33
      ->select([
34
        'pe.id AS company_id',
35
        'pe.name AS company_name',
36
        'pe.alias AS company_alias',
37
        'p.id AS product_id',
38
        'p.sku AS sku',
39
        'p.product AS product_name',
40
        'p.description AS description',
41
        'p.type AS type',
42
        'SUM(pi.available + pi.ordered + pi.transit - pi.sales) AS stock',
43
        'SUM(pi.minimum) AS minimum',
44
        'GREATEST(0, SUM(pi.available + pi.ordered + pi.transit - pi.minimum - pi.sales) * -1) AS needed'
45
      ])
46
      ->join('p.company', 'pe')
47
      ->join('ControleOnline\Entity\ProductInventory', 'pi', 'WITH', 'pi.product = p.id')
48
      ->andWhere('p.type NOT IN (:excludedTypes)')
49
      ->andWhere('pe IN (:companies)')
50
      ->groupBy('p.id, p.product, p.description, p.sku, pe.id, pe.name, pe.alias')
51
      ->having('needed > 0')
52
      ->setParameter('excludedTypes', ['custom', 'component'])
53
      ->setParameter(
54
        'companies',
55
        $this->peopleService->getMyCompanies()
56
      );
57
58
    if ($company)
59
      $qb->andWhere('pe = :company')->setParameter('company',  $company);
60
61
    return $qb->getQuery()->getResult();
62
  }
63
}
64