/**
  * Verifies that querying for a part works correctly.
  * 
  * There was a bug where only lower-case strings matched against parts, fixed 2012-03-16.
  */
 public function testPartNameQuerying()
 {
     $partName = "testPartNameQuerying";
     $part = new Part();
     $part->setName($partName);
     $part->setCategory(PartCategoryManager::getInstance()->getRootNode()->getNode());
     $part->setStorageLocation(StorageLocationManager::getInstance()->getStorageLocation(self::$storageLocation));
     PartKeepr::getEM()->persist($part);
     PartKeepr::getEM()->flush();
     $service = new PartService(array("query" => $partName));
     $response = $service->get();
     $this->assertEquals(1, $response["totalCount"], "The resultset totalCount is wrong.");
     $this->assertEquals($response["data"][0]["name"], $part->getName());
 }
 /**
  * Returns a project report.
  * 
  * The input format is an array with the following keys per entry:
  * - project: The project ID
  * - amount: Specifies how many copies of the project need to be reported
  * 
  * The output format is an array which contains the following keys:
  * - quantity: The overall quantity of parts needed (for a specific part)
  * - part: The serialized part entity
  * - storageLocation_name: The storage location name
  * - available: The overall amount of available parts
  * - sum_order: Always set to 0 because calculation happens in the frontend
  * 
  * @see PartKeepr\Service.RestfulService::get()
  */
 public function get()
 {
     $reports = json_decode($this->getParameter("reports"), true);
     $aPartResults = array();
     // Loop over all reports and calculate the overall quantities
     foreach ($reports as $report) {
         $dql = "SELECT pp.quantity, pro.name AS projectname, pp.remarks, p.id FROM ";
         $dql .= "PartKeepr\\Project\\ProjectPart pp JOIN pp.part p ";
         $dql .= "JOIN pp.project pro WHERE pp.project = :project";
         $query = PartKeepr::getEM()->createQuery($dql);
         $query->setParameter("project", $report["project"]);
         foreach ($query->getArrayResult() as $result) {
             $part = Part::loadById($result["id"]);
             if (array_key_exists($result["id"], $aPartResults)) {
                 // Only update the quantity of the part
                 $aPartResults[$result["id"]]["quantity"] += $result["quantity"] * $report["amount"];
                 $aPartResults[$result["id"]]["projects"][] = $result["projectname"];
                 if ($result["remarks"] != "") {
                     $aPartResults[$result["id"]]["remarks"][] = $result["projectname"] . ": " . $result["remarks"];
                 }
             } else {
                 // Create a full resultset
                 $aPartResults[$result["id"]] = array("quantity" => $result["quantity"] * $report["amount"], "part" => array("response" => array("totalCount" => 1, "data" => $part->serialize())), "storageLocation_name" => $part->getStorageLocation()->getName(), "available" => $part->getStockLevel(), "sum_order" => 0, "projects" => array($result["projectname"]), "remarks" => array());
                 if ($result["remarks"] != "") {
                     $aPartResults[$result["id"]]["remarks"] = array($result["projectname"] . ": " . $result["remarks"]);
                 }
             }
         }
     }
     $aFinalResult = array();
     // Iterate over all results and calculate how many parts are missing
     foreach ($aPartResults as $key => $partResult) {
         $missing = $partResult["quantity"] - $partResult["available"];
         if ($missing < 0) {
             $missing = 0;
         }
         $partResult["missing"] = $missing;
         $partResult["remarks"] = implode(", ", $partResult["remarks"]);
         $partResult["projects"] = implode(", ", $partResult["projects"]);
         $aFinalResult[] = $partResult;
     }
     return array("data" => $aFinalResult);
 }
Beispiel #3
0
 public function movePart()
 {
     $this->requireParameter("targetCategory");
     if ($this->getParameter("parts", false) !== false) {
         /* We are moving multiple parts */
         foreach ($this->getParameter("parts") as $part) {
             $part = Part::loadById($part);
             $category = PartCategory::loadById($this->getParameter("targetCategory"));
             $part->setCategory($category);
         }
     } else {
         $part = Part::loadById($this->getParameter("part"));
         $category = PartCategory::loadById($this->getParameter("targetCategory"));
         $part->setCategory($category);
     }
     PartKeepr::getEM()->flush();
 }
Beispiel #4
0
 private function processAttachmentChanges(Part $part, array $data)
 {
     if (array_key_exists("updates", $data)) {
         foreach ($data["updates"] as $record) {
             foreach ($part->getAttachments() as $partAttachment) {
                 if ($partAttachment->getId() == $record["id"]) {
                     $partAttachment->setDescription($record["description"]);
                     break;
                 }
             }
         }
     }
     if (array_key_exists("removals", $data)) {
         foreach ($data["removals"] as $record) {
             foreach ($part->getAttachments() as $partAttachment) {
                 if ($partAttachment->getId() == $record["id"]) {
                     PartKeepr::getEM()->remove($partAttachment);
                     $part->getAttachments()->removeElement($partAttachment);
                     break;
                 }
             }
         }
     }
     if (array_key_exists("inserts", $data)) {
         foreach ($data["inserts"] as $record) {
             $attachment = new PartAttachment();
             $attachment->setPart($part);
             $attachment->setDescription($record["description"]);
             $file = TempUploadedFile::loadById($record["tmp_id"]);
             $attachment->replace($file->getFilename());
             $attachment->setOriginalFilename($file->getOriginalFilename());
             $part->getAttachments()->add($attachment);
         }
     }
 }
Beispiel #5
0
 /**
  * Deserializes the project
  * @param array $parameters The array with the parameters to set
  */
 public function deserialize(array $parameters)
 {
     foreach ($parameters as $key => $value) {
         switch ($key) {
             case "remarks":
                 $this->setRemarks($value);
                 break;
             case "quantity":
                 $this->setQuantity($value);
                 break;
             case "part_id":
                 $part = Part::loadById($value);
                 $this->setPart($part);
                 break;
         }
     }
 }