1 | <?php |
||||
2 | |||||
3 | namespace App\Services\Parts; |
||||
4 | |||||
5 | use App\Entity\LogSystem\PartStockChangedLogEntry; |
||||
6 | use App\Entity\Parts\Part; |
||||
7 | use App\Entity\Parts\PartLot; |
||||
8 | use App\Services\LogSystem\EventCommentHelper; |
||||
9 | use App\Services\LogSystem\EventLogger; |
||||
10 | |||||
11 | final class PartLotWithdrawAddHelper |
||||
12 | { |
||||
13 | private EventLogger $eventLogger; |
||||
14 | private EventCommentHelper $eventCommentHelper; |
||||
15 | |||||
16 | |||||
17 | public function __construct(EventLogger $eventLogger, EventCommentHelper $eventCommentHelper) |
||||
18 | { |
||||
19 | $this->eventLogger = $eventLogger; |
||||
20 | $this->eventCommentHelper = $eventCommentHelper; |
||||
21 | } |
||||
22 | |||||
23 | /** |
||||
24 | * Checks whether the given part can |
||||
25 | * @param PartLot $partLot |
||||
26 | * @return bool |
||||
27 | */ |
||||
28 | public function canAdd(PartLot $partLot): bool |
||||
29 | { |
||||
30 | //We cannot add or withdraw parts from lots with unknown instock value. |
||||
31 | if($partLot->isInstockUnknown()) { |
||||
32 | return false; |
||||
33 | } |
||||
34 | |||||
35 | //So far all other restrictions are defined at the storelocation level |
||||
36 | if($partLot->getStorageLocation() === null) { |
||||
37 | return true; |
||||
38 | } |
||||
39 | |||||
40 | //We can not add parts if the storage location of the lot is marked as full |
||||
41 | if($partLot->getStorageLocation()->isFull()) { |
||||
42 | return false; |
||||
43 | } |
||||
44 | |||||
45 | return true; |
||||
46 | } |
||||
47 | |||||
48 | public function canWithdraw(PartLot $partLot): bool |
||||
49 | { |
||||
50 | //We cannot add or withdraw parts from lots with unknown instock value. |
||||
51 | if ($partLot->isInstockUnknown()) { |
||||
52 | return false; |
||||
53 | } |
||||
54 | |||||
55 | //Part must contain more than 0 parts |
||||
56 | if ($partLot->getAmount() <= 0) { |
||||
57 | return false; |
||||
58 | } |
||||
59 | |||||
60 | return true; |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * Withdraw the specified amount of parts from the given part lot. |
||||
65 | * Please note that the changes are not flushed to DB yet, you have to do this yourself |
||||
66 | * @param PartLot $partLot The partLot from which the instock should be taken (which value should be decreased) |
||||
67 | * @param float $amount The amount of parts that should be taken from the part lot |
||||
68 | * @param string|null $comment The optional comment describing the reason for the withdrawal |
||||
69 | * @return PartLot The modified part lot |
||||
70 | */ |
||||
71 | public function withdraw(PartLot $partLot, float $amount, ?string $comment = null): PartLot |
||||
72 | { |
||||
73 | //Ensure that amount is positive |
||||
74 | if ($amount <= 0) { |
||||
75 | throw new \InvalidArgumentException('Amount must be positive'); |
||||
76 | } |
||||
77 | |||||
78 | $part = $partLot->getPart(); |
||||
79 | |||||
80 | //Check whether we have to round the amount |
||||
81 | if (!$part->useFloatAmount()) { |
||||
82 | $amount = round($amount); |
||||
83 | } |
||||
84 | |||||
85 | //Ensure that we can withdraw from the part lot |
||||
86 | if (!$this->canWithdraw($partLot)) { |
||||
87 | throw new \RuntimeException("Cannot withdraw from this part lot!"); |
||||
88 | } |
||||
89 | |||||
90 | //Ensure that there is enough stock to withdraw |
||||
91 | if ($amount > $partLot->getAmount()) { |
||||
92 | throw new \RuntimeException('Not enough stock to withdraw!'); |
||||
93 | } |
||||
94 | |||||
95 | //Subtract the amount from the part lot |
||||
96 | $oldAmount = $partLot->getAmount(); |
||||
97 | $partLot->setAmount($oldAmount - $amount); |
||||
98 | |||||
99 | $event = PartStockChangedLogEntry::withdraw($partLot, $oldAmount, $partLot->getAmount(), $part->getAmountSum() , $comment); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
100 | $this->eventLogger->log($event); |
||||
101 | |||||
102 | //Apply the comment also to global events, so it gets associated with the elementChanged log entry |
||||
103 | if (!$this->eventCommentHelper->isMessageSet() && !empty($comment)) { |
||||
104 | $this->eventCommentHelper->setMessage($comment); |
||||
105 | } |
||||
106 | |||||
107 | return $partLot; |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * Add the specified amount of parts to the given part lot. |
||||
112 | * Please note that the changes are not flushed to DB yet, you have to do this yourself |
||||
113 | * @param PartLot $partLot The partLot from which the instock should be taken (which value should be decreased) |
||||
114 | * @param float $amount The amount of parts that should be taken from the part lot |
||||
115 | * @param string|null $comment The optional comment describing the reason for the withdrawal |
||||
116 | * @return PartLot The modified part lot |
||||
117 | */ |
||||
118 | public function add(PartLot $partLot, float $amount, ?string $comment = null): PartLot |
||||
119 | { |
||||
120 | if ($amount <= 0) { |
||||
121 | throw new \InvalidArgumentException('Amount must be positive'); |
||||
122 | } |
||||
123 | |||||
124 | $part = $partLot->getPart(); |
||||
125 | |||||
126 | //Check whether we have to round the amount |
||||
127 | if (!$part->useFloatAmount()) { |
||||
128 | $amount = round($amount); |
||||
129 | } |
||||
130 | |||||
131 | //Ensure that we can add to the part lot |
||||
132 | if (!$this->canAdd($partLot)) { |
||||
133 | throw new \RuntimeException("Cannot add to this part lot!"); |
||||
134 | } |
||||
135 | |||||
136 | $oldAmount = $partLot->getAmount(); |
||||
137 | $partLot->setAmount($oldAmount + $amount); |
||||
138 | |||||
139 | $event = PartStockChangedLogEntry::add($partLot, $oldAmount, $partLot->getAmount(), $part->getAmountSum() , $comment); |
||||
0 ignored issues
–
show
It seems like
$comment can also be of type null ; however, parameter $comment of App\Entity\LogSystem\Par...kChangedLogEntry::add() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
140 | $this->eventLogger->log($event); |
||||
141 | |||||
142 | //Apply the comment also to global events, so it gets associated with the elementChanged log entry |
||||
143 | if (!$this->eventCommentHelper->isMessageSet() && !empty($comment)) { |
||||
144 | $this->eventCommentHelper->setMessage($comment); |
||||
145 | } |
||||
146 | |||||
147 | return $partLot; |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * Move the specified amount of parts from the given source part lot to the given target part lot. |
||||
152 | * Please note that the changes are not flushed to DB yet, you have to do this yourself |
||||
153 | * @param PartLot $origin The part lot from which the parts should be taken |
||||
154 | * @param PartLot $target The part lot to which the parts should be added |
||||
155 | * @param float $amount The amount of parts that should be moved |
||||
156 | * @param string|null $comment A comment describing the reason for the move |
||||
157 | * @return void |
||||
158 | */ |
||||
159 | public function move(PartLot $origin, PartLot $target, float $amount, ?string $comment = null): void |
||||
160 | { |
||||
161 | if ($amount <= 0) { |
||||
162 | throw new \InvalidArgumentException('Amount must be positive'); |
||||
163 | } |
||||
164 | |||||
165 | $part = $origin->getPart(); |
||||
166 | |||||
167 | //Ensure that both part lots belong to the same part |
||||
168 | if($origin->getPart() !== $target->getPart()) { |
||||
169 | throw new \RuntimeException("Cannot move instock between different parts!"); |
||||
170 | } |
||||
171 | |||||
172 | //Check whether we have to round the amount |
||||
173 | if (!$part->useFloatAmount()) { |
||||
174 | $amount = round($amount); |
||||
175 | } |
||||
176 | |||||
177 | //Ensure that we can withdraw from origin and add to target |
||||
178 | if (!$this->canWithdraw($origin) || !$this->canAdd($target)) { |
||||
179 | throw new \RuntimeException("Cannot move instock between these part lots!"); |
||||
180 | } |
||||
181 | |||||
182 | //Ensure that there is enough stock to withdraw |
||||
183 | if ($amount > $origin->getAmount()) { |
||||
184 | throw new \RuntimeException('Not enough stock to withdraw!'); |
||||
185 | } |
||||
186 | |||||
187 | $oldOriginAmount = $origin->getAmount(); |
||||
188 | |||||
189 | //Subtract the amount from the part lot |
||||
190 | $origin->setAmount($origin->getAmount() - $amount); |
||||
191 | //And add it to the target |
||||
192 | $target->setAmount($target->getAmount() + $amount); |
||||
193 | |||||
194 | $event = PartStockChangedLogEntry::move($origin, $oldOriginAmount, $origin->getAmount(), $part->getAmountSum() , $comment, $target); |
||||
0 ignored issues
–
show
It seems like
$comment can also be of type null ; however, parameter $comment of App\Entity\LogSystem\Par...ChangedLogEntry::move() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
195 | $this->eventLogger->log($event); |
||||
196 | |||||
197 | //Apply the comment also to global events, so it gets associated with the elementChanged log entry |
||||
198 | if (!$this->eventCommentHelper->isMessageSet() && !empty($comment)) { |
||||
199 | $this->eventCommentHelper->setMessage($comment); |
||||
200 | } |
||||
201 | } |
||||
202 | } |