PartController.php (5810B)
1 <?php 2 3 namespace PartKeepr\PartBundle\Controller; 4 5 use Dunglas\ApiBundle\Api\IriConverter; 6 use FOS\RestBundle\Controller\Annotations\View; 7 use FOS\RestBundle\Controller\FOSRestController; 8 use PartKeepr\PartBundle\Entity\Part; 9 use PartKeepr\ProjectBundle\Entity\Project; 10 use PartKeepr\ProjectBundle\Entity\ProjectRun; 11 use PartKeepr\ProjectBundle\Entity\ProjectRunPart; 12 use PartKeepr\StockBundle\Entity\StockEntry; 13 use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing; 14 use Symfony\Component\HttpFoundation\Request; 15 16 class PartController extends FOSRestController 17 { 18 /** 19 * @Routing\Route("/api/parts/massRemoveStock", defaults={"method" = "get","_format" = "json"}) 20 * @View() 21 * 22 * @param Request $request 23 * 24 * @throws \Exception Thrown if parameters are formatted incorrectly 25 */ 26 public function massRemoveStockAction(Request $request) 27 { 28 /** 29 * @var IriConverter 30 */ 31 $iriConverter = $this->get('api.iri_converter'); 32 33 $removals = json_decode($request->get('removals')); 34 35 if (!is_array($removals)) { 36 throw new \Exception('removals parameter must be an array'); 37 } 38 39 $projects = json_decode($request->get('projects')); 40 41 if (!is_array($projects)) { 42 throw new \Exception('projects parameter must be an array'); 43 } 44 45 /** 46 * @var ProjectRun[] $projectRuns 47 */ 48 $projectRuns = []; 49 50 foreach ($projects as $projectInfo) { 51 /** 52 * @var Project $project 53 */ 54 $project = $iriConverter->getItemFromIri($projectInfo->project); 55 56 $projectRun = new ProjectRun(); 57 $projectRun->setQuantity($projectInfo->quantity); 58 $projectRun->setRunDateTime(new \DateTime()); 59 $projectRun->setProject($project); 60 61 $projectRuns[$projectInfo->project] = $projectRun; 62 } 63 64 $user = $this->get('partkeepr.userservice')->getUser(); 65 66 foreach ($removals as $removal) { 67 if (!property_exists($removal, 'part')) { 68 throw new \Exception('Each removal must have the part property defined'); 69 } 70 71 if (!property_exists($removal, 'amount')) { 72 throw new \Exception('Each removal must have the amount property defined'); 73 } 74 75 if (!property_exists($removal, 'lotNumber')) { 76 throw new \Exception('Each removal must have the lotNumber property defined'); 77 } 78 79 /** 80 * @var Part $part 81 */ 82 $part = $iriConverter->getItemFromIri($removal->part); 83 84 $stock = new StockEntry(); 85 $stock->setStockLevel(0 - intval($removal->amount)); 86 $stock->setUser($user); 87 88 if (property_exists($removal, 'comment')) { 89 $stock->setComment($removal->comment); 90 } 91 92 $part->addStockLevel($stock); 93 94 $projectRunPart = new ProjectRunPart(); 95 $projectRunPart->setLotNumber($removal->lotNumber); 96 $projectRunPart->setPart($part); 97 $projectRunPart->setQuantity($removal->amount); 98 99 foreach ($projectRuns as $projectRun) { 100 $projectRun->addPart($projectRunPart); 101 } 102 } 103 104 foreach ($projectRuns as $projectRun) { 105 $this->get('doctrine.orm.entity_manager')->persist($projectRun); 106 } 107 108 $this->get('doctrine.orm.entity_manager')->flush(); 109 } 110 111 /** 112 * @Routing\Route("/api/parts/getPartParameterNames", defaults={"method" = "get","_format" = "json"}) 113 * @View() 114 * 115 * @return array An array with name and description properties 116 */ 117 public function getParameterNamesAction() 118 { 119 $dql = "SELECT p.name, p.description, p.valueType, u.name AS unitName, u.symbol AS unitSymbol FROM PartKeepr\PartBundle\Entity\PartParameter p LEFT JOIN p.unit u GROUP BY p.name, p.description, p.valueType, u.name, u.symbol"; 120 121 $query = $this->get("doctrine.orm.default_entity_manager")->createQuery($dql); 122 123 return $query->getArrayResult(); 124 } 125 126 /** 127 * @Routing\Route("/api/parts/getPartParameterValues", defaults={"method" = "get","_format" = "json"}) 128 * @View() 129 * 130 * @param $request Request The Request to process 131 * 132 * @return array An array with name and description properties 133 */ 134 public function getParameterValuesAction(Request $request) 135 { 136 if (!$request->query->has("name")) { 137 throw new \InvalidArgumentException("The parameter 'name' must be given"); 138 } 139 140 if (!$request->query->has("valueType")) { 141 throw new \InvalidArgumentException("The parameter 'valueType' must be given"); 142 } 143 144 if ($request->query->get("valueType") == "string") { 145 $dql = "SELECT p.stringValue AS value FROM PartKeepr\PartBundle\Entity\PartParameter p WHERE p.name = :name AND p.valueType = :valueType GROUP BY p.stringValue"; 146 $query = $this->get("doctrine.orm.default_entity_manager")->createQuery($dql); 147 $query->setParameter("name", $request->query->get("name")); 148 $query->setParameter("valueType", $request->query->get("valueType")); 149 150 return $query->getArrayResult(); 151 } else { 152 $dql = "SELECT p.value FROM PartKeepr\PartBundle\Entity\PartParameter p WHERE p.name = :name AND p.valueType = :valueType GROUP BY p.value"; 153 154 $query = $this->get("doctrine.orm.default_entity_manager")->createQuery($dql); 155 $query->setParameter("name", $request->query->get("name")); 156 $query->setParameter("valueType", $request->query->get("valueType")); 157 158 return $query->getArrayResult(); 159 } 160 } 161 }