1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2023 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
|
|
|
namespace App\Helpers\Projects; |
22
|
|
|
|
23
|
|
|
use App\Entity\Parts\PartLot; |
24
|
|
|
use App\Entity\ProjectSystem\Project; |
25
|
|
|
use App\Entity\ProjectSystem\ProjectBOMEntry; |
26
|
|
|
use App\Validator\Constraints\ProjectSystem\ValidProjectBuildRequest; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @ValidProjectBuildRequest() |
30
|
|
|
*/ |
31
|
|
|
final class ProjectBuildRequest |
32
|
|
|
{ |
33
|
|
|
private Project $project; |
34
|
|
|
private int $number_of_builds; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<int, float> |
38
|
|
|
*/ |
39
|
|
|
private array $withdraw_amounts = []; |
40
|
|
|
|
41
|
|
|
private string $comment = ''; |
42
|
|
|
|
43
|
|
|
private ?PartLot $builds_lot = null; |
44
|
|
|
|
45
|
|
|
private bool $add_build_to_builds_part = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Project $project The project that should be build |
49
|
|
|
* @param int $number_of_builds The number of builds that should be created |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Project $project, int $number_of_builds) |
52
|
|
|
{ |
53
|
|
|
$this->project = $project; |
54
|
|
|
$this->number_of_builds = $number_of_builds; |
55
|
|
|
|
56
|
|
|
$this->initializeArray(); |
57
|
|
|
|
58
|
|
|
//By default, use the first available lot of builds part if there is one. |
59
|
|
|
if($project->getBuildPart() !== null) { |
60
|
|
|
$this->add_build_to_builds_part = true; |
61
|
|
|
foreach( $project->getBuildPart()->getPartLots() as $lot) { |
62
|
|
|
if (!$lot->isInstockUnknown()) { |
63
|
|
|
$this->builds_lot = $lot; |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function initializeArray(): void |
71
|
|
|
{ |
72
|
|
|
//Completely reset the array |
73
|
|
|
$this->withdraw_amounts = []; |
74
|
|
|
|
75
|
|
|
//Now create an array for each BOM entry |
76
|
|
|
foreach ($this->getPartBomEntries() as $bom_entry) { |
77
|
|
|
$remaining_amount = $this->getNeededAmountForBOMEntry($bom_entry); |
78
|
|
|
foreach($this->getPartLotsForBOMEntry($bom_entry) as $lot) { |
79
|
|
|
//If the lot has instock use it for the build |
80
|
|
|
$this->withdraw_amounts[$lot->getID()] = min($remaining_amount, $lot->getAmount()); |
81
|
|
|
$remaining_amount -= max(0, $this->withdraw_amounts[$lot->getID()]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Ensure that the projectBOMEntry belongs to the project, otherwise throw an exception. |
88
|
|
|
* @param ProjectBOMEntry $entry |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
private function ensureBOMEntryValid(ProjectBOMEntry $entry): void |
92
|
|
|
{ |
93
|
|
|
if ($entry->getProject() !== $this->project) { |
94
|
|
|
throw new \InvalidArgumentException('The given BOM entry does not belong to the project!'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the partlot where the builds should be added to, or null if it should not be added to any lot. |
100
|
|
|
* @return PartLot|null |
101
|
|
|
*/ |
102
|
|
|
public function getBuildsPartLot(): ?PartLot |
103
|
|
|
{ |
104
|
|
|
return $this->builds_lot; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return if the builds should be added to the builds part of this project as new stock |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function getAddBuildsToBuildsPart(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->add_build_to_builds_part; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set if the builds should be added to the builds part of this project as new stock |
118
|
|
|
* @param bool $new_value |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setAddBuildsToBuildsPart(bool $new_value): self |
122
|
|
|
{ |
123
|
|
|
$this->add_build_to_builds_part = $new_value; |
124
|
|
|
|
125
|
|
|
if ($new_value === false) { |
126
|
|
|
$this->builds_lot = null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the partlot where the builds should be added to, or null if it should not be added to any lot. |
134
|
|
|
* The part lot must belong to the project build part, or an exception is thrown! |
135
|
|
|
* @param PartLot|null $new_part_lot |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function setBuildsPartLot(?PartLot $new_part_lot): self |
139
|
|
|
{ |
140
|
|
|
//Ensure that this new_part_lot belongs to the project |
141
|
|
|
if (($new_part_lot !== null && $new_part_lot->getPart() !== $this->project->getBuildPart()) || $this->project->getBuildPart() === null) { |
142
|
|
|
throw new \InvalidArgumentException('The given part lot does not belong to the projects build part!'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($new_part_lot !== null) { |
146
|
|
|
$this->setAddBuildsToBuildsPart(true); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->builds_lot = $new_part_lot; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the comment where the user can write additional information about the build. |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getComment(): string |
159
|
|
|
{ |
160
|
|
|
return $this->comment; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets the comment where the user can write additional information about the build. |
165
|
|
|
* @param string $comment |
166
|
|
|
*/ |
167
|
|
|
public function setComment(string $comment): void |
168
|
|
|
{ |
169
|
|
|
$this->comment = $comment; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the amount of parts that should be withdrawn from the given lot for the corresponding BOM entry. |
174
|
|
|
* @param PartLot|int $lot The part lot (or the ID of the part lot) for which the withdraw amount should be get |
175
|
|
|
* @return float |
176
|
|
|
*/ |
177
|
|
|
public function getLotWithdrawAmount($lot): float |
178
|
|
|
{ |
179
|
|
|
if ($lot instanceof PartLot) { |
180
|
|
|
$lot_id = $lot->getID(); |
181
|
|
|
} elseif (is_int($lot)) { |
|
|
|
|
182
|
|
|
$lot_id = $lot; |
183
|
|
|
} else { |
184
|
|
|
throw new \InvalidArgumentException('The given lot must be an instance of PartLot or an ID of a PartLot!'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (! array_key_exists($lot_id, $this->withdraw_amounts)) { |
188
|
|
|
throw new \InvalidArgumentException('The given lot is not in the withdraw amounts array!'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->withdraw_amounts[$lot_id]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Sets the amount of parts that should be withdrawn from the given lot for the corresponding BOM entry. |
196
|
|
|
* @param PartLot|int $lot The part lot (or the ID of the part lot) for which the withdraw amount should be get |
197
|
|
|
* @param float $amount |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function setLotWithdrawAmount($lot, float $amount): self |
201
|
|
|
{ |
202
|
|
|
if ($lot instanceof PartLot) { |
203
|
|
|
$lot_id = $lot->getID(); |
204
|
|
|
} elseif (is_int($lot)) { |
|
|
|
|
205
|
|
|
$lot_id = $lot; |
206
|
|
|
} else { |
207
|
|
|
throw new \InvalidArgumentException('The given lot must be an instance of PartLot or an ID of a PartLot!'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$this->withdraw_amounts[$lot_id] = $amount; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns the sum of all withdraw amounts for the given BOM entry. |
217
|
|
|
* @param ProjectBOMEntry $entry |
218
|
|
|
* @return float |
219
|
|
|
*/ |
220
|
|
|
public function getWithdrawAmountSum(ProjectBOMEntry $entry): float |
221
|
|
|
{ |
222
|
|
|
$this->ensureBOMEntryValid($entry); |
223
|
|
|
|
224
|
|
|
$sum = 0; |
225
|
|
|
foreach ($this->getPartLotsForBOMEntry($entry) as $lot) { |
226
|
|
|
$sum += $this->getLotWithdrawAmount($lot); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if ($entry->getPart() && !$entry->getPart()->useFloatAmount()) { |
230
|
|
|
$sum = round($sum); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $sum; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns the number of available lots to take stock from for the given BOM entry. |
238
|
|
|
* @param ProjectBOMEntry $entry |
239
|
|
|
* @return PartLot[]|null Returns null if the entry is a non-part BOM entry |
240
|
|
|
*/ |
241
|
|
|
public function getPartLotsForBOMEntry(ProjectBOMEntry $projectBOMEntry): ?array |
242
|
|
|
{ |
243
|
|
|
$this->ensureBOMEntryValid($projectBOMEntry); |
244
|
|
|
|
245
|
|
|
if ($projectBOMEntry->getPart() === null) { |
246
|
|
|
return null; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
//Filter out all lots which have unknown instock |
250
|
|
|
return $projectBOMEntry->getPart()->getPartLots()->filter(fn (PartLot $lot) => !$lot->isInstockUnknown())->toArray(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the needed amount of parts for the given BOM entry. |
255
|
|
|
* @param ProjectBOMEntry $entry |
256
|
|
|
* @return float |
257
|
|
|
*/ |
258
|
|
|
public function getNeededAmountForBOMEntry(ProjectBOMEntry $entry): float |
259
|
|
|
{ |
260
|
|
|
$this->ensureBOMEntryValid($entry); |
261
|
|
|
|
262
|
|
|
return $entry->getQuantity() * $this->number_of_builds; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the list of all bom entries that have to be build. |
267
|
|
|
* @return ProjectBOMEntry[] |
268
|
|
|
*/ |
269
|
|
|
public function getBomEntries(): array |
270
|
|
|
{ |
271
|
|
|
return $this->project->getBomEntries()->toArray(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Returns the all part bom entries that have to be build. |
276
|
|
|
* @return ProjectBOMEntry[] |
277
|
|
|
*/ |
278
|
|
|
public function getPartBomEntries(): array |
279
|
|
|
{ |
280
|
|
|
return $this->project->getBomEntries()->filter(function (ProjectBOMEntry $entry) { |
281
|
|
|
return $entry->isPartBomEntry(); |
282
|
|
|
})->toArray(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns which project should be build |
287
|
|
|
* @return Project |
288
|
|
|
*/ |
289
|
|
|
public function getProject(): Project |
290
|
|
|
{ |
291
|
|
|
return $this->project; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns the number of builds that should be created. |
296
|
|
|
* @return int |
297
|
|
|
*/ |
298
|
|
|
public function getNumberOfBuilds(): int |
299
|
|
|
{ |
300
|
|
|
return $this->number_of_builds; |
301
|
|
|
} |
302
|
|
|
} |