bug fixes in patient_files summary directory (#1964)
[openemr.git] / interface / patient_file / encounter / diagnosis.php
blob109bbd99cec7e2f329efada35779b4ada37d5550
1 <?php
2 /**
3 * diagnosis.php
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Brady Miller <brady.g.miller@gmail.com>
8 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once("../../globals.php");
14 require_once("$srcdir/billing.inc");
15 require_once("$srcdir/acl.inc");
17 $mode = $_REQUEST['mode'];
18 $type = $_REQUEST['type'];
19 $code = $_REQUEST['code'];
20 $modifier = $_REQUEST['modifier'];
21 $units = $_REQUEST['units'];
22 $fee = $_REQUEST['fee'];
23 $text = $_REQUEST['text'];
24 $payment_method = $_REQUEST['payment_method'];
25 $insurance_company = $_REQUEST['insurance_company'];
27 $target = '_parent';
29 // Possible units of measure for NDC drug quantities.
30 $ndc_uom_choices = array(
31 'ML' => 'ML',
32 'GR' => 'Grams',
33 'ME' => 'Milligrams',
34 'F2' => 'I.U.',
35 'UN' => 'Units'
38 if ($payment_method == "insurance") {
39 $payment_method = "insurance: " . $insurance_company;
42 if (isset($mode)) {
43 if (!verifyCsrfToken($_GET["csrf_token_form"])) {
44 csrfNotVerified();
47 if ($mode == "add") {
48 // Get the provider ID from the new encounter form if possible, otherwise
49 // it's the logged-in user.
50 $tmp = sqlQuery("SELECT users.id FROM forms, users WHERE " .
51 "forms.pid = ? AND forms.encounter = ? AND " .
52 "forms.formdir='newpatient' AND users.username = forms.user AND " .
53 "users.authorized = 1", array($pid, $encounter));
54 $provid = $tmp['id'] ? $tmp['id'] : $_SESSION["authUserID"];
56 if (strtolower($type) == "copay") {
57 addBilling(
58 $encounter,
59 $type,
60 sprintf("%01.2f", $code),
61 $payment_method,
62 $pid,
63 $userauthorized,
64 $provid,
65 $modifier,
66 $units,
67 sprintf("%01.2f", 0 - $code)
69 } elseif (strtolower($type) == "other") {
70 addBilling(
71 $encounter,
72 $type,
73 $code,
74 $text,
75 $pid,
76 $userauthorized,
77 $provid,
78 $modifier,
79 $units,
80 sprintf("%01.2f", $fee)
82 } else {
83 $ndc_info = '';
84 // If HCPCS, get and save default NDC data.
85 if (strtolower($type) == "hcpcs") {
86 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
87 "code_type = 'HCPCS' AND code = ? AND ndc_info LIKE 'N4%' " .
88 "ORDER BY date DESC LIMIT 1", array($code));
89 if (!empty($tmp)) {
90 $ndc_info = $tmp['ndc_info'];
94 addBilling(
95 $encounter,
96 $type,
97 $code,
98 $text,
99 $pid,
100 $userauthorized,
101 $provid,
102 $modifier,
103 $units,
104 $fee,
105 $ndc_info
108 } elseif ($mode == "justify") {
109 $diags = $_POST['code']['diag'];
110 $procs = $_POST['code']['proc'];
111 $sql = array();
112 if (!empty($procs) && !empty($diags)) {
113 $sql = array();
114 foreach ($procs as $proc) {
115 $justify_string = "";
116 foreach ($diags as $diag) {
117 $justify_string .= $diag . ":";
120 $sql[] = "UPDATE billing set justify = concat(justify,'" . add_escape_custom($justify_string) ."') where encounter = '" . add_escape_custom($_POST['encounter_id']) . "' and pid = '" . add_escape_custom($_POST['patient_id']) . "' and code = '" . add_escape_custom($proc) . "'";
124 if (!empty($sql)) {
125 foreach ($sql as $q) {
126 $results = sqlQ($q);
130 // Save NDC fields, if present.
131 $ndcarr = $_POST['ndc'];
132 for ($lino = 1; !empty($ndcarr["$lino"]['code']); ++$lino) {
133 $ndc = $ndcarr["$lino"];
134 $ndc_info = '';
135 if ($ndc['ndcnum']) {
136 $ndc_info = 'N4' . trim($ndc['ndcnum']) . ' ' . $ndc['ndcuom'] .
137 trim($ndc['ndcqty']);
140 sqlStatement("UPDATE billing SET ndc_info = ? WHERE " .
141 "encounter = ? AND " .
142 "pid = ? AND " .
143 "code = ?", array($ndc_info, $_POST['encounter_id'], $_POST['patient_id'], $ndc['code']));
149 <html>
150 <head>
151 <?php html_header_show();?>
152 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
154 <script language="JavaScript">
156 function validate(f) {
157 for (var lino = 1; f['ndc['+lino+'][code]']; ++lino) {
158 var pfx = 'ndc['+lino+']';
159 if (f[pfx+'[ndcnum]'] && f[pfx+'[ndcnum]'].value) {
160 // Check NDC number format.
161 var ndcok = true;
162 var ndc = f[pfx+'[ndcnum]'].value;
163 var a = ndc.split('-');
164 if (a.length != 3) {
165 ndcok = false;
167 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
168 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
169 ndcok = false;
171 else {
172 for (var i = 0; i < 3; ++i) {
173 for (var j = 0; j < a[i].length; ++j) {
174 var c = a[i].charAt(j);
175 if (c < '0' || c > '9') ndcok = false;
179 if (!ndcok) {
180 alert(<?php echo xlj('Format incorrect for NDC'); ?> + ' ' + ndc +
181 ', ' + <?php echo xlj('should be like nnnnn-nnnn-nn'); ?>);
182 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
183 return false;
185 // Check for valid quantity.
186 var qty = f[pfx+'[ndcqty]'].value - 0;
187 if (isNaN(qty) || qty <= 0) {
188 alert(<?php echo xlj('Quantity for NDC'); ?> + ' ' + ndc +
189 ' ' + <?php echo xlj('is not valid (decimal fractions are OK).'); ?>);
190 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
191 return false;
195 top.restoreSession();
196 return true;
199 </script>
201 </head>
203 <body class="body_bottom">
205 <?php
206 $thisauth = acl_check('encounters', 'coding_a');
207 if (!$thisauth) {
208 $erow = sqlQuery("SELECT user FROM forms WHERE " .
209 "encounter = ? AND formdir = 'newpatient' LIMIT 1", array($encounter));
210 if ($erow['user'] == $_SESSION['authUser']) {
211 $thisauth = acl_check('encounters', 'coding');
215 if ($thisauth) {
216 $tmp = getPatientData($pid, "squad");
217 if ($tmp['squad'] && ! acl_check('squads', $tmp['squad'])) {
218 $thisauth = 0;
222 if (!$thisauth) {
223 echo "<p>(" . xlt('Coding not authorized') . ")</p>\n";
224 echo "</body>\n</html>\n";
225 exit();
229 <form name="diagnosis" method="post" action="diagnosis.php?mode=justify&csrf_token_form=<?php echo attr_url(collectCsrfToken()); ?>"
230 onsubmit="return validate(this)">
231 <table border=0 cellspacing=0 cellpadding=0 height=100%>
232 <tr>
234 <td valign=top>
236 <dl>
237 <dt>
238 <a href="diagnosis_full.php" target="<?php echo attr($target); ?>" onclick="top.restoreSession()">
239 <span class=title><?php echo ($GLOBALS['phone_country_code'] == '1') ? xlt('Billing') : xlt('Coding'); ?></span>
240 <font class=more><?php echo text($tmore); ?></font></a>
242 <?php
243 if (!empty($_GET["back"]) || !empty($_POST["back"])) {
244 print "&nbsp;<a href=\"superbill_codes.php\" target=\"" . attr($target) . "\" onclick=\"top.restoreSession()\"><font class=more>" . text($tback) . "</font></a>";
245 print "<input type=\"hidden\" name=\"back\" value=\"1\">";
248 <?php if (!$GLOBALS['weight_loss_clinic']) { ?>
249 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
250 <input type="submit" name="justify" value="<?php echo xla('Justify/Save');?>">
251 <?php } ?>
252 </dt>
253 </dl>
255 <a href="cash_receipt.php?csrf_token_form=<?php echo attr_url(collectCsrfToken()); ?>" class='link_submit' target='new' onclick='top.restoreSession()'>
256 [<?php echo xlt('Receipt'); ?>]
257 </a>
258 <table border="0">
259 <?php
260 if ($result = getBillingByEncounter($pid, $encounter, "*")) {
261 $billing_html = array();
262 $total = 0.0;
263 $ndclino = 0;
264 foreach ($result as $iter) {
265 if ($iter["code_type"] == "ICD9") {
266 $html = "<tr>";
267 $html .= "<td valign=\"middle\">" .
268 '<input style="width: 11px;height: 11px;" name="code[diag][' .
269 attr($iter["code"]) . ']" type="checkbox" value="' . attr($iter["code"]) . '">' .
270 "</td><td><div><a target='" . attr($target) . "' class='small' " .
271 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
272 text($iter{"code"}) . "</b> " . text($iter{"code_text"}) .
273 "</a></div></td></tr>\n";
274 $billing_html[$iter["code_type"]] .= $html;
275 $counter++;
276 } elseif ($iter["code_type"] == "COPAY") {
277 $billing_html[$iter["code_type"]] .=
278 "<tr><td></td><td><a target='" . attr($target) . "' class='small' " .
279 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
280 text(oeFormatMoney($iter['code'])) . "</b> " .
281 text(ucwords(strtolower($iter['code_text']))) .
282 ' ' . xlt('payment entered on') . ' ' .
283 text(oeFormatShortDate(substr($iter['date'], 0, 10))) . text(substr($iter['date'], 10, 6)) . "</a></td></tr>\n";
284 } else {
285 $billing_html[$iter["code_type"]] .=
286 "<tr><td>" . '<input style="width: 11px;height: 11px;" name="code[proc][' .
287 attr($iter["code"]) . ']" type="checkbox" value="' . attr($iter["code"]) . '">' .
288 "</td><td><a target='$target' class='small' " .
289 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
290 text($iter{"code"}) . ' ' . text($iter['modifier']) . "</b> " .
291 text(ucwords(strtolower($iter{"code_text"}))) . ' ' . text(oeFormatMoney($iter['fee'])) .
292 "</a><span class=\"small\">";
293 $total += $iter['fee'];
294 $js = explode(":", $iter['justify']);
295 $counter = 0;
296 foreach ($js as $j) {
297 if (!empty($j)) {
298 if ($counter == 0) {
299 $billing_html[$iter["code_type"]] .= " (<b>" . text($j) . "</b>)";
300 } else {
301 $billing_html[$iter["code_type"]] .= " (" . text($j) . ")";
304 $counter++;
308 $billing_html[$iter["code_type"]] .= "</span></td></tr>\n";
310 // If this is HCPCS, write NDC line.
311 if ($iter['code_type'] == 'HCPCS') {
312 ++$ndclino;
313 $ndcnum = '';
314 $ndcuom = '';
315 $ndcqty = '';
316 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $iter['ndc_info'], $tmp)) {
317 $ndcnum = $tmp[1];
318 $ndcuom = $tmp[2];
319 $ndcqty = $tmp[3];
322 $billing_html[$iter["code_type"]] .=
323 "<tr><td>&nbsp;</td><td class='small'>NDC:&nbsp;\n" .
324 "<input type='hidden' name='ndc[" . attr($ndclino) . "][code]' value='" . attr($iter["code"]) . "'>" .
325 "<input type='text' name='ndc[" . attr($ndclino) . "][ndcnum]' value='" . attr($ndcnum) . "' " .
326 "size='11' style='background-color:transparent'>" .
327 " &nbsp;Qty:&nbsp;" .
328 "<input type='text' name='ndc[" . attr($ndclino) . "][ndcqty]' value='" . attr($ndcqty) . "' " .
329 "size='3' style='background-color:transparent;text-align:right'> " .
330 "<select name='ndc[" . attr($ndclino) . "][ndcuom]' style='background-color:transparent'>";
331 foreach ($ndc_uom_choices as $key => $value) {
332 $billing_html[$iter["code_type"]] .= "<option value='" . attr($key) . "'";
333 if ($key == $ndcuom) {
334 $billing_html[$iter["code_type"]] .= " selected";
337 $billing_html[$iter["code_type"]] .= ">" . text($value) . "</option>";
340 $billing_html[$iter["code_type"]] .= "</select></td></tr>\n";
345 $billing_html["CPT4"] .= "<tr><td>" . xlt('total') . ":</td><td>" . text(oeFormatMoney($total)) . "</td></tr>\n";
346 foreach ($billing_html as $key => $val) {
347 print "<tr><td>" . text($key) . "</td><td><table>" . $val . "</table><td></tr><tr><td height=\"5\"></td></tr>\n";
351 </tr></table>
352 </td>
353 </tr>
354 <input type="hidden" name="encounter_id" value="<?php echo attr($encounter); ?>">
355 <input type="hidden" name="patient_id" value="<?php echo attr($pid); ?>">
356 </form>
357 </table>
359 </body>
360 </html>