+++ /dev/null
-<?php
-class DB extends SQLite3 {
- function __construct(){
- $this->open("data/data.db");
- }
- function checkUser($username, $password){
- $stmt=$this->prepare("SELECT hash FROM users WHERE user=:user");
- $stmt->bindValue(":user", $username);
- $result=$stmt->execute();
- $hash=$result->fetchArray();
- if($hash){
- return(password_verify($password, $hash["hash"]));
- }
- else{
- return(false);
- }
- }
- function admit($post){
- $quer=$this->prepare("SELECT count(rowid) FROM patients WHERE pid=:pid");
- $quer->bindValue(":pid", $post["pid"]);
- $exist=$quer->execute();
- if($exist->fetchArray()[0]==0){
- $stmt=$this->prepare("INSERT INTO patients (pid,name,age,sex,status,summary,ward,bed,diagnosis,data) VALUES (:pid,:name,:age,:sex,'admitted',:summary,:ward,:bed,:diagnosis,:data);");
- }
- else{
- $stmt=$this->prepare("UPDATE patients SET name=:name,age=:age,sex=:sex,ward=:ward,bed=:bed,diagnosis=:diagnosis,summary=:summary,data=:data WHERE pid=:pid;");
- }
- $stmt->bindValue(":pid", $post["pid"]);
- $stmt->bindValue(":name", $post["name"]);
- $stmt->bindValue(":age", $post["age"]);
- $stmt->bindValue(":sex", $post["sex"]);
- $stmt->bindValue(":status", "admitted");
- $stmt->bindValue(":ward", $post["ward"]);
- $stmt->bindValue(":bed", $post["bed"]);
- $stmt->bindValue(":diagnosis", $post["diagnosis"]);
- $stmt->bindValue(":summary", $post["summary"]);
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function updateHistory($post, $pid){
- $stmt=$this->prepare("UPDATE patients SET history=:history WHERE pid=:pid;");
- $stmt->bindValue(":history", json_encode($post));
- $stmt->bindValue(":pid", $pid);
- $stmt->execute();
- }
- function addPhysician($post, $pid){
- $stmt=$this->prepare("INSERT INTO physician (pid, time, data) VALUES (:pid, :time, :data);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function editPhysician($post, $pid, $id){
- $stmt=$this->prepare("UPDATE physician SET time=:time,data=:data WHERE pid=:pid AND rowid=:id;");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":id", $id);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function addNursing($post, $pid){
- $stmt=$this->prepare("INSERT INTO nursing (pid, time, data) VALUES (:pid, :time, :data);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function editNursing($post, $pid, $id){
- $stmt=$this->prepare("UPDATE nursing SET time=:time,data=:data WHERE pid=:pid AND rowid=:id;");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":id", $id);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function addReport($post, $pid, $form){
- $stmt=$this->prepare("INSERT INTO reports (pid, time, form, data) VALUES (:pid, :time, :form, :data);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":form", $post["form"]);
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function editReport($post, $pid, $id, $form){
- $stmt=$this->prepare("UPDATE reports SET time=:time,data=:data WHERE pid=:pid AND rowid=:id;");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":id", $id);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- }
- function addDrug($pid, $drug, $dose, $route, $frequency, $date, $time, $duration, $addl){
- $stmt=$this->prepare("INSERT INTO treatment (pid, drug, dose, route, frequency, start, duration, omit, addl) VALUES (:pid, :drug, :dose, :route, :frequency, :start, :duration, :omit, :addl);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":drug", $drug);
- $stmt->bindValue(":dose", $dose);
- $stmt->bindValue(":route", $route);
- $stmt->bindValue(":frequency", $frequency);
- $stmt->bindValue(":start", strtotime($date." ".$time));
- $stmt->bindValue(":duration", $duration);
- $stmt->bindValue(":addl", $addl);
- $stmt->bindValue(":omit", false);
- $stmt->execute();
- }
- function omitDrug($id){
- $stmt=$this->prepare("UPDATE treatment SET end=:end,omit=:omit WHERE rowid=:id;");
- $stmt->bindValue(":end", time());
- $stmt->bindValue(":omit", true);
- $stmt->bindValue(":id", $id);
- $stmt->execute();
- }
- function addRequisition($pid, $test, $date, $time, $room){
- $stmt=$this->prepare("INSERT INTO requisition (pid, test, time, room, sample, status) VALUES (:pid, :test, :time, :room, :sample, :status);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":test", $test);
- $stmt->bindValue(":time", strtotime($date." ".$time));
- $stmt->bindValue(":room", $room);
- $stmt->bindValue(":status", "sent");
- $stmt->execute();
- }
- function omitRequisition($id){
- $stmt=$this->prepare("UPDATE requisition SET status=:status WHERE rowid=:id;");
- $stmt->bindValue(":status", "done");
- $stmt->bindValue(":id", $id);
- $stmt->execute();
- }
- function addAdvice($pid, $name, $dose, $route, $frequency, $duration, $addl){
- $stmt=$this->prepare("INSERT INTO discharge (pid, name, dose, route, frequency, duration, addl) VALUES (:pid, :name, :dose, :route, :frequency, :duration, :addl);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":name", $name);
- $stmt->bindValue(":dose", $dose);
- $stmt->bindValue(":route", $route);
- $stmt->bindValue(":frequency", $frequency);
- $stmt->bindValue(":duration", $duration);
- $stmt->bindValue(":addl", $addl);
- $stmt->execute();
- }
- function deleteAdvice($id){
- $stmt=$this->prepare("DELETE FROM discharge WHERE rowid=:id;");
- $stmt->bindValue(":id", $id);
- $stmt->execute();
- }
- function setDischarged($pid){
- $stmt=$this->prepare("UPDATE patients SET status=:discharged WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $stmt->execute();
- }
- function setDead($pid, $post){
- $stmt=$this->prepare("INSERT INTO death (pid, time, data) VALUES (:pid, :time, :data);");
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":time", strtotime($post["date"].$post["time"]));
- $stmt->bindValue(":data", json_encode($post));
- $stmt->execute();
- $stmt=$this->prepare("UPDATE patients SET status='expired' WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $stmt->execute();
- }
- function getDrugs($pid){
- $stmt=$this->prepare("SELECT rowid,* FROM treatment WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getRequisitions($pid){
- $stmt=$this->prepare("SELECT rowid,* FROM requisition WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getAdvice($pid){
- $stmt=$this->prepare("SELECT rowid,* FROM discharge WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getName($pid){
- $stmt=$this->prepare("SELECT name FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getAge($pid){
- $stmt=$this->prepare("SELECT age FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getSex($pid){
- $stmt=$this->prepare("SELECT sex FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getWard($pid){
- $stmt=$this->prepare("SELECT ward FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getBed($pid){
- $stmt=$this->prepare("SELECT bed FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getStatus($pid){
- $stmt=$this->prepare("SELECT status FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getDiagnosis($pid){
- $stmt=$this->prepare("SELECT diagnosis FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getList(){
- $stmt=$this->prepare("SELECT pid FROM patients;");
- $result=$stmt->execute();
- return($result);
- }
- function getForm($id){
- $stmt=$this->prepare("SELECT form FROM reports WHERE rowid=:id;");
- $stmt->bindValue(":id", $id);
- $result=$stmt->execute();
- return($result);
- }
- function getAdmission($pid){
- $stmt=$this->prepare("SELECT data FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getHistory($pid){
- $stmt=$this->prepare("SELECT history FROM patients WHERE pid=:pid;");
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
- function getData($pid, $id, $cat){
- if($cat=="physician"){
- $stmt=$this->prepare("SELECT data FROM physician WHERE pid=:pid AND rowid=:id;");
- } elseif($cat=="nursing"){
- $stmt=$this->prepare("SELECT data FROM nursing WHERE pid=:pid AND rowid=:id;");
- } elseif($cat=="reports"){
- $stmt=$this->prepare("SELECT data FROM reports WHERE pid=:pid AND rowid=:id;");
- } else{
- return(false);
- }
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":id", $id);
- $result=$stmt->execute();
- return($result);
- }
- function getAllData($pid, $cat){
- if($cat=="physician"){
- $stmt=$this->prepare("SELECT rowid,data FROM physician WHERE pid=:pid;");
- } elseif($cat=="nursing"){
- $stmt=$this->prepare("SELECT rowid,data FROM nursing WHERE pid=:pid;");
- } elseif($cat=="reports"){
- $stmt=$this->prepare("SELECT rowid,data FROM reports WHERE pid=:pid;");
- } elseif($cat=="info"){
- $stmt=$this->prepare("SELECT rowid,data FROM patients WHERE pid=:pid;");
- } elseif($cat=="history"){
- $stmt=$this->prepare("SELECT rowid,history FROM patients WHERE pid=:pid;");
- } else{
- return(false);
- }
- $stmt->bindValue(":pid", $pid);
- $result=$stmt->execute();
- return($result);
- }
-}
-$db = new DB();
-?>
--- /dev/null
+<?php
+require("lib/db.php");
+require("lib/functions.php");
+session_start();
+if(empty($_SESSION["user"])){
+ header("Location: login.php");
+ exit();
+}
+function json2tex($data){
+ $data=json_decode($data);
+ if(!empty($data->form)){
+ $schema=json_decode(file_get_contents("forms/".$data->form.".schema.json"));
+ }
+ unset($data->cat);
+ $view="\begin{tabularx}{\\textwidth}{l X}\n";
+ foreach($data as $field=>$value){
+ if($field!="form" && !empty($value)){
+ if(!empty($schema->properties->$field)){
+ $view=$view.$schema->properties->$field->description." & ".$value."\\\\\n";
+ }
+ else{
+ $view=$view.$field." & ".$value."\n";
+ }
+ }
+ }
+ $view=$view."\\end{tabularx}\n";
+ return $view;
+}
+if(!empty($_GET["pid"])){
+ $pid=$_GET["pid"];
+ $template=file_get_contents("discharge.tex");
+ if(!empty($_POST["discharge-note"])){
+ $template=str_replace("%[note]%", $_POST["discharge-note"], $template);
+ }
+ $template=str_replace("%[name]%", $db->getName($pid)->fetcharray()["name"], $template);
+ $template=str_replace("%[age]%", $db->getAge($pid)->fetcharray()["age"], $template);
+ $template=str_replace("%[sex]%", $db->getSex($pid)->fetcharray()["sex"], $template);
+ $template=str_replace("%[pid]%", $pid, $template);
+ $template=str_replace("%[diagnosis]%", $db->getDiagnosis($pid)->fetcharray()["diagnosis"], $template);
+ $template=str_replace("%[doa]%", $db->getAdmission($pid)->fetcharray()["admission"], $template);
+ $template=str_replace("%[dod]%", $db->getDeparture($pid)->fetcharray()["departure"], $template);
+ $template=str_replace("%[summary]%", $db->getSummary($pid)->fetcharray()["summary"], $template);
+ $list=$db->getAdvice($pid);
+ $view="";
+ while($drug=$list->fetchArray()){
+ $view=$view."\item ".$drug["drug"]." ".$drug["dose"]." ".$drug["route"]." ".$drug["frequency"]." ".$drug["duration"]." ".$drug["addl"]."\n";
+ }
+ if($view){
+ $template=str_replace("%[advice]%", "\begin{enumerate}\n".$view."\\end{enumerate}", $template);
+ }
+ $reports=[];
+ $reportsArray=$db->getAllData($pid, "reports");
+ while($r=$reportsArray->fetchArray()){
+ $template=str_replace("%[reports]%", json2tex($r["data"]), $template);
+ }
+ //echo $template;
+ $f=str_replace("/", "", $pid)."-".time()."-".rand();
+ file_put_contents("data/discharge/".$f.".tex", $template);
+ exec("pdflatex --output-directory data/discharge/ data/discharge/".$f.".tex", $out, $ret);
+ //var_dump($out);
+ //var_dump($ret);
+ if($ret!=0){
+ header("Content-Type: text/plain");
+ echo "Failed to generate discharge certificate. Please check whether patient information, summary, reports, discharge advices are properly filled up.";
+ }
+ else{
+ $db->setDischarged($pid);
+ header("Content-Type: application/pdf");
+ readFile("data/discharge/".$f.".pdf");
+ exec(" rm data/discharge/".$f."*");
+ }
+}
+?>