-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.php
157 lines (137 loc) · 4.26 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
$LAST_NAME_INPUT_ID = "A01";
$FIRST_NAME_INPUT_ID = "A02";
$DOB_INPUT_ID = "A03";
$SEX_INPUT_ID = "A04";
$json_content = file_get_contents('php://input');
$json_obj = json_decode($json_content);
$form_type = $json_obj->{'type'};
$cform = $json_obj->{'cform'};
$cform_json = json_encode($cform);
include '../sites/'.$json_obj->{'site'}.'/sqlconf.php';
$conn = new mysqli($host, $login, $pass, $dbase, $port) or die("mysqli connection failed: %s\n". $conn -> error);
// r = response
$lname = $cform->{$LAST_NAME_INPUT_ID}->{'r'};
$fname = $cform->{$FIRST_NAME_INPUT_ID}->{'r'};
$sex = $cform->{$SEX_INPUT_ID}->{'r'};
// format dob for mysql
$dob = date_parse($cform->{$DOB_INPUT_ID}->{'r'});
$dob = $dob[year].'-'.str_pad($dob[month],2,'0',STR_PAD_LEFT).'-'.str_pad($dob[day],2,'0',STR_PAD_LEFT);
$cform->{$DOB_INPUT_ID}->{'r'} = $dob;
$upper_lname = strtoupper($lname);
$upper_fname = strtoupper($fname);
// Check if patient already exists
$pid = '';
$sql = "SELECT pid FROM patient_data WHERE UPPER(lname)='$upper_lname' AND UPPER(fname)='$upper_fname' AND DOB like '$dob%'";
$result = mysqli_query($conn, $sql);
if(!result){
echo "{\"status\":\"SQL Error 1: ".mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
$row = mysqli_fetch_row($result);
if($row){
$pid = $row[0];
}
// Create a new record if patient doesnt exist
if($pid==''){
// Get the next pid seq
$sql = "SELECT pid FROM patient_data ORDER BY pid DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
if(!result){
echo "{\"status\":\"SQL Error 2: ".mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
$row = mysqli_fetch_row($result);
$last_pid = $row[0];
$pid = intval($last_pid) + 1;
// Save the patient_data record
$sql = <<<EOT
INSERT INTO patient_data(pid, fname, lname, DOB, sex)
VALUES(
$pid,
'$fname',
'$lname',
'$dob',
'$sex'
);
EOT;
if(!mysqli_query($conn, $sql)){
echo "{\"status\":\"SQL Error 3: ".$sql.' '.mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
// Save the patient_history and insurance_data stubs
$sql = <<<EOT
INSERT INTO history_data(pid, date)
VALUES(
$pid,
curdate()
);
EOT;
if(!mysqli_query($conn, $sql)){
echo "{\"status\":\"SQL Error 4: ".$sql.' '.mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
$sql = <<<EOT
INSERT INTO insurance_data(type,subscriber_DOB,pid,date,accept_assignment)
VALUES(
'primary',
CURDATE(),
$pid,
CURDATE(),
TRUE
)
EOT;
if(!mysqli_query($conn, $sql)){
echo "{\"status\":\"SQL Error 5: ".$sql.' '.mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
}
// Save the cform data
$sql = <<<EOT
INSERT INTO cforms(form_type, pid, fname, lname, dob, data)
VALUES (
'$form_type',
$pid,
'$fname',
'$lname',
'$dob',
'$cform_json'
)
EOT;
if(!mysqli_query($conn, $sql)){
echo "{\"status\":\"SQL Error 6: ".mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
// Get the cform record ID
$sql = "SELECT LAST_INSERT_ID()";
$result = mysqli_query($conn, $sql);
if(!result){
echo "{\"status\":\"SQL Error 7: ".mysqli_error($conn)."\"}";
http_response_code(500);
return;
}
$row = mysqli_fetch_row($result);
$record_id = $row[0];
$dob = str_replace("-","",$dob);
$filename=str_replace(" ","_",("report-".$record_id."_".$lname."_".$fname."_".$dob.".pdf"));
$retval = 0;
// Create PDF for patient update and uncomment below
//$cmd = "chrome --headless --disable-gpu --print-to-pdf=/var/www/html/openemr/cforms/reports/".$filename http://localhost/cforms/report.php?id=$record_id";
//$cmd = "echo 'test' > /var/www/html/openemr/cforms/reports/".$filename;
//system($cmd, $retval);
header('Content-Type: application/json; charset=utf-8');
if($retval!=0){
echo "{\"status\":\"File Write Error $retval\"}";
http_response_code(500);
}else{
echo "{\"status\":\"success\"}";
http_response_code(200);
}
$conn -> close();
?>