Passed
Pull Request — master (#78)
by
unknown
10:34
created

PartRepository::getPartCountWithLowStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
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\PartLot;
46
use Doctrine\ORM\QueryBuilder;
47
use Doctrine\ORM\Query\ResultSetMapping;
48
49
class PartRepository extends NamedDBElementRepository
50
{
51
    /**
52
     * Gets the summed up instock of all parts (only parts without an measurent unit).
53
     *
54
     * @throws \Doctrine\ORM\NoResultException
55
     * @throws \Doctrine\ORM\NonUniqueResultException
56
     */
57
    public function getPartsInstockSum(): float
58
    {
59
        $qb = new QueryBuilder($this->getEntityManager());
60
        $qb->select('SUM(part_lot.amount)')
61
            ->from(PartLot::class, 'part_lot')
62
            ->leftJoin('part_lot.part', 'part')
63
            ->where('part.partUnit IS NULL');
64
65
        $query = $qb->getQuery();
66
67
        return (float) ($query->getSingleScalarResult() ?? 0.0);
68
    }
69
70
    /**
71
     * Gets the number of parts that has price informations.
72
     *
73
     * @throws \Doctrine\ORM\NoResultException
74
     * @throws \Doctrine\ORM\NonUniqueResultException
75
     */
76
    public function getPartsCountWithPrice(): int
77
    {
78
        $qb = $this->createQueryBuilder('part');
79
        $qb->select('COUNT(DISTINCT part)')
80
            ->innerJoin('part.orderdetails', 'orderdetail')
81
            ->innerJoin('orderdetail.pricedetails', 'pricedetail')
82
            ->where('pricedetail.price > 0.0');
83
84
        $query = $qb->getQuery();
85
86
        return (int) ($query->getSingleScalarResult() ?? 0);
87
    }
88
89
   /**
90
     * Gets the number of parts that are low in stock.
91
     *
92
     * That is, it's total amount is smaller than the minimal amount.
93
     *
94
     * @throws \Doctrine\ORM\NoResultException
95
     * @throws \Doctrine\ORM\NonUniqueResultException
96
     */
97
    public function getPartCountWithLowStock(): int
98
    {
99
        /* Query to get total amount for every part.
100
         * As sub-queries are not supported -> resort to native SQL request.*/
101
        $rsm = new ResultSetMapping;
102
        $rsm->addScalarResult("cnt", "count", 'integer');
103
        $query = $this->getEntityManager()->createNativeQuery('
104
            SELECT COUNT(DISTINCT parts.id) as cnt FROM parts
105
            INNER JOIN (
106
                SELECT parts.id FROM part_lots
107
                INNER JOIN parts ON parts.id=part_lots.id_part
108
                GROUP BY parts.id
109
                HAVING SUM(part_lots.amount)<parts.minamount
110
            ) AS low
111
            ON low.id=parts.id',$rsm);
112
113
        return (int) ($query->getSingleScalarResult() ?? 0);
114
    }
115
}
116