2 // Copyright (C) 2007-2008 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once(dirname(__FILE__
) . "/classes/Address.class.php");
10 require_once(dirname(__FILE__
) . "/classes/InsuranceCompany.class.php");
11 require_once(dirname(__FILE__
) . "/sql-ledger.inc");
12 require_once(dirname(__FILE__
) . "/invoice_summary.inc.php");
14 // This enforces the X12 Basic Character Set. Page A2.
16 function x12clean($str) {
17 return preg_replace('/[^A-Z0-9!"\\&\'()+,\\-.\\/;?= ]/', '', strtoupper($str));
22 var $pid; // patient id
23 var $encounter_id; // encounter id
24 var $procs; // array of procedure rows from billing table
25 var $x12_partner; // row from x12_partners table
26 var $encounter; // row from form_encounter table
27 var $facility; // row from facility table
28 var $billing_facility; // row from facility table
29 var $provider; // row from users table (rendering provider)
30 var $referrer; // row from users table (referring provider)
31 var $insurance_numbers; // row from insurance_numbers table for current payer
32 var $patient_data; // row from patient_data table
33 var $billing_options; // row from form_misc_billing_options table
34 var $invoice; // result from get_invoice_summary()
35 var $payers; // array of arrays, for all payers
37 function loadPayerInfo(&$billrow) {
39 $encounter_date = substr($this->encounter
['date'], 0, 10);
41 // Create the $payers array. This contains data for all insurances
42 // with the current one always at index 0, and the others in payment
43 // order starting at index 1.
45 $this->payers
= array();
46 $this->payers
[0] = array();
47 $query = "SELECT * FROM insurance_data WHERE " .
48 "pid = '{$this->pid}' AND provider != '' AND " .
49 "date <= '$encounter_date' " .
50 "ORDER BY type ASC, date DESC";
51 $dres = sqlStatement($query);
53 while ($drow = sqlFetchArray($dres)) {
54 if (strcmp($prevtype, $drow['type']) == 0) continue;
55 $prevtype = $drow['type'];
56 $ins = count($this->payers
);
57 if ($drow['provider'] == $billrow['payer_id'] && empty($this->payers
[0]['data'])) $ins = 0;
58 $crow = sqlQuery("SELECT * FROM insurance_companies WHERE " .
59 "id = '" . $drow['provider'] . "'");
60 $orow = new InsuranceCompany($drow['provider']);
61 $this->payers
[$ins] = array();
62 $this->payers
[$ins]['data'] = $drow;
63 $this->payers
[$ins]['company'] = $crow;
64 $this->payers
[$ins]['object'] = $orow;
67 // This kludge hands most cases of a rare ambiguous situation, where
68 // the primary insurance company is the same as the secondary. It seems
69 // nobody planned for that!
71 for ($i = 1; $i < count($this->payers
); ++
$i) {
72 if ($billrow['process_date'] &&
73 $this->payers
[0]['data']['provider'] == $this->payers
[$i]['data']['provider'])
75 $tmp = $this->payers
[0];
76 $this->payers
[0] = $this->payers
[$i];
77 $this->payers
[$i] = $tmp;
81 $this->using_modifiers
= true;
83 // Get payment and adjustment details if there are any previous payers.
85 $this->invoice
= array();
86 if ($this->payerSequence() != 'P') {
87 if ($GLOBALS['oer_config']['ws_accounting']['enabled']) {
89 $arres = SLQuery("select id from ar where invnumber = " .
90 "'{$this->pid}.{$this->encounter_id}'");
91 if ($sl_err) die($sl_err);
92 $arrow = SLGetRow($arres, 0);
94 $this->invoice
= get_invoice_summary($arrow['id'], true);
98 // Secondary claims might not have modifiers in SQL-Ledger data.
99 // In that case, note that we should not try to match on them.
100 $this->using_modifiers
= false;
101 foreach ($this->invoice
as $key => $trash) {
102 if (strpos($key, ':')) $this->using_modifiers
= true;
107 // Constructor. Loads relevant database information.
109 function Claim($pid, $encounter_id) {
111 $this->encounter_id
= $encounter_id;
112 $this->procs
= array();
114 // We need the encounter date before we can identify the payers.
115 $sql = "SELECT * FROM form_encounter WHERE " .
116 "pid = '{$this->pid}' AND " .
117 "encounter = '{$this->encounter_id}'";
118 $this->encounter
= sqlQuery($sql);
120 // Sort by procedure timestamp in order to get some consistency. In particular
121 // we determine the provider from the first procedure in this array.
122 $sql = "SELECT * FROM billing WHERE " .
123 "encounter = '{$this->encounter_id}' AND pid = '{$this->pid}' AND " .
124 "(code_type = 'CPT4' OR code_type = 'HCPCS') AND " .
125 "activity = '1' ORDER BY date, id";
126 $res = sqlStatement($sql);
127 while ($row = sqlFetchArray($res)) {
128 if (!$row['units']) $row['units'] = 1;
129 // Load prior payer data at the first opportunity in order to get
130 // the using_modifiers flag that is referenced below.
131 if (empty($this->procs
)) $this->loadPayerInfo($row);
132 // Consolidate duplicate procedures.
133 foreach ($this->procs
as $key => $trash) {
134 if (strcmp($this->procs
[$key]['code'],$row['code']) == 0 &&
135 (strcmp($this->procs
[$key]['modifier'],$row['modifier']) == 0 ||
136 !$this->using_modifiers
))
138 $this->procs
[$key]['units'] +
= $row['units'];
139 $this->procs
[$key]['fee'] +
= $row['fee'];
140 continue 2; // skip to next table row
143 $this->procs
[] = $row;
146 $sql = "SELECT * FROM x12_partners WHERE " .
147 "id = '" . $this->procs
[0]['x12_partner_id'] . "'";
148 $this->x12_partner
= sqlQuery($sql);
150 $sql = "SELECT * FROM facility WHERE " .
151 "id = '" . addslashes($this->encounter
['facility_id']) . "' " .
153 $this->facility
= sqlQuery($sql);
155 $provider_id = $this->procs
[0]['provider_id'];
156 $sql = "SELECT * FROM users WHERE id = '$provider_id'";
157 $this->provider
= sqlQuery($sql);
159 $sql = "SELECT * FROM facility " .
160 "ORDER BY billing_location DESC, id ASC LIMIT 1";
161 $this->billing_facility
= sqlQuery($sql);
163 $sql = "SELECT * FROM insurance_numbers WHERE " .
164 "(insurance_company_id = '" . $this->procs
[0]['payer_id'] .
165 "' OR insurance_company_id is NULL) AND " .
166 "provider_id = '" . $this->provider
['id'] .
167 "' order by insurance_company_id DESC LIMIT 1";
168 $this->insurance_numbers
= sqlQuery($sql);
170 $sql = "SELECT * FROM patient_data WHERE " .
171 "pid = '{$this->pid}' " .
172 "ORDER BY id LIMIT 1";
173 $this->patient_data
= sqlQuery($sql);
175 $sql = "SELECT fpa.* FROM forms JOIN form_misc_billing_options AS fpa " .
176 "ON fpa.id = forms.form_id WHERE " .
177 "forms.encounter = '{$this->encounter_id}' AND " .
178 "forms.pid = '{$this->pid}' AND " .
179 "forms.formdir = 'misc_billing_options' " .
180 "ORDER BY forms.date";
181 $this->billing_options
= sqlQuery($sql);
183 $referrer_id = (empty($GLOBALS['MedicareReferrerIsRenderer']) ||
184 $this->insurance_numbers
['provider_number_type'] != '1C') ?
185 $this->patient_data
['providerID'] : $provider_id;
186 $sql = "SELECT * FROM users WHERE id = '$referrer_id'";
187 $this->referrer
= sqlQuery($sql);
188 if (!$this->referrer
) $this->referrer
= array();
192 // Return an array of adjustments from the designated prior payer for the
193 // designated procedure key (might be procedure:modifier), or for the claim
194 // level. For each adjustment give date, group code, reason code, amount.
195 // Note this will include "patient responsibility" adjustments which are
196 // not adjustments to OUR invoice, but they reduce the amount that the
197 // insurance company pays.
199 function payerAdjustments($ins, $code='Claim') {
202 // If we have no modifiers stored in SQL-Ledger for this claim,
203 // then we cannot use a modifier passed in with the key.
204 $tmp = strpos($code, ':');
205 if ($tmp && !$this->using_modifiers
) $code = substr($code, 0, $tmp);
207 // For payments, source always starts with "Ins" or "Pt".
208 // Nonzero adjustment reason examples:
209 // Ins1 adjust code 42 (Charges exceed ... (obsolete))
210 // Ins1 adjust code 45 (Charges exceed your contracted/ legislated fee arrangement)
211 // Ins1 adjust code 97 (Payment is included in the allowance for another service/procedure)
212 // Ins1 adjust code A2 (Contractual adjustment)
215 // Zero adjustment reason examples:
217 // Coinsurance: 11.46 (code 2) Note: fix remits to identify insurance
218 // To deductible: 0.22 (code 1) Note: fix remits to identify insurance
219 // To copay Ins1 (manual entry)
220 // To ded'ble Ins1 (manual entry)
222 if (!empty($this->invoice
[$code])) {
226 $inslabel = ($this->payerSequence($ins) == 'S') ?
'Ins2' : 'Ins1';
227 $insnumber = substr($inslabel, 3);
229 // Compute this procedure's patient responsibility amount as of this
230 // prior payer, which is the original charge minus all insurance
231 // payments and "hard" adjustments up to this payer.
232 $ptresp = $this->invoice
[$code]['chg'] +
$this->invoice
[$code]['adj'];
233 foreach ($this->invoice
[$code]['dtl'] as $key => $value) {
234 if (preg_match("/^Ins(\d)/i", $value['src'], $tmp)) {
235 if ($tmp[1] <= $insnumber) $ptresp -= $value['pmt'];
237 else if (trim(substr($key, 0, 10))) { // not an adjustment if no date
238 if (!preg_match("/Ins(\d)/i", $value['rsn'], $tmp) ||
$tmp[1] <= $insnumber)
239 $ptresp +
= $value['chg']; // adjustments are negative charges
242 if ($ptresp < 0) $ptresp = 0; // we may be insane but try to hide it
244 // Main loop, to extract adjustments for this payer and procedure.
245 foreach ($this->invoice
[$code]['dtl'] as $key => $value) {
246 $tmp = str_replace('-', '', trim(substr($key, 0, 10)));
247 if ($tmp) $date = $tmp;
248 if ($tmp && $value['pmt'] == 0) { // not original charge and not a payment
249 $rsn = $value['rsn'];
250 $chg = 0 - $value['chg']; // adjustments are negative charges
252 $gcode = 'CO'; // default group code = contractual obligation
253 $rcode = '45'; // default reason code = max fee exceeded (code 42 is obsolete)
255 if (preg_match("/Ins adjust $inslabel/i", $rsn, $tmp)) {
256 // From manual post. Take the defaults.
258 else if (preg_match("/To copay $inslabel/i", $rsn, $tmp) && !$chg) {
259 $coinsurance = $ptresp; // from manual post
262 else if (preg_match("/To ded'ble $inslabel/i", $rsn, $tmp) && !$chg) {
263 $deductible = $ptresp; // from manual post
266 else if (preg_match("/$inslabel copay: (\S+)/i", $rsn, $tmp) && !$chg) {
267 $coinsurance = $tmp[1]; // from 835 as of 6/2007
270 else if (preg_match("/$inslabel coins: (\S+)/i", $rsn, $tmp) && !$chg) {
271 $coinsurance = $tmp[1]; // from 835 and manual post as of 6/2007
274 else if (preg_match("/$inslabel dedbl: (\S+)/i", $rsn, $tmp) && !$chg) {
275 $deductible = $tmp[1]; // from 835 and manual post as of 6/2007
278 else if (preg_match("/$inslabel ptresp: (\S+)/i", $rsn, $tmp) && !$chg) {
279 continue; // from 835 as of 6/2007
281 else if (preg_match("/$inslabel adjust code (\S+)/i", $rsn, $tmp)) {
282 $rcode = $tmp[1]; // from 835
284 else if (preg_match("/$inslabel/i", $rsn, $tmp)) {
285 // Take the defaults.
287 else if (preg_match('/Ins(\d)/i', $rsn, $tmp) && $tmp[1] != $insnumber) {
288 continue; // it's for some other payer
290 else if ($insnumber == '1') {
291 if (preg_match("/\$\s*adjust code (\S+)/i", $rsn, $tmp)) {
292 $rcode = $tmp[1]; // from 835
295 // Other adjustments default to Ins1.
297 else if (preg_match("/Co-pay: (\S+)/i", $rsn, $tmp) ||
298 preg_match("/Coinsurance: (\S+)/i", $rsn, $tmp)) {
299 $coinsurance = 0 +
$tmp[1]; // from 835 before 6/2007
302 else if (preg_match("/To deductible: (\S+)/i", $rsn, $tmp)) {
303 $deductible = 0 +
$tmp[1]; // from 835 before 6/2007
307 continue; // there is no adjustment amount
311 continue; // it's for primary and that's not us
314 if ($rcode == '42') $rcode= '45'; // reason 42 is obsolete
315 $aadj[] = array($date, $gcode, $rcode, sprintf('%.2f', $chg));
320 // If we really messed it up, at least avoid negative numbers.
321 if ($coinsurance > $ptresp) $coinsurance = $ptresp;
322 if ($deductible > $ptresp) $deductible = $ptresp;
324 // Find out if this payer paid anything at all on this claim. This will
325 // help us allocate any unknown patient responsibility amounts.
326 $thispaidanything = 0;
327 foreach($this->invoice
as $codekey => $codeval) {
328 foreach ($codeval['dtl'] as $key => $value) {
329 if (preg_match("/$inslabel/i", $value['src'], $tmp)) {
330 $thispaidanything +
= $value['pmt'];
335 // Allocate any unknown patient responsibility by guessing if the
336 // deductible has been satisfied.
337 if ($thispaidanything)
338 $coinsurance = $ptresp - $deductible;
340 $deductible = $ptresp - $coinsurance;
342 if ($date && $deductible != 0)
343 $aadj[] = array($date, 'PR', '1', sprintf('%.2f', $deductible));
344 if ($date && $coinsurance != 0)
345 $aadj[] = array($date, 'PR', '2', sprintf('%.2f', $coinsurance));
352 // Return date, total payments and total "hard" adjustments from the given
353 // prior payer. If $code is specified then only that procedure key is
354 // selected, otherwise it's for the whole claim.
356 function payerTotals($ins, $code='') {
357 // If we have no modifiers stored in SQL-Ledger for this claim,
358 // then we cannot use a modifier passed in with the key.
359 $tmp = strpos($code, ':');
360 if ($tmp && !$this->using_modifiers
) $code = substr($code, 0, $tmp);
362 $inslabel = ($this->payerSequence($ins) == 'S') ?
'Ins2' : 'Ins1';
366 foreach($this->invoice
as $codekey => $codeval) {
367 if ($code && strcmp($codekey,$code) != 0) continue;
368 foreach ($codeval['dtl'] as $key => $value) {
369 if (preg_match("/$inslabel/i", $value['src'], $tmp)) {
370 if (!$date) $date = str_replace('-', '', trim(substr($key, 0, 10)));
371 $paytotal +
= $value['pmt'];
374 $aarr = $this->payerAdjustments($ins, $codekey);
375 foreach ($aarr as $a) {
376 if (strcmp($a[1],'PR') != 0) $adjtotal +
= $a[3];
377 if (!$date) $date = $a[0];
380 return array($date, sprintf('%.2f', $paytotal), sprintf('%.2f', $adjtotal));
383 // Return the amount already paid by the patient.
385 function patientPaidAmount() {
387 // TBD: This does not work for a primary claim because $this->invoice
388 // is not loaded. Might be good to get the co-pay from the billing
389 // table in that case.
392 foreach($this->invoice
as $codekey => $codeval) {
393 foreach ($codeval['dtl'] as $key => $value) {
394 if (!preg_match("/Ins/i", $value['src'], $tmp)) {
395 $amount +
= $value['pmt'];
399 return sprintf('%.2f', $amount);
402 // Return invoice total, including adjustments but not payments.
404 function invoiceTotal() {
406 foreach($this->invoice
as $codekey => $codeval) {
407 $amount +
= $codeval['chg'];
409 return sprintf('%.2f', $amount);
412 // Number of procedures in this claim.
413 function procCount() {
414 return count($this->procs
);
417 // Number of payers for this claim. Ranges from 1 to 3.
418 function payerCount() {
419 return count($this->payers
);
422 function x12gsversionstring() {
423 return x12clean(trim($this->x12_partner
['x12_version']));
426 function x12gssenderid() {
427 $tmp = $this->x12_partner
['x12_sender_id'];
428 while (strlen($tmp) < 15) $tmp .= " ";
432 function x12gsreceiverid() {
433 $tmp = $this->x12_partner
['x12_receiver_id'];
434 while (strlen($tmp) < 15) $tmp .= " ";
438 function cliaCode() {
439 return x12clean(trim($this->facility
['domain_identifier']));
442 function billingFacilityName() {
443 return x12clean(trim($this->billing_facility
['name']));
446 function billingFacilityStreet() {
447 return x12clean(trim($this->billing_facility
['street']));
450 function billingFacilityCity() {
451 return x12clean(trim($this->billing_facility
['city']));
454 function billingFacilityState() {
455 return x12clean(trim($this->billing_facility
['state']));
458 function billingFacilityZip() {
459 return x12clean(trim($this->billing_facility
['postal_code']));
462 function billingFacilityETIN() {
463 return x12clean(trim(str_replace('-', '', $this->billing_facility
['federal_ein'])));
466 function billingFacilityNPI() {
467 return x12clean(trim($this->billing_facility
['facility_npi']));
470 function billingContactName() {
471 return x12clean(trim($this->billing_facility
['attn']));
474 function billingContactPhone() {
475 if (preg_match("/([2-9]\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
476 $this->billing_facility
['phone'], $tmp))
478 return $tmp[1] . $tmp[2] . $tmp[3];
483 function facilityName() {
484 return x12clean(trim($this->facility
['name']));
487 function facilityStreet() {
488 return x12clean(trim($this->facility
['street']));
491 function facilityCity() {
492 return x12clean(trim($this->facility
['city']));
495 function facilityState() {
496 return x12clean(trim($this->facility
['state']));
499 function facilityZip() {
500 return x12clean(trim($this->facility
['postal_code']));
503 function facilityETIN() {
504 return x12clean(trim(str_replace('-', '', $this->facility
['federal_ein'])));
507 function facilityNPI() {
508 return x12clean(trim($this->facility
['facility_npi']));
511 function facilityPOS() {
512 return x12clean(trim($this->facility
['pos_code']));
515 function clearingHouseName() {
516 return x12clean(trim($this->x12_partner
['name']));
519 function clearingHouseETIN() {
520 return x12clean(trim(str_replace('-', '', $this->x12_partner
['id_number'])));
523 function providerNumberType() {
524 return $this->insurance_numbers
['provider_number_type'];
527 function providerNumber() {
528 return x12clean(trim(str_replace('-', '', $this->insurance_numbers
['provider_number'])));
531 // Returns 'P', 'S' or 'T'.
533 function payerSequence($ins=0) {
534 return strtoupper(substr($this->payers
[$ins]['data']['type'], 0, 1));
537 // Returns the HIPAA code of the patient-to-subscriber relationship.
539 function insuredRelationship($ins=0) {
540 $tmp = strtolower($this->payers
[$ins]['data']['subscriber_relationship']);
541 if (strcmp($tmp,'self' ) == 0) return '18';
542 if (strcmp($tmp,'spouse') == 0) return '01';
543 if (strcmp($tmp,'child' ) == 0) return '19';
544 if (strcmp($tmp,'other' ) == 0) return 'G8';
545 return $tmp; // should not happen
548 function insuredTypeCode($ins=0) {
549 if (strcmp($this->claimType($ins),'MB') == 0 && $this->payerSequence($ins) != 'P')
550 return '12'; // medicare secondary working aged beneficiary or
551 // spouse with employer group health plan
555 // Is the patient also the subscriber?
557 function isSelfOfInsured($ins=0) {
558 $tmp = strtolower($this->payers
[$ins]['data']['subscriber_relationship']);
559 return (strcmp($tmp,'self') == 0);
562 function groupNumber($ins=0) {
563 return x12clean(trim($this->payers
[$ins]['data']['group_number']));
566 function groupName($ins=0) {
567 return x12clean(trim($this->payers
[$ins]['data']['subscriber_employer']));
570 function claimType($ins=0) {
571 if (empty($this->payers
[$ins]['object'])) return '';
572 return $this->payers
[$ins]['object']->get_freeb_claim_type();
575 function insuredLastName($ins=0) {
576 return x12clean(trim($this->payers
[$ins]['data']['subscriber_lname']));
579 function insuredFirstName($ins=0) {
580 return x12clean(trim($this->payers
[$ins]['data']['subscriber_fname']));
583 function insuredMiddleName($ins=0) {
584 return x12clean(trim($this->payers
[$ins]['data']['subscriber_mname']));
587 function policyNumber($ins=0) { // "ID"
588 return x12clean(trim($this->payers
[$ins]['data']['policy_number']));
591 function insuredStreet($ins=0) {
592 return x12clean(trim($this->payers
[$ins]['data']['subscriber_street']));
595 function insuredCity($ins=0) {
596 return x12clean(trim($this->payers
[$ins]['data']['subscriber_city']));
599 function insuredState($ins=0) {
600 return x12clean(trim($this->payers
[$ins]['data']['subscriber_state']));
603 function insuredZip($ins=0) {
604 return x12clean(trim($this->payers
[$ins]['data']['subscriber_postal_code']));
607 function insuredDOB($ins=0) {
608 return str_replace('-', '', $this->payers
[$ins]['data']['subscriber_DOB']);
611 function insuredSex($ins=0) {
612 return strtoupper(substr($this->payers
[$ins]['data']['subscriber_sex'], 0, 1));
615 function payerName($ins=0) {
616 return x12clean(trim($this->payers
[$ins]['company']['name']));
619 function payerStreet($ins=0) {
620 if (empty($this->payers
[$ins]['object'])) return '';
621 $tmp = $this->payers
[$ins]['object'];
622 $tmp = $tmp->get_address();
623 return x12clean(trim($tmp->get_line1()));
626 function payerCity($ins=0) {
627 if (empty($this->payers
[$ins]['object'])) return '';
628 $tmp = $this->payers
[$ins]['object'];
629 $tmp = $tmp->get_address();
630 return x12clean(trim($tmp->get_city()));
633 function payerState($ins=0) {
634 if (empty($this->payers
[$ins]['object'])) return '';
635 $tmp = $this->payers
[$ins]['object'];
636 $tmp = $tmp->get_address();
637 return x12clean(trim($tmp->get_state()));
640 function payerZip($ins=0) {
641 if (empty($this->payers
[$ins]['object'])) return '';
642 $tmp = $this->payers
[$ins]['object'];
643 $tmp = $tmp->get_address();
644 return x12clean(trim($tmp->get_zip()));
647 function payerID($ins=0) {
648 return x12clean(trim($this->payers
[$ins]['company']['cms_id']));
651 function patientLastName() {
652 return x12clean(trim($this->patient_data
['lname']));
655 function patientFirstName() {
656 return x12clean(trim($this->patient_data
['fname']));
659 function patientMiddleName() {
660 return x12clean(trim($this->patient_data
['mname']));
663 function patientStreet() {
664 return x12clean(trim($this->patient_data
['street']));
667 function patientCity() {
668 return x12clean(trim($this->patient_data
['city']));
671 function patientState() {
672 return x12clean(trim($this->patient_data
['state']));
675 function patientZip() {
676 return x12clean(trim($this->patient_data
['postal_code']));
679 function patientDOB() {
680 return str_replace('-', '', $this->patient_data
['DOB']);
683 function patientSex() {
684 return strtoupper(substr($this->patient_data
['sex'], 0, 1));
687 function cptCode($prockey) {
688 return x12clean(trim($this->procs
[$prockey]['code']));
691 function cptModifier($prockey) {
692 return x12clean(trim($this->procs
[$prockey]['modifier']));
695 // Returns the procedure code, followed by ":modifier" if there is one.
696 function cptKey($prockey) {
697 $tmp = $this->cptModifier($prockey);
698 return $this->cptCode($prockey) . ($tmp ?
":$tmp" : "");
701 function cptCharges($prockey) {
702 return x12clean(trim($this->procs
[$prockey]['fee']));
705 function cptUnits($prockey) {
706 if (empty($this->procs
[$prockey]['units'])) return '1';
707 return x12clean(trim($this->procs
[$prockey]['units']));
711 function cptNDCID($prockey) {
712 $ndcinfo = $this->procs
[$prockey]['ndc_info'];
713 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndcinfo, $tmp)) {
715 if (preg_match('/^(\d+)-(\d+)-(\d+)$/', $ndc, $tmp)) {
716 return sprintf('%05d-%04d-%02d', $tmp[1], $tmp[2], $tmp[3]);
718 return x12clean($ndc); // format is bad but return it anyway
723 // NDC drug unit of measure code.
724 function cptNDCUOM($prockey) {
725 $ndcinfo = $this->procs
[$prockey]['ndc_info'];
726 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndcinfo, $tmp))
727 return x12clean($tmp[2]);
731 // NDC drug number of units.
732 function cptNDCQuantity($prockey) {
733 $ndcinfo = $this->procs
[$prockey]['ndc_info'];
734 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndcinfo, $tmp)) {
735 return x12clean(ltrim($tmp[3], '0'));
740 function onsetDate() {
741 return str_replace('-', '', substr($this->encounter
['onset_date'], 0, 10));
744 function serviceDate() {
745 return str_replace('-', '', substr($this->encounter
['date'], 0, 10));
748 function priorAuth() {
749 return x12clean(trim($this->billing_options
['prior_auth_number']));
752 // Returns an array of unique diagnoses. Periods are stripped.
753 function diagArray() {
755 foreach ($this->procs
as $row) {
756 $atmp = explode(':', $row['justify']);
757 foreach ($atmp as $tmp) {
759 $diag = str_replace('.', '', $tmp);
767 // Compute one 1-relative index in diagArray for the given procedure.
768 // This function is obsolete, use diagIndexArray() instead.
769 function diagIndex($prockey) {
770 $da = $this->diagArray();
771 $tmp = explode(':', $this->procs
[$prockey]['justify']);
772 if (empty($tmp)) return '';
773 $diag = str_replace('.', '', $tmp[0]);
775 foreach ($da as $value) {
777 if (strcmp($value,$diag) == 0) return $i;
782 // Compute array of 1-relative diagArray indices for the given procedure.
783 function diagIndexArray($prockey) {
785 $da = $this->diagArray();
786 $atmp = explode(':', $this->procs
[$prockey]['justify']);
787 foreach ($atmp as $tmp) {
789 $diag = str_replace('.', '', $tmp);
791 foreach ($da as $value) {
793 if (strcmp($value,$diag) == 0) $dia[] = $i;
800 function providerLastName() {
801 return x12clean(trim($this->provider
['lname']));
804 function providerFirstName() {
805 return x12clean(trim($this->provider
['fname']));
808 function providerMiddleName() {
809 return x12clean(trim($this->provider
['mname']));
812 function providerNPI() {
813 return x12clean(trim($this->provider
['npi']));
816 function providerUPIN() {
817 return x12clean(trim($this->provider
['upin']));
820 function providerSSN() {
821 return x12clean(trim(str_replace('-', '', $this->provider
['federaltaxid'])));
824 function referrerLastName() {
825 return x12clean(trim($this->referrer
['lname']));
828 function referrerFirstName() {
829 return x12clean(trim($this->referrer
['fname']));
832 function referrerMiddleName() {
833 return x12clean(trim($this->referrer
['mname']));
836 function referrerNPI() {
837 return x12clean(trim($this->referrer
['npi']));
840 function referrerUPIN() {
841 return x12clean(trim($this->referrer
['upin']));
844 function referrerSSN() {
845 return x12clean(trim(str_replace('-', '', $this->referrer
['federaltaxid'])));