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\Parts; |
24
|
|
|
|
25
|
|
|
use App\Entity\Attachments\Attachment; |
26
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement; |
27
|
|
|
use App\Entity\Attachments\PartAttachment; |
28
|
|
|
use App\Entity\Parts\PartTraits\ProjectTrait; |
29
|
|
|
use App\Entity\ProjectSystem\Project; |
30
|
|
|
use App\Entity\Parameters\ParametersTrait; |
31
|
|
|
use App\Entity\Parameters\PartParameter; |
32
|
|
|
use App\Entity\Parts\PartTraits\AdvancedPropertyTrait; |
33
|
|
|
use App\Entity\Parts\PartTraits\BasicPropertyTrait; |
34
|
|
|
use App\Entity\Parts\PartTraits\InstockTrait; |
35
|
|
|
use App\Entity\Parts\PartTraits\ManufacturerTrait; |
36
|
|
|
use App\Entity\Parts\PartTraits\OrderTrait; |
37
|
|
|
use App\Entity\ProjectSystem\ProjectBOMEntry; |
38
|
|
|
use DateTime; |
39
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
40
|
|
|
use Doctrine\Common\Collections\Collection; |
41
|
|
|
use Doctrine\ORM\Mapping as ORM; |
42
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
43
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
44
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Part class. |
48
|
|
|
* |
49
|
|
|
* The class properties are split over various traits in directory PartTraits. |
50
|
|
|
* Otherwise this class would be too big, to be maintained. |
51
|
|
|
* |
52
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\PartRepository") |
53
|
|
|
* @ORM\Table("`parts`", indexes={ |
54
|
|
|
* @ORM\Index(name="parts_idx_datet_name_last_id_needs", columns={"datetime_added", "name", "last_modified", "id", "needs_review"}), |
55
|
|
|
* @ORM\Index(name="parts_idx_name", columns={"name"}), |
56
|
|
|
* @ORM\Index(name="parts_idx_ipn", columns={"ipn"}), |
57
|
|
|
* }) |
58
|
|
|
* @UniqueEntity(fields={"ipn"}, message="part.ipn.must_be_unique") |
59
|
|
|
*/ |
60
|
|
|
class Part extends AttachmentContainingDBElement |
61
|
|
|
{ |
62
|
|
|
use AdvancedPropertyTrait; |
63
|
|
|
//use MasterAttachmentTrait; |
64
|
|
|
use BasicPropertyTrait; |
65
|
|
|
use InstockTrait; |
66
|
|
|
use ManufacturerTrait; |
67
|
|
|
use OrderTrait; |
68
|
|
|
use ParametersTrait; |
69
|
|
|
use ProjectTrait; |
70
|
|
|
|
71
|
|
|
/** @var Collection<int, PartParameter> |
72
|
|
|
* @Assert\Valid() |
73
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Parameters\PartParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
74
|
|
|
* @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"}) |
75
|
|
|
*/ |
76
|
|
|
protected $parameters; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"}) |
80
|
|
|
*/ |
81
|
|
|
protected ?DateTime $addedDate = null; |
82
|
|
|
|
83
|
|
|
/** ************************************************************* |
84
|
|
|
* Overridden properties |
85
|
|
|
* (They are defined here and not in a trait, to avoid conflicts). |
86
|
|
|
****************************************************************/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string The name of this part |
90
|
|
|
* @ORM\Column(type="string") |
91
|
|
|
*/ |
92
|
|
|
protected string $name = ''; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Collection<int, PartAttachment> |
96
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
97
|
|
|
* @ORM\OrderBy({"name" = "ASC"}) |
98
|
|
|
* @Assert\Valid() |
99
|
|
|
*/ |
100
|
|
|
protected $attachments; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var DateTime the date when this element was modified the last time |
104
|
|
|
* @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"}) |
105
|
|
|
*/ |
106
|
|
|
protected ?DateTime $lastModified = null; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var Attachment |
110
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\Attachments\Attachment") |
111
|
|
|
* @ORM\JoinColumn(name="id_preview_attachement", referencedColumnName="id") |
112
|
|
|
* @Assert\Expression("value == null or value.isPicture()", message="part.master_attachment.must_be_picture") |
113
|
|
|
*/ |
114
|
|
|
protected ?Attachment $master_picture_attachment = null; |
115
|
|
|
|
116
|
|
|
public function __construct() |
117
|
|
|
{ |
118
|
|
|
parent::__construct(); |
119
|
|
|
$this->partLots = new ArrayCollection(); |
120
|
|
|
$this->orderdetails = new ArrayCollection(); |
121
|
|
|
$this->parameters = new ArrayCollection(); |
122
|
|
|
$this->project_bom_entries = new ArrayCollection(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __clone() |
126
|
|
|
{ |
127
|
|
|
if ($this->id) { |
|
|
|
|
128
|
|
|
//Deep clone part lots |
129
|
|
|
$lots = $this->partLots; |
130
|
|
|
$this->partLots = new ArrayCollection(); |
131
|
|
|
foreach ($lots as $lot) { |
132
|
|
|
$this->addPartLot(clone $lot); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//Deep clone order details |
136
|
|
|
$orderdetails = $this->orderdetails; |
137
|
|
|
$this->orderdetails = new ArrayCollection(); |
138
|
|
|
foreach ($orderdetails as $orderdetail) { |
139
|
|
|
$this->addOrderdetail(clone $orderdetail); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
//Deep clone parameters |
143
|
|
|
$parameters = $this->parameters; |
144
|
|
|
$this->parameters = new ArrayCollection(); |
145
|
|
|
foreach ($parameters as $parameter) { |
146
|
|
|
$this->addParameter(clone $parameter); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
parent::__clone(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @Assert\Callback |
154
|
|
|
*/ |
155
|
|
|
public function validate(ExecutionContextInterface $context, $payload) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
//Ensure that the part name fullfills the regex of the category |
158
|
|
|
if ($this->category) { |
159
|
|
|
$regex = $this->category->getPartnameRegex(); |
160
|
|
|
if (!empty($regex)) { |
161
|
|
|
if (!preg_match($regex, $this->name)) { |
162
|
|
|
$context->buildViolation('part.name.must_match_category_regex') |
163
|
|
|
->atPath('name') |
164
|
|
|
->setParameter('%regex%', $regex) |
165
|
|
|
->addViolation(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: