ProjectReportController.php (4211B)
1 <?php 2 3 namespace PartKeepr\ProjectBundle\Controller; 4 5 use Dunglas\ApiBundle\Action\ActionUtilTrait; 6 use Dunglas\ApiBundle\Api\ResourceInterface; 7 use FOS\RestBundle\Controller\FOSRestController; 8 use PartKeepr\ProjectBundle\Entity\ProjectPart; 9 use PartKeepr\ProjectBundle\Entity\Report; 10 use Symfony\Component\HttpFoundation\Request; 11 12 class ProjectReportController extends FOSRestController 13 { 14 use ActionUtilTrait; 15 16 /** 17 * @param Request $request 18 * 19 * @throws \Exception Thrown if parameters are formatted incorrectly 20 * 21 * @return \Symfony\Component\HttpFoundation\Response 22 */ 23 public function createReportAction(Request $request) 24 { 25 /** 26 * @var ResourceInterface 27 */ 28 list($resourceType, $format) = $this->extractAttributes($request); 29 $report = $this->get("api.serializer")->deserialize( 30 $request->getContent(), 31 $resourceType->getEntityClass(), 32 $format, 33 $resourceType->getDenormalizationContext() 34 ); 35 36 /** 37 * @var $report Report 38 */ 39 foreach ($report->getReportProjects() as $reportProject) { 40 foreach ($reportProject->getProject()->getParts() as $projectPart) { 41 if ($projectPart->getOverageType() === ProjectPart::OVERAGE_TYPE_PERCENT) { 42 $overage = $reportProject->getQuantity() * $projectPart->getQuantity() * ($projectPart->getOverage( 43 ) / 100); 44 } else { 45 $overage = $projectPart->getOverage(); 46 } 47 48 $quantity = $reportProject->getQuantity() * $projectPart->getQuantity() + $overage; 49 $report->addPartQuantity($projectPart->getPart(), $projectPart, $quantity); 50 } 51 } 52 53 $this->get("doctrine.orm.default_entity_manager")->persist($report); 54 $this->get("doctrine.orm.default_entity_manager")->flush(); 55 56 $response = new \Symfony\Component\HttpFoundation\Response( 57 $this->get('serializer')->serialize( 58 $report, 59 'jsonld' 60 ) 61 ); 62 63 $response->headers->set("Content-Type", "text/json"); 64 65 return $response; 66 } 67 68 /** 69 * @param Request $request 70 * 71 * @throws \Exception Thrown if parameters are formatted incorrectly 72 * 73 * @return \Symfony\Component\HttpFoundation\Response 74 */ 75 public function getReportAction(Request $request, $id) 76 { 77 /** 78 * @var $report Report 79 */ 80 $report = $this->get("doctrine.orm.default_entity_manager")->getRepository( 81 "PartKeeprProjectBundle:Report" 82 )->find($id); 83 $this->calculateMissingParts($report); 84 $this->prepareMetaPartInformation($report); 85 86 $response = new \Symfony\Component\HttpFoundation\Response( 87 $this->get('serializer')->serialize( 88 $report, 89 'jsonld' 90 ) 91 ); 92 93 $response->headers->set("Content-Type", "text/json"); 94 95 return $response; 96 } 97 98 public function calculateMissingParts(Report $report) 99 { 100 foreach ($report->getReportParts() as $reportPart) { 101 $missing = $reportPart->getQuantity() - $reportPart->getPart()->getStockLevel(); 102 103 if ($missing < 0) { 104 $missing = 0; 105 } 106 107 $reportPart->setMissing($missing); 108 } 109 } 110 111 public function prepareMetaPartInformation(Report $report) 112 { 113 foreach ($report->getReportParts() as $reportPart) { 114 $subParts = []; 115 116 if ($reportPart->getPart()->isMetaPart()) { 117 $matchingParts = $this->container->get("partkeepr.part_service")->getMatchingMetaParts( 118 $reportPart->getPart() 119 ); 120 foreach ($matchingParts as $matchingPart) { 121 $subParts[] = $this->get('serializer')->normalize( 122 $matchingPart, 123 'jsonld' 124 ); 125 } 126 $reportPart->setMetaPart(true); 127 $reportPart->setSubParts($subParts); 128 } 129 } 130 } 131 }