Issues (257)

src/Entity/ProjectSystem/Project.php (2 issues)

1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2022 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
namespace App\Entity\ProjectSystem;
24
25
use App\Entity\Attachments\ProjectAttachment;
26
use App\Entity\Base\AbstractStructuralDBElement;
27
use App\Entity\Parameters\ProjectParameter;
28
use App\Entity\Parts\Part;
29
use Doctrine\Common\Collections\ArrayCollection;
30
use Doctrine\Common\Collections\Collection;
31
use Doctrine\ORM\Mapping as ORM;
32
use InvalidArgumentException;
33
use Symfony\Component\Validator\Constraints as Assert;
34
use Symfony\Component\Validator\Context\ExecutionContextInterface;
35
36
/**
37
 * Class AttachmentType.
38
 *
39
 * @ORM\Entity(repositoryClass="App\Repository\Parts\DeviceRepository")
40
 * @ORM\Table(name="projects")
41
 */
42
class Project extends AbstractStructuralDBElement
43
{
44
    /**
45
     * @ORM\OneToMany(targetEntity="Project", mappedBy="parent")
46
     * @ORM\OrderBy({"name" = "ASC"})
47
     * @var Collection
48
     */
49
    protected $children;
50
51
    /**
52
     * @ORM\ManyToOne(targetEntity="Project", inversedBy="children")
53
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
54
     */
55
    protected $parent;
56
57
    /**
58
     * @ORM\OneToMany(targetEntity="ProjectBOMEntry", mappedBy="project", cascade={"persist", "remove"}, orphanRemoval=true)
59
     * @Assert\Valid()
60
     */
61
    protected $bom_entries;
62
63
    /**
64
     * @ORM\Column(type="integer")
65
     */
66
    protected int $order_quantity = 0;
67
68
    /**
69
     * @var string The current status of the project
70
     * @ORM\Column(type="string", length=64, nullable=true)
71
     * @Assert\Choice({"draft","planning","in_production","finished","archived"})
72
     */
73
    protected ?string $status = null;
74
75
76
    /**
77
     * @var Part|null The (optional) part that represents the builds of this project in the stock
78
     * @ORM\OneToOne(targetEntity="App\Entity\Parts\Part", mappedBy="built_project", cascade={"persist"}, orphanRemoval=true)
79
     */
80
    protected ?Part $build_part = null;
81
82
    /**
83
     * @ORM\Column(type="boolean")
84
     */
85
    protected bool $order_only_missing_parts = false;
86
87
    /**
88
     * @ORM\Column(type="text", nullable=false, columnDefinition="DEFAULT ''")
89
     */
90
    protected string $description = '';
91
92
    /**
93
     * @var Collection<int, ProjectAttachment>
94
     * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ProjectAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
95
     * @ORM\OrderBy({"name" = "ASC"})
96
     */
97
    protected $attachments;
98
99
    /** @var Collection<int, ProjectParameter>
100
     * @ORM\OneToMany(targetEntity="App\Entity\Parameters\ProjectParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
101
     * @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"})
102
     */
103
    protected $parameters;
104
105
    /********************************************************************************
106
     *
107
     *   Getters
108
     *
109
     *********************************************************************************/
110
111
    public function __construct()
112
    {
113
        parent::__construct();
114
        $this->bom_entries = new ArrayCollection();
115
    }
116
117
    public function __clone()
118
    {
119
        //When cloning this project, we have to clone each bom entry too.
120
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
121
            $bom_entries = $this->bom_entries;
122
            $this->bom_entries = new ArrayCollection();
123
            //Set master attachment is needed
124
            foreach ($bom_entries as $bom_entry) {
125
                $clone = clone $bom_entry;
126
                $this->bom_entries->add($clone);
127
            }
128
        }
129
130
        //Parent has to be last call, as it resets the ID
131
        parent::__clone();
132
    }
133
134
    /**
135
     *  Get the order quantity of this device.
136
     *
137
     * @return int the order quantity
138
     */
139
    public function getOrderQuantity(): int
140
    {
141
        return $this->order_quantity;
142
    }
143
144
    /**
145
     *  Get the "order_only_missing_parts" attribute.
146
     *
147
     * @return bool the "order_only_missing_parts" attribute
148
     */
149
    public function getOrderOnlyMissingParts(): bool
150
    {
151
        return $this->order_only_missing_parts;
152
    }
