fix: set default x12 partner for item in billing manager (#7502)
[openemr.git] / interface / reports / custom_report_range.php
blobd72b25b83bf4ab376d58f22707e0867330b6146c
1 <?php
3 /**
4 * Superbill Report
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once(dirname(__file__) . "/../globals.php");
14 require_once("$srcdir/forms.inc.php");
15 require_once("$srcdir/patient.inc.php");
16 require_once("$srcdir/report.inc.php");
18 use OpenEMR\Common\Acl\AclMain;
19 use OpenEMR\Billing\BillingUtilities;
20 use OpenEMR\Common\Csrf\CsrfUtils;
21 use OpenEMR\Common\Twig\TwigContainer;
22 use OpenEMR\Core\Header;
23 use OpenEMR\Services\FacilityService;
25 if (!AclMain::aclCheckCore('encounters', 'coding_a')) {
26 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Superbill")]);
27 exit;
30 if (!empty($_POST)) {
31 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
32 CsrfUtils::csrfNotVerified();
36 $facilityService = new FacilityService();
38 $startdate = $enddate = "";
39 if (empty($_POST['start']) || empty($_POST['end'])) {
40 // set some default dates
41 $startdate = date('Y-m-d', (time() - 30 * 24 * 60 * 60));
42 $enddate = date('Y-m-d', time());
43 } else {
44 // set dates
45 $startdate = DateToYYYYMMDD($_POST['start']);
46 $enddate = DateToYYYYMMDD($_POST['end']);
49 //Patient related stuff
50 if (!empty($_POST["form_patient"])) {
51 $form_patient = isset($_POST['form_patient']) ? $_POST['form_patient'] : '';
54 $form_pid = isset($_POST['form_pid']) ? $_POST['form_pid'] : '';
55 if (empty($form_patient)) {
56 $form_pid = '';
59 <html>
61 <head>
63 <?php Header::setupHeader('datetime-picker'); ?>
65 <style>
67 @media print {
68 .title {
69 visibility: hidden;
71 .pagebreak {
72 page-break-after: always;
73 border: none;
74 visibility: hidden;
77 #superbill_description {
78 visibility: hidden;
81 #report_parameters {
82 visibility: hidden;
84 #superbill_results {
85 margin-top: -30px;
89 @media screen {
90 .title {
91 visibility: visible;
93 #superbill_description {
94 visibility: visible;
96 .pagebreak {
97 width: 100%;
98 border: 2px dashed var(--black);
100 #report_parameters {
101 visibility: visible;
104 #superbill_description,
105 #superbill_startingdate,
106 #superbill_endingdate {
107 margin: 10px;
109 #superbill_patientdata h1 {
110 font-weight: bold;
111 font-size: 1.2rem;
112 margin: 0px;
113 padding: 5px;
114 width: 100%;
115 background-color: var(--gray200);
116 border: 1px solid var(--black);
118 #superbill_insurancedata {
119 margin-top: 10px;
121 #superbill_insurancedata h1 {
122 font-weight: bold;
123 font-size: 1.2rem;
124 margin: 0px;
125 padding: 5px;
126 width: 100%;
127 background-color: var(--gray200);
128 border: 1px solid var(--black);
130 #superbill_insurancedata h2 {
131 font-weight: bold;
132 font-size: 1.0rem;
133 margin: 0px;
134 padding: 0px;
135 width: 100%;
136 background-color: var(--gray200);
138 #superbill_billingdata {
139 margin-top: 10px;
141 #superbill_billingdata h1 {
142 font-weight: bold;
143 font-size: 1.2rem;
144 margin: 0px;
145 padding: 5px;
146 width: 100%;
147 background-color: var(--gray200);
148 border: 1px solid var(--black);
150 </style>
152 <script>
153 $(function () {
154 var win = top.printLogSetup ? top : opener.top;
155 win.printLogSetup(document.getElementById('printbutton'));
157 $('.datepicker').datetimepicker({
158 <?php $datetimepicker_timepicker = false; ?>
159 <?php $datetimepicker_showseconds = false; ?>
160 <?php $datetimepicker_formatInput = true; ?>
161 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
162 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
166 // CapMinds :: invokes find-patient popup.
167 function sel_patient() {
168 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
171 // CapMinds :: callback by the find-patient popup.
172 function setpatient(pid, lname, fname, dob) {
173 var f = document.theform;
174 f.form_patient.value = lname + ', ' + fname;
175 f.form_pid.value = pid;
178 </script>
179 </head>
181 <body class="body_top">
183 <span class='title'><?php echo xlt('Reports'); ?> - <?php echo xlt('Superbill'); ?></span>
185 <div id="superbill_description" class='text'>
186 <?php echo xlt('Superbills, sometimes referred to as Encounter Forms or Routing Slips, are an essential part of most medical practices.'); ?>
187 </div>
189 <div id="report_parameters">
191 <form method="post" name="theform" id='theform' action="custom_report_range.php">
192 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
193 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
194 <table>
195 <tr>
196 <td width='650px'>
197 <div style='float: left'>
199 <table class='text'>
200 <tr>
201 <td class='label_custom'>
202 <?php echo xlt('Start Date'); ?>:
203 </td>
204 <td>
205 <input type='text' class='form-control datepicker' name='start' id="form_from_date" size='10' value='<?php echo attr(oeFormatShortDate($startdate)); ?>' />
206 </td>
207 <td class='label_custom'>
208 <?php echo xlt('End Date'); ?>:
209 </td>
210 <td>
211 <input type='text' class='form-control datepicker' name='end' id="form_to_date" size='10' value='<?php echo attr(oeFormatShortDate($enddate)); ?>' />
212 </td>
214 <td>
215 &nbsp;&nbsp;<span class='text'><?php echo xlt('Patient'); ?>: </span>
216 </td>
217 <td>
218 <input type='text' class='form-control' size='20' name='form_patient' style='width:100%;cursor:pointer;' value='<?php echo (!empty($form_patient)) ? attr($form_patient) : xla('Click To Select'); ?>' onclick='sel_patient()' title='<?php echo xla('Click to select patient'); ?>' />
219 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
220 </td>
221 </tr>
222 <tr><td>
223 </tr>
224 </table>
226 </div>
228 </td>
229 <td class='h-100' align='left' valign='middle'>
230 <table class="w-100 h-100" style='border-left:1px solid;' >
231 <tr>
232 <td>
233 <div style='margin-left:15px'>
234 <a href='#' class='btn btn-primary' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
235 <span>
236 <?php echo xlt('Submit'); ?>
237 </span>
238 </a>
240 <?php if (!empty($_POST['form_refresh'])) { ?>
241 <a href='#' class='btn btn-primary' id='printbutton'>
242 <span>
243 <?php echo xlt('Print'); ?>
244 </span>
245 </a>
246 <?php } ?>
247 </div>
248 </td>
249 </tr>
250 </table>
251 </td>
252 </tr>
253 </table>
254 </div> <!-- end of parameters -->
256 </form>
258 <div id="superbill_results">
260 <?php
261 if (!(empty($_POST['start']) || empty($_POST['end']))) {
262 $facility = $facilityService->getPrimaryBillingLocation();
265 <h2><?php echo text($facility['name'] ?? '')?></h2>
266 <?php echo text($facility['street'] ?? '')?><br />
267 <?php echo text($facility['city'] ?? '')?>, <?php echo text($facility['state'] ?? '')?> <?php echo text($facility['postal_code'] ?? '')?><br />
269 </p>
270 <?php
271 $sqlBindArray = array();
272 $res_query = "select * from forms where " .
273 "form_name = 'New Patient Encounter' and " .
274 "date between ? and ? " ;
275 array_push($sqlBindArray, $startdate, $enddate);
276 if ($form_pid) {
277 $res_query .= " and pid=? ";
278 array_push($sqlBindArray, $form_pid);
281 $res_query .= " order by date DESC" ;
282 $res = sqlStatement($res_query, $sqlBindArray);
284 while ($result = sqlFetchArray($res)) {
285 if ($result["form_name"] == "New Patient Encounter") {
286 $newpatient[] = $result["form_id"] . ":" . $result["encounter"];
287 $pids[] = $result["pid"];
291 $N = 6;
293 function postToGet($newpatient, $pids)
295 $getstring = "";
296 $serialnewpatient = serialize($newpatient);
297 $serialpids = serialize($pids);
298 $getstring = "newpatient=" . urlencode($serialnewpatient) . "&pids=" . urlencode($serialpids);
300 return $getstring;
303 $iCounter = 0;
304 if (empty($newpatient)) {
305 $newpatient = array();
308 foreach ($newpatient as $patient) {
310 $inclookupres = sqlStatement("select distinct formdir from forms where pid='".$pids[$iCounter]."'");
311 while($result = sqlFetchArray($inclookupres)) {
312 include_once("{$GLOBALS['incdir']}/forms/" . $result["formdir"] . "/report.php");
316 print "<div id='superbill_patientdata'>";
317 print "<h1>" . xlt('Patient Data') . ":</h1>";
318 printRecDataOne($patient_data_array, getRecPatientData($pids[$iCounter]), $N);
319 print "</div>";
321 print "<div id='superbill_insurancedata'>";
322 print "<h1>" . xlt('Insurance Data') . ":</h1>";
323 print "<h2>" . xlt('Primary') . ":</h2>";
324 printRecDataOne($insurance_data_array, getRecInsuranceData($pids[$iCounter], "primary"), $N);
325 print "<h2>" . xlt('Secondary') . ":</h2>";
326 printRecDataOne($insurance_data_array, getRecInsuranceData($pids[$iCounter], "secondary"), $N);
327 print "<h2>" . xlt('Tertiary') . ":</h2>";
328 printRecDataOne($insurance_data_array, getRecInsuranceData($pids[$iCounter], "tertiary"), $N);
329 print "</div>";
331 print "<div id='superbill_billingdata'>";
332 print "<h1>" . xlt('Billing Information') . ":</h1>";
333 if (!empty($patient) && is_array($patient) && count($patient) > 0) {
334 $billings = array();
335 echo "<table class='table w-100'>";
336 echo "<tr>";
337 echo "<td class='bold' width='10%'>" . xlt('Date') . "</td>";
338 echo "<td class='bold' width='20%'>" . xlt('Provider') . "</td>";
339 echo "<td class='bold' width='40%'>" . xlt('Code') . "</td>";
340 echo "<td class='bold' width='10%'>" . xlt('Fee') . "</td></tr>\n";
341 $total = 0.00;
342 $copays = 0.00;
343 //foreach ($patient as $be) {
345 $ta = explode(":", $patient);
346 $billing = getPatientBillingEncounter($pids[$iCounter], $ta[1]);
348 $billings[] = $billing;
349 foreach ($billing as $b) {
350 // grab the date to reformat it in the output
351 $bdate = strtotime($b['date']);
353 echo "<tr>\n";
354 echo "<td class='text' style='font-size: 0.8em'>" . text(oeFormatShortDate(date("Y-m-d", $bdate))) . "<BR>" . date("h:i a", $bdate) . "</td>";
355 echo "<td class='text'>" . text($b['provider_name']) . "</td>";
356 echo "<td class='text'>";
357 echo text($b['code_type']) . ":\t" . text($b['code']) . "&nbsp;" . text($b['modifier']) . "&nbsp;&nbsp;&nbsp;" . text($b['code_text']) . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
358 echo "</td>\n";
359 echo "<td class='text'>";
360 echo oeFormatMoney($b['fee']);
361 echo "</td>\n";
362 echo "</tr>\n";
363 $total += $b['fee'];
366 // Calculate the copay for the encounter
367 $copays = BillingUtilities::getPatientCopay($pids[$iCounter], $ta[1]);
369 echo "<tr><td>&nbsp;</td></tr>";
370 echo "<tr><td class='font-weight-bold text-right' colspan='3'>" . xlt('Sub-Total') . "</td><td class='text'>" . text(oeFormatMoney($total + abs($copays))) . "</td></tr>";
371 echo "<tr><td class='font-weight-bold text-right' colspan='3'>" . xlt('Copay Paid') . "</td><td class='text'>" . text(oeFormatMoney(abs($copays))) . "</td></tr>";
372 echo "<tr><td class='font-weight-bold text-right' colspan='3'>" . xlt('Total') . "</td><td class='text'>" . text(oeFormatMoney($total)) . "</td></tr>";
373 echo "</table>";
374 echo "<pre>";
375 //print_r($billings);
376 echo "</pre>";
379 echo "</div>";
381 ++$iCounter;
382 print "<br/><br/>" . xlt('Physician Signature') . ": _______________________________________________";
383 print "<hr class='pagebreak' />";
387 </div>
389 </body>
391 </html>