22
22
*/
23
23
namespace App \Services \Parts ;
24
24
25
+ use App \Entity \Parts \StorageLocation ;
25
26
use Symfony \Bundle \SecurityBundle \Security ;
26
27
use App \Entity \Parts \Category ;
27
28
use App \Entity \Parts \Footprint ;
35
36
use Symfony \Component \HttpFoundation \RedirectResponse ;
36
37
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
37
38
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
39
+ use Symfony \Contracts \Translation \TranslatableInterface ;
40
+
41
+ use function Symfony \Component \Translation \t ;
38
42
39
43
final class PartsTableActionHandler
40
44
{
@@ -61,8 +65,9 @@ public function idStringToArray(string $ids): array
61
65
/**
62
66
* @param Part[] $selected_parts
63
67
* @return RedirectResponse|null Returns a redirect response if the user should be redirected to another page, otherwise null
68
+ * @phpstan-param-out array<array{'part': Part, 'message': string|TranslatableInterface}> $errors
64
69
*/
65
- public function handleAction (string $ action , array $ selected_parts , ?int $ target_id , ?string $ redirect_url = null ): ?RedirectResponse
70
+ public function handleAction (string $ action , array $ selected_parts , ?int $ target_id , ?string $ redirect_url = null , array & $ errors = [] ): ?RedirectResponse
66
71
{
67
72
if ($ action === 'add_to_project ' ) {
68
73
return new RedirectResponse (
@@ -161,6 +166,29 @@ public function handleAction(string $action, array $selected_parts, ?int $target
161
166
$ this ->denyAccessUnlessGranted ('@measurement_units.read ' );
162
167
$ part ->setPartUnit (null === $ target_id ? null : $ this ->entityManager ->find (MeasurementUnit::class, $ target_id ));
163
168
break ;
169
+ case 'change_location ' :
170
+ $ this ->denyAccessUnlessGranted ('@storelocations.read ' );
171
+ //Retrieve the first part lot and set the location for it
172
+ $ part_lots = $ part ->getPartLots ();
173
+ if ($ part_lots ->count () > 0 ) {
174
+ if ($ part_lots ->count () > 1 ) {
175
+ $ errors [] = [
176
+ 'part ' => $ part ,
177
+ 'message ' => t ('parts.table.action_handler.error.part_lots_multiple ' ),
178
+ ];
179
+ break ;
180
+ }
181
+
182
+ $ part_lot = $ part_lots ->first ();
183
+ $ part_lot ->setStorageLocation (null === $ target_id ? null : $ this ->entityManager ->find (StorageLocation::class, $ target_id ));
184
+ } else { //Create a new part lot if there are none
185
+ $ part_lot = new PartLot ();
186
+ $ part_lot ->setPart ($ part );
187
+ $ part_lot ->setInstockUnknown (true ); //We do not know how many parts are in stock, so we set it to true
188
+ $ part_lot ->setStorageLocation (null === $ target_id ? null : $ this ->entityManager ->find (StorageLocation::class, $ target_id ));
189
+ $ this ->entityManager ->persist ($ part_lot );
190
+ }
191
+ break ;
164
192
165
193
default :
166
194
throw new InvalidArgumentException ('The given action is unknown! ( ' .$ action .') ' );
0 commit comments