/**
  * @param \DLigo\Animaltool\Domain\Model\Animal $animal
  * @param array $treatment
  * @param string $tag
  * @param string $passId
  * @param string $contunue   
  * @param string $release   
  * @param string $extern   
  * @param string $newextern   
  * @param string $dead   
  * @param string $origRFID   
  * @param string $origEarTag   
  * @Flow\Validate (argumentName="$treatment", type="\DLigo\Animaltool\Validation\Validator\TreatmentValidator")
  * @Flow\Validate (argumentName="$animal.healthCondition",type="NotEmpty")   
  * @Flow\Validate (argumentName="$animal.photo", type="NotEmpty")
  * @return void
  */
 public function treatAction(Animal $animal, $treatment, $tag = '', $passId = '', $continue = null, $release = null, $extern = null, $newextern = null, $dead = null, $origRFID = '', $origEarTag = '')
 {
     $duplicate = false;
     //    \TYPO3\Flow\var_dump($this->request);
     $done = false;
     $mergeMessage = '';
     $merge = '';
     $tr = $animal->getOpenTreatment();
     if (!$tr) {
         $tr = new \DLigo\Animaltool\Domain\Model\Treatment();
         $tr->setStartDate(new \DateTime('now'));
         $tr->setAnimal($animal);
         //      $tr->setDoctor($this->session->getUser());
         $animal->getTreatments()->add($tr);
     }
     $did = isset($treatment['doctor']['__identity']) ? $treatment['doctor']['__identity'] : $treatment['doctor'];
     $tr->setDoctor($this->userRepository->findByIdentifier($did));
     $therapies = new \Doctrine\Common\Collections\ArrayCollection();
     $extras = new \Doctrine\Common\Collections\ArrayCollection();
     if (is_array($treatment['therapies'])) {
         foreach ($treatment['therapies'] as $thid) {
             $therapy = $this->therapyRepository->findByIdentifier($thid);
             if ($therapy->getHasExtraData()) {
                 $extra = $this->treatmentExtraRepository->findByTherapyAndTreatment($thid, $tr)->getFirst();
                 if (!$extra) {
                     $extra = new \DLigo\Animaltool\Domain\Model\TreatmentExtra();
                     $extra->setTreatment($tr);
                     $extra->setTherapy($therapy);
                 }
                 $extra->setValue($treatment['treatmentExtra'][$thid]);
                 unset($treatment['treatmentExtra'][$thid]);
                 $extras->add($extra);
             }
             $therapies->add($therapy);
         }
     }
     $tr->setTherapies($therapies);
     $tr->setTreatmentExtras($extras);
     $tr->setComment($treatment['comment']);
     if ($this->persistenceManager->isNewObject($tr)) {
         $this->treatmentRepository->add($tr);
     } else {
         $this->treatmentRepository->update($tr);
     }
     if (isset($treatment['treatmentExtra']) && is_array($treatment['treatmentExtra'])) {
         foreach ($treatment['treatmentExtra'] as $thid => $value) {
             $extra = $this->treatmentExtraRepository->findByTherapyAndTreatment($thid, $tr)->getFirst();
             if ($extra != null) {
                 $this->treatmentExtraRepository->remove($extra);
             }
         }
     }
     if ($continue) {
         $animal->setStayStatus(Animal::THERAPY);
         $animal->setTherapyStatus(Animal::OPERATION);
         $animal->stopOpenExternals();
         $animal->stopOpenTreatments();
     } elseif ($release) {
         $animal->setStayStatus(Animal::RELEASING);
         $animal->setTherapyStatus(Animal::READY);
         $animal->stopOpenExternals();
         $animal->stopOpenTreatments();
         $animal->getLastAction()->setReleaseDate(new \DateTime('now'));
     } elseif ($extern) {
         if ($animal->getStayStatus() == Animal::CATCHED) {
             $animal->setStayStatus(Animal::THERAPY);
         }
         if ($animal->getTherapyStatus() == Animal::WAIT) {
             $animal->setTherapyStatus(Animal::OPERATION);
         }
         $animal->stopOpenTreatments();
     } elseif ($newextern) {
         $animal->stopOpenExternals();
         $animal->stopOpenTreatments();
     } elseif ($dead) {
         $animal->setStayStatus(Animal::RELEASED);
         $animal->setTherapyStatus(Animal::DEAD);
         $animal->stopOpenExternals();
         $animal->stopOpenTreatments();
     }
     $oldAnimal = null;
     if ($animal->getIsPrivate()) {
         $animal->setRFID($tag);
         if ($tag != $origRFID && !empty($tag)) {
             $oldAnimal = $this->animalRepository->findByRFID($tag)->getFirst();
         }
         $animal->getOwner()->setPassId($passId);
     } else {
         $animal->setEarTag($tag);
         if ($tag != $origEarTag && !empty($tag)) {
             $oldAnimal = $this->animalRepository->findByEarTag($tag)->getFirst();
         }
     }
     if (!empty($oldAnimal)) {
         $tag = "_DUMMY_" . $tag . "_" . time();
         $duplicate = true;
         $animal->setIsDummy(true);
         $animal->setRFID($tag);
     }
     $this->animalRepository->update($animal);
     if ($duplicate) {
         $this->redirect('duplicate', null, null, array('animal' => $animal, 'oldAnimal' => $oldAnimal, 'external' => $extern != null));
     }
     if ($extern || $newextern) {
         $this->redirect('open', 'External', null, array('animal' => $animal));
     }
     $this->addFlashMessage('Updated the animal.' . $mergeMessage, '', \TYPO3\Flow\Error\Message::SEVERITY_OK, array(), 'flash.animal.update' . $merge);
     $this->redirect('index');
     return '';
 }
 public function addAction()
 {
     $currentUser = $this->authenticationManager->getSecurityContext()->getAccount()->getParty();
     $photoInfo = $this->request->getArgument("photo");
     $actionData = $this->request->getArgument("action");
     $actionData = json_decode($actionData, true);
     $animalData = $this->request->getArgument("animal");
     $animalData = json_decode($animalData, true);
     $ownerData = $this->request->getArgument("owner");
     $ownerData = json_decode($ownerData, true);
     $photoData = $this->request->getArgument("photo");
     $action = $this->propertyMapper->convert($actionData, 'DLigo\\Animaltool\\Domain\\Model\\Action');
     $action->setDate(new \DateTime('now'));
     $user = $this->propertyMapper->convert($actionData['team'], 'DLigo\\Animaltool\\Domain\\Model\\User');
     $lastId = $user->getLastBoxID();
     $action->setTeam($user);
     $box = explode('-', $actionData['boxID']);
     if ($lastId > $box[1]) {
         $action->setBoxID($user->getTeamID() . '-' . ($lastId + 1));
     }
     $animal = $this->propertyMapper->convert($animalData, 'DLigo\\Animaltool\\Domain\\Model\\Animal');
     $birthday = \DateTime::createFromFormat("U", $animalData["birthday"]);
     if (!empty($animalData["birthday"])) {
         $birthday = \DateTime::createFromFormat("U", $animalData["birthday"]);
         $birthday->setTime(0, 0, 0);
         $animal->setBirthday($birthday);
     }
     $action->setAnimal($animal);
     $owner = null;
     if (isset($animalData["isPrivate"]) && $animalData["isPrivate"]) {
         $owner = $this->propertyMapper->convert($ownerData, 'DLigo\\Animaltool\\Domain\\Model\\Owner');
         $animal->setOwner($owner);
         $animal->setEarTag(null);
         if (!empty($animalData["earTag"])) {
             $rfid = $animalData["earTag"];
             $oldAnimal = $this->animalRepository->findOneByRFID($rfid);
             if ($oldAnimal == null) {
                 $animal->setRFID($rfid);
             }
         }
     } else {
         if (!empty($animalData["earTag"])) {
             $eartag = $animalData["earTag"];
             $oldAnimal = $this->animalRepository->findOneByEarTag($eartag);
             if ($oldAnimal != null) {
                 $animal->setEarTag(null);
             }
         }
     }
     $photo = $this->resourceManager->importUploadedResource($photoInfo);
     //$this->systemLogger->log(\TYPO3\Flow\var_dump($photo,"Photo",true,true),LOG_INFO);
     $animal->setPhoto($photo);
     if ($owner) {
         $this->ownerRepository->add($owner);
     }
     $this->animalRepository->add($animal);
     $this->actionRepository->add($action);
     $this->userRepository->update($user);
     $this->persistenceManager->persistAll();
     $this->response->setStatus(201);
     echo "{" . '"lastID": ' . $currentUser->getLastBoxID() . "}";
     flush();
     ob_flush();
 }