Passed
Pull Request — master (#78)
by
unknown
09:17 queued 04:28
created

PartRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPartsInstockSum() 0 11 1
A getPartCountWithLowStock() 0 16 1
A getPartsCountWithPrice() 0 11 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Repository;
44
45
use App\Entity\Parts\Part;
46
use App\Entity\Parts\PartLot;
47
use Doctrine\ORM\QueryBuilder;
48
use Doctrine\ORM\Query\ResultSetMapping;
49
50
class PartRepository extends NamedDBElementRepository
51
{
52
    /**
53
     * Gets the summed up instock of all parts (only parts without an measurent unit).
54
     *
55
     * @throws \Doctrine\ORM\NoResultException
56
     * @throws \Doctrine\ORM\NonUniqueResultException
57
     */
58
    public function getPartsInstockSum(): float
59
    {
60
        $qb = new QueryBuilder($this->getEntityManager());
61
        $qb->select('SUM(part_lot.amount)')
62
            ->from(PartLot::class, 'part_lot')
63
            ->leftJoin('part_lot.part', 'part')
64
            ->where('part.partUnit IS NULL');
65
66
        $query = $qb->getQuery();
67
68
        return (float) ($query->getSingleScalarResult() ?? 0.0);
69
    }
70
71
    /**
72
     * Gets the number of parts that has price informations.
73
     *
74
     * @throws \Doctrine\ORM\NoResultException
75
     * @throws \Doctrine\ORM\NonUniqueResultException
76
     */
77
    public function getPartsCountWithPrice(): int
78
    {
79
        $qb = $this->createQueryBuilder('part');
80
        $qb->select('COUNT(DISTINCT part)')
81
            ->innerJoin('part.orderdetails', 'orderdetail')
82
            ->innerJoin('orderdetail.pricedetails', 'pricedetail')
83
            ->where('pricedetail.price > 0.0');
84
85
        $query = $qb->getQuery();
86
87
        return (int) ($query->getSingleScalarResult() ?? 0);
88
    }
89
90
   /**
91
     * Gets the number of parts that are low in stock.
92
     *
93
     * That is, it's total amount is smaller than the minimal amount.
94
     *
95
     * @throws \Doctrine\ORM\NoResultException
96
     * @throws \Doctrine\ORM\NonUniqueResultException
97
     */
98
    public function getPartCountWithLowStock(): int
99
    {
100
        $in = $this->getEntityManager()->createQueryBuilder();
101
        $in->select("parts.id")
102
            ->from(Part::class, "parts")
103
            ->leftJoin("parts.partLots", "lots")
104
            ->groupBy("parts.id")
105
            ->having("SUM(lots.amount)<parts.minamount");
106
        $qb = $this->getEntityManager()->createQueryBuilder();
107
        $qb->select("COUNT(part.id)")
108
            ->from(Part::class, "part")
109
            ->where($qb->expr()->in("part.id", $in->getDQL()));
110
111
        $query = $qb->getQuery();
112
113
        return (int) ($query->getSingleScalarResult() ?? 0);
114
    }
115
}
116