src/Controller/MedComController.php line 304

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. #use Symfony\Component\HTTPFoundation\Response;
  4. #use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use phpseclib\Net\SFTP;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use App\Entity\MedComCorrespondence;
  10. use App\Entity\MedComMessages;
  11. use App\Entity\Patient;
  12. use App\Entity\Dialogue;
  13. use App\Entity\User;
  14. use App\Entity\JoMasterInformation;
  15. use App\Entity\JoPostMail;
  16. use App\Entity\JoPostMailAttachment;
  17. use App\Entity\PredefinedAttachmentType;
  18. use App\Entity\PredefinedCaseCard;
  19. use App\Entity\DialogueCaseCard;
  20. use App\Entity\DialogueCaseCardMeasure;
  21. use App\Entity\DialogueAttachmentDetails;
  22. use App\Entity\CasecardMeasure;
  23. use App\Entity\DialogueAttachments;
  24. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  25. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  26. class MedComController extends AbstractController {
  27.     private $EANS "";
  28.     private $medcomID "";
  29.     private $medcomIDCode "";
  30.     private $orgName "";
  31.     private $departmentName "";
  32.     private $unitName "";
  33.     private $districtName "";
  34.     private $postalCode "";
  35.     private $address "";
  36.     private $city "";
  37.     
  38.     private $Info;
  39.     private $em;
  40.     private $repo;
  41.     private $user;
  42.     private $userRoleType;
  43.     private $session;
  44.     private $dialogueControllerObj;
  45.     private $selectedPatient;
  46.     private $selectedDialogue;
  47.     private $patientObj;
  48.     private $dialogueObj;
  49.     private $cprNo;
  50.     private $areaNames;
  51.     private $getFullDate;
  52.     private $bdateYear;
  53.     private $dateDiff;
  54.     private $ean = array();
  55.     function __construct() {
  56.     }
  57.     private function auth($request) {
  58.         $twig $this->container->get('twig');
  59.         $globals $twig->getGlobals();
  60.         $this->EANS $globals["medcom"]["ean"];
  61.         $this->medcomID $globals["medcom"]["medcomID"];
  62.         $this->medcomIDCode $globals["medcom"]["medcomIDCode"];
  63.         $this->orgName $globals["medcom"]["orgName"];
  64.         $this->departmentName $globals["medcom"]["departmentName"];
  65.         $this->unitName $globals["medcom"]["unitName"];
  66.         $this->districtName $globals["medcom"]["districtName"];
  67.         $this->postalCode $globals["medcom"]["postalCode"];
  68.         $this->address $globals["medcom"]["address"];
  69.         $this->city $globals["medcom"]["city"];
  70.     
  71.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  72.         $this->Info = array("EAN" => $this->EANS"orgName" => $this->orgName"departmentName" => $this->departmentName"unitName" => $this->unitName"District" => $this->districtName"postal" => $this->postalCode"address" => $this->address"city" => $this->city);
  73.         $this->em $this->getDoctrine()->getManager();
  74.         $this->repo $this->em->getRepository(MedComCorrespondence::class);
  75.         $this->repo->setEm($this->em);
  76.         $this->_receiveVans($request);
  77.         $this->user $this->get('security.token_storage')->getToken()->getUser();
  78.         $this->userRoleType $this->user->hasRole('ROLE_CSV') ? : ($this->user->hasRole('ROLE_DTS') ? 0);
  79.         $this->session $request->getSession();
  80.         $this->dialogueControllerObj = new DialogueController();
  81.         $this->selectedPatient $this->session->get("selectedPatient"null);
  82.         $this->selectedDialogue $this->session->get("selectedDialogue"null);
  83.         if($this->selectedDialogue != null)
  84.             $this->dialogueObj $this->em->getRepository(Dialogue::class)->find($this->selectedDialogue);
  85.         if($this->selectedPatient != null)
  86.             $this->patientObj $this->em->getRepository(Patient::class)->find($this->selectedPatient);
  87.         $this->areaNames $this->em->getRepository(JoMasterInformation::class)->getAreaNames();
  88.         if($this->selectedPatient != null)
  89.             $this->cprNo $this->patientObj->getCpr();
  90.         if($this->selectedPatient != null)
  91.             $this->getFullDate $this->dialogueControllerObj->getBirthDate($this->cprNo);
  92.         if($this->selectedPatient != null)
  93.             $this->bdateYear date_format($this->getFullDate"d.m.Y");
  94.         $this->dateDiff $this->dialogueControllerObj->getUserAge($this->bdateYear);
  95.     }
  96.     public function getMedComNewFiles(Request $request) {
  97.         $directory getcwd()."/Medcom/In/";
  98.         
  99.         // Initialize filecount variavle
  100.         $filecount 0;
  101.         
  102.         $files2 glob$directory ."*.xml" );
  103.         if( $files2 ) {
  104.             $filecount count($files2);
  105.         }
  106.         return new Response(json_encode($filecount), Response::HTTP_OK,['content-type' => "text/json"]);
  107.     }
  108.     
  109.     private function getMedComSpecialtyCodes() {
  110.         $array = array();
  111.         $array["Ikkeklassificeret"] = "Ikke klassificeret";
  112.         $array["ikkeklassificeret"] = "Ikke klassificeret";
  113.         $array["blandet"] = "Blandet medicin";
  114.         $array["intern_medicin_sygehus"] = "Intern medicin";
  115.         $array["geriatri"] = "Geriatri";
  116.         $array["hepatologi"] = "Hepatologi";
  117.         $array["haematologi"] = "Hæmatologi";
  118.         $array["Infektionsmedicin"] = "Infektionsmedicin";
  119.         $array["kardiologi"] = "Kardiologi";
  120.         $array["med_allergologi"] = "Med. allergologi";
  121.         $array["med_endokrinologi"] = "Med. endokrinologi";
  122.         $array["med_gastroenterologi"] = "Med. gastroenterologi";
  123.         $array["med_lungesygdomme"] = "Med. lungesygdomme";
  124.         $array["nefrologi"] = "Nefrologi";
  125.         $array["reumatologi"] = "Reumatologi";
  126.         $array["palliativ"] = "Palliativ medicin";
  127.         $array["akut"] = "Akut medicin";
  128.         $array["dermato_venerologi_sygehus"] = "Dermato-venerologi";
  129.         $array["neurologi"] = "Neurologi";
  130.         $array["onkologi"] = "Onkologi";
  131.         $array["kirurgi_sygehus"] = "Kirurgi";
  132.         $array["karkirurgi"] = "Karkirurgi";
  133.         $array["kir_gastroenterologi"] = "Kir. gastroenterologi";
  134.         $array["plastikkirurgi"] = "Plastikkirurgi";
  135.         $array["thoraxkirurgi"] = "Thoraxkirurgi";
  136.         $array["urologi"] = "Urologi";
  137.         $array["gynaekologi_obstetrik_sygehus"] = "Gynækologi og obstetrik";
  138.         $array["sexologi"] = "Sexologi";
  139.         $array["neurokirurgi"] = "Neurokirurgi";
  140.         $array["ortopaedisk_kirurgi_sygehus"] = "Ortopædisk kirurgi";
  141.         $array["oftalmologi"] = "Oftalmologi";
  142.         $array["oto_rhino_laryngologi"] = "Oto-, rhino-, laryngologi";
  143.         $array["hospitalsodontologi"] = "Hospitalsodontologi";
  144.         $array["psykiatri_sygehus"] = "Psykiatri";
  145.         $array["boerne_ungdomspsykiatri"] = "Børne- og ungdomspsykiatri";
  146.         $array["klin_biokemi"] = "Klin. biokemi";
  147.         $array["klin_fys_nuklearmedicin"] = "Klin fys og nuklearmedicin";
  148.         $array["klin_immunologi"] = "Klin. immunologi";
  149.         $array["klin_mikrobiologi"] = "Klin. mikrobiologi";
  150.         $array["klin_neurofysiologi"] = "Klin. neurofysiologi";
  151.         $array["patologisk_anatomi"] = "Patologisk anatomi";
  152.         $array["diagnostisk_radiologi"] = "Diagnostisk radiologi";
  153.         $array["klin_farmakologi"] = "Klin. farmakologi";
  154.         $array["klin_genetik"] = "Klin. genetik";
  155.         $array["paediatri_sygehus"] = "Pædiatri";
  156.         $array["anaestesiologi_sygehus"] = "Anæstesiologi";
  157.         $array["arbejdsmedicin"] = "Arbejdsmedicin";
  158.         $array["almen_medicin"] = "Almen medicin";
  159.         $array["samfundsmedicin"] = "Samfundsmedicin";
  160.         $array["retsmedicin"] = "Retsmedicin";
  161.         $array["fysioterapi_sygehus"] = "Fysio- og ergoterapi";
  162.         $array["anaestesiologi_praksis"] = "Anæstesiologi";
  163.         $array["roentgen_kbh"] = "Røntgen (København)";
  164.         $array["dermato_venerologi_praksis"] = "Dermatologi-venerologi";
  165.         $array["roentgen"] = "Røntgen";
  166.         $array["reumatologi_fysiurgi"] = "Reumatologi (Fysiurgi)";
  167.         $array["gynaekologi_obstetrik_praksis"] = "Gynækologi/obstetrik";
  168.         $array["intern_medicin_praksis"] = "Intern Medicin";
  169.         $array["kirurgi_praksis"] = "Kirurgi";
  170.         $array["klinisk_kemi"] = "Klinisk kemi";
  171.         $array["neurokirurgi_praksis"] = "Neurokirurgi";
  172.         $array["neuromedicin"] = "Neuromedicin";
  173.         $array["oejenlaege"] = "Øjenlæge";
  174.         $array["ortopaedisk_kirurgi_praksis"] = "Ortopædisk Kirurgi";
  175.         $array["oere_naese_halslaege"] = "Øre, Næse halslæge";
  176.         $array["patologi"] = "Patologi";
  177.         $array["plastkirurgi"] = "Plastkirurgi";
  178.         $array["psykiatri_praksis"] = "Psykiatri";
  179.         $array["paediatri"] = "Pædiatri";
  180.         $array["boernepsykiatri"] = "Børnepsykiatri";
  181.         $array["tropemedicin"] = "Tropemedicin";
  182.         $array["med_laboratorier_kpll"] = "KPLL";
  183.         $array["med_laboratorier"] = "Med. laboratorier";
  184.         $array["omegnslaboratorier"] = "Omegnslaboratorier";
  185.         $array["med_laboratorier_ssi"] = "SSI";
  186.         $array["tandplejere"] = "Tandplejere";
  187.         $array["tandlaege"] = "Tandlæge";
  188.         $array["fysioterapi"] = "Fysioterapi";
  189.         $array["briller"] = "Briller";
  190.         $array["kiropraktor"] = "Kiropraktor";
  191.         $array["fodterapi"] = "Fodterapi";
  192.         $array["fodbehandlking"] = "Fodbehandling";
  193.         $array["ridefysioterapi"] = "Ridefysioterapi";
  194.         $array["teddy"] = "Teddy Øfeldt";
  195.         $array["fodterapi_radioaktiv"] = "Fodterapi - følger radioaktiv bestråling";
  196.         $array["fodterapi_leddegigt"] = "Fodterapi - svær leddegigt";
  197.         $array["fysioterapi_vederlagsfri"] = "Vederlagsfri fysioterapi";
  198.         $array["psykolog"] = "Psykolog";
  199.         $array["kiropraktor_64"] = "Kiropraktik - ordning 64";
  200.         $array["ridefysioterapi_vederlagsfri"] = "Vederlagsfri ridefysioterapi";
  201.         $array["almenlaege_laegevagt"] = "Almenlæge og lægevagt";
  202.         $array["vagtlaege"] = "Almenlæge og lægevagt";
  203.         $array["almenlaege_vagtkoersel"] = "Almen lægers vagtkørsel";
  204.         $array["almenlaege_vagtlaegehjaelp"] = "Vagtlægehjælp, region hovedstaden";
  205.         $array["vagtlaegehjaelp_kbh"] = "Vagtlægehjælp, region hovedstaden";
  206.         $array["vagtlaegehjaelp"] = "Vagtlægehjælp";
  207.         return $array;
  208.     }
  209.     private function getData($type$request$journal false$deleted false) {
  210.         $this->type $type;
  211.         $this->auth($request);
  212.         $currentPage = ($request->get("page") != null && is_nan($request->get("page")) == false $request->get("page") : 1);
  213.         $c 10;
  214.         $search = ($request->get("search") != null $request->get("search") : "");
  215.         $ctotal $this->repo->fetechCorrespondencesTotal($this->type$journal$search$deleted);
  216.         $pagination = array("c" => $c
  217.             "ctotal" => $ctotal
  218.             "pages" => ceil(($ctotal $c)), 
  219.             "page" => $currentPage
  220.             "skip" => ($currentPage 1)*$c
  221.         );
  222.         $corr $this->repo->fetechCorrespondences($this->type$journal,$pagination['c'], $pagination['skip'], $search$deleted);
  223.         $templateName "index.html.twig";
  224.         return $this->render('medcom/'.$templateName, array(
  225.             'journal' => $journal,
  226.             'patientObj' => $this->patientObj,
  227.             'dialogueObj' => $this->dialogueObj,
  228.             'areaNames' => $this->areaNames,
  229.             'dateDiff' => $this->dateDiff,
  230.             'type' => $this->type,
  231.             'cor' => $corr,
  232.             'info' => $this->Info,
  233.             'deleted' => $deleted,
  234.             'medcomSpecialtyCodes' => $this->getMedComSpecialtyCodes(),
  235.             'MedComHelper' => new \App\Service\MedcomTwig(),
  236.             'pagination' => $pagination,
  237.             "search" => $search
  238.         ));
  239.     }
  240.     public function uploadList(Request $request) {
  241.         set_time_limit(0);
  242.         $this->auth($request);
  243.         $repo $this->em->getRepository(MedComCorrespondence::class);
  244.         $repo->setEm($this->em);
  245.         $file fopen("medcom.csv","r");
  246.         $_lineCount 0;
  247.         $keys = array();
  248.         $medcomList = array();
  249.         while(($line fgets($file)) !== false)
  250.         {
  251.             
  252.             $t explode(";",$line);
  253.             $medcomrow = array();
  254.             for($i=0;$i<count($t);$i++) {
  255.                 if($_lineCount != 0) {
  256.                     $key count($keys)-1<$i $i $keys[$i];
  257.                     //$medcomList[($_lineCount-1)][$key] = $t[$i];
  258.                     $medcomrow[$key] = utf8_encode($t[$i]); 
  259.                 } else {
  260.                     $keys[$i] = $t[$i];
  261.                 }
  262.             }
  263.             if($_lineCount != && ($_lineCount 75000 && $_lineCount 80000)) {
  264.                 $obj $medcomrow;
  265.                 $ean $obj["EanLocationCode"];
  266.                 if($ean != null || $ean != "") {
  267.                     $repo->insertList(null$obj["EntityName"], $obj["PostalAddress"], $obj["P_Postnummer"], $obj["PostalDistrictName"], $obj["PostalDistrictName"], $obj["SorId"], $ean);
  268.                 }
  269.             }
  270.             $_lineCount++;
  271.         }
  272.         fclose($file);
  273.         return $this->redirectToRoute("medcom_index");
  274.         //
  275.         /*set_time_limit(0);
  276.         $this->auth($request);
  277.         $medcomList = $this->getMedComData();
  278.         $repo = $this->em->getRepository(MedComCorrespondence::class);
  279.         $repo->setEm($this->em);
  280.         foreach($medcomList as $obj) {
  281.             $ean = ($obj["EanLocationCode"] != null || $obj["EanLocationCode"] != "" ? $obj["EanLocationCode"] : (isset($this->ean[$obj["ParentSorId"]]) ? $this->ean[$obj["ParentSorId"]] : ""));
  282.             $repo->insertList($obj["ParentSorEntityName"], $obj["EntityName"], $obj["PostalAddress"], $obj["PostalPostCodeId"], $obj["PostalDistrictName"], $obj["PostalDistrictName"], $obj["SorId"], $ean);
  283.         }
  284.         return $this->redirectToRoute("medcom_index");*/
  285.     }
  286.     public function trashcan(Request $request) {
  287.         return $this->getData(3$requestfalsetrue);
  288.     }
  289.     public function journal(Request $request) {
  290.         return $this->getData(3$requesttrue);
  291.     }
  292.     public function index(Request $request) {
  293.         return $this->getData(3$request);
  294.     }
  295.     public function outbox(Request $request) {
  296.         return $this->getData(2$request);
  297.     }
  298.     public function drafts(Request $request) {
  299.         return $this->getData(1$request);
  300.     }
  301.     public function journalize(Request $request) {
  302.         $this->auth($request);
  303.         $searchFilter "ROLE_DTS";
  304.         if ($this->user->hasRole('ROLE_CSV')) {
  305.             $searchFilter "ROLE_CSV";
  306.         }
  307.         $predefinedCaseCardAll $this->em->getRepository(PredefinedCaseCard::class)->findBy(array('userType' => $searchFilter'isForIndividual' => 1));
  308.         $joGroupObj NULL;
  309.         if($this->dialogueObj != null) {
  310.             if(($this->dialogueObj->getDialogueType() == || $this->dialogueObj->getDialogueType() == 4) && $searchFilter == "ROLE_DTS" ) {
  311.                 $joGroupObj $this->em->getRepository(JoGroup::class)->findOneBy(array('Dialogue' => $this->dialogueObj));
  312.                 
  313.                 $predefinedCaseCardAll $this->em->getRepository(PredefinedCaseCard::class)->fetchGroupPredefinedCaseCardListByType($searchFilter$joGroupObj->getJoMasterInformation()->getInfoType(), $joGroupObj->getJoMasterInformation()->getKkorg());
  314.             } else if(($this->dialogueObj->getDialogueType() == 4) && $searchFilter == "ROLE_CSV" ) {
  315.                 $joGroupObj $this->em->getRepository(JoGroup::class)->findOneBy(array('Dialogue' => $this->dialogueObj));
  316.                 $predefinedCaseCardAll $this->em->getRepository(PredefinedCaseCard::class)->findBy(array('userType' => $searchFilter'isForGroup' => 1));
  317.             }
  318.         }
  319.         $dialogueCaseCardAll $this->em->getRepository(DialogueCaseCard::class)->findBy(array('dialogue' => $this->dialogueObj));
  320.         $userCreatedCaseCardsId $userCreatedDialogueCaseCard = array();
  321.         foreach ($dialogueCaseCardAll as $dialogueCaseCard) {
  322.             if (!$dialogueCaseCard->getIsClosed()) {
  323.                 $userCreatedCaseCardsId[] = $dialogueCaseCard->getPCaseCardId()->getId();
  324.                 $userCreatedDialogueCaseCard[$dialogueCaseCard->getPCaseCardId()->getId()] = $dialogueCaseCard;
  325.             }
  326.         }
  327.         $dialogueCaseCardMeasureAll $this->em->getRepository(DialogueCaseCardMeasure::class)->fetchMeasureTilDateMedcom('F',new \DateTime());
  328.         //get files
  329.         $id $request->get("id");
  330.         return $this->render('medcom/_journalize.twig', array(
  331.             'thisID' => $id,
  332.             "userCreatedDialogueCaseCard" => $userCreatedDialogueCaseCard,
  333.             'predefinedCaseCardAll' => $predefinedCaseCardAll,
  334.             'dialogueCaseCardMeasureAll' => $dialogueCaseCardMeasureAll,
  335.             'userCreatedCaseCardsId' => $userCreatedCaseCardsId,
  336.             'patientObj' => $this->patientObj,
  337.             'dialogueObj' => $this->dialogueObj,
  338.             'areaNames' => $this->areaNames,
  339.             'dateDiff' => $this->dateDiff,
  340.             'info' => $this->Info            
  341.         ));
  342.     }
  343.     public function journalized(Request $request) {
  344.         $this->auth($request);
  345.         $id $request->get("id");
  346.         $_cor $this->repo->fetechCorrespondenceJournal($id);
  347.         if($id != null && $id != && count($_cor) != 0) {
  348.             $cor $_cor[0];
  349.             return $this->render('medcom/_journalized.twig', array(
  350.                 'journal' => true,
  351.                 'patientObj' => $this->patientObj,
  352.                 'dialogueObj' => $this->dialogueObj,
  353.                 'areaNames' => $this->areaNames,
  354.                 'dateDiff' => $this->dateDiff,
  355.                 'type' => "3",
  356.                 'thisID' => $request->get("id"),
  357.                 'cor' => $_cor,
  358.                 'info' => $this->Info,
  359.                 'medcomSpecialtyCodes' => $this->getMedComSpecialtyCodes(),
  360.             ));
  361.         }
  362.     }
  363.     public function saveJournalize(Request $request) {
  364.         $this->auth($request);
  365.         $id $request->get('id');
  366.         $_cor $this->repo->fetechCorrespondence($id);
  367.         if($id != null && $id != && count($_cor) != 0) {
  368.             $cor $_cor[0];
  369.             $dialogueCaseCard $request->get('dialogueCaseCard');
  370.             $predefinedCaseCardId $request->get('predefinedCaseCardId');
  371.             $dialogueCaseCardMeasureId $request->get('dialogueCaseCardMeasureId');
  372.             $measureId $request->get('measureId');
  373.             
  374.             $dialogueCaseCardObj $this->em->getRepository(DialogueCaseCard::class)->find($dialogueCaseCard);
  375.             $predefinedCaseCardObj $this->em->getRepository(PredefinedCaseCard::class)->find($predefinedCaseCardId);
  376.             $predefinedAttachmentTypeObj $this->em->getRepository(PredefinedAttachmentType::class)->findOneBy(array('attachmentType' => 'medcom')); //Medcom
  377.     
  378.     
  379.             $dialogueAttachmentDetailObj = new DialogueAttachmentDetails();
  380.             $dialogueAttachmentDetailObj->setPCaseCardId($predefinedCaseCardObj);
  381.             $dialogueAttachmentDetailObj->setPatient($this->patientObj);
  382.             $dialogueAttachmentDetailObj->setDialogue($this->dialogueObj);
  383.             if($dialogueCaseCardObj){
  384.                 $dialogueAttachmentDetailObj->setDialogueCaseCard($dialogueCaseCardObj);
  385.             }
  386.             
  387.             $dialogueCaseCardMeasureObj $this->em->getRepository(DialogueCaseCardMeasure::class)->find($dialogueCaseCardMeasureId);
  388.             $dialogueAttachmentDetailObj->setDialogueCaseCardMeasureId($dialogueCaseCardMeasureObj);
  389.             $caseCardMeasureObj $this->em->getRepository(CasecardMeasure::class)->find($measureId);
  390.             $dialogueAttachmentDetailObj->setMeasureId($caseCardMeasureObj);
  391.             
  392.             $dialogueAttachmentDetailObj->setUser($this->user);
  393.     
  394.             $dialogueAttachmentDetailObj->setPAtttachmentType($predefinedAttachmentTypeObj);
  395.             if($this->dialogueObj->getDialogueType() == || $this->dialogueObj->getDialogueType() == 2){
  396.                 $dialogueAttachmentDetailObj->setAttachmentDate(new \DateTime());
  397.             }else{
  398.                 $dialogueAttachmentDetailObj->setAttachmentDate($cor->getUpdatedAt());
  399.             }
  400.             $dialogueAttachmentDetailObj->setIsDraft(false);
  401.             $dialogueAttachmentDetailObj->setVisibleStatus(0);
  402.             $dialogueAttachmentDetailObj->setActivityType(11);
  403.             
  404.             $heading $cor->getSubject();
  405.             $heading $request->get("subject");
  406.             $dialogueAttachmentDetailObj->setAttachmentHeading($heading);
  407.             
  408.             $this->em->persist($dialogueAttachmentDetailObj);
  409.             $this->em->flush();
  410.             $ccID $dialogueAttachmentDetailObj->getId();
  411.             $cor->journalize($ccID);
  412.             $cor->setPatient($this->patientObj);
  413.             $this->em->persist($cor);
  414.             $this->em->flush();
  415.             return new Response(json_encode('success:'.$ccID), Response::HTTP_OK,['content-type' => "text/json"]);
  416.         }
  417.         return new Response(json_encode('error:true'), Response::HTTP_OK,['content-type' => "text/json"]);
  418.     }
  419.     public function saveJournalizeFiles(Request $request) {
  420.         $this->auth($request);
  421.         $id $request->get('id');
  422.         $_msg $this->repo->fetechMessage($id);
  423.         $filename $request->get('postMailAttachmentName');
  424.         if($id != null && $id != && count($_msg) != && $filename != null && $filename != "") {
  425.             $msg $_msg[0];
  426.             $msg->setFilename($filename);
  427.             $this->em->persist($msg);
  428.             $this->em->flush();
  429.             return new Response(json_encode('success:'.$id), Response::HTTP_OK,['content-type' => "text/json"]);
  430.         }
  431.         return new Response(json_encode('error:true'), Response::HTTP_OK,['content-type' => "text/json"]);
  432.     }
  433.     public function saveFile($type$medcom,$obj,$cbj,$name,$ean null,$parentID null,$files null) {
  434.         $medcom = ($medcom != null $medcom $this->repo->getMedcomList($cbj->getMedComUser()));
  435.         $ean = ($ean != null $ean $medcom->getEAN());
  436.         if($type == "xdis91") {
  437.             $parentID = ($parentID != null $parentID $this->repo->lastMessageID);
  438.             $files = ($files != null $files $this->repo->fetechMessageParent($parentID));
  439.             $_xml $this->getXmlDis91($obj$cbj$medcom$files);
  440.         }
  441.         elseif($type == "xbin01")
  442.             $_xml $this->getXmlbin01($obj,$medcom);
  443.         $xml $this->getVansXml($_xml$type$ean$this->repo);
  444.         $myfile fopen("Medcom/Out/".$name.".xml""w") or die("Unable to open file!");
  445.         fwrite($myfile$xml->asXML());
  446.         fclose($myfile);
  447.     }
  448.     public function send(Request $request) {
  449.         $this->auth($request);
  450.         $type = ($request->get("submit") == "send" 1);
  451.         $type = ($request->get("submit") == "inbox" $type);
  452.         $cor;
  453.         $medcom;
  454.         if($request->files->get("new_medcom_file") != null) {
  455.             //$ean = ($medcom["EanLocationCode"] != null || $medcom["EanLocationCode"] != "" ? $medcom["EanLocationCode"] : (isset($this->ean[$medcom["ParentSorId"]]) ? $this->ean[$medcom["ParentSorId"]] : ""));
  456.             $cor $this->repo->insert($request->get("medcom_user"), $request->get("subject"), $this->user$this->patientObj$request->get("priority"), $request->get("message"), $type,false);
  457.             $i 1;
  458.             $medcom $this->repo->getMedcomList($cor->getMedComUser());
  459.             $id $this->repo->lastMessageID;
  460.             foreach($request->files->get("new_medcom_file") as $f) {
  461.                 $file file_get_contents($f);
  462.                 $ext pathinfo($f->getClientOriginalName(), PATHINFO_EXTENSION);
  463.                 $filename pathinfo($f->getClientOriginalName(), PATHINFO_FILENAME);
  464.                 $base base64_encode($file);
  465.                 $filesize filesize($f);
  466.                 $obj $this->repo->insertFileOnly($filesize,$request->get("medcom_user"), $this->user$this->patientObj,$type,$filename,$ext,$base,$cor,$request->get("priority"));
  467.                 $obj->setParentID($id);
  468.                 $this->em->persist($obj);
  469.                 $this->em->flush();
  470.                 if($request->get("submit") != 2) {
  471.                     $this->saveFile("xbin01"$medcom$obj$cor$id."_file".$i);
  472.                     $i++;
  473.                 }
  474.             }
  475.         } else {
  476.             $cor $this->repo->insert($request->get("medcom_user"), $request->get("subject"), $this->user$this->patientObj$request->get("priority"), $request->get("message"), $type);
  477.             $id $this->repo->lastMessageID;
  478.         }
  479.         if($request->get("submit") != 2) {
  480.             $medcom $this->repo->getMedcomList($cor->getMedComUser());
  481.             $ean $medcom->getEAN();
  482.             $_obj $this->repo->fetechMessage($id);
  483.             $this->saveFile("xdis91"$medcom$_obj[0], $cor$id,$ean,$id);
  484.         }
  485.         return new Response(json_encode('success:'.$id), Response::HTTP_OK,['content-type' => "text/json"]);
  486.     }
  487.     public function sendDraft(Request $request) {
  488.         $this->auth($request);
  489.         $id $request->get("objID");
  490.         $_msg $this->repo->fetechMessage($id);
  491.         if($_msg != null) {
  492.             $msg $_msg[0];
  493.             $cor $msg->getCorrospondence();
  494.             $msg->sendDraft();
  495.             $this->em->persist($msg);
  496.             $this->em->flush();
  497.             $cor->sendDraft();
  498.             $this->em->persist($cor);
  499.             $this->em->flush();
  500.             $medcom $this->repo->getMedcomList($cor->getMedComUser());
  501.             $this->saveFile("xdis91"$medcom$msg$cor$id);
  502.             $i 1;
  503.             $files $this->repo->fetechFiles($cor);
  504.             foreach($files as $file) {
  505.                 $this->saveFile("xbin01"$medcom$file$cor$id."_file".$i);
  506.                 $i++;
  507.             }
  508.             return new Response(json_encode('success'), Response::HTTP_OK,['content-type' => "text/json"]);
  509.         } else {
  510.             return new Response(json_encode('error'), Response::HTTP_OK,['content-type' => "text/json"]);
  511.         }
  512.         return $this->redirectToRoute("medcom_index");
  513.     }
  514.     public function edi(Request $request) {
  515.     }
  516.     private function getVansXml($xml$code$ean,$repo) {
  517.         $baseData base64_encode(str_replace("&lt;Break/&gt;&#13;""<Break/>"$xml->asXml()));
  518.         $size = (int) (strlen(rtrim($baseData'=')) * 4);                
  519.         $env = new \SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><VANSEnvelope />');
  520.         $env->addAttribute("xmlns""urn:oio:medcom:vans-envelope:1.0.4");
  521.         $esend $env->addChild('SenderID'$this->EANS);
  522.         $esend->addAttribute("EndPointType""EAN");
  523.         $erec $env->addChild('ReceiverID'$ean);
  524.         $erec->addAttribute("EndPointType""EAN");
  525.         $env->addChild('EnvelopeIdentifier'$repo->create_guid());
  526.         $env->addChild('SentDateTime'date("Y-m-d")."T".date("H:i:s"));
  527.         $message $env->addChild("Message");
  528.             $meta $message->addChild('MetaInformation');
  529.                 $meta->addChild('Identifier'$repo->create_guid());
  530.                 $doc $meta->addChild('Document');
  531.                 $doc->addChild('Format'"XML");
  532.                 if($code == "xdis91") {
  533.                     $doc->addChild('Name','MCM:XDIS91');
  534.                     $doc->addChild('Version'"XD9134L");
  535.                 }
  536.                 elseif($code == "xctl02") {
  537.                     $doc->addChild('Name','MCM:XCTL02');
  538.                     $doc->addChild('Version'"XC0230Q");
  539.                 }
  540.                 elseif($code == "xctl03") {
  541.                     $doc->addChild('Name','MCM:XCTL03');
  542.                     $doc->addChild('Version'"XC0330Q");
  543.                 }
  544.                 elseif($code == "xbin01") {
  545.                     $doc->addChild('Name','MCM:XBIN01');
  546.                     $doc->addChild('Version'"XB0131X");
  547.                 }
  548.                 $doc->addChild('SizeInBytes'$size);
  549.             $data $message->addChild('Data'$baseData);
  550.         return $env;
  551.     }
  552.     public function vans(Request $request$code$_id null$_refuseCode null$_refuseText null$_ean null) {
  553.         $this->auth($request);
  554.         $id = ($_id != null $_id $request->get("id"));
  555.         $refuseText = ($_refuseText != null $_refuseText $request->get("RefuseText"));
  556.         $refuseCode = ($_refuseCode != null $_refuseCode $request->get("RefuseCode"));
  557.         if($id != null) {
  558.             $_obj $this->repo->fetechMessage($id);
  559.             if(count($_obj) != 0) {
  560.                 $obj $_obj[0];
  561.                 $cor $obj->getCorrospondence();
  562.                 $medcom $this->repo->getMedcomList($cor->getMedComUser());
  563.                 $ean $medcom->getEAN();
  564.                 $orgVersionCode = (strtolower($obj->getType()) == "bilag"  "XB0131X" "XD9134L");
  565.                 $files $this->repo->fetechMessageParent($id);
  566.                 if($code == "xdis91")
  567.                 $_xml $this->getXmlDis91($obj$cor$medcom$files);
  568.                 elseif($code == "xctl02")
  569.                 $_xml $this->getXmlctl02($obj$cor$medcom$ean,$orgVersionCode$refuseCode$refuseText);
  570.                 elseif($code == "xctl03")
  571.                 $_xml $this->getXmlctl03($obj$cor$medcom$ean);
  572.                 elseif($code == "xbin01")
  573.                 $_xml $this->getXmlbin01($obj$medcom);
  574.                 else
  575.                 return new Response("ERROR");
  576.                 $xml $_xml->asXML();
  577.                 $env $this->getVansXml($xml$code$ean$repo);
  578.                 $vans_exist $this->repo->fetechMessageParentVans($obj->getId(),$code);
  579.                 if(true) {
  580.                     $vans MedComMessages::withInsert($code,$ean,null,$cor,4,'Vans'.$code,false);
  581.                     $vans->setFileData($env->asXML());
  582.                     $vans->setParentID($obj->getId());
  583.                     $this->em->persist($vans);
  584.                     $this->em->flush();
  585.                     if(count($vans_exist) != 0) {
  586.                         $vans_exist[0]->setType("VansExpired");
  587.                         $this->em->persist($vans_exist[0]);
  588.                         $this->em->flush();
  589.                     }
  590.                 }
  591.                 $res = new Response($env->asXML(), Response::HTTP_OK,['content-type' => "text/xml"]);
  592.                 $res->setCharset('ISO-8859-1');
  593.                 return $res;
  594.             }
  595.             return new Response("ERROR");
  596.         }
  597.         return new Response("ERROR");
  598.     }
  599.     public function _receiveVans(Request $request) {
  600.         $files scandir('Medcom/In');
  601.         foreach($files as $file) {
  602.             $ext pathinfo('Medcom/In/'.$filePATHINFO_EXTENSION);
  603.             if(is_dir('Medcom/In/'.$file) == false && $ext == "xml") {
  604.                 //libxml_use_internal_errors(true);
  605.                 $fileData file_get_contents('Medcom/In/'.$file);
  606.                 //$xml_ = simplexml_load_file('Medcom/In/'.$file);
  607.                 $xml_ simplexml_load_string($fileData);
  608.                 //$errors = libxml_get_errors();
  609.                 if($xml_ != false) {
  610.                     $xml = ($xml_->VANSEnvelope != null && $xml_->VANSEnvelope != "" $xml_->VANSEnvelope $xml_);
  611.                     $ean $xml->SenderID;
  612.                     $EnvelopeIdentifier $xml->EnvelopeIdentifier;
  613.                     
  614.                     $code $xml->Message->MetaInformation->Document->Version;
  615.                     $id 0;
  616.                     $_xml;
  617.                     $messageBase $xml->Message->Data;
  618.                     $message base64_decode($messageBase);
  619.                     $check_size_in_bytes = (int) (strlen(rtrim($message'=')) * 4);
  620.                     //var_dump($check_size_in_bytes);
  621.                     //exit;
  622.                     $vans MedComMessages::withInsert($code,$ean,null,null,4,'Vans',false);
  623.                     $vans->setFileData($xml->asXML());
  624.                     $this->em->persist($vans);
  625.                     $this->em->flush();
  626.                     $var $this->receiveXml($request$message);
  627.                     if($var != null) {
  628.                         $myfile fopen("Medcom/Out/".$EnvelopeIdentifier.".kvittering.xml""w") or die("Unable to open file!");
  629.                         fwrite($myfile$var->asXML());
  630.                         fclose($myfile);
  631.                     }
  632.                     unlink('Medcom/In/'.$file);
  633.                 }
  634.             }
  635.         }
  636.     }
  637.     public function receiveVans(Request $request) {
  638.         exit;
  639.     }
  640.     public function xml(Request $request) {
  641.         $this->auth($request);
  642.         $id $request->get("id");
  643.         if($id != null) {
  644.             $_obj $this->repo->fetechMessage($id);
  645.             if(count($_obj) != 0) {
  646.                 $obj $_obj[0];
  647.                 $cor $obj->getCorrospondence();
  648.                 $medcom $this->repo->getMedcomList($cor->getMedComUser());
  649.                 $files $this->repo->fetechMessageParent($id);
  650.                 $last9 $this->repo->fetechMessageLast9($id,$cor);
  651.                 $emssage $this->getXmlDis91($obj$cor$medcom,$files,$last9);
  652.                 $res = new Response($emssage->asXML(), Response::HTTP_OK,['content-type' => "text/xml"]);
  653.                 $res->setCharset('ISO-8859-1');
  654.                 return $res;
  655.             }
  656.             return new Response("ERROR");
  657.         }
  658.         return new Response("ERROR");
  659.     }
  660.     public function xml02(Request $request$_id null$_refuseText null$_refuseCode null$_ean null$_orgVersionCode null) {
  661.         $this->auth($request);
  662.         $id = ($_id != null $_id $request->get("id"));
  663.         $refuseText = ($_refuseText != null $_refuseText $request->get("RefuseText"));
  664.         $refuseCode = ($_refuseCode != null $_refuseCode $request->get("RefuseCode"));
  665.         if($id != null) {
  666.             $_obj $this->repo->fetechMessage($id);
  667.             if($_obj != null && count($_obj) != 0) {
  668.                 $obj $_obj[0];
  669.                 $cor $obj->getCorrospondence();
  670.                 $orgVersionCode = ($_orgVersionCode != null $_orgVersionCode : (strtolower($obj->getType()) == "bilag"  "XB0131X" "XD9134L"));
  671.                 $medcom $this->repo->getMedcomList($cor->getMedComUser());
  672.                 $ean $medcom->getEAN();
  673.                 $emssage $this->getXmlctl02($obj,$cor,$medcom,$ean,$orgVersionCode,$refuseCode,$refuseText);
  674.                 $res = new Response($emssage->asXML(), Response::HTTP_OK,['content-type' => "text/xml"]);
  675.                 $res->setCharset('ISO-8859-1');
  676.                 return $res;
  677.             }
  678.             return new Response("ERROR");
  679.         }
  680.         return new Response("ERROR");
  681.     }
  682.     public function xml03(Request $request$_id null$_ean null) {
  683.         $this->auth($request);
  684.         $id = ($_id != null $_id $request->get("id"));
  685.         if($id != null) {
  686.             $_obj $this->repo->fetechMessage($id);
  687.             if(count($_obj) != 0) {
  688.                 $obj $_obj[0];
  689.                 $cor $obj->getCorrospondence();
  690.                 $medcom $this->repo->getMedcomList($cor->getMedComUser());
  691.                 $ean $medcom->getEAN();
  692.                 $emssage $this->getXmlctl03($obj,$cor,$medcom,$ean);
  693.                 $res = new Response($emssage->asXML(), Response::HTTP_OK,['content-type' => "text/xml"]);
  694.                 $res->setCharset('ISO-8859-1');
  695.                 return $res;
  696.             }
  697.             return new Response("ERROR");
  698.         }
  699.         return new Response("ERROR");
  700.     }
  701.     public function xml04(Request $request) {
  702.         $this->auth($request);
  703.         $id $request->get("id");
  704.         if($id != null) {
  705.             $_obj $this->repo->fetechMessage($id);
  706.             if(count($_obj) != 0) {
  707.                 $obj $_obj[0];
  708.                 $cor $obj->getCorrospondence();
  709.                 $medcom $this->repo->getMedcomList($cor->getMedComUser());
  710.                 $emssage $this->getXmlbin01($obj,$medcom);
  711.                 $res = new Response($emssage->asXML(), Response::HTTP_OK,['content-type' => "text/xml"]);
  712.                 $res->setCharset('ISO-8859-1');
  713.                 return $res;
  714.             }
  715.             return new Response("ERROR");
  716.         }
  717.         return new Response("ERROR");
  718.     }
  719.     public function receiveXml(Request $request$vansXml null) {
  720.         if($this->em == null)
  721.             $this->auth($request);
  722.         $repo $this->repo;
  723.         if (file_exists('import.xml') || $vansXml != null) {
  724.             //libxml_use_internal_errors(true);
  725.             $xml_ = ($vansXml != null simplexml_load_string($vansXml'SimpleXMLElement'LIBXML_COMPACT LIBXML_PARSEHUGE) : simplexml_load_file('import.xml'));
  726.             $xml = ($xml_->Emessage != null && $xml_->Emessage != "" $xml_->Emessage $xml_);
  727.             //$errors = libxml_get_errors();
  728.             $id 0$_xml$moveFoward true;
  729.             $mServ = new \App\Service\MedcomXml($this->em$xml$this->repo);
  730.             $vars $mServ->getVariables();
  731.             $this->patientObj $this->em->getRepository(Patient::class)->findOneBy(array('cpr' => $vars["cpr"]));
  732.             if($this->patientObj == null) {
  733.                 //handle check here
  734.                 //$rootdir = $this->get('kernel')->getRootDir();
  735.                 //$patientServ = new \App\Service\CreatePatient($this->em, $request,$rootdir);
  736.                 //$patientServ->createPatient($vars["cpr"]);
  737.             }
  738.             $_xml $vars["xml"];
  739.             if(isset($xml->ClinicalEmail) && $xml->ClinicalEmail !== null && $xml->ClinicalEmail !== "") {
  740.                 $m_id 0;
  741.                 $cor;
  742.                 $vars $mServ->handleErrors("xdis91");
  743.                 $msg_exist =  $this->repo->fetechMessageUUID($vars["letterID"]);
  744.                 if(count($msg_exist) == 0) {
  745.                     if($_obj $this->repo->fetechCorrespondenceUUID($vars["id"]) && count($_obj) != 0) {
  746.                         $cor $_obj[0];
  747.                         $m MedComMessages::withInsert($vars["xdis91"]["message"],"",null,$cor,3,'Korrespondancebrev',$vars["xdis91"]["prio"]);
  748.                         $m->setMedicalSpecialityCode($MedicalSpecialityCode);
  749.                         $m->setPatientString($_xml->Patient->AsXML());
  750.                         $m->setDeleted(($vars["error"]["error"] ? true false)); $m->setUUID($vars["letterID"]); $m->setEUUID($vars["envID"]); $this->em->persist($m); $this->em->flush();
  751.                         $m_id=$m->getId();
  752.                         $cor->setDeleted(($vars["error"]["error"] ? true false)); $cor->setStatus(3); $cor->setUpdatedAt(new \DateTime()); $this->em->persist($cor); $this->em->flush();
  753.                         
  754.                     } else {
  755.                         $cor $this->repo->insertUUID($vars["sender"], $vars["xdis91"]["subject"], null$this->patientObj$vars["xdis91"]["prio"], $vars["xdis91"]["message"], 3$vars["id"], $vars["letterID"], $vars["envID"], $vars["xdis91"]["MedicalSpecialityCode"]);
  756.                         $cor->setDeleted(($vars["error"]["error"] ? true false)); $cor->setUpdatedAt(new \DateTime()); $this->em->persist($cor); $this->em->flush();
  757.                         $m_id=$this->repo->lastMessageID;
  758.                         $msg =  $this->repo->fetechMessage($m_id);
  759.                         $msg[0]->setPatientString($_xml->Patient->AsXML());
  760.                         $this->em->persist($msg[0]);
  761.                         $this->em->flush();
  762.     
  763.                     }
  764.                     $mServ->xdisReference(function ($ext$filesize$objID$_extFile) use (&$cor, &$m_id, &$vars) {
  765.                         if(count($_extFile) != 0) {
  766.                             $obj $_extFile[0];
  767.                             $old_cor $obj->getCorrospondence();
  768.                             if($old_cor != null && count($old_cor) != 0) {
  769.                                 $old_cor->setDeleted(true);
  770.                                 $this->em->persist($old_cor);
  771.                                 $this->em->flush();
  772.                             }
  773.                         }
  774.                         else {
  775.                             $obj $this->repo->insertFileOnly($filesize,$vars["sender"],null,$this->patientObj,3,"bilag.".date_timestamp_get(date_create()),$ext,null,$cor,false,$objID);
  776.                         }
  777.                         $obj->setPatientString($_xml->Patient->AsXML());
  778.                         $obj->setParentID($m_id);
  779.                         $obj->setCor($cor);
  780.                         $obj->setDeleted(($vars["error"]["error"] ? true false)); 
  781.                         $this->em->persist($obj);
  782.                         $this->em->flush();
  783.                     });
  784.                 }
  785.                 if(count($msg_exist) == null || count($msg_exist) == || $msg_exist[0]->getReceipt() != true || ($msg_exist !== null && $msg_exist[0]->getEUUID() != $vars["envID"])) {
  786.                     $msg null;
  787.                     $msg =  $this->repo->fetechMessageUUID($vars["letterID"]);
  788.                     $msg[0]->setReceipt(true);
  789.                     $this->em->persist($msg[0]);
  790.                     $this->em->flush();
  791.                     $_id $msg[0]->getID();
  792.                     if($vars["error"]["error"]) {
  793.                         if($vansXml != null) {
  794.                             $_xdisXml $this->getXmlctl02($msg[0],$cor,null,$vars["sender"],$vars["VersionCode"],$vars["error"]["refuseCode"],$vars["error"]["refuseText"]);
  795.                             return $this->getVansXml($_xdisXml"xctl02"$vars["sender"], $this->repo);
  796.                         }
  797.                         return $this->xml02($request,$_id,$vars["error"]["refuseText"],$vars["error"]["refuseCode"],$vars["error"]["_ean"],$vars["VersionCode"].'');
  798.                     } else {
  799.                         if($vansXml != null) {
  800.                             $_xdisXml $this->getXmlctl03($msg[0],$cor,null,$vars["sender"]);
  801.                             return $this->getVansXml($_xdisXml"xctl03"$vars["sender"], $this->repo);
  802.                         }
  803.                         return $this->xml03($request,$_id,$vars["sender"]);
  804.                     }
  805.                 } else {
  806.                     return null;
  807.                 }
  808.             }
  809.             elseif(isset($xml->BinaryLetter) && $xml->BinaryLetter !== null && $xml->BinaryLetter !== "") {
  810.                 $vars $mServ->handleErrors("xbin01");
  811.                 $obj null;
  812.                 $cor null;
  813.                 $_id null;
  814.                 $msg null;
  815.                 $mServ->xbinloop(function ($extFile$xbin,$i) use (&$cor, &$obj, &$vars, &$_id, &$_xml, &$msg) {
  816.                     if($extFile != null) {
  817.                         $cor $extFile->getCorrospondence();
  818.                         $cor->setUpdatedAt(new \DateTime()); $this->em->persist($cor); $this->em->flush();
  819.                         $obj $this->repo->insertFileOnly($xbin["filesize"],$vars["sender"],null,$this->patientObj,3,"bilag.".date_timestamp_get(date_create())."_".$i,$xbin["ext"],$xbin["baseData"],$cor,false,$xbin["objID"]);
  820.                         $extFile->setDeleted(true);
  821.                         $this->em->persist($extFile);
  822.                         $this->em->flush();
  823.                         $msg $extFile;
  824.                     } else {
  825.                         $cor = ($cor != null $cor $this->repo->insertUUID($vars["sender"], "Bilag uden meddelelse"null$this->patientObjfalse"bilag uden meddelelse"3$vars["id"], $vars["letterID"], $vars["envID"]));
  826.                         $cor->setDeleted(($vars["error"]["error"] ? true false)); $cor->setUpdatedAt(new \DateTime()); $this->em->persist($cor); $this->em->flush();
  827.                         $obj $this->repo->insertFileOnly($xbin["filesize"],$vars["sender"],null,$this->patientObj,3,"bilag.".date_timestamp_get(date_create())."_".$i,$xbin["ext"],$xbin["baseData"],$cor,false,$xbin["objID"]);
  828.                         $m_id=$this->repo->lastMessageID;
  829.                         $_message $this->repo->fetechMessage($m_id);
  830.                         $_message[0]->setPatientString($_xml->Patient->asXML());
  831.                         $this->em->persist($_message[0]);
  832.                         $this->em->flush();
  833.                         $obj->setParentID($m_id);
  834.                         $msg $_message[0];
  835.                     }
  836.                     $obj->setPatientString($_xml->Patient->asXML());
  837.                     $obj->setDeleted(($vars["error"]["error"] ? true false)); 
  838.                     $_id $obj->getId();
  839.                     $this->em->persist($obj);
  840.                     $this->em->flush();
  841.                 });
  842.                 if($vansXml != null) {
  843.                     //$msg =  $this->repo->fetechMessageUUID($vars["letterID"]);
  844.                     $_xdisXml = ($vars["error"]["error"] ? $this->getXmlctl02($msg,$cor,null,$vars["sender"],$vars["VersionCode"],$vars["error"]["refuseCode"],$vars["error"]["refuseText"]) : $this->getXmlctl03($msg,$cor,null,$vars["sender"]));
  845.                     return $this->getVansXml($_xdisXml, ($vars["error"]["error"] ? "xctl02" "xctl03"), $vars["sender"], $this->repo);
  846.                 } else {
  847.                     if($vars["error"]["error"]) {
  848.                         return $this->xml02($request,$_id,$vars["error"]["refuseText"],$vars["error"]["refuseCode"],$vars["sender"]);
  849.                     } else {
  850.                         return $this->xml03($request,$_id,$vars["sender"]);
  851.                     }
  852.                 }
  853.             } 
  854.             if($vars["is_reciept"]) {
  855.                 $_obj $this->repo->fetechMessageUUID($vars["id"]);
  856.                 $code $_xml->Letter->VersionCode;
  857.                 $ean $_xml->Sender->EANIdentifier;
  858.                 if(count($_obj) != 0) {
  859.                     $obj $_obj[0];
  860.                     $cor $obj->getCorrospondence();
  861.                     $delivered true;
  862.                     //$obj->setError(null);
  863.                     //$obj->setErrorType(null);
  864.                     if(($_xml->OriginalEmessage->OriginalLetter->RefuseText != null && $_xml->OriginalEmessage->OriginalLetter->RefuseText != ""
  865.                     || ($_xml->OriginalEmessage->RefuseText != null && $_xml->OriginalEmessage->RefuseText != "")) {
  866.                         if($_xml->OriginalEmessage->OriginalLetter->RefuseText != null && $_xml->OriginalEmessage->OriginalLetter->RefuseText != "") {
  867.                             $rT $_xml->OriginalEmessage->OriginalLetter->RefuseText;
  868.                             $rC $_xml->OriginalEmessage->OriginalLetter->RefuseCode;
  869.                         }
  870.                         if($_xml->OriginalEmessage->RefuseText != null && $_xml->OriginalEmessage->RefuseText != "") {
  871.                             $rT $_xml->OriginalEmessage->RefuseText;
  872.                             $rC $_xml->OriginalEmessage->RefuseCode;
  873.                         }
  874.                         $obj->setError('Afvist af VANS: '.$rC ' - ' .str_replace("<Break/>","<br />",$rT));
  875.                         $obj->setErrorType($code);
  876.                         $delivered false;
  877.                         $cor->setStatus(3);
  878.                         $this->em->persist($cor);
  879.                         $this->em->flush();
  880.                     }
  881.                     $obj->setRecieptDate();
  882.                     $obj->setDelivered($delivered);
  883.                     $reciept MedComMessages::withInsert($code,$ean,null,$cor,4,'Kvittering',false);
  884.                     $reciept->setFileData($xml->asXML());
  885.                     $this->em->persist($obj);
  886.                     $this->em->flush();
  887.                     $reciept->setParentID($obj->getId());
  888.                     $this->em->persist($reciept);
  889.                     $this->em->flush();
  890.                     //return new Response('success', Response::HTTP_OK,['content-type' => "text/json"]);
  891.                     return true;
  892.                 }
  893.                 $reciept MedComMessages::withInsert($code,$ean,null,null,4,'Kvittering',false);
  894.                 $reciept->setFileData($xml->asXML());
  895.                 $this->em->persist($reciept);
  896.                 $this->em->flush();
  897.                 //return new Response('failure', Response::HTTP_OK,['content-type' => "text/json"]);
  898.                 return false;
  899.             }
  900.         }
  901.     }
  902.     public function reply(Request $request) {
  903.         $this->auth($request);
  904.         $cbj $this->em->getRepository(MedComCorrespondence::class)->find($request->get("objID"));
  905.         $obj $this->repo->reply($this->user$request->get("message"), $request->get("priority"), $cbj,2);
  906.         if($cbj->getPatient() == null) {
  907.             $str $this->repo->getPatientData($cbj);
  908.             if($str != null) {
  909.                 $obj->setPatientString($str);
  910.                 $this->em->persist($obj);
  911.                 $this->em->flush();
  912.             }
  913.         }
  914.         $this->saveFile("xdis91"null$obj$cbj$this->repo->lastMessageID);
  915.         return new Response(json_encode('success'), Response::HTTP_OK,['content-type' => "text/json"]);
  916.     }
  917.     public function delete(Request $request) {
  918.         $this->auth($request);
  919.         $cbj $this->em->getRepository(MedComCorrespondence::class)->find($request->get("objID"));
  920.         $this->repo->delete($cbj);
  921.         return new Response(json_encode('success'), Response::HTTP_OK,['content-type' => "text/json"]);
  922.     }
  923.     public function deleteMessage(Request $request) {
  924.         $this->auth($request);
  925.         $cbj $this->em->getRepository(MedComMessages::class)->find($request->get("objID"));
  926.         $this->repo->delete($cbj);
  927.         return new Response(json_encode('success'), Response::HTTP_OK,['content-type' => "text/json"]);
  928.     }
  929.     public function medcom_receivers(Request $request) {
  930.         $this->em $this->getDoctrine()->getManager();
  931.         $repo $this->em->getRepository(MedComCorrespondence::class);
  932.         $repo->setEm($this->em);
  933.         $s $request->get("search");
  934.         $_data $repo->getMedcomListSearch($s);
  935.         $data = [];
  936.         foreach($_data as $_obj) {
  937.             $obj $_obj[0];
  938.             $data[] = ["full" => $_obj["full"],"id" => $obj->getId(), "EAN" => $obj->getEAN(), "UUID" => $obj->getSorId(), "entity" => $obj->getEntityName(), "parent" => $obj->getParent(), "type" => $obj->getEntityTypeName(), "address" => $obj->getPostalAddress(), "district" => $obj->getPostalDistrictName(), "postal" => $obj->getPostalPostCodeId()];
  939.         }
  940.         return new Response(json_encode($data), Response::HTTP_OK,['content-type' => "text/json"]);
  941.     }
  942.     public function resendMessage(Request $request) {
  943.         $this->auth($request);
  944.         $cbj $this->em->getRepository(MedComMessages::class)->find($request->get("objID"));
  945.         //resend
  946.         //new ids
  947.         return new Response(json_encode('success'), Response::HTTP_OK,['content-type' => "text/json"]);
  948.     }
  949.     private function getXmlAddDatetimeChild($parent,$name) {
  950.         $child $parent->addChild($name);
  951.         $child->addChild('Date'date("Y-m-d"));//datetime now
  952.         $child->addChild('Time'date("H:i"));//datetime now
  953.     }
  954.     private function getXmlStart($xmlns$euuid) {
  955.         $emssage = new \SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><Emessage />');
  956.         $emssage->addAttribute("xmlns"$xmlns);
  957.         $env $emssage->addChild('Envelope');
  958.         $this->getXmlAddDatetimeChild($env,"Sent");
  959.         $env->addChild('Identifier'$euuid);
  960.         $env->addChild('AcknowledgementCode'"minuspositivkvitt");
  961.         return $emssage;
  962.     }
  963.     private function getPatientData($parent$patientObj$address true) {
  964.         $patient $parent->addChild("Patient");
  965.         if($patientObj == null) {
  966.             $patientObj = new Patient();
  967.             $str $this->repo->getPatientData($cbj);
  968.             if($str != null) {
  969.                 $xml simplexml_load_string($str);
  970.                 $patientObj->setCpr($str->CivilRegistrationNumber);
  971.                 $$patientObj->setFirstName($str->PersonGivenName);
  972.                 $$patientObj->setLastName($str->PersonSurnameName);
  973.                 $patientObj->setCity($str->DistrictName);
  974.                 $patientObj->setPostNumber($str->PostCodeIdentifier);
  975.                 $patientObj->setAddressLine1($str->StreetName);
  976.             }
  977.         }
  978.         $patient->addChild("CivilRegistrationNumber"$patientObj->getCpr());
  979.         $patient->addChild("PersonSurnameName"$patientObj->getLastName());
  980.         $patient->addChild("PersonGivenName"$patientObj->getFirstName());
  981.         if($address) {
  982.             $patient->addChild("StreetName"$patientObj->getAddressLine1());
  983.             $patient->addChild("DistrictName"$patientObj->getCity());
  984.             $patient->addChild("PostCodeIdentifier"$patientObj->getPostNumber());
  985.             $patient->addChild("EpisodeOfCareStatusCode""inaktiv");
  986.         }
  987.     }
  988.     private function getXmlDis91($obj$cor$medcom,$files null$last9 null) {
  989.         $emssage $this->getXmlStart("http://rep.oio.dk/sundcom.dk/medcom.dk/xml/schemas/2006/07/01/"$obj->getEUUID());
  990.             $cEmail $emssage->addChild('ClinicalEmail');
  991.                 $letter $cEmail->addChild("Letter");
  992.                     $letter->addChild('Identifier'$obj->getUUID());
  993.                     $letter->addChild('VersionCode'"XD9134L");
  994.                     $letter->addChild('StatisticalCode'"XDIS91");
  995.                     $auth $letter->addChild('Authorisation');
  996.                         $auth->addChild('Date'date("Y-m-d")."");//datetime now
  997.                         $auth->addChild('Time'date("H:i")."");//datetime now
  998.                     $letter->addChild('TypeCode'"XDIS91");
  999.                     $letter->addChild('StatusCode'"nytbrev");
  1000.                     $letter->addChild('EpisodeOfCareIdentifier'$cor->getUUID());
  1001.                 $sender $cEmail->addChild("Sender");
  1002.                     $sender->addChild("EANIdentifier",$this->EANS);
  1003.                     $sender->addChild("Identifier"$this->medcomID);
  1004.                     $sender->addChild("IdentifierCode",$this->medcomIDCode);
  1005.                     $sender->addChild("OrganisationName",$this->orgName);
  1006.                     $sender->addChild("DepartmentName",$this->departmentName);
  1007.                     $sender->addChild("UnitName",$this->unitName);
  1008.                     $sender->addChild("DistrictName",$this->districtName);
  1009.                     $sender->addChild("PostCodeIdentifier",$this->postalCode);
  1010.                     $sender->addChild("MedicalSpecialityCode","ikkeklassificeret");
  1011.                 $receiver $cEmail->addChild("Receiver");
  1012.                     $receiver->addChild("EANIdentifier",$medcom->getEAN());
  1013.                     $receiver->addChild("Identifier",$medcom->getSorId());
  1014.                     $receiver->addChild("IdentifierCode","sygehusafdelingsnummer");
  1015.                     $receiver->addChild("OrganisationName",$medcom->getEntityName());
  1016.                     $receiver->addChild("DepartmentName",$medcom->getEntityTypeName());
  1017.                     $receiver->addChild("StreetName",$medcom->getPostalAddress());
  1018.                     $receiver->addChild("DistrictName",$medcom->getPostalDistrictName());
  1019.                     $receiver->addChild("PostCodeIdentifier",$medcom->getPostalPostCodeId());
  1020.                 $this->getPatientData($cEmail$cor->getPatient());
  1021.                 $addInfo $cEmail->addChild("AdditionalInformation");
  1022.                     $addInfo->addChild("Priority", ($obj->isHighPrio() ? "høj_prioritet" "rutine"));//rutine
  1023.                     $addInfo->addChild("Subject"$cor->getSubject());
  1024.                 $cInfo1 $cEmail->addChild("ClinicalInformation");
  1025.                     $length strlen($obj->getMessageBreakXML());
  1026.                     $cInfo1->addChild("Text01",$obj->getMessageBreakXML());
  1027.                     if($last9 != null && false) {
  1028.                         foreach($last9 as $_txt) {
  1029.                             $length += strlen(utf8_decode($_txt["message"]));
  1030.                             if($length <= 25000)
  1031.                                 $cEmail->addChild("ClinicalInformation")->addChild("Text01",utf8_decode($_txt["message"]));
  1032.                         }
  1033.                     }
  1034.                     //GET ALL FILES RELATED TO THIS
  1035.                     if($files != null && count($files) != 0) {
  1036.                         foreach($files as $file) {
  1037.                             $ref $cEmail->addChild("Reference");
  1038.                                 $ref->addChild("RefDescription","BIN");
  1039.                                 $bin $ref->addChild("BIN");
  1040.                                     $bin->addChild('ObjectIdentifier'str_replace('-','',$file->getGUID()));
  1041.                                     $object_type_code = ($file->getFileType() == "jpg" || $file->getFileType() == "jpeg" || $file->getFileType() == "gif" || $file->getFileType() == "png"  || $file->getFileType() == "bmp" || $file->getFileType() == "pcx" || $file->getFileType() == "tiff" "billede" "");
  1042.                                     $object_type_code = ($file->getFileType() == "pdf" "vektor_grafik"  $object_type_code);
  1043.                                     $object_type_code = ($file->getFileType() == "zip" "proprietaert_indhold"  $object_type_code);
  1044.                                     $object_type_code = ($file->getFileType() == "txt" || $file->getFileType() == "rtf" || $file->getFileType() == "doc" || $file->getFileType() == "xls" || $file->getFileType() == "wpd" "tekstfil" $object_type_code);
  1045.                                     $bin->addChild('ObjectCode'$object_type_code);
  1046.                                     $bin->addChild('ObjectExtensionCode', ($file->getFileType() == "jpg" "jpeg" $file->getFileType()));
  1047.                                     $bin->addChild('OriginalObjectSize'$file->getFileSize());
  1048.                         }
  1049.                     } else {
  1050.                         $ref $cEmail->addChild("Reference");
  1051.                             $ref->addChild("RefDescription","Der Findes yderliger oplysninger i SUP");
  1052.                             $ref->addChild("SUP""true");
  1053.                     }
  1054.         return $emssage;
  1055.     }
  1056.     private function getXmlctl02($obj,$cor,$medcom,$ean,$orgVersionCode,$refuseCode,$refuseText) {
  1057.         $emssage = new \SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><Emessage />');
  1058.         $emssage->addAttribute("xmlns""http://rep.oio.dk/sundcom.dk/medcom.dk/xml/schemas/2005/08/07/");
  1059.             $env $emssage->addChild('Envelope');
  1060.                 $envSent $env->addChild('Sent');
  1061.                     $envSent->addChild('Date'date("Y-m-d"));//datetime now
  1062.                     $envSent->addChild('Time'date("H:i"));//datetime now
  1063.                 $env->addChild('Identifier'uniqid());
  1064.                 $env->addChild('AcknowledgementCode'"minuspositivkvitt");
  1065.             $msg $emssage->addChild('NegativeReceipt');
  1066.                 $letter $msg->addChild("Letter");
  1067.                     $letter->addChild('Identifier',  uniqid());
  1068.                     $letter->addChild('VersionCode'"XC0230Q");
  1069.                     $letter->addChild('StatisticalCode'"XCTL02");
  1070.                 $sender $msg->addChild("Sender");
  1071.                     $sender->addChild("EANIdentifier",$this->EANS);
  1072.                 $receiver $msg->addChild("Receiver");
  1073.                     $receiver->addChild("EANIdentifier",$ean);
  1074.                 $orgmsg $msg->addChild("OriginalEmessage");
  1075.                 $orgmsg->addChild("OriginalEnvelopeIdentifier"$obj->getEUUID());
  1076.                 $orgSender $orgmsg->addChild("OriginalSender");
  1077.                     $orgSender->addChild("EANIdentifier",$ean);
  1078.                 $orgReceiver $orgmsg->addChild("OriginalReceiver");
  1079.                     $orgReceiver->addChild("EANIdentifier",$this->EANS);
  1080.                 $orgletter $orgmsg->addChild("OriginalLetter");
  1081.                     $orgletter->addChild("OriginalLetterIdentifier",$obj->getUUID());
  1082.                     $orgletter->addChild("OriginalVersionCode",$orgVersionCode);
  1083.                     if($refuseText !== null && $refuseCode !== "")
  1084.                         $orgletter->addChild("RefuseCode",$refuseCode);
  1085.                     if($refuseText !== null && $refuseText !== "")
  1086.                         $orgletter->addChild("RefuseText",$refuseText);
  1087.         return $emssage;
  1088.     }
  1089.     private function getXmlctl03($obj,$cor,$medcom,$ean) {
  1090.         $emssage = new \SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><Emessage />');
  1091.             $emssage->addAttribute("xmlns""http://rep.oio.dk/sundcom.dk/medcom.dk/xml/schemas/2005/08/07/");
  1092.             $env $emssage->addChild('Envelope');
  1093.                 $envSent $env->addChild('Sent');
  1094.                     $envSent->addChild('Date'date("Y-m-d"));//datetime now
  1095.                     $envSent->addChild('Time'date("H:i"));//datetime now
  1096.                 $env->addChild('Identifier'uniqid());
  1097.                 $env->addChild('AcknowledgementCode'"minuspositivkvitt");
  1098.             $msg $emssage->addChild('PositiveReceipt');
  1099.                 $letter $msg->addChild("Letter");
  1100.                     $letter->addChild('Identifier'uniqid());
  1101.                     $letter->addChild('VersionCode'"XC0330Q");
  1102.                     $letter->addChild('StatisticalCode'"XCTL03");
  1103.                 $sender $msg->addChild("Sender");
  1104.                     $sender->addChild("EANIdentifier",$this->EANS);
  1105.                 $receiver $msg->addChild("Receiver");
  1106.                     $receiver->addChild("EANIdentifier"$ean);
  1107.                 $orgmsg $msg->addChild("OriginalEmessage");
  1108.                 $orgmsg->addChild("OriginalEnvelopeIdentifier"$obj->getEUUID());
  1109.                 $orgSender $orgmsg->addChild("OriginalSender");
  1110.                     $orgSender->addChild("EANIdentifier",$ean);
  1111.                 $orgReceiver $orgmsg->addChild("OriginalReceiver");
  1112.                     $orgReceiver->addChild("EANIdentifier",$this->EANS);
  1113.                 $orgletter $orgmsg->addChild("OriginalLetter");
  1114.                     $orgletter->addChild("OriginalLetterIdentifier",$obj->getUUID());
  1115.                     if(strtolower($obj->getType()) == "bilag"){
  1116.                         $orgletter->addChild("OriginalVersionCode","XB0131X");
  1117.                     } else {
  1118.                         $orgletter->addChild("OriginalVersionCode","XD9134L");
  1119.                     }
  1120.         return $emssage;
  1121.                         
  1122.     }
  1123.     private function getXmlbin01($obj,$medcom) {
  1124.         $emssage = new \SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><Emessage />');
  1125.         $emssage->addAttribute("xmlns""http://rep.oio.dk/medcom.dk/xml/schemas/2012/03/28/");
  1126.             $env $emssage->addChild('Envelope');
  1127.                 $envSent $env->addChild('Sent');
  1128.                     $envSent->addChild('Date'date("Y-m-d"));//datetime now
  1129.                     $envSent->addChild('Time'date("H:i"));//datetime now
  1130.                 $env->addChild('Identifier'$obj->getEUUID());
  1131.                 $env->addChild('AcknowledgementCode'"minuspositivkvitt");
  1132.             $cEmail $emssage->addChild('BinaryLetter');
  1133.                 $letter $cEmail->addChild("Letter");
  1134.                     $letter->addChild('Identifier'$obj->getUUID());
  1135.                     $letter->addChild('VersionCode'"XB0131X");
  1136.                     $letter->addChild('StatisticalCode'"XBIN01");
  1137.                     $auth $letter->addChild('Authorisation');
  1138.                         $auth->addChild('Date'date("Y-m-d")."");//datetime now
  1139.                         $auth->addChild('Time'date("H:i")."");//datetime now
  1140.                     $letter->addChild('TypeCode'"XBIN01");
  1141.                     $letter->addChild('StatusCode'"nytbrev");
  1142.                 $sender $cEmail->addChild("Sender");
  1143.                     $sender->addChild("EANIdentifier",$this->EANS);
  1144.                     $sender->addChild("Identifier"$this->medcomID);
  1145.                     $sender->addChild("IdentifierCode",$this->medcomIDCode);
  1146.                     $sender->addChild("OrganisationName",$this->orgName);
  1147.                     $sender->addChild("DepartmentName",$this->departmentName);
  1148.                     $sender->addChild("UnitName",$this->unitName);
  1149.                     $sender->addChild("DistrictName",$this->districtName);
  1150.                     $sender->addChild("PostCodeIdentifier",$this->postalCode);
  1151.                     $sender->addChild("MedicalSpecialityCode","ikkeklassificeret");
  1152.                 $receiver $cEmail->addChild("Receiver");
  1153.                     $receiver->addChild("EANIdentifier",$medcom->getEAN());
  1154.                     $receiver->addChild("Identifier",$medcom->getSorId());
  1155.                     $receiver->addChild("IdentifierCode","sygehusafdelingsnummer");
  1156.                     $receiver->addChild("OrganisationName",$medcom->getEntityName());
  1157.                     $receiver->addChild("DepartmentName",$medcom->getEntityTypeName());
  1158.                     $receiver->addChild("StreetName",$medcom->getPostalAddress());
  1159.                     $receiver->addChild("DistrictName",$medcom->getPostalDistrictName());
  1160.                     $receiver->addChild("PostCodeIdentifier",$medcom->getPostalPostCodeId());
  1161.                 $cor $obj->getCorrospondence();
  1162.                 $this->getPatientData($cEmail$cor->getPatient(), false);
  1163.                 $msg $cEmail->addChild('BinaryObject');
  1164.                     $msg->addChild('ObjectIdentifier'$obj->getGUID());
  1165.                     $object_type_code = ($obj->getFileType() == "jpg" || $obj->getFileType() == "jpeg" || $obj->getFileType() == "gif" || $obj->getFileType() == "png"  || $obj->getFileType() == "bmp" || $obj->getFileType() == "pcx" || $obj->getFileType() == "tiff" "billede" "");
  1166.                     $object_type_code = ($obj->getFileType() == "pdf" "vektor_grafik"  $object_type_code);
  1167.                     $object_type_code = ($obj->getFileType() == "zip" "proprietaert_indhold"  $object_type_code);
  1168.                     $object_type_code = ($obj->getFileType() == "txt" || $obj->getFileType() == "rtf" || $obj->getFileType() == "doc" || $obj->getFileType() == "xls" || $obj->getFileType() == "wpd" "tekstfil" $object_type_code);
  1169.                     $msg->addChild('ObjectCode'$object_type_code);
  1170.                     $msg->addChild('ObjectExtensionCode', ($obj->getFileType() == "jpg" "jpeg" $obj->getFileType()));
  1171.                     $msg->addChild('OriginalObjectSize'$obj->getFileSize());
  1172.                     $msg->addChild('Object_Base64Encoded'$obj->getFileData());
  1173.         return $emssage;
  1174.     }
  1175. }