/**
  * 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);
 }
示例#2
0
 /**
  * Processes the result after it was retrieved. In the default configuration, it returns an array result, or
  * if no query fields are specified, it tries to serialize all objects.
  */
 protected function getResult(Query $query)
 {
     $result = parent::getResult($query);
     $partIds = array();
     foreach ($result as $key => $item) {
         $partIds[] = $item["id"];
     }
     $attachmentCounts = $this->getAttachmentCounts($partIds);
     $projects = $this->getProjects($partIds);
     foreach ($result as $key => $item) {
         if (array_key_exists($item["id"], $attachmentCounts)) {
             $result[$key]["attachmentCount"] = $attachmentCounts[$item["id"]];
         } else {
             $result[$key]["attachmentCount"] = 0;
         }
         if (array_key_exists($item["id"], $projects)) {
             $result[$key]["projects"] = implode(", ", $projects[$item["id"]]);
         }
         $result[$key]["createDate"] = $result[$key]["createDate"]->format("Y-m-d H:i:s");
         $part = Part::loadById($item["id"]);
         $result[$key]["attachments"] = $part->serializeChildren($part->getAttachments());
     }
     return $result;
 }
示例#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();
 }
示例#4
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;
         }
     }
 }