Ejemplo n.º 1
0
function generate_receipt($patient_id, $encounter = 0)
{
    global $sl_err, $sl_cash_acc, $css_header, $details, $INTEGRATED_AR;
    // Get details for what we guess is the primary facility.
    $frow = sqlQuery("SELECT * FROM facility " . "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
    $patdata = getPatientData($patient_id, 'fname,mname,lname,pubpid,street,city,state,postal_code,providerID');
    // Get the most recent invoice data or that for the specified encounter.
    //
    // Adding a provider check so that their info can be displayed on receipts
    if ($INTEGRATED_AR) {
        if ($encounter) {
            $ferow = sqlQuery("SELECT id, date, encounter, provider_id FROM form_encounter " . "WHERE pid = ? AND encounter = ?", array($patient_id, $encounter));
        } else {
            $ferow = sqlQuery("SELECT id, date, encounter, provider_id FROM form_encounter " . "WHERE pid = ? " . "ORDER BY id DESC LIMIT 1", array($patient_id));
        }
        if (empty($ferow)) {
            die(xlt("This patient has no activity."));
        }
        $trans_id = $ferow['id'];
        $encounter = $ferow['encounter'];
        $svcdate = substr($ferow['date'], 0, 10);
        if ($GLOBALS['receipts_by_provider']) {
            if (isset($ferow['provider_id'])) {
                $encprovider = $ferow['provider_id'];
            } else {
                if (isset($patdata['providerID'])) {
                    $encprovider = $patdata['providerID'];
                } else {
                    $encprovider = -1;
                }
            }
        }
        if ($encprovider) {
            $providerrow = sqlQuery("SELECT fname, mname, lname, title, street, streetb, " . "city, state, zip, phone, fax FROM users WHERE id = ?", array($encprovider));
        }
    } else {
        SLConnect();
        //
        $arres = SLQuery("SELECT * FROM ar WHERE " . "invnumber LIKE '{$patient_id}.%' " . "ORDER BY id DESC LIMIT 1");
        if ($sl_err) {
            die(text($sl_err));
        }
        if (!SLRowCount($arres)) {
            die(xlt("This patient has no activity."));
        }
        $arrow = SLGetRow($arres, 0);
        //
        $trans_id = $arrow['id'];
        //
        // Determine the date of service.  An 8-digit encounter number is
        // presumed to be a date of service imported during conversion or
        // associated with prescriptions only.  Otherwise look it up in the
        // form_encounter table.
        //
        $svcdate = "";
        list($trash, $encounter) = explode(".", $arrow['invnumber']);
        if (strlen($encounter) >= 8) {
            $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) . "-" . substr($encounter, 6, 2);
        } else {
            if ($encounter) {
                $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " . "encounter = ?", array($encounter));
                $svcdate = substr($tmp['date'], 0, 10);
            }
        }
    }
    // end not $INTEGRATED_AR
    // Get invoice reference number.
    $encrow = sqlQuery("SELECT invoice_refno FROM form_encounter WHERE " . "pid = ? AND encounter = ? LIMIT 1", array($patient_id, $encounter));
    $invoice_refno = $encrow['invoice_refno'];
    ?>
<html>
<head>
<?php 
    html_header_show();
    ?>
<link rel='stylesheet' href='<?php 
    echo $css_header;
    ?>
' type='text/css'>
<title><?php 
    echo xlt('Receipt for Payment');
    ?>
</title>
<script type="text/javascript" src="../../library/dialog.js"></script>
<script language="JavaScript">

<?php 
    require $GLOBALS['srcdir'] . "/restoreSession.php";
    ?>

 // Process click on Print button.
 function printme() {
  var divstyle = document.getElementById('hideonprint').style;
  divstyle.display = 'none';
  window.print();
  return false;
 }

 // Process click on Delete button.
 function deleteme() {
  dlgopen('deleter.php?billing=<?php 
    echo attr("{$patient_id}.{$encounter}");
    ?>
', '_blank', 500, 450);
  return false;
 }

 // Called by the deleteme.php window on a successful delete.
 function imdeleted() {
  window.close();
 }

</script>
</head>
<body class="body_top">
<center>
<?php 
    if ($GLOBALS['receipts_by_provider'] && !empty($providerrow)) {
        printProviderHeader($providerrow);
    } else {
        printFacilityHeader($frow);
    }
    echo xlt("Receipt Generated") . ":" . text(date(' F j, Y'));
    if ($invoice_refno) {
        echo " " . xlt("Invoice Number") . ": " . text($invoice_refno) . " " . xlt("Service Date") . ": " . text($svcdate);
    }
    ?>
<br>&nbsp;
</b></p>
</center>
<p>
<?php 
    echo text($patdata['fname']) . ' ' . text($patdata['mname']) . ' ' . text($patdata['lname']);
    ?>
<br><?php 
    echo text($patdata['street']);
    ?>
<br><?php 
    echo text($patdata['city']) . ', ' . text($patdata['state']) . ' ' . text($patdata['postal_code']);
    ?>
<br>&nbsp;
</p>
<center>
<table cellpadding='5'>
 <tr>
  <td><b><?php 
    echo xlt('Date');
    ?>
</b></td>
  <td><b><?php 
    echo xlt('Description');
    ?>
</b></td>
  <td align='right'><b><?php 
    echo $details ? xlt('Price') : '&nbsp;';
    ?>
</b></td>
  <td align='right'><b><?php 
    echo $details ? xlt('Qty') : '&nbsp;';
    ?>
</b></td>
  <td align='right'><b><?php 
    echo xlt('Total');
    ?>
</b></td>
 </tr>

<?php 
    $charges = 0.0;
    if ($INTEGRATED_AR) {
        // Product sales
        $inres = sqlStatement("SELECT s.sale_id, s.sale_date, s.fee, " . "s.quantity, s.drug_id, d.name " . "FROM drug_sales AS s LEFT JOIN drugs AS d ON d.drug_id = s.drug_id " . "WHERE s.pid = ? AND s.encounter = ? " . "ORDER BY s.sale_id", array($patient_id, $encounter));
        while ($inrow = sqlFetchArray($inres)) {
            $charges += sprintf('%01.2f', $inrow['fee']);
            receiptDetailLine($inrow['sale_date'], $inrow['name'], $inrow['fee'], $inrow['quantity']);
        }
        // Service and tax items
        $inres = sqlStatement("SELECT * FROM billing WHERE " . "pid = ? AND encounter = ? AND " . "code_type != 'COPAY' AND activity = 1 " . "ORDER BY id", array($patient_id, $encounter));
        while ($inrow = sqlFetchArray($inres)) {
            $charges += sprintf('%01.2f', $inrow['fee']);
            receiptDetailLine($svcdate, $inrow['code_text'], $inrow['fee'], $inrow['units']);
        }
        // Adjustments.
        $inres = sqlStatement("SELECT " . "a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, " . "s.payer_id, s.reference, s.check_date, s.deposit_date " . "FROM ar_activity AS a " . "LEFT JOIN ar_session AS s ON s.session_id = a.session_id WHERE " . "a.pid = ? AND a.encounter = ? AND " . "a.adj_amount != 0 " . "ORDER BY s.check_date, a.sequence_no", array($patient_id, $encounter));
        while ($inrow = sqlFetchArray($inres)) {
            $charges -= sprintf('%01.2f', $inrow['adj_amount']);
            $payer = empty($inrow['payer_type']) ? 'Pt' : 'Ins' . $inrow['payer_type'];
            receiptDetailLine($svcdate, $payer . ' ' . $inrow['memo'], 0 - $inrow['adj_amount'], 1);
        }
    } else {
        // Request all line items with money belonging to the invoice.
        $inres = SLQuery("SELECT * FROM invoice WHERE " . "trans_id = {$trans_id} AND sellprice != 0 ORDER BY id");
        if ($sl_err) {
            die($sl_err);
        }
        for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
            $row = SLGetRow($inres, $irow);
            $amount = sprintf('%01.2f', $row['sellprice'] * $row['qty']);
            $charges += $amount;
            $desc = preg_replace('/^.{1,6}:/', '', $row['description']);
            receiptDetailLine($svcdate, $desc, $amount, $row['qty']);
        }
    }
    // end not $INTEGRATED_AR
    ?>

 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>
 <tr>
  <td><?php 
    echo text(oeFormatShortDate($svcdispdate));
    ?>
</td>
  <td><b><?php 
    echo xlt('Total Charges');
    ?>
</b></td>
  <td align='right'>&nbsp;</td>
  <td align='right'>&nbsp;</td>
  <td align='right'><?php 
    echo text(oeFormatMoney($charges, true));
    ?>
</td>
 </tr>
 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>

<?php 
    if ($INTEGRATED_AR) {
        // Get co-pays.
        $inres = sqlStatement("SELECT fee, code_text FROM billing WHERE " . "pid = ? AND encounter = ?  AND " . "code_type = 'COPAY' AND activity = 1 AND fee != 0 " . "ORDER BY id", array($patient_id, $encounter));
        while ($inrow = sqlFetchArray($inres)) {
            $charges += sprintf('%01.2f', $inrow['fee']);
            receiptPaymentLine($svcdate, 0 - $inrow['fee'], $inrow['code_text']);
        }
        // Get other payments.
        $inres = sqlStatement("SELECT " . "a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, " . "s.payer_id, s.reference, s.check_date, s.deposit_date " . "FROM ar_activity AS a " . "LEFT JOIN ar_session AS s ON s.session_id = a.session_id WHERE " . "a.pid = ? AND a.encounter = ? AND " . "a.pay_amount != 0 " . "ORDER BY s.check_date, a.sequence_no", array($patient_id, $encounter));
        $payer = empty($inrow['payer_type']) ? 'Pt' : 'Ins' . $inrow['payer_type'];
        while ($inrow = sqlFetchArray($inres)) {
            $charges -= sprintf('%01.2f', $inrow['pay_amount']);
            receiptPaymentLine($svcdate, $inrow['pay_amount'], $payer . ' ' . $inrow['reference']);
        }
    } else {
        $chart_id_cash = SLQueryValue("select id from chart where accno = '{$sl_cash_acc}'");
        if ($sl_err) {
            die($sl_err);
        }
        if (!$chart_id_cash) {
            die("There is no COA entry for cash account '{$sl_cash_acc}'");
        }
        //
        // Request all cash entries belonging to the invoice.
        $atres = SLQuery("SELECT * FROM acc_trans WHERE " . "trans_id = {$trans_id} AND chart_id = {$chart_id_cash} ORDER BY transdate");
        if ($sl_err) {
            die($sl_err);
        }
        //
        for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
            $row = SLGetRow($atres, $irow);
            $amount = sprintf('%01.2f', $row['amount']);
            // negative
            $charges += $amount;
            $rowsource = $row['source'];
            if (strtolower($rowsource) == 'co-pay') {
                $rowsource = '';
            }
            receiptPaymentLine($row['transdate'], 0 - $amount, $rowsource);
        }
    }
    // end not $INTEGRATED_AR
    ?>
 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td><b><?php 
    echo xlt('Balance Due');
    ?>
