ProductRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 21
Bugs 0 Features 0
Metric Value
eloc 83
dl 0
loc 122
rs 10
c 21
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A updateProductInventory() 0 28 2
A getProductsInventory() 0 27 2
A getPurchasingSuggestion() 0 36 2
A findProductBySkuAsInteger() 0 11 2
1
<?php
2
3
namespace ControleOnline\Repository;
4
5
use ControleOnline\Entity\Product;
6
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...
7
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...
8
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...
9
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...
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 Product|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method Product|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method Product[]    findAll()
17
 * @method Product[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class ProductRepository extends ServiceEntityRepository
20
{
21
22
    private $inventory_exclude_types = ['custom', 'component', 'manufactured'];
23
24
    public function __construct(
25
        private PeopleService $peopleService,
26
        ManagerRegistry $registry
27
    ) {
28
        parent::__construct($registry, Product::class);
29
    }
30
31
    public function updateProductInventory(): void
32
    {
33
        $companies = implode(',', array_map(
34
            fn($c) => $c->getId(),
35
            $this->peopleService->getMyCompanies()
36
        ));
37
38
        $purchasing_status = '7';
39
        $ordered_status = '5';
40
        $transit_status = '6';
41
        $sales_status = '6,7';
42
        $all_status = '5,6,7';
43
44
        try {
45
            $conn = $this->getEntityManager()->getConnection();
46
            $conn->executeStatement(
47
                'CALL update_product_inventory(?, ?, ?, ?, ?, ?)',
48
                [
49
                    $companies,
50
                    $purchasing_status,
51
                    $ordered_status,
52
                    $transit_status,
53
                    $sales_status,
54
                    $all_status
55
                ]
56
            );
57
        } catch (\Exception $e) {
58
            throw new \Exception($e->getMessage());
59
        }
60
    }
61
62
    public function getProductsInventory(?People $company): array
63
    {
64
        $qb = $this->createQueryBuilder('p');
65
        $qb->select([
66
            'p.id as product_id',
67
            'p.product as product_name',
68
            'p.description',
69
            'pu.productUnit',
70
            'pi.available',
71
            'pi.minimum',
72
            'pi.maximum',
73
            'c.name as company_name',
74
            'i.inventory as inventory_name'
75
        ])
76
            ->join('p.productUnit', 'pu')
77
            ->join('p.company', 'c')
78
            ->leftJoin('ControleOnline\Entity\ProductInventory', 'pi', 'WITH', 'pi.product = p.id')
79
            ->leftJoin('pi.inventory', 'i')
80
            ->orderBy('p.product', 'ASC')
81
            ->andWhere('p.type NOT IN (:excludedTypes)')
82
            ->setParameter('excludedTypes', $this->inventory_exclude_types);
83
84
        if ($company !== null) {
85
            $qb->andWhere('p.company = :company')
86
                ->setParameter('company', $company);
87
        }
88
        return $qb->getQuery()->getArrayResult();
89
    }
90
91
92
    public function getPurchasingSuggestion(?People $company): array
93
    {
94
        $this->updateProductInventory();
95
        $qb = $this->createQueryBuilder('p')
96
            ->select([
97
                'pe.id AS company_id',
98
                'pe.name AS company_name',
99
                'pe.alias AS company_alias',
100
                'p.id AS product_id',
101
                'p.sku AS sku',
102
                'p.product AS product_name',
103
                'p.description AS description',
104
                'p.type AS type',
105
                'SUM(pi.available + pi.ordered + pi.transit - pi.sales) AS stock',
106
                'SUM(pi.minimum) AS minimum',
107
                'CASE WHEN SUM(pi.available + pi.ordered + pi.transit - pi.minimum - pi.sales) * -1 < 0 THEN 0 ELSE SUM(pi.available + pi.ordered + pi.transit - pi.minimum - pi.sales) * -1 END AS needed',
108
                'pu.productUnit AS unity'
109
            ])
110
            ->join('p.company', 'pe')
111
            ->leftJoin('ControleOnline\Entity\ProductInventory', 'pi', 'WITH', 'pi.product = p.id')
112
            ->join('p.productUnit', 'pu')
113
            ->andWhere('p.type NOT IN (:excludedTypes)')
114
            ->andWhere('pe IN (:companies)')
115
            ->groupBy('p.id, p.product, p.description, p.sku, pe.id, pe.name, pe.alias, pu.productUnit')
116
            ->having('needed > 0')
117
            ->addOrderBy('p.product', 'ASC')
118
            ->setParameter('excludedTypes', $this->inventory_exclude_types)
119
            ->setParameter(
120
                'companies',
121
                $this->peopleService->getMyCompanies()
122
            );
123
124
        if ($company)
125
            $qb->andWhere('pe = :company')->setParameter('company', $company);
126
127
        return $qb->getQuery()->getResult();
128
    }
129
130
    public function findProductBySkuAsInteger(int $sku, People $company): ?Product
131
    {
132
        $conn = $this->getEntityManager()->getConnection();
133
        $sql = 'SELECT * FROM product WHERE CAST(sku AS UNSIGNED) = :sku AND company_id = :company';
134
        $stmt = $conn->prepare($sql);
135
        $stmt->bindValue('sku', $sku);
136
        $stmt->bindValue('company', $company->getId());
137
        $result = $stmt->executeQuery();
138
139
        $data = $result->fetchAssociative();
140
        return $data ? $this->getEntityManager()->getRepository(Product::class)->find($data['id']) : null;
141
    }
142
143
}
144