minor billing fixes (#1265)
[openemr.git] / library / gen_hcfa_1500.inc.php
blobe6b0ffd4bd3cd3c6775357313b6f617b3635f9c0
1 <?php
2 /*
3 * This program creates the HCFA 1500 claim form.
5 * @package OpenEMR
6 * @author Rod Roark <rod@sunsetsystems.com>
7 * @author Stephen Waite <stephen.waite@cmsvt.com>
8 * @copyright Copyright (c) 2011 Rod Roark <rod@sunsetsystems.com>
9 * @copyright Copyright (C) 2017 Stephen Waite <stephen.waite@cmsvt.com>
10 * @link http://www.open-emr.org
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once("gen_hcfa_1500_02_12.inc.php");
15 require_once(dirname(__FILE__) . "/invoice_summary.inc.php");
17 use OpenEMR\Billing\Claim;
19 $hcfa_curr_line = 1;
20 $hcfa_curr_col = 1;
21 $hcfa_data = '';
22 $hcfa_proc_index = 0;
25 /**
26 * take the data element and place it at the correct coordinates on the page
28 * @global int $hcfa_curr_line
29 * @global type $hcfa_curr_col
30 * @global type $hcfa_data
31 * @param type $line
32 * @param type $col
33 * @param type $maxlen
34 * @param type $data
35 * @param type $strip regular expression for what to strip from the data. period and has are the defaults
36 * 02/12 version needs to include periods in the diagnoses hence the need to override
38 function put_hcfa($line, $col, $maxlen, $data, $strip = '/[.#]/')
40 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data;
41 if ($line < $hcfa_curr_line) {
42 die("Data item at ($line, $col) precedes current line.");
45 while ($hcfa_curr_line < $line) {
46 $hcfa_data .= "\n";
47 ++$hcfa_curr_line;
48 $hcfa_curr_col = 1;
51 if ($col < $hcfa_curr_col) {
52 die("Data item at ($line, $col) precedes current column.");
55 while ($hcfa_curr_col < $col) {
56 $hcfa_data .= " ";
57 ++$hcfa_curr_col;
60 $data = preg_replace($strip, '', strtoupper($data));
61 $len = min(strlen($data), $maxlen);
62 $hcfa_data .= substr($data, 0, $len);
63 $hcfa_curr_col += $len;
66 function gen_hcfa_1500($pid, $encounter, &$log)
68 global $hcfa_data, $hcfa_proc_index;
70 $hcfa_data = '';
71 $hcfa_proc_index = 0;
73 $today = time();
74 $claim = new Claim($pid, $encounter);
76 $log .= "Generating HCFA claim $pid-$encounter for " .
77 $claim->patientFirstName() . ' ' .
78 $claim->patientMiddleName() . ' ' .
79 $claim->patientLastName() . ' on ' .
80 date('Y-m-d H:i', $today) . ".\n";
82 while ($hcfa_proc_index < $claim->procCount()) {
83 if ($hcfa_proc_index) {
84 $hcfa_data .= "\014"; // append form feed for new page
87 gen_hcfa_1500_page($pid, $encounter, $log, $claim);
90 $log .= "\n";
91 return $hcfa_data;
94 function gen_hcfa_1500_page($pid, $encounter, &$log, &$claim)
96 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data, $hcfa_proc_index;
98 $hcfa_curr_line = 1;
99 $hcfa_curr_col = 1;
101 // According to:
102 // http://www.ngsmedicare.com/NGSMedicare/PartB/EducationandSupport/ToolsandMaterials/CMS_ClaimFormInst.aspx
103 // Medicare interprets sections 9 and 11 of the claim form in its own
104 // special way. This flag tells us to do that. However I'm not 100%
105 // sure that it applies nationwide, and if you find that it is not right
106 // for you then set it to false. -- Rod 2009-03-26
107 $new_medicare_logic = $claim->claimType() == 'MB';
109 // Payer name, attn, street.
110 put_hcfa(2, 41, 31, $claim->payerName());
111 put_hcfa(3, 41, 31, $claim->payerAttn());
112 put_hcfa(4, 41, 31, $claim->payerStreet());
114 // Payer city, state, zip.
115 $tmp = $claim->payerCity() ? ($claim->payerCity() . ', ') : '';
116 put_hcfa(5, 41, 31, $tmp . $claim->payerState() . ' ' . $claim->payerZip());
118 // Box 1. Insurance Type
119 // claimTypeRaw() gets the integer value from insurance_companies.ins_type_code.
120 // Previous version of this code called claimType() which maps ins_type_code to
121 // a 2-character code and that was not specific enough.
122 $ct = $claim->claimTypeRaw();
123 $tmpcol = 45; // Other
124 if ($ct == 2) {
125 $tmpcol = 1; // Medicare
126 } else if ($ct == 3) {
127 $tmpcol = 8; // Medicaid
128 } else if ($ct == 5) {
129 $tmpcol = 15; // TriCare (formerly CHAMPUS)
130 } else if ($ct == 4) {
131 $tmpcol = 24; // Champus VA
132 } else if ($ct == 6) {
133 $tmpcol = 31; // Group Health Plan (only BCBS?)
134 } else if ($ct == 7) {
135 $tmpcol = 39; // FECA
138 put_hcfa(8, $tmpcol, 1, 'X');
140 // Box 1a. Insured's ID Number
141 put_hcfa(8, 50, 17, $claim->policyNumber());
143 // Box 2. Patient's Name
144 $tmp = $claim->patientLastName() . ', ' . $claim->patientFirstName();
145 if ($claim->patientMiddleName()) {
146 $tmp .= ', ' . substr($claim->patientMiddleName(), 0, 1);
149 put_hcfa(10, 1, 28, $tmp);
151 // Box 3. Patient's Birth Date and Sex
152 $tmp = $claim->patientDOB();
153 put_hcfa(10, 31, 2, substr($tmp, 4, 2));
154 put_hcfa(10, 34, 2, substr($tmp, 6, 2));
155 put_hcfa(10, 37, 4, substr($tmp, 0, 4));
156 put_hcfa(10, $claim->patientSex() == 'M' ? 42 : 47, 1, 'X');
158 // Box 4. Insured's Name
159 $tmp = $claim->insuredLastName() . ', ' . $claim->insuredFirstName();
160 if ($claim->insuredMiddleName()) {
161 $tmp .= ', ' . substr($claim->insuredMiddleName(), 0, 1);
164 put_hcfa(10, 50, 28, $tmp);
166 // Box 5. Patient's Address
167 put_hcfa(12, 1, 28, $claim->patientStreet());
169 // Box 6. Patient Relationship to Insured
170 $tmp = $claim->insuredRelationship();
171 $tmpcol = 47; // Other
172 if ($tmp === '18') {
173 $tmpcol = 33; // self
174 } else if ($tmp === '01') {
175 $tmpcol = 38; // spouse
176 } else if ($tmp === '19') {
177 $tmpcol = 42; // child
180 put_hcfa(12, $tmpcol, 1, 'X');
182 // Box 7. Insured's Address
183 put_hcfa(12, 50, 28, $claim->insuredStreet());
185 // Box 5 continued. Patient's City and State
186 put_hcfa(14, 1, 20, $claim->patientCity());
187 put_hcfa(14, 26, 2, $claim->patientState());
189 // Box 8. Patient (Marital) Status
190 if (!hcfa_1500_version_02_12()) { // Box 8 Reserved for NUCC Use in 02/12
191 $tmp = $claim->patientStatus();
192 $tmpcol = 47; // Other
193 if ($tmp === 'S') {
194 $tmpcol = 35; // Single
195 } else if ($tmp === 'M') {
196 $tmpcol = 41; // Married
199 put_hcfa(14, $tmpcol, 1, 'X');
202 // Box 7 continued. Insured's City and State
203 put_hcfa(14, 50, 20, $claim->insuredCity());
204 put_hcfa(14, 74, 2, $claim->insuredState());
206 // Box 5 continued. Patient's Zip Code and Telephone
207 put_hcfa(16, 1, 10, $claim->patientZip());
208 $tmp = $claim->patientPhone();
209 put_hcfa(16, 15, 3, substr($tmp, 0, 3));
210 put_hcfa(16, 19, 7, substr($tmp, 3));
212 // Box 8 continued. Patient (Employment) Status
213 if (!hcfa_1500_version_02_12()) { // Box 8 Reserved for NUCC Use in 02/12
214 $tmp = $claim->patientOccupation();
215 if ($tmp === 'STUDENT') {
216 put_hcfa(16, 41, 1, 'X');
217 } else if ($tmp === 'PT STUDENT') {
218 put_hcfa(16, 47, 1, 'X');
219 } else if ($tmp !== 'UNEMPLOYED') {
220 put_hcfa(16, 35, 1, 'X');
224 // Box 7 continued. Insured's Zip Code and Telephone
225 put_hcfa(16, 50, 10, $claim->insuredZip());
226 $tmp = $claim->insuredPhone();
227 put_hcfa(16, 65, 3, substr($tmp, 0, 3));
228 put_hcfa(16, 69, 7, substr($tmp, 3));
230 // Box 9. Other Insured's Name
231 if ($new_medicare_logic) {
232 // TBD: Medigap stuff? How do we know if this is a Medigap transfer?
233 } else {
234 if ($claim->payerCount() > 1) {
235 $tmp = $claim->insuredLastName(1) . ', ' . $claim->insuredFirstName(1);
236 if ($claim->insuredMiddleName(1)) {
237 $tmp .= ', ' . substr($claim->insuredMiddleName(1), 0, 1);
240 put_hcfa(18, 1, 28, $tmp);
244 // Box 11. Insured's Group Number
245 if ($new_medicare_logic) {
246 // If this is Medicare secondary then we need the primary's policy number
247 // here, otherwise the word "NONE".
248 $tmp = $claim->payerSequence() == 'P' ? 'NONE' : $claim->policyNumber(1);
249 } else {
250 $tmp = $claim->groupNumber();
253 put_hcfa(18, 50, 30, $tmp);
255 // Box 9a. Other Insured's Policy or Group Number
256 if ($new_medicare_logic) {
257 // TBD: Medigap stuff?
258 } else {
259 if ($claim->payerCount() > 1) {
260 put_hcfa(20, 1, 28, $claim->policyNumber(1));
264 // Box 10a. Employment Related
265 put_hcfa(20, $claim->isRelatedEmployment() ? 35 : 41, 1, 'X');
267 // Box 11a. Insured's Birth Date and Sex
268 if ($new_medicare_logic) {
269 $tmpdob = $tmpsex = '';
270 if ($claim->payerSequence() != 'P') {
271 $tmpdob = $claim->insuredDOB(1);
272 $tmpsex = $claim->insuredSex(1);
274 } else {
275 $tmpdob = $claim->insuredDOB();
276 $tmpsex = $claim->insuredSex();
279 if ($tmpdob) {
280 put_hcfa(20, 53, 2, substr($tmpdob, 4, 2));
281 put_hcfa(20, 56, 2, substr($tmpdob, 6, 2));
282 put_hcfa(20, 59, 4, substr($tmpdob, 0, 4));
285 if ($tmpsex) {
286 put_hcfa(20, $tmpsex == 'M' ? 68 : 75, 1, 'X');
289 // Box 9b. Other Insured's Birth Date and Sex
290 if (!hcfa_1500_version_02_12()) { // Box 9b Reserved for NUCC Use in 02/12
291 if ($new_medicare_logic) {
292 // TBD: Medigap stuff?
293 } else {
294 if ($claim->payerCount() > 1) {
295 $tmp = $claim->insuredDOB(1);
296 put_hcfa(22, 2, 2, substr($tmp, 4, 2));
297 put_hcfa(22, 5, 2, substr($tmp, 6, 2));
298 put_hcfa(22, 8, 4, substr($tmp, 0, 4));
299 put_hcfa(22, $claim->insuredSex(1) == 'M' ? 18 : 24, 1, 'X');
304 // Box 10b. Auto Accident
305 put_hcfa(22, $claim->isRelatedAuto() ? 35 : 41, 1, 'X');
306 if ($claim->isRelatedAuto()) {
307 put_hcfa(22, 45, 2, $claim->autoAccidentState());
310 // Box 11b. Insured's Employer/School Name
311 if ($new_medicare_logic) {
312 $tmp = $claim->payerSequence() == 'P' ? '' : $claim->groupName(1);
313 } else {
314 $tmp = $claim->groupName();
317 put_hcfa(22, 50, 30, $tmp);
319 // Box 9c. Other Insured's Employer/School Name
320 if (!hcfa_1500_version_02_12()) { // Box 9c Reserved for NUCC Use in 02/12
321 if ($new_medicare_logic) {
322 // TBD: Medigap stuff?
323 } else {
324 if ($claim->payerCount() > 1) {
325 put_hcfa(24, 1, 28, $claim->groupName(1));
330 // Box 10c. Other Accident
331 put_hcfa(24, $claim->isRelatedOther() ? 35 : 41, 1, 'X');
333 // Box 11c. Insurance Plan Name or Program Name
334 if ($new_medicare_logic) {
335 $tmp = '';
336 if ($claim->payerSequence() != 'P') {
337 $tmp = $claim->planName(1);
338 if (!$tmp) {
339 $tmp = $claim->payerName(1);
342 } else {
343 $tmp = $claim->planName();
346 put_hcfa(24, 50, 30, $tmp);
348 // Box 9d. Other Insurance Plan Name or Program Name
349 if ($new_medicare_logic) {
350 // TBD: Medigap stuff?
351 } else {
352 if ($claim->payerCount() > 1) {
353 put_hcfa(26, 1, 28, $claim->planName(1));
357 // Box 10d. Claim Codes medicaid_referral_code
359 if ($claim->epsdtFlag()) {
360 put_hcfa(26, 34, 2, $claim->medicaidReferralCode());
363 // Box 11d. Is There Another Health Benefit Plan
365 if (!$new_medicare_logic) {
366 put_hcfa(26, $claim->payerCount() > 1 ? 52 : 57, 1, 'X');
369 // Box 12. Patient's or Authorized Person's Signature
370 put_hcfa(29, 7, 17, 'Signature on File');
371 // Note: Date does not apply unless the person physically signs the form.
373 // Box 13. Insured's or Authorized Person's Signature
374 put_hcfa(29, 55, 17, 'Signature on File');
376 // Box 14. Date of Current Illness/Injury/Pregnancy
377 $tmp = $claim->miscOnsetDate();
378 put_hcfa(32, 2, 2, substr($tmp, 4, 2));
379 put_hcfa(32, 5, 2, substr($tmp, 6, 2));
380 put_hcfa(32, 8, 4, substr($tmp, 0, 4));
382 if (hcfa_1500_version_02_12() && !empty($tmp)) {
383 // Only include the Box 14 qualifier if using version 02/12 and there is a Box 14 date.
384 put_hcfa(32, 16, 3, $claim->box14Qualifier());
387 // Box 15. First Date of Same or Similar Illness, if applicable
388 $tmp = $claim->dateInitialTreatment();
389 if (hcfa_1500_version_02_12() && !empty($tmp)) {
390 // Only include the Box 15 qualifier if using version 02/12 and there is a Box 15 date.
391 put_hcfa(32, 31, 3, $claim->box15Qualifier());
394 put_hcfa(32, 37, 2, substr($tmp, 4, 2));
395 put_hcfa(32, 40, 2, substr($tmp, 6, 2));
396 put_hcfa(32, 43, 4, substr($tmp, 0, 4));
398 // Box 16. Dates Patient Unable to Work in Current Occupation
399 if ($claim->isUnableToWork()) {
400 $tmp = $claim->offWorkFrom();
401 put_hcfa(32, 54, 2, substr($tmp, 4, 2));
402 put_hcfa(32, 57, 2, substr($tmp, 6, 2));
403 put_hcfa(32, 60, 4, substr($tmp, 0, 4));
404 $tmp = $claim->offWorkTo();
405 put_hcfa(32, 68, 2, substr($tmp, 4, 2));
406 put_hcfa(32, 71, 2, substr($tmp, 6, 2));
407 put_hcfa(32, 74, 4, substr($tmp, 0, 4));
410 // Referring provider stuff. Reports are that for primary care doctors,
411 // Medicare forbids an entry here and other payers require one.
412 // There is still confusion over this.
414 if ($claim->referrerLastName() || $claim->billingProviderLastName() &&
415 (empty($GLOBALS['MedicareReferrerIsRenderer']) || $claim->claimType() != 'MB')) {
416 // Box 17a. Referring Provider Alternate Identifier
417 // Commented this out because UPINs are obsolete, leaving the code as an
418 // example in case some other identifier needs to be supported.
419 /*****************************************************************
420 if ($claim->referrerUPIN() && $claim->claimType() != 'MB') {
421 put_hcfa(33, 30, 2, '1G');
422 put_hcfa(33, 33, 15, $claim->referrerUPIN());
424 *****************************************************************/
425 if ($claim->claimType() == 'MC') {
426 put_hcfa(33, 30, 2, 'ZZ');
427 put_hcfa(33, 33, 14, $claim->referrerTaxonomy());
430 // Box 17. Name of Referring Provider or Other Source leave it like it is just check if there is info in misc_billing the use it provider_qualifier_code
431 # Changed to look first at the misc hcfa billing form to complete this box if nothing on misc hcfa form use referrer
432 if (strlen($claim->billingProviderLastName()) !=0) {
433 $tmp2 = $claim->billingProviderLastName() . ', ' . $claim->billingProviderFirstName();
434 if ($claim->billingProviderMiddleName()) {
435 $tmp2 .= ', ' . substr($claim->billingProviderMiddleName(), 0, 1);
438 put_hcfa(34, 1, 3, $claim->billing_options['provider_qualifier_code']);
439 put_hcfa(34, 4, 25, $tmp2);
440 if ($claim->billingProviderNPI()) {
441 put_hcfa(34, 33, 15, $claim->billingProviderNPI());
443 } else {
444 $tmp = $claim->referrerLastName() . ', ' . $claim->referrerFirstName();
445 if ($claim->referrerMiddleName()) {
446 $tmp .= ', ' . substr($claim->referrerMiddleName(), 0, 1);
449 put_hcfa(34, 1, 3, 'DN');
450 put_hcfa(34, 4, 25, $tmp);
451 if ($claim->referrerNPI()) {
452 put_hcfa(34, 33, 15, $claim->referrerNPI());
457 // Box 18. Hospitalization Dates Related to Current Services
458 if ($claim->isHospitalized()) {
459 $tmp = $claim->hospitalizedFrom();
460 put_hcfa(34, 54, 2, substr($tmp, 4, 2));
461 put_hcfa(34, 57, 2, substr($tmp, 6, 2));
462 put_hcfa(34, 60, 4, substr($tmp, 0, 4));
463 $tmp = $claim->hospitalizedTo();
464 put_hcfa(34, 68, 2, substr($tmp, 4, 2));
465 put_hcfa(34, 71, 2, substr($tmp, 6, 2));
466 put_hcfa(34, 74, 4, substr($tmp, 0, 4));
469 // Box 19. Reserved for Local Use
470 put_hcfa(36, 1, 48, $claim->additionalNotes());
472 // Box 20. Outside Lab
473 put_hcfa(36, $claim->isOutsideLab() ? 52 : 57, 1, 'X');
474 if ($claim->isOutsideLab()) {
475 // Note here that put_hcfa strips the decimal point, as required.
476 // We right-justify this amount (ending in col. 69).
477 put_hcfa(36, 63, 8, sprintf('%8s', $claim->outsideLabAmount()));
480 if (hcfa_1500_version_02_12()) {
481 process_diagnoses_02_12($claim, $log);
482 } else {
483 // Box 21. Diagnoses
484 $tmp = $claim->diagArray();
485 $diags = array();
486 foreach ($tmp as $diag) {
487 $diags[] = $diag;
490 if (!empty($diags[0])) {
491 put_hcfa(38, 3, 3, substr($diags[0], 0, 3));
492 put_hcfa(38, 7, 2, substr($diags[0], 3));
495 if (!empty($diags[2])) {
496 put_hcfa(38, 30, 3, substr($diags[2], 0, 3));
497 put_hcfa(38, 34, 2, substr($diags[2], 3));
500 // Box 22. Medicaid Resubmission Code and Original Ref. No.
501 put_hcfa(38, 50, 10, $claim->medicaidResubmissionCode());
502 put_hcfa(38, 62, 15, $claim->medicaidOriginalReference());
504 // Box 21 continued. Diagnoses
505 if (!empty($diags[1])) {
506 put_hcfa(40, 3, 3, substr($diags[1], 0, 3));
507 put_hcfa(40, 7, 2, substr($diags[1], 3));
510 if (!empty($diags[3])) {
511 put_hcfa(40, 30, 3, substr($diags[3], 0, 3));
512 put_hcfa(40, 34, 2, substr($diags[3], 3));
515 // Box 23. Prior Authorization Number
516 put_hcfa(40, 50, 28, $claim->priorAuth());
519 $proccount = $claim->procCount(); // number of procedures
521 // Charges, adjustments and payments are accumulated by line item so that
522 // each page of a multi-page claim will stand alone. Payments include the
523 // co-pay for the first page only.
524 $clm_total_charges = 0;
525 $clm_amount_adjusted = 0;
526 $clm_amount_paid = $hcfa_proc_index ? 0 : $claim->patientPaidAmount();
528 // Procedure loop starts here.
530 for ($svccount = 0; $svccount < 6 && $hcfa_proc_index < $proccount; ++$hcfa_proc_index) {
531 $dia = $claim->diagIndexArray($hcfa_proc_index);
533 if (!$claim->cptCharges($hcfa_proc_index)) {
534 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
535 "' has no charges!\n";
538 if (empty($dia)) {
539 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
540 "' is not justified!\n";
543 $clm_total_charges += $claim->cptCharges($hcfa_proc_index);
545 // Compute prior payments and "hard" adjustments.
546 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
547 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
548 continue; // skip future payers
551 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($hcfa_proc_index));
552 $clm_amount_paid += $payerpaid[1];
553 $clm_amount_adjusted += $payerpaid[2];
556 ++$svccount;
557 $lino = $svccount * 2 + 41;
559 // Drug Information. Medicaid insurers want this with HCPCS codes.
561 $ndc = $claim->cptNDCID($hcfa_proc_index);
562 if ($ndc) {
563 if (preg_match('/^(\d\d\d\d\d)-(\d\d\d\d)-(\d\d)$/', $ndc, $tmp)) {
564 $ndc = $tmp[1] . $tmp[2] . $tmp[3];
565 } else if (preg_match('/^\d{11}$/', $ndc)) {
566 } else {
567 $log .= "*** NDC code '$ndc' has invalid format!\n";
570 put_hcfa($lino, 1, 50, "N4$ndc " . $claim->cptNDCUOM($hcfa_proc_index) .
571 $claim->cptNDCQuantity($hcfa_proc_index));
574 //Note Codes.
575 put_hcfa($lino, 25, 7, $claim->cptNotecodes($hcfa_proc_index));
577 // 24i and 24j Top. ID Qualifier and Rendering Provider ID
578 if ($claim->supervisorNumber()) {
579 // If there is a supervising provider and that person has a
580 // payer-specific provider number, then we assume that the SP
581 // must be identified on the claim and this is how we do it
582 // (but the NPI of the actual rendering provider appears below).
583 // BCBS of TN indicated they want it this way. YMMV. -- Rod
584 put_hcfa($lino, 65, 2, $claim->supervisorNumberType());
585 put_hcfa($lino, 68, 10, $claim->supervisorNumber());
586 } else if ($claim->providerNumber($hcfa_proc_index)) {
587 put_hcfa($lino, 65, 2, $claim->providerNumberType($hcfa_proc_index));
588 put_hcfa($lino, 68, 10, $claim->providerNumber($hcfa_proc_index));
589 } else if ($claim->claimType() == 'MC') {
590 put_hcfa($lino, 65, 2, 'ZZ');
591 put_hcfa($lino, 68, 14, $claim->providerTaxonomy());
594 ++$lino;
596 // 24a. Date of Service
597 $tmp = $claim->serviceDate();
598 put_hcfa($lino, 1, 2, substr($tmp, 4, 2));
599 put_hcfa($lino, 4, 2, substr($tmp, 6, 2));
600 put_hcfa($lino, 7, 2, substr($tmp, 2, 2));
601 put_hcfa($lino, 10, 2, substr($tmp, 4, 2));
602 put_hcfa($lino, 13, 2, substr($tmp, 6, 2));
603 put_hcfa($lino, 16, 2, substr($tmp, 2, 2));
605 // 24b. Place of Service
606 put_hcfa($lino, 19, 2, $claim->facilityPOS());
608 // 24c. EMG
609 // Not currently supported.
611 // 24d. Procedures, Services or Supplies
612 put_hcfa($lino, 25, 7, $claim->cptCode($hcfa_proc_index));
613 // replace colon with space for printing
614 put_hcfa($lino, 33, 12, str_replace(':', ' ', $claim->cptModifier($hcfa_proc_index)));
616 // 24e. Diagnosis Pointer
617 $tmp = '';
618 foreach ($claim->diagIndexArray($hcfa_proc_index) as $value) {
619 if (hcfa_1500_version_02_12()) {// For 02/12 Version convert number to letter.
620 // ASCII A is 65, since diagIndexArray is ones based, this will make 1->A, 2->B...
621 $value=chr($value+64);
624 $tmp .= $value;
627 put_hcfa($lino, 45, 4, $tmp);
629 // 24f. Charges
630 put_hcfa($lino, 50, 8, str_replace(
631 '.',
632 ' ',
633 sprintf('%8.2f', $claim->cptCharges($hcfa_proc_index))
636 // 24g. Days or Units
637 put_hcfa($lino, 59, 3, $claim->cptUnits($hcfa_proc_index));
639 // 24h. EPSDT Family Plan
641 if ($claim->epsdtFlag()) {
642 put_hcfa($lino, 63, 2, '03');
645 // 24j. Rendering Provider NPI
646 put_hcfa($lino, 68, 10, $claim->providerNPI($hcfa_proc_index));
649 // 25. Federal Tax ID Number
650 // FrreB hard coded EIN. Changed it to included SSN as well.
651 put_hcfa(56, 1, 15, $claim->billingFacilityETIN());
652 if ($claim->federalIdType()=='SY') {
653 put_hcfa(56, 17, 1, 'X'); // The SSN checkbox
654 } else {
655 put_hcfa(56, 19, 1, 'X'); // The EIN checkbox
658 // 26. Patient's Account No.
659 // Instructions say hyphens are not allowed.
660 put_hcfa(56, 23, 15, "$pid-$encounter");
662 // 27. Accept Assignment
663 put_hcfa(56, $claim->billingFacilityAssignment() ? 38 : 43, 1, 'X');
665 // 28. Total Charge
666 put_hcfa(56, 52, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_total_charges)));
667 if (!$clm_total_charges) {
668 $log .= "*** This claim has no charges!\n";
671 // 29. Amount Paid
672 put_hcfa(56, 62, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_amount_paid)));
674 // 30. Balance Due
675 // For secondary payers this reflects primary "contracted rate" adjustments,
676 // so in general box 30 will not equal box 28 minus box 29.
677 if (!hcfa_1500_version_02_12()) { // Box 30 Reserved for NUCC Use in 02/12
678 put_hcfa(56, 71, 8, str_replace('.', ' ', sprintf(
679 '%8.2f',
680 $clm_total_charges - $clm_amount_paid - $clm_amount_adjusted
681 )));
684 // 33. Billing Provider: Phone Number
685 $tmp = $claim->billingContactPhone();
686 put_hcfa(57, 66, 3, substr($tmp, 0, 3));
687 put_hcfa(57, 70, 3, substr($tmp, 3)); // slight adjustment for better look smw 030315
688 put_hcfa(57, 73, 1, '-');
689 put_hcfa(57, 74, 4, substr($tmp, 6));
691 // 32. Service Facility Location Information: Name
692 put_hcfa(58, 23, 25, $claim->facilityName());
694 // 33. Billing Provider: Name
695 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
696 $firstName = $claim->providerFirstName();
697 $lastName = $claim->providerLastName();
698 $middleName = $claim->providerMiddleName();
699 $suffixName = $claim->providerSuffixName();
700 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
701 put_hcfa(58, 50, 25, $billingProviderName);
702 } else {
703 put_hcfa(58, 50, 25, $claim->billingFacilityName());
706 // 32. Service Facility Location Information: Street
707 put_hcfa(59, 23, 25, $claim->facilityStreet());
709 // 33. Billing Provider: Name
710 put_hcfa(59, 50, 25, $claim->billingFacilityStreet());
712 // 31. Signature of Physician or Supplier
714 if ($GLOBALS['cms_1500_box_31_format']==0) {
715 put_hcfa(60, 1, 20, 'Signature on File');
716 } else if ($GLOBALS['cms_1500_box_31_format']==1) {
717 put_hcfa(60, 1, 22, $claim->providerFirstName()." ".$claim->providerLastName());
721 // $tmp = $claim->providerFirstName();
722 // if ($claim->providerMiddleName()) $tmp .= ' ' . substr($claim->providerMiddleName(),0,1);
723 // put_hcfa(60, 1, 20, $tmp . ' ' . $claim->providerLastName());
725 // 32. Service Facility Location Information: City State Zip
726 $tmp = $claim->facilityCity() ? ($claim->facilityCity() . ' ') : '';
727 put_hcfa(60, 23, 27, $tmp . $claim->facilityState() . ' ' .
728 $claim->facilityZip());
730 // 33. Billing Provider: City State Zip
731 $tmp = $claim->billingFacilityCity() ? ($claim->billingFacilityCity() . ' ') : '';
732 put_hcfa(60, 50, 27, $tmp . $claim->billingFacilityState() . ' ' .
733 $claim->billingFacilityZip());
735 // 31. Signature of Physician or Supplier: Date
736 if ($GLOBALS['cms_1500_box_31_date']>0) {
737 if ($GLOBALS['cms_1500_box_31_date']==1) {
738 $date_of_service= $claim->serviceDate();
739 $MDY=substr($date_of_service, 4, 2)." ".substr($date_of_service, 6, 2)." ".substr($date_of_service, 2, 2);
740 } else if ($GLOBALS['cms_1500_box_31_date']==2) {
741 $MDY=date("m/d/y");
744 put_hcfa(61, 6, 10, $MDY);
747 // 32a. Service Facility NPI
748 put_hcfa(61, 23, 10, $claim->facilityNPI());
750 // 32b. Service Facility Other ID
751 // Note that Medicare does NOT want this any more.
752 if ($claim->providerGroupNumber()) {
753 put_hcfa(61, 36, 2, $claim->providerNumberType());
754 put_hcfa(61, 38, 11, $claim->providerGroupNumber());
757 // 33a. Billing Facility NPI
758 put_hcfa(61, 50, 10, $claim->billingFacilityNPI());
760 // 33b. Billing Facility Other ID
761 // Note that Medicare does NOT want this any more.
762 if ($claim->claimType() == 'MC') {
763 put_hcfa(61, 63, 2, 'ZZ');
764 put_hcfa(61, 65, 14, $claim->providerTaxonomy());
765 } elseif ($claim->providerGroupNumber() && $claim->claimType() != 'MB') {
766 put_hcfa(61, 63, 2, $claim->providerNumberType());
767 put_hcfa(61, 65, 14, $claim->providerGroupNumber());
770 // Put an extra line here for compatibility with old hcfa text generated form
771 put_hcfa(62, 1, 1, ' ');
772 // put a couple more in so that multiple claims correctly print through the text file download
773 put_hcfa(63, 1, 1, ' ');
774 put_hcfa(64, 1, 1, ' ');
775 return;