/** * @Routing\Route("/api/parts/massRemoveStock", defaults={"method" = "get","_format" = "json"}) * @View() * @param Request $request * @throws \Exception Thrown if parameters are formatted incorrectly */ public function massRemoveStockAction(Request $request) { $removals = json_decode($request->get("removals")); if (!is_array($removals)) { throw new \Exception("removals parameter must be an array"); } /** * @var IriConverter $iriConverter */ $iriConverter = $this->get("api.iri_converter"); $user = $this->get("partkeepr.userservice")->getUser(); foreach ($removals as $removal) { if (!property_exists($removal, "part")) { throw new \Exception("Each removal must have the part property defined"); } if (!property_exists($removal, "amount")) { throw new \Exception("Each removal must have the amount property defined"); } /** * @var Part $part */ $part = $iriConverter->getItemFromIri($removal->part); $stock = new StockEntry(0 - intval($removal->amount), $user); if (!property_exists($removal, "comment")) { $stock->setComment($removal->comment); } $part->addStockEntry($stock); } $this->get("doctrine.orm.entity_manager")->flush(); }
/** * Retrieves a collection of resources. * * @param Request $request The request * @param int $id The ID of the part * * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable * * @throws RuntimeException|RootNodeNotFoundException */ public function __invoke(Request $request, $id) { list($resourceType) = $this->extractAttributes($request); $part = $this->getItem($this->dataProvider, $resourceType, $id); /** * @var $part Part */ $quantity = $request->request->get("quantity"); $user = $this->userService->getUser(); $stock = new StockEntry(0 - intval($quantity), $user); if ($request->request->has("comment") && $request->request->get("comment") !== null) { $stock->setComment($request->request->get("comment")); } $part->addStockEntry($stock); $this->registry->getManager()->persist($stock); $this->registry->getManager()->flush(); return $part; }
/** * Retrieves a collection of resources. * * @param Request $request The request * @param int $id The ID of the part * * @throws RuntimeException|RootNodeNotFoundException * * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable */ public function __invoke(Request $request, $id) { list($resourceType) = $this->extractAttributes($request); $part = $this->getItem($this->dataProvider, $resourceType, $id); /* * @var $part Part */ $quantity = $request->request->get('quantity'); $user = $this->userService->getUser(); $oldQuantity = $part->getStockLevel(); $correctionQuantity = $quantity - $oldQuantity; if ($correctionQuantity != 0) { $stock = new StockEntry(); $stock->setStockLevel($correctionQuantity); $stock->setUser($user); if ($request->request->has('comment') && $request->request->get('comment') !== null) { $stock->setComment($request->request->get('comment')); } $part->addStockLevel($stock); $this->registry->getManager()->persist($stock); $this->registry->getManager()->flush(); } return $part; }