minor changes to prior commit
[openemr.git] / interface / patient_file / encounter / diagnosis.php
blobfeeca97e81b29702da3fe52711345baa340026ef
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("../../globals.php");
8 require_once("$srcdir/billing.inc");
9 require_once("$srcdir/acl.inc");
11 $mode = $_REQUEST['mode'];
12 $type = $_REQUEST['type'];
13 $code = $_REQUEST['code'];
14 $modifier = $_REQUEST['modifier'];
15 $units = $_REQUEST['units'];
16 $fee = $_REQUEST['fee'];
17 $text = $_REQUEST['text'];
18 $payment_method = $_REQUEST['payment_method'];
19 $insurance_company = $_REQUEST['insurance_company'];
21 $target = '_parent';
23 // Possible units of measure for NDC drug quantities.
24 $ndc_uom_choices = array(
25 'ML' => 'ML',
26 'GR' => 'Grams',
27 'ME' => 'Milligrams',
28 'F2' => 'I.U.',
29 'UN' => 'Units'
32 if ($payment_method == "insurance") {
33 $payment_method = "insurance: " . $insurance_company;
36 if (isset($mode)) {
37 if (!verifyCsrfToken($_GET["csrf_token_form"])) {
38 die(xlt('Authentication Error'));
41 if ($mode == "add") {
42 // Get the provider ID from the new encounter form if possible, otherwise
43 // it's the logged-in user.
44 $tmp = sqlQuery("SELECT users.id FROM forms, users WHERE " .
45 "forms.pid = ? AND forms.encounter = ? AND " .
46 "forms.formdir='newpatient' AND users.username = forms.user AND " .
47 "users.authorized = 1", array($pid, $encounter));
48 $provid = $tmp['id'] ? $tmp['id'] : $_SESSION["authUserID"];
50 if (strtolower($type) == "copay") {
51 addBilling(
52 $encounter,
53 $type,
54 sprintf("%01.2f", $code),
55 $payment_method,
56 $pid,
57 $userauthorized,
58 $provid,
59 $modifier,
60 $units,
61 sprintf("%01.2f", 0 - $code)
63 } elseif (strtolower($type) == "other") {
64 addBilling(
65 $encounter,
66 $type,
67 $code,
68 $text,
69 $pid,
70 $userauthorized,
71 $provid,
72 $modifier,
73 $units,
74 sprintf("%01.2f", $fee)
76 } else {
77 $ndc_info = '';
78 // If HCPCS, get and save default NDC data.
79 if (strtolower($type) == "hcpcs") {
80 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
81 "code_type = 'HCPCS' AND code = ? AND ndc_info LIKE 'N4%' " .
82 "ORDER BY date DESC LIMIT 1", array($code));
83 if (!empty($tmp)) {
84 $ndc_info = $tmp['ndc_info'];
88 addBilling(
89 $encounter,
90 $type,
91 $code,
92 $text,
93 $pid,
94 $userauthorized,
95 $provid,
96 $modifier,
97 $units,
98 $fee,
99 $ndc_info
102 } elseif ($mode == "justify") {
103 $diags = $_POST['code']['diag'];
104 $procs = $_POST['code']['proc'];
105 $sql = array();
106 if (!empty($procs) && !empty($diags)) {
107 $sql = array();
108 foreach ($procs as $proc) {
109 $justify_string = "";
110 foreach ($diags as $diag) {
111 $justify_string .= $diag . ":";
114 $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) . "'";
118 if (!empty($sql)) {
119 foreach ($sql as $q) {
120 $results = sqlQ($q);
124 // Save NDC fields, if present.
125 $ndcarr = $_POST['ndc'];
126 for ($lino = 1; !empty($ndcarr["$lino"]['code']); ++$lino) {
127 $ndc = $ndcarr["$lino"];
128 $ndc_info = '';
129 if ($ndc['ndcnum']) {
130 $ndc_info = 'N4' . trim($ndc['ndcnum']) . ' ' . $ndc['ndcuom'] .
131 trim($ndc['ndcqty']);
134 sqlStatement("UPDATE billing SET ndc_info = ? WHERE " .
135 "encounter = ? AND " .
136 "pid = ? AND " .
137 "code = ?", array($ndc_info, $_POST['encounter_id'], $_POST['patient_id'], $ndc['code']));
143 <html>
144 <head>
145 <?php html_header_show();?>
146 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
148 <script language="JavaScript">
150 function validate(f) {
151 for (var lino = 1; f['ndc['+lino+'][code]']; ++lino) {
152 var pfx = 'ndc['+lino+']';
153 if (f[pfx+'[ndcnum]'] && f[pfx+'[ndcnum]'].value) {
154 // Check NDC number format.
155 var ndcok = true;
156 var ndc = f[pfx+'[ndcnum]'].value;
157 var a = ndc.split('-');
158 if (a.length != 3) {
159 ndcok = false;
161 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
162 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
163 ndcok = false;
165 else {
166 for (var i = 0; i < 3; ++i) {
167 for (var j = 0; j < a[i].length; ++j) {
168 var c = a[i].charAt(j);
169 if (c < '0' || c > '9') ndcok = false;
173 if (!ndcok) {
174 alert('<?php echo xls('Format incorrect for NDC'); ?> "' + ndc +
175 '", <?php echo xls('should be like nnnnn-nnnn-nn'); ?>');
176 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
177 return false;
179 // Check for valid quantity.
180 var qty = f[pfx+'[ndcqty]'].value - 0;
181 if (isNaN(qty) || qty <= 0) {
182 alert('<?php echo xls('Quantity for NDC'); ?> "' + ndc +
183 '" <?php echo xls('is not valid (decimal fractions are OK).'); ?>');
184 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
185 return false;
189 top.restoreSession();
190 return true;
193 </script>
195 </head>
197 <body class="body_bottom">
199 <?php
200 $thisauth = acl_check('encounters', 'coding_a');
201 if (!$thisauth) {
202 $erow = sqlQuery("SELECT user FROM forms WHERE " .
203 "encounter = ? AND formdir = 'newpatient' LIMIT 1", array($encounter));
204 if ($erow['user'] == $_SESSION['authUser']) {
205 $thisauth = acl_check('encounters', 'coding');
209 if ($thisauth) {
210 $tmp = getPatientData($pid, "squad");
211 if ($tmp['squad'] && ! acl_check('squads', $tmp['squad'])) {
212 $thisauth = 0;
216 if (!$thisauth) {
217 echo "<p>(".xlt('Coding not authorized').")</p>\n";
218 echo "</body>\n</html>\n";
219 exit();
223 <form name="diagnosis" method="post" action="diagnosis.php?mode=justify&csrf_token_form=<?php echo attr(urlencode(collectCsrfToken())); ?>"
224 onsubmit="return validate(this)">
225 <table border=0 cellspacing=0 cellpadding=0 height=100%>
226 <tr>
228 <td valign=top>
230 <dl>
231 <dt>
232 <a href="diagnosis_full.php" target="<?php echo attr($target); ?>" onclick="top.restoreSession()">
233 <span class=title><?php echo ($GLOBALS['phone_country_code'] == '1') ? xlt('Billing') : xlt('Coding'); ?></span>
234 <font class=more><?php echo text($tmore); ?></font></a>
236 <?php
237 if (!empty($_GET["back"]) || !empty($_POST["back"])) {
238 print "&nbsp;<a href=\"superbill_codes.php\" target=\"" . attr($target) . "\" onclick=\"top.restoreSession()\"><font class=more>" . text($tback) . "</font></a>";
239 print "<input type=\"hidden\" name=\"back\" value=\"1\">";
242 <?php if (!$GLOBALS['weight_loss_clinic']) { ?>
243 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
244 <input type="submit" name="justify" value="<?php echo xla('Justify/Save');?>">
245 <?php } ?>
246 </dt>
247 </dl>
249 <a href="cash_receipt.php?" class='link_submit' target='new' onclick='top.restoreSession()'>
250 [<?php echo xlt('Receipt'); ?>]
251 </a>
252 <table border="0">
253 <?php
254 if ($result = getBillingByEncounter($pid, $encounter, "*")) {
255 $billing_html = array();
256 $total = 0.0;
257 $ndclino = 0;
258 foreach ($result as $iter) {
259 if ($iter["code_type"] == "ICD9") {
260 $html = "<tr>";
261 $html .= "<td valign=\"middle\">" .
262 '<input style="width: 11px;height: 11px;" name="code[diag][' .
263 attr($iter["code"]) . ']" type="checkbox" value="' . attr($iter["code"]) . '">' .
264 "</td><td><div><a target='" . attr($target) . "' class='small' " .
265 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
266 text($iter{"code"}) . "</b> " . text($iter{"code_text"}) .
267 "</a></div></td></tr>\n";
268 $billing_html[$iter["code_type"]] .= $html;
269 $counter++;
270 } elseif ($iter["code_type"] == "COPAY") {
271 $billing_html[$iter["code_type"]] .=
272 "<tr><td></td><td><a target='" . attr($target) . "' class='small' " .
273 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
274 text(oeFormatMoney($iter['code'])) . "</b> " .
275 text(ucwords(strtolower($iter['code_text']))) .
276 ' ' . xlt('payment entered on') . ' ' .
277 text(oeFormatShortDate(substr($iter['date'], 0, 10))) . text(substr($iter['date'], 10, 6)) . "</a></td></tr>\n";
278 } else {
279 $billing_html[$iter["code_type"]] .=
280 "<tr><td>" . '<input style="width: 11px;height: 11px;" name="code[proc][' .
281 attr($iter["code"]) . ']" type="checkbox" value="' . attr($iter["code"]) . '">' .
282 "</td><td><a target='$target' class='small' " .
283 "href='diagnosis_full.php' onclick='top.restoreSession()'><b>" .
284 text($iter{"code"}) . ' ' . text($iter['modifier']) . "</b> " .
285 text(ucwords(strtolower($iter{"code_text"}))) . ' ' . text(oeFormatMoney($iter['fee'])) .
286 "</a><span class=\"small\">";
287 $total += $iter['fee'];
288 $js = explode(":", $iter['justify']);
289 $counter = 0;
290 foreach ($js as $j) {
291 if (!empty($j)) {
292 if ($counter == 0) {
293 $billing_html[$iter["code_type"]] .= " (<b>" . text($j) . "</b>)";
294 } else {
295 $billing_html[$iter["code_type"]] .= " (" . text($j) . ")";
298 $counter++;
302 $billing_html[$iter["code_type"]] .= "</span></td></tr>\n";
304 // If this is HCPCS, write NDC line.
305 if ($iter['code_type'] == 'HCPCS') {
306 ++$ndclino;
307 $ndcnum = '';
308 $ndcuom = '';
309 $ndcqty = '';
310 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $iter['ndc_info'], $tmp)) {
311 $ndcnum = $tmp[1];
312 $ndcuom = $tmp[2];
313 $ndcqty = $tmp[3];
316 $billing_html[$iter["code_type"]] .=
317 "<tr><td>&nbsp;</td><td class='small'>NDC:&nbsp;\n" .
318 "<input type='hidden' name='ndc[" . attr($ndclino) . "][code]' value='" . attr($iter["code"]) . "'>" .
319 "<input type='text' name='ndc[" . attr($ndclino) . "][ndcnum]' value='" . attr($ndcnum) . "' " .
320 "size='11' style='background-color:transparent'>" .
321 " &nbsp;Qty:&nbsp;" .
322 "<input type='text' name='ndc[" . attr($ndclino) . "][ndcqty]' value='" . attr($ndcqty) . "' " .
323 "size='3' style='background-color:transparent;text-align:right'> " .
324 "<select name='ndc[" . attr($ndclino) . "][ndcuom]' style='background-color:transparent'>";
325 foreach ($ndc_uom_choices as $key => $value) {
326 $billing_html[$iter["code_type"]] .= "<option value='" . attr($key) . "'";
327 if ($key == $ndcuom) {
328 $billing_html[$iter["code_type"]] .= " selected";
331 $billing_html[$iter["code_type"]] .= ">" . text($value) . "</option>";
334 $billing_html[$iter["code_type"]] .= "</select></td></tr>\n";
339 $billing_html["CPT4"] .= "<tr><td>" . xlt('total') . ":</td><td>" . text(oeFormatMoney($total)) . "</td></tr>\n";
340 foreach ($billing_html as $key => $val) {
341 print "<tr><td>" . text($key) . "</td><td><table>" . $val . "</table><td></tr><tr><td height=\"5\"></td></tr>\n";
345 </tr></table>
346 </td>
347 </tr>
348 <input type="hidden" name="encounter_id" value="<?php echo attr($encounter); ?>">
349 <input type="hidden" name="patient_id" value="<?php echo attr($pid); ?>">
350 </form>
351 </table>
353 </body>
354 </html>