/**
* @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();
}