Negative values means part removal, positive values means part adding.
public setStockLevel ( integer $stockLevel ) | ||
$stockLevel | integer | The stock level |
/** * @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 = $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 = $iriConverter->getItemFromIri($removal->part); $stock = new StockEntry(); $stock->setStockLevel(0 - intval($removal->amount)); $stock->setUser($user); if (property_exists($removal, 'comment')) { $stock->setComment($removal->comment); } $part->addStockLevel($stock); } $this->get('doctrine.orm.entity_manager')->flush(); }
public function testStockHistory() { $client = static::makeClient(true); /** * @var $part1 Part */ $part1 = $this->fixtures->getReference("part.1"); $stockLevel = new StockEntry(); $stockLevel->setPart($part1); $stockLevel->setStockLevel(5); $fosUser = $this->fixtures->getReference("user.admin"); $userService = $this->getContainer()->get("partkeepr.userservice"); $user = $userService->getProxyUser($fosUser->getUsername(), $userService->getBuiltinProvider(), true); $stockLevel->setUser($user); $part1->addStockLevel($stockLevel); $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush(); $iriCoverter = $this->getContainer()->get("api.iri_converter"); $iri = $iriCoverter->getIriFromItem($part1); $client->request("GET", $iri); $response = $client->getResponse()->getContent(); $responseObj = json_decode($response, true); $responseObj["stockLevels"] = array(); $client->request("PUT", $iri, array(), array(), array(), json_encode($responseObj)); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->getContainer()->get("doctrine.orm.default_entity_manager")->refresh($part1); $this->assertEquals(1, count($part1->getStockLevels())); }
/** * 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(); $stock = new StockEntry(); $stock->setStockLevel(0 - intval($quantity)); $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; }
/** * 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(); $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; }