</b></td>
  <td colspan='2'>&nbsp;</td>
  <td align='right'><?php 
    echo text(oeFormatMoney($charges, true));
    ?>
</td>
 </tr>
</table>
</center>
<div id='hideonprint'>
<p>
&nbsp;
<a href='#' onclick='return printme();'><?php 
    echo xlt('Print');
    ?>
</a>
<?php 
    if (acl_check('acct', 'disc')) {
        ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href='#' onclick='return deleteme();'><?php 
        echo xlt('Undo Checkout');
        ?>
</a>
<?php 
    }
    ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php 
    if ($details) {
        ?>
<a href='pos_checkout.php?details=0&ptid=<?php 
        echo attr($patient_id);
        ?>
&enc=<?php 
        echo attr($encounter);
        ?>
'><?php 
        echo xlt('Hide Details');
        ?>
</a>
<?php 
    } else {
        ?>
<a href='pos_checkout.php?details=1&ptid=<?php 
        echo attr($patient_id);
        ?>
&enc=<?php 
        echo attr($encounter);
        ?>
'><?php 
        echo xlt('Show Details');
        ?>
</a>
<?php 
    }
    ?>
</p>
</div>
</body>
</html>
<?php 
    if (!$INTEGRATED_AR) {
        SLClose();
    }
}
Ejemplo n.º 2
0
function generate_receipt($patient_id, $encounter = 0)
{
    global $sl_err, $sl_cash_acc, $css_header, $details;
    // Get details for what we guess is the primary facility.
    $frow = sqlQuery("SELECT * FROM facility " . "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
    $patdata = getPatientData($patient_id, 'fname,mname,lname,pubpid,street,city,state,postal_code,providerID');
    // Get the most recent invoice data or that for the specified encounter.
    //
    // Adding a provider check so that their info can be displayed on receipts
    if ($encounter) {
        $ferow = sqlQuery("SELECT id, date, encounter, provider_id FROM form_encounter " . "WHERE pid = ? AND encounter = ?", array($patient_id, $encounter));
    } else {
        $ferow = sqlQuery("SELECT id, date, encounter, provider_id FROM form_encounter " . "WHERE pid = ? " . "ORDER BY id DESC LIMIT 1", array($patient_id));
    }
    if (empty($ferow)) {
        die(xlt("This patient has no activity."));
    }
    $trans_id = $ferow['id'];
    $encounter = $ferow['encounter'];
    $svcdate = substr($ferow['date'], 0, 10);
    if ($GLOBALS['receipts_by_provider']) {
        if (isset($ferow['provider_id'])) {
            $encprovider = $ferow['provider_id'];
        } else {
            if (isset($patdata['providerID'])) {
                $encprovider = $patdata['providerID'];
            } else {
                $encprovider = -1;
            }
        }
    }
    if ($encprovider) {
        $providerrow = sqlQuery("SELECT fname, mname, lname, title, street, streetb, " . "city, state, zip, phone, fax FROM users WHERE id = ?", array($encprovider));
    }
    // Get invoice reference number.
    $encrow = sqlQuery("SELECT invoice_refno FROM form_encounter WHERE " . "pid = ? AND encounter = ? LIMIT 1", array($patient_id, $encounter));
    $invoice_refno = $encrow['invoice_refno'];
    ?>
<html>
<head>
<?php 
    html_header_show();
    ?>
<link rel='stylesheet' href='<?php 
    echo $css_header;
    ?>
' type='text/css'>
<title><?php 
    echo xlt('Receipt for Payment');
    ?>
</title>
<script type="text/javascript" src="<?php 
    echo $GLOBALS['assets_static_relative'];
    ?>
/jquery-min-1-2-2/index.js"></script>
<script type="text/javascript" src="../../library/dialog.js"></script>
<script language="JavaScript">

<?php 
    require $GLOBALS['srcdir'] . "/restoreSession.php";
    ?>

 $(document).ready(function() {
  var win = top.printLogSetup ? top : opener.top;
  win.printLogSetup(document.getElementById('printbutton'));
 });

 // Process click on Print button.
 function printlog_before_print() {
  var divstyle = document.getElementById('hideonprint').style;
  divstyle.display = 'none';
 }

 // Process click on Delete button.
 function deleteme() {
  dlgopen('deleter.php?billing=<?php 
    echo attr("{$patient_id}.{$encounter}");
    ?>
', '_blank', 500, 450);
  return false;
 }

 // Called by the deleteme.php window on a successful delete.
 function imdeleted() {
  window.close();
 }

</script>
</head>
<body class="body_top">
<center>
<?php 
    if ($GLOBALS['receipts_by_provider'] && !empty($providerrow)) {
        printProviderHeader($providerrow);
    } else {
        printFacilityHeader($frow);
    }
    echo xlt("Receipt Generated") . ":" . text(date(' F j, Y'));
    if ($invoice_refno) {
        echo " " . xlt("Invoice Number") . ": " . text($invoice_refno) . " " . xlt("Service Date") . ": " . text($svcdate);
    }
    ?>
<br>&nbsp;
</b></p>
</center>
<p>
<?php 
    echo text($patdata['fname']) . ' ' . text($patdata['mname']) . ' ' . text($patdata['lname']);
    ?>
<br><?php 
    echo text($patdata['street']);
    ?>
<br><?php 
    echo text($patdata['city']) . ', ' . text($patdata['state']) . ' ' . text($patdata['postal_code']);
    ?>
<br>&nbsp;
</p>
<center>
<table cellpadding='5'>
 <tr>
  <td><b><?php 
    echo xlt('Date');
    ?>
</b></td>
  <td><b><?php 
    echo xlt('Description');
    ?>
</b></td>
  <td align='right'><b><?php 
    echo $details ? xlt('Price') : '&nbsp;';
    ?>
</b></td>
  <td align='right'><b><?php 
    echo $details ? xlt('Qty') : '&nbsp;';
    ?>
</b></td>
  <td align='right'><b><?php 
    echo xlt('Total');
    ?>
</b></td>
 </tr>

<?php 
    $charges = 0.0;
    // Product sales
    $inres = sqlStatement("SELECT s.sale_id, s.sale_date, s.fee, " . "s.quantity, s.drug_id, d.name " . "FROM drug_sales AS s LEFT JOIN drugs AS d ON d.drug_id = s.drug_id " . "WHERE s.pid = ? AND s.encounter = ? " . "ORDER BY s.sale_id", array($patient_id, $encounter));
    while ($inrow = sqlFetchArray($inres)) {
        $charges += sprintf('%01.2f', $inrow['fee']);
        receiptDetailLine($inrow['sale_date'], $inrow['name'], $inrow['fee'], $inrow['quantity']);
    }
    // Service and tax items
    $inres = sqlStatement("SELECT * FROM billing WHERE " . "pid = ? AND encounter = ? AND " . "code_type != 'COPAY' AND activity = 1 " . "ORDER BY id", array($patient_id, $encounter));
    while ($inrow = sqlFetchArray($inres)) {
        $charges += sprintf('%01.2f', $inrow['fee']);
        receiptDetailLine($svcdate, $inrow['code_text'], $inrow['fee'], $inrow['units']);
    }
    // Adjustments.
    $inres = sqlStatement("SELECT " . "a.code_type, a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, " . "s.payer_id, s.reference, s.check_date, s.deposit_date " . "FROM ar_activity AS a " . "LEFT JOIN ar_session AS s ON s.session_id = a.session_id WHERE " . "a.pid = ? AND a.encounter = ? AND " . "a.adj_amount != 0 " . "ORDER BY s.check_date, a.sequence_no", array($patient_id, $encounter));
    while ($inrow = sqlFetchArray($inres)) {
        $charges -= sprintf('%01.2f', $inrow['adj_amount']);
        $payer = empty($inrow['payer_type']) ? 'Pt' : 'Ins' . $inrow['payer_type'];
        receiptDetailLine($svcdate, $payer . ' ' . $inrow['memo'], 0 - $inrow['adj_amount'], 1);
    }
    ?>

 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>
 <tr>
  <td><?php 
    echo text(oeFormatShortDate($svcdispdate));
    ?>
</td>
  <td><b><?php 
    echo xlt('Total Charges');
    ?>
</b></td>
  <td align='right'>&nbsp;</td>
  <td align='right'>&nbsp;</td>
  <td align='right'><?php 
    echo text(oeFormatMoney($charges, true));
    ?>
</td>
 </tr>
 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>

<?php 
    // Get co-pays.
    $inres = sqlStatement("SELECT fee, code_text FROM billing WHERE " . "pid = ? AND encounter = ?  AND " . "code_type = 'COPAY' AND activity = 1 AND fee != 0 " . "ORDER BY id", array($patient_id, $encounter));
    while ($inrow = sqlFetchArray($inres)) {
        $charges += sprintf('%01.2f', $inrow['fee']);
        receiptPaymentLine($svcdate, 0 - $inrow['fee'], $inrow['code_text']);
    }
    // Get other payments.
    $inres = sqlStatement("SELECT " . "a.code_type, a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, " . "s.payer_id, s.reference, s.check_date, s.deposit_date " . "FROM ar_activity AS a " . "LEFT JOIN ar_session AS s ON s.session_id = a.session_id WHERE " . "a.pid = ? AND a.encounter = ? AND " . "a.pay_amount != 0 " . "ORDER BY s.check_date, a.sequence_no", array($patient_id, $encounter));
    while ($inrow = sqlFetchArray($inres)) {
        $payer = empty($inrow['payer_type']) ? 'Pt' : 'Ins' . $inrow['payer_type'];
        $charges -= sprintf('%01.2f', $inrow['pay_amount']);
        receiptPaymentLine($svcdate, $inrow['pay_amount'], $payer . ' ' . $inrow['reference']);
    }
    ?>
 <tr>
  <td colspan='5'>&nbsp;</td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td><b><?php 
    echo xlt('Balance Due');
    ?>
</b></td>
  <td colspan='2'>&nbsp;</td>
  <td align='right'><?php 
    echo text(oeFormatMoney($charges, true));
    ?>
</td>
 </tr>
</table>
</center>
<div id='hideonprint'>
<p>
&nbsp;
<a href='#' id='printbutton'><?php 
    echo xlt('Print');
    ?>
</a>
<?php 
    if (acl_check('acct', 'disc')) {
        ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href='#' onclick='return deleteme();'><?php 
        echo xlt('Undo Checkout');
        ?>
</a>
<?php 
    }
    ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php 
    if ($details) {
        ?>
<a href='pos_checkout.php?details=0&ptid=<?php 
        echo attr($patient_id);
        ?>
&enc=<?php 
        echo attr($encounter);
        ?>
' onclick='top.restoreSession()'><?php 
        echo xlt('Hide Details');
        ?>
</a>
<?php 
    } else {
        ?>
<a href='pos_checkout.php?details=1&ptid=<?php 
        echo attr($patient_id);
        ?>
&enc=<?php 
        echo attr($encounter);
        ?>
' onclick='top.restoreSession()'><?php 
        echo xlt('Show Details');
        ?>
</a>
<?php 
    }
    ?>
</p>
</div>
</body>
</html>
<?php 
}