Inheritance: extends PartKeepr\CoreBundle\Entity\BaseEntity
示例#1
0
 /**
  * @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();
 }
 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()));
 }
示例#3
0
 /**
  * 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;
 }
示例#4
0
 /**
  * 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;
 }
示例#5
0
 /**
  * Adds a new stock entry to this part
  *
  * @param StockEntry $stockEntry
  */
 public function addStockEntry(StockEntry $stockEntry)
 {
     $this->executeSaveListener();
     $stockEntry->setPart($this);
     $this->getStockLevels()->add($stockEntry);
 }
示例#6
0
 /**
  * Removes a stock entry from this part
  *
  * @param StockEntry $stockEntry
  */
 public function removeStockLevel($stockEntry)
 {
     $stockEntry->setPart(null);
     $this->stockLevels->removeElement($stockEntry);
 }