153
154
    /********************************************************************************
155
     *
156
     *   Setters
157
     *
158
     *********************************************************************************/
159
160
    /**
161
     *  Set the order quantity.
162
     *
163
     * @param int $new_order_quantity the new order quantity
164
     *
165
     * @return $this
166
     */
167
    public function setOrderQuantity(int $new_order_quantity): self
168
    {
169
        if ($new_order_quantity < 0) {
170
            throw new InvalidArgumentException('The new order quantity must not be negative!');
171
        }
172
        $this->order_quantity = $new_order_quantity;
173
174
        return $this;
175
    }
176
177
    /**
178
     *  Set the "order_only_missing_parts" attribute.
179
     *
180
     * @param bool $new_order_only_missing_parts the new "order_only_missing_parts" attribute
181
     *
182
     * @return Project
183
     */
184
    public function setOrderOnlyMissingParts(bool $new_order_only_missing_parts): self
185
    {
186
        $this->order_only_missing_parts = $new_order_only_missing_parts;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return Collection<int, ProjectBOMEntry>|ProjectBOMEntry[]
193
     */
194
    public function getBomEntries(): Collection
195
    {
196
        return $this->bom_entries;
197
    }
198
199
    /**
200
     * @param  ProjectBOMEntry  $entry
201
     * @return $this
202
     */
203
    public function addBomEntry(ProjectBOMEntry $entry): self
204
    {
205
        $entry->setProject($this);
206
        $this->bom_entries->add($entry);
207
        return $this;
208
    }
209
210
    /**
211
     * @param  ProjectBOMEntry  $entry
212
     * @return $this
213
     */
214
    public function removeBomEntry(ProjectBOMEntry $entry): self
215
    {
216
        $this->bom_entries->removeElement($entry);
217
        return $this;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function getDescription(): string
224
    {
225
        return $this->description;
226
    }
227
228
    /**
229
     * @param  string  $description
230
     * @return Project
231
     */
232
    public function setDescription(string $description): Project
233
    {
234
        $this->description = $description;
235
        return $this;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getStatus(): ?string
242
    {
243
        return $this->status;
244
    }
245
246
    /**
247
     * @param  string  $status
248
     */
249
    public function setStatus(?string $status): void
250
    {
251
        $this->status = $status;
252
    }
253
254
    /**
255
     * Checks if this project has an associated part representing the builds of this project in the stock.
256
     * @return bool
257
     */
258
    public function hasBuildPart(): bool
259
    {
260
        return $this->build_part !== null;
261
    }
262
263
    /**
264
     * Gets the part representing the builds of this project in the stock, if it is existing
265
     * @return Part|null
266
     */
267
    public function getBuildPart(): ?Part
268
    {
269
        return $this->build_part;
270
    }
271
272
    /**
273
     * Sets the part representing the builds of this project in the stock.
274
     * @param  Part|null  $build_part
275
     */
276
    public function setBuildPart(?Part $build_part): void
277
    {
278
        $this->build_part = $build_part;
279
        if ($build_part) {
280
            $build_part->setBuiltProject($this);
281
        }
282
    }
283
284
    /**
285
     * @Assert\Callback
286
     */
287
    public function validate(ExecutionContextInterface $context, $payload)
0 ignored issues
show
The parameter $payload is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

287
    public function validate(ExecutionContextInterface $context, /** @scrutinizer ignore-unused */ $payload)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
288
    {
289
        //If this project has subprojects, and these have builds part, they must be included in the BOM
290
        foreach ($this->getChildren() as $child) {
291
            /** @var $child Project */
292
            if ($child->getBuildPart() === null) {
293
                continue;
294
            }
295
            //We have to search all bom entries for the build part
296
            $found = false;
297
            foreach ($this->getBomEntries() as $bom_entry) {
298
                if ($bom_entry->getPart() === $child->getBuildPart()) {
299
                    $found = true;
300
                    break;
301
                }
302
            }
303
304
            //When the build part is not found, we have to add an error
305
            if (!$found) {
306
                $context->buildViolation('project.bom_has_to_include_all_subelement_parts')
307
                    ->atPath('bom_entries')
308
                    ->setParameter('%project_name%', $child->getName())
309
                    ->setParameter('%part_name%', $child->getBuildPart()->getName())
310
                    ->addViolation();
311
            }
312
        }
313
    }
314
}
315