* @copyright Copyright (c) 2017-2018 Brady Miller * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once("../globals.php"); require_once("../../library/patient.inc"); use OpenEMR\Common\Acl\AclMain; use OpenEMR\Common\Csrf\CsrfUtils; use OpenEMR\Common\Twig\TwigContainer; use OpenEMR\Core\Header; if (!AclMain::aclCheckCore('acct', 'rep_a')) { echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Patient Insurance Distribution")]); exit; } if (!empty($_POST)) { if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) { CsrfUtils::csrfNotVerified(); } } $form_from_date = (!empty($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : ''; $form_to_date = (!empty($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d'); if (!empty($_POST['form_csvexport'])) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=insurance_distribution.csv"); header("Content-Description: File Transfer"); // CSV headers: if (true) { echo csvEscape("Insurance") . ','; echo csvEscape("Charges") . ','; echo csvEscape("Visits") . ','; echo csvEscape("Patients") . ','; echo csvEscape("Pt Pct") . "\n"; } } else { ?> <?php echo xlt('Patient Insurance Distribution'); ?> -
: :
= ? AND fe.date <= ? " . "AND b.pid = fe.pid AND b.encounter = fe.encounter " . "AND b.code_type != 'COPAY' AND b.activity > 0 AND b.fee != 0 " . "GROUP BY b.pid, b.encounter ORDER BY b.pid, b.encounter"; $res = sqlStatement($query, array((!empty($form_from_date)) ? $form_from_date : '0000-00-00', $form_to_date)); $insarr = array(); $prev_pid = 0; $patcount = 0; while ($row = sqlFetchArray($res)) { $patient_id = $row['pid']; $encounter_date = $row['date']; $irow = sqlQuery("SELECT insurance_companies.name " . "FROM insurance_data, insurance_companies WHERE " . "insurance_data.pid = ? AND " . "insurance_data.type = 'primary' AND " . "(insurance_data.date <= ? OR insurance_data.date IS NULL) AND " . "insurance_companies.id = insurance_data.provider " . "ORDER BY insurance_data.date DESC LIMIT 1", array($patient_id, $encounter_date)); $plan = (!empty($irow['name'])) ? $irow['name'] : '-- No Insurance --'; $insarr[$plan]['visits'] = $insarr[$plan]['visits'] ?? null; $insarr[$plan]['visits'] += 1; $insarr[$plan]['charges'] = $insarr[$plan]['charges'] ?? null; $insarr[$plan]['charges'] += sprintf('%0.2f', $row['charges']); if ($patient_id != $prev_pid) { ++$patcount; $insarr[$plan]['patients'] = $insarr[$plan]['patients'] ?? null; $insarr[$plan]['patients'] += 1; $prev_pid = $patient_id; } } ksort($insarr); foreach ($insarr as $key => $val) { if ($_POST['form_csvexport']) { echo csvEscape($key) . ','; echo csvEscape(oeFormatMoney($val['charges'])) . ','; echo csvEscape($val['visits']) . ','; echo csvEscape($val['patients']) . ','; echo csvEscape(sprintf("%.1f", $val['patients'] * 100 / $patcount)) . "\n"; } else { ?>