2222 */
2323namespace App \Services \Parts ;
2424
25+ use App \Entity \Parts \StorageLocation ;
2526use Symfony \Bundle \SecurityBundle \Security ;
2627use App \Entity \Parts \Category ;
2728use App \Entity \Parts \Footprint ;
3536use Symfony \Component \HttpFoundation \RedirectResponse ;
3637use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
3738use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
39+ use Symfony \Contracts \Translation \TranslatableInterface ;
40+
41+ use function Symfony \Component \Translation \t ;
3842
3943final class PartsTableActionHandler
4044{
@@ -61,8 +65,9 @@ public function idStringToArray(string $ids): array
6165 /**
6266 * @param Part[] $selected_parts
6367 * @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
6469 */
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
6671 {
6772 if ($ action === 'add_to_project ' ) {
6873 return new RedirectResponse (
@@ -161,6 +166,29 @@ public function handleAction(string $action, array $selected_parts, ?int $target
161166 $ this ->denyAccessUnlessGranted ('@measurement_units.read ' );
162167 $ part ->setPartUnit (null === $ target_id ? null : $ this ->entityManager ->find (MeasurementUnit::class, $ target_id ));
163168 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 ;
164192
165193 default :
166194 throw new InvalidArgumentException ('The given action is unknown! ( ' .$ action .') ' );
0 commit comments