$t_res = SLQuery($query); if ($sl_err) { die($sl_err); } for ($irow = 0; $irow < SLRowCount($t_res); ++$irow) { $row = SLGetRow($t_res, $irow); list($patient_id, $encounter_id) = explode(".", $row['invnumber']); // If a facility was specified then skip invoices whose encounters // do not indicate that facility. if ($form_facility) { $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " . "pid = '{$patient_id}' AND encounter = '{$encounter_id}' AND " . "facility_id = '{$form_facility}'"); if (empty($tmp['count'])) { continue; } } thisLineItem($patient_id, $encounter_id, '', $row['description'], $row['transdate'], $row['qty'], $row['sellprice'] * $row['qty']); } // end for } // end not $INTEGRATED_AR if ($_POST['form_csvexport']) { if (!$_POST['form_details']) { echo '"' . display_desc($product) . '",'; echo '"' . $productqty . '",'; echo '"'; bucks($producttotal); echo '"' . "\n"; } } else { ?>
// $res = sqlStatement($query); while ($row = sqlFetchArray($res)) { thisLineItem($row['pid'], $row['encounter'], $row['code'] . ' ' . $row['code_text'], substr($row['date'], 0, 10), $row['units'], $row['cyp_factor'], $row['invoice_refno']); } // $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " . "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " . "FROM drug_sales AS s " . "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " . "JOIN form_encounter AS fe ON " . "fe.pid = s.pid AND fe.encounter = s.encounter AND " . "fe.date >= '{$from_date} 00:00:00' AND fe.date <= '{$to_date} 23:59:59' " . "WHERE s.fee != 0"; // If a facility was specified. if ($form_facility) { $query .= " AND fe.facility_id = '{$form_facility}'"; } $query .= " ORDER BY d.name, fe.date, fe.id"; // $res = sqlStatement($query); while ($row = sqlFetchArray($res)) { thisLineItem($row['pid'], $row['encounter'], $row['name'], substr($row['date'], 0, 10), $row['quantity'], $row['cyp_factor'], $row['invoice_refno']); } if ($_POST['form_csvexport']) { if (!$_POST['form_details']) { echo '"' . display_desc($product) . '",'; echo '"' . $productqty . '",'; echo '"' . formatcyp($productcyp) . '",'; echo '"' . formatcyp($producttotal) . '"' . "\n"; } } else { ?> <tr bgcolor="#ddddff"> <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>
} // end not export // If generating a report. // if ($_POST['form_refresh'] || $_POST['form_csvexport']) { $from_date = $form_from_date; $to_date = $form_to_date; $query = "SELECT po.patient_id, po.date_ordered, " . "pd.pubpid, " . "CONCAT(pd.lname, ', ', pd.fname, ' ', pd.mname) AS patient_name, " . "u1.lname AS provider_lname, u1.fname AS provider_fname, u1.mname AS provider_mname, " . "pp.name AS organization, " . "lop.title AS priority_name, " . "los.title AS status_name, " . "pr.procedure_report_id, pr.date_report, pr.report_status " . "FROM procedure_order AS po " . "JOIN form_encounter AS fe ON fe.pid = po.patient_id AND fe.encounter = po.encounter_id " . "JOIN patient_data AS pd ON pd.pid = po.patient_id " . "LEFT JOIN users AS u1 ON u1.id = po.provider_id " . "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " . "LEFT JOIN list_options AS lop ON lop.list_id = 'ord_priority' AND lop.option_id = po.order_priority " . "LEFT JOIN list_options AS los ON los.list_id = 'ord_status' AND los.option_id = po.order_status " . "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id " . "WHERE " . "po.date_ordered >= '{$from_date}' AND po.date_ordered <= '{$to_date}' AND " . "( pr.report_status IS NULL OR pr.report_status = 'prelim' )"; // TBD: What if preliminary and final reports for the same order? if ($form_facility) { $query .= " AND fe.facility_id = '{$form_facility}'"; } $query .= " ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, " . "po.date_ordered, po.procedure_order_id"; $res = sqlStatement($query); while ($row = sqlFetchArray($res)) { thisLineItem($row); } } // end report generation if (!$_POST['form_csvexport']) { ?> </table> </form> </center> </body> <!-- stuff for the popup calendar --> <style type="text/css">@import url(../../library/dynarch_calendar.css);</style> <script type="text/javascript" src="../../library/dynarch_calendar.js"></script> <?php
$rowmethod = trim(substr($row['notes'], $i + 5, $j - $i - 5)); break; } } } // end foreach } else { $rowmethod = trim($row['source']); if ($form_report_by != '3') { // Extract only the first word as the payment method because any // following text will be some petty detail like a check number. $rowmethod = substr($rowmethod, 0, strcspn($rowmethod, ' /')); } } // end reporting by method thisLineItem($patient_id, $encounter_id, $row['memo'], $row['transdate'], $rowmethod, $rowpayamount, $rowadjamount, $payer_type); } // end for } // end not $INTEGRATED_AR // Not payer summary. if ($form_report_by != '1' || $_POST['form_details']) { if ($form_report_by == '1') { // by payer with details // Sort and dump saved info, and consolidate items with all key // fields being the same. usort($insarray, 'payerCmp'); $b = array(); foreach ($insarray as $a) { if (empty($a[4])) { $a[4] = xl('Patient');
} } } else { if (empty($row['session_id'])) { $rowmethod = trim($row['memo']); } else { $rowmethod = trim($row['reference']); } if ($form_report_by != '3') { // Extract only the first word as the payment method because any // following text will be some petty detail like a check number. $rowmethod = substr($rowmethod, 0, strcspn($rowmethod, ' /')); } } // thisLineItem($row['pid'], $row['encounter'], $row['code'], $thedate, $rowmethod, $row['pay_amount'], $row['adj_amount'], $row['payer_type'], $row['invoice_refno']); } // Not payer summary. if ($form_report_by != '1' || $_POST['form_details']) { if ($form_report_by == '1') { // by payer with details // Sort and dump saved info, and consolidate items with all key // fields being the same. usort($insarray, 'payerCmp'); $b = array(); foreach ($insarray as $a) { if (empty($a[4])) { $a[4] = xl('Patient'); } if (empty($b)) { $b = $a;
$qtys[1] = 0 - $row['quantity']; } else { if ($row['fee'] != 0) { $qtys[2] = 0 - $row['quantity']; } else { // no pid, distributor, source lot or fee: must be an adjustment $qtys[4] = 0 - $row['quantity']; } } } } } thisLineItem($row['drug_id'], $row['warehouse_id'], $row['pid'] + 0, $row['encounter'] + 0, $row['name'], $row['title'], $row['sale_date'], $qtys, $row['invoice_refno']); } // Generate totals for last product and warehouse. thisLineItem(0, '~', 0, 0, '', '', '0000-00-00', array(0, 0, 0, 0, 0)); // Grand totals line. if ($form_action != 'export') { // if submit $grei = getEndInventory(); ?> <tr bgcolor="#dddddd"> <td class="detail" colspan="4"> <?php echo htmlspecialchars(xl('Grand Total')); ?> </td> <td class="dehead" align="right"> <?php echo $grei - $grandqtys[0] - $grandqtys[1] - $grandqtys[2] - $grandqtys[3] - $grandqtys[4]; ?>
$res = sqlStatement($query); while ($row = sqlFetchArray($res)) { $patient_id = $row['patient_id']; $date_ordered = $row['date_ordered']; $relcodes = explode(';', $row['related_code']); foreach ($relcodes as $codestring) { if ($codestring === '') { continue; } list($codetype, $code) = explode(':', $codestring); $brow = sqlQuery("SELECT count(*) AS count " . "FROM billing AS b, form_encounter AS fe WHERE " . "b.pid = '{$patient_id}' AND " . "b.code_type = '{$codetype}' AND " . "b.code = '{$code}' AND " . "b.activity = 1 AND " . "fe.pid = b.pid AND fe.encounter = b.encounter AND " . "fe.date >= '{$date_ordered} 00:00:00'"); // If there was such a service, then this followup is not pending. if (!empty($brow['count'])) { continue; } thisLineItem($row, $codetype, $code); } } } // end report generation if (!$_POST['form_csvexport']) { ?> </table> </form> </center> </body> <!-- stuff for the popup calendar --> <style type="text/css">@import url(../../library/dynarch_calendar.css);</style> <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
$query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " . "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " . "FROM drug_sales AS s " . "JOIN drugs AS d ON d.drug_id = s.drug_id " . "JOIN form_encounter AS fe ON " . "fe.pid = s.pid AND fe.encounter = s.encounter AND " . "fe.date >= ? AND fe.date <= ? " . "WHERE s.fee != 0"; array_push($sqlBindArray, $from_date, $to_date); // If a facility was specified. if ($form_facility) { $query .= " AND fe.facility_id = ?"; array_push($sqlBindArray, $form_facility); } if ($form_provider) { $query .= " AND fe.provider_id = ?"; array_push($sqlBindArray, $form_provider); } $query .= " ORDER BY d.name, fe.date, fe.id"; // $res = sqlStatement($query, $sqlBindArray); while ($row = sqlFetchArray($res)) { thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'], substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']); } if ($_POST['form_csvexport']) { if (!$_POST['form_details']) { echo '"' . display_desc($product) . '",'; echo '"' . $productqty . '",'; echo '"'; bucks($producttotal); echo '"' . "\n"; } } else { ?> <tr bgcolor="#ddddff"> <td class="detail"> <?php
function thisLineItem($row, $xfer = false) { global $grandtotal, $grandqty, $encount, $form_action; $invnumber = ''; $dpname = ''; if (!empty($row['pid'])) { $ttype = xl('Sale'); $dpname = $row['plname']; if (!empty($row['pfname'])) { $dpname .= ', ' . $row['pfname']; if (!empty($row['pmname'])) { $dpname .= ' ' . $row['pmname']; } } $invnumber = empty($row['invoice_refno']) ? "{$row['pid']}.{$row['encounter']}" : $row['invoice_refno']; } else { if (!empty($row['distributor_id'])) { $ttype = xl('Distribution'); if (!empty($row['organization'])) { $dpname = $row['organization']; } else { $dpname = $row['dlname']; if (!empty($row['dfname'])) { $dpname .= ', ' . $row['dfname']; if (!empty($row['dmname'])) { $dpname .= ' ' . $row['dmname']; } } } } else { if (!empty($row['xfer_inventory_id']) || $xfer) { $ttype = xl('Transfer'); } else { if ($row['fee'] != 0) { $ttype = xl('Purchase'); } else { $ttype = xl('Adjustment'); } } } } if ($form_action == 'export') { echo '"' . oeFormatShortDate($row['sale_date']) . '",'; echo '"' . $ttype . '",'; echo '"' . esc4Export($row['name']) . '",'; echo '"' . esc4Export($row['lot_number']) . '",'; echo '"' . esc4Export($row['warehouse']) . '",'; echo '"' . esc4Export($dpname) . '",'; echo '"' . (0 - $row['quantity']) . '",'; echo '"' . bucks($row['fee']) . '",'; echo '"' . $row['billed'] . '",'; echo '"' . esc4Export($row['notes']) . '"' . "\n"; } else { $bgcolor = ++$encount & 1 ? "#ddddff" : "#ffdddd"; ?> <tr bgcolor="<?php echo $bgcolor; ?> "> <td class="detail"> <?php echo htmlspecialchars(oeFormatShortDate($row['sale_date'])); ?> </td> <td class="detail"> <?php echo htmlspecialchars($ttype); ?> </td> <td class="detail"> <?php echo htmlspecialchars($row['name']); ?> </td> <td class="detail"> <?php echo htmlspecialchars($row['lot_number']); ?> </td> <td class="detail"> <?php echo htmlspecialchars($row['warehouse']); ?> </td> <td class="detail"> <?php echo htmlspecialchars($dpname); ?> </td> <td class="detail" align="right"> <?php echo htmlspecialchars(0 - $row['quantity']); ?> </td> <td class="detail" align="right"> <?php echo htmlspecialchars(bucks($row['fee'])); ?> </td> <td class="detail" align="center"> <?php echo empty($row['billed']) ? ' ' : '*'; ?> </td> <td class="detail"> <?php echo htmlspecialchars($row['notes']); ?> </td> </tr> <?php } // End not csv export $grandtotal += $row['fee']; $grandqty -= $row['quantity']; // In the special case of a transfer, generate a second line item for // the source lot. if (!empty($row['xfer_inventory_id'])) { $row['xfer_inventory_id'] = 0; $row['lot_number'] = $row['lot_number_2']; $row['warehouse'] = $row['warehouse_2']; $row['quantity'] = 0 - $row['quantity']; $row['fee'] = 0 - $row['fee']; thisLineItem($row, true); } }