Highway to PSR2
[openemr.git] / library / gen_hcfa_1500.inc.php
blobe8360d393bf2e786ff8be21e27e38ef2d6818709
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("Claim.class.php");
15 require_once("gen_hcfa_1500_02_12.inc.php");
17 $hcfa_curr_line = 1;
18 $hcfa_curr_col = 1;
19 $hcfa_data = '';
20 $hcfa_proc_index = 0;
23 /**
24 * take the data element and place it at the correct coordinates on the page
26 * @global int $hcfa_curr_line
27 * @global type $hcfa_curr_col
28 * @global type $hcfa_data
29 * @param type $line
30 * @param type $col
31 * @param type $maxlen
32 * @param type $data
33 * @param type $strip regular expression for what to strip from the data. period and has are the defaults
34 * 02/12 version needs to include periods in the diagnoses hence the need to override
36 function put_hcfa($line, $col, $maxlen, $data, $strip = '/[.#]/')
38 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data;
39 if ($line < $hcfa_curr_line) {
40 die("Data item at ($line, $col) precedes current line.");
43 while ($hcfa_curr_line < $line) {
44 $hcfa_data .= "\n";
45 ++$hcfa_curr_line;
46 $hcfa_curr_col = 1;
49 if ($col < $hcfa_curr_col) {
50 die("Data item at ($line, $col) precedes current column.");
53 while ($hcfa_curr_col < $col) {
54 $hcfa_data .= " ";
55 ++$hcfa_curr_col;
58 $data = preg_replace($strip, '', strtoupper($data));
59 $len = min(strlen($data), $maxlen);
60 $hcfa_data .= substr($data, 0, $len);
61 $hcfa_curr_col += $len;
64 function gen_hcfa_1500($pid, $encounter, &$log)
66 global $hcfa_data, $hcfa_proc_index;
68 $hcfa_data = '';
69 $hcfa_proc_index = 0;
71 $today = time();
72 $claim = new Claim($pid, $encounter);
74 $log .= "Generating HCFA claim $pid-$encounter for " .
75 $claim->patientFirstName() . ' ' .
76 $claim->patientMiddleName() . ' ' .
77 $claim->patientLastName() . ' on ' .
78 date('Y-m-d H:i', $today) . ".\n";
80 while ($hcfa_proc_index < $claim->procCount()) {
81 if ($hcfa_proc_index) {
82 $hcfa_data .= "\014"; // append form feed for new page
85 gen_hcfa_1500_page($pid, $encounter, $log, $claim);
88 $log .= "\n";
89 return $hcfa_data;
92 function gen_hcfa_1500_page($pid, $encounter, &$log, &$claim)
94 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data, $hcfa_proc_index;
96 $hcfa_curr_line = 1;
97 $hcfa_curr_col = 1;
99 // According to:
100 // http://www.ngsmedicare.com/NGSMedicare/PartB/EducationandSupport/ToolsandMaterials/CMS_ClaimFormInst.aspx
101 // Medicare interprets sections 9 and 11 of the claim form in its own
102 // special way. This flag tells us to do that. However I'm not 100%
103 // sure that it applies nationwide, and if you find that it is not right
104 // for you then set it to false. -- Rod 2009-03-26
105 $new_medicare_logic = $claim->claimType() == 'MB';
107 // Payer name, attn, street.
108 put_hcfa(2, 41, 31, $claim->payerName());
109 put_hcfa(3, 41, 31, $claim->payerAttn());
110 put_hcfa(4, 41, 31, $claim->payerStreet());
112 // Payer city, state, zip.
113 $tmp = $claim->payerCity() ? ($claim->payerCity() . ', ') : '';
114 put_hcfa(5, 41, 31, $tmp . $claim->payerState() . ' ' . $claim->payerZip());
116 // Box 1. Insurance Type
117 // claimTypeRaw() gets the integer value from insurance_companies.ins_type_code.
118 // Previous version of this code called claimType() which maps ins_type_code to
119 // a 2-character code and that was not specific enough.
120 $ct = $claim->claimTypeRaw();
121 $tmpcol = 45; // Other
122 if ($ct == 2) {
123 $tmpcol = 1; // Medicare
124 } else if ($ct == 3) {
125 $tmpcol = 8; // Medicaid
126 } else if ($ct == 5) {
127 $tmpcol = 15; // TriCare (formerly CHAMPUS)
128 } else if ($ct == 4) {
129 $tmpcol = 24; // Champus VA
130 } else if ($ct == 6) {
131 $tmpcol = 31; // Group Health Plan (only BCBS?)
132 } else if ($ct == 7) {
133 $tmpcol = 39; // FECA
136 put_hcfa(8, $tmpcol, 1, 'X');
138 // Box 1a. Insured's ID Number
139 put_hcfa(8, 50, 17, $claim->policyNumber());
141 // Box 2. Patient's Name
142 $tmp = $claim->patientLastName() . ', ' . $claim->patientFirstName();
143 if ($claim->patientMiddleName()) {
144 $tmp .= ', ' . substr($claim->patientMiddleName(), 0, 1);
147 put_hcfa(10, 1, 28, $tmp);
149 // Box 3. Patient's Birth Date and Sex
150 $tmp = $claim->patientDOB();
151 put_hcfa(10, 31, 2, substr($tmp, 4, 2));
152 put_hcfa(10, 34, 2, substr($tmp, 6, 2));
153 put_hcfa(10, 37, 4, substr($tmp, 0, 4));
154 put_hcfa(10, $claim->patientSex() == 'M' ? 42 : 47, 1, 'X');
156 // Box 4. Insured's Name
157 $tmp = $claim->insuredLastName() . ', ' . $claim->insuredFirstName();
158 if ($claim->insuredMiddleName()) {
159 $tmp .= ', ' . substr($claim->insuredMiddleName(), 0, 1);
162 put_hcfa(10, 50, 28, $tmp);
164 // Box 5. Patient's Address
165 put_hcfa(12, 1, 28, $claim->patientStreet());
167 // Box 6. Patient Relationship to Insured
168 $tmp = $claim->insuredRelationship();
169 $tmpcol = 47; // Other
170 if ($tmp === '18') {
171 $tmpcol = 33; // self
172 } else if ($tmp === '01') {
173 $tmpcol = 38; // spouse
174 } else if ($tmp === '19') {
175 $tmpcol = 42; // child
178 put_hcfa(12, $tmpcol, 1, 'X');
180 // Box 7. Insured's Address
181 put_hcfa(12, 50, 28, $claim->insuredStreet());
183 // Box 5 continued. Patient's City and State
184 put_hcfa(14, 1, 20, $claim->patientCity());
185 put_hcfa(14, 26, 2, $claim->patientState());
187 // Box 8. Patient (Marital) Status
188 if (!hcfa_1500_version_02_12()) { // Box 8 Reserved for NUCC Use in 02/12
189 $tmp = $claim->patientStatus();
190 $tmpcol = 47; // Other
191 if ($tmp === 'S') {
192 $tmpcol = 35; // Single
193 } else if ($tmp === 'M') {
194 $tmpcol = 41; // Married
197 put_hcfa(14, $tmpcol, 1, 'X');
200 // Box 7 continued. Insured's City and State
201 put_hcfa(14, 50, 20, $claim->insuredCity());
202 put_hcfa(14, 74, 2, $claim->insuredState());
204 // Box 5 continued. Patient's Zip Code and Telephone
205 put_hcfa(16, 1, 10, $claim->patientZip());
206 $tmp = $claim->patientPhone();
207 put_hcfa(16, 15, 3, substr($tmp, 0, 3));
208 put_hcfa(16, 19, 7, substr($tmp, 3));
210 // Box 8 continued. Patient (Employment) Status
211 if (!hcfa_1500_version_02_12()) { // Box 8 Reserved for NUCC Use in 02/12
212 $tmp = $claim->patientOccupation();
213 if ($tmp === 'STUDENT') {
214 put_hcfa(16, 41, 1, 'X');
215 } else if ($tmp === 'PT STUDENT') {
216 put_hcfa(16, 47, 1, 'X');
217 } else if ($tmp !== 'UNEMPLOYED') {
218 put_hcfa(16, 35, 1, 'X');
222 // Box 7 continued. Insured's Zip Code and Telephone
223 put_hcfa(16, 50, 10, $claim->insuredZip());
224 $tmp = $claim->insuredPhone();
225 put_hcfa(16, 65, 3, substr($tmp, 0, 3));
226 put_hcfa(16, 69, 7, substr($tmp, 3));
228 // Box 9. Other Insured's Name
229 if ($new_medicare_logic) {
230 // TBD: Medigap stuff? How do we know if this is a Medigap transfer?
231 } else {
232 if ($claim->payerCount() > 1) {
233 $tmp = $claim->insuredLastName(1) . ', ' . $claim->insuredFirstName(1);
234 if ($claim->insuredMiddleName(1)) {
235 $tmp .= ', ' . substr($claim->insuredMiddleName(1), 0, 1);
238 put_hcfa(18, 1, 28, $tmp);
242 // Box 11. Insured's Group Number
243 if ($new_medicare_logic) {
244 // If this is Medicare secondary then we need the primary's policy number
245 // here, otherwise the word "NONE".
246 $tmp = $claim->payerSequence() == 'P' ? 'NONE' : $claim->policyNumber(1);
247 } else {
248 $tmp = $claim->groupNumber();
251 put_hcfa(18, 50, 30, $tmp);
253 // Box 9a. Other Insured's Policy or Group Number
254 if ($new_medicare_logic) {
255 // TBD: Medigap stuff?
256 } else {
257 if ($claim->payerCount() > 1) {
258 put_hcfa(20, 1, 28, $claim->policyNumber(1));
262 // Box 10a. Employment Related
263 put_hcfa(20, $claim->isRelatedEmployment() ? 35 : 41, 1, 'X');
265 // Box 11a. Insured's Birth Date and Sex
266 if ($new_medicare_logic) {
267 $tmpdob = $tmpsex = '';
268 if ($claim->payerSequence() != 'P') {
269 $tmpdob = $claim->insuredDOB(1);
270 $tmpsex = $claim->insuredSex(1);
272 } else {
273 $tmpdob = $claim->insuredDOB();
274 $tmpsex = $claim->insuredSex();
277 if ($tmpdob) {
278 put_hcfa(20, 53, 2, substr($tmpdob, 4, 2));
279 put_hcfa(20, 56, 2, substr($tmpdob, 6, 2));
280 put_hcfa(20, 59, 4, substr($tmpdob, 0, 4));
283 if ($tmpsex) {
284 put_hcfa(20, $tmpsex == 'M' ? 68 : 75, 1, 'X');
287 // Box 9b. Other Insured's Birth Date and Sex
288 if (!hcfa_1500_version_02_12()) { // Box 9b Reserved for NUCC Use in 02/12
289 if ($new_medicare_logic) {
290 // TBD: Medigap stuff?
291 } else {
292 if ($claim->payerCount() > 1) {
293 $tmp = $claim->insuredDOB(1);
294 put_hcfa(22, 2, 2, substr($tmp, 4, 2));
295 put_hcfa(22, 5, 2, substr($tmp, 6, 2));
296 put_hcfa(22, 8, 4, substr($tmp, 0, 4));
297 put_hcfa(22, $claim->insuredSex(1) == 'M' ? 18 : 24, 1, 'X');
302 // Box 10b. Auto Accident
303 put_hcfa(22, $claim->isRelatedAuto() ? 35 : 41, 1, 'X');
304 if ($claim->isRelatedAuto()) {
305 put_hcfa(22, 45, 2, $claim->autoAccidentState());
308 // Box 11b. Insured's Employer/School Name
309 if ($new_medicare_logic) {
310 $tmp = $claim->payerSequence() == 'P' ? '' : $claim->groupName(1);
311 } else {
312 $tmp = $claim->groupName();
315 put_hcfa(22, 50, 30, $tmp);
317 // Box 9c. Other Insured's Employer/School Name
318 if (!hcfa_1500_version_02_12()) { // Box 9c Reserved for NUCC Use in 02/12
319 if ($new_medicare_logic) {
320 // TBD: Medigap stuff?
321 } else {
322 if ($claim->payerCount() > 1) {
323 put_hcfa(24, 1, 28, $claim->groupName(1));
328 // Box 10c. Other Accident
329 put_hcfa(24, $claim->isRelatedOther() ? 35 : 41, 1, 'X');
331 // Box 11c. Insurance Plan Name or Program Name
332 if ($new_medicare_logic) {
333 $tmp = '';
334 if ($claim->payerSequence() != 'P') {
335 $tmp = $claim->planName(1);
336 if (!$tmp) {
337 $tmp = $claim->payerName(1);
340 } else {
341 $tmp = $claim->planName();
344 put_hcfa(24, 50, 30, $tmp);
346 // Box 9d. Other Insurance Plan Name or Program Name
347 if ($new_medicare_logic) {
348 // TBD: Medigap stuff?
349 } else {
350 if ($claim->payerCount() > 1) {
351 put_hcfa(26, 1, 28, $claim->planName(1));
355 // Box 10d. Claim Codes medicaid_referral_code
357 if ($claim->epsdtFlag()) {
358 put_hcfa(26, 34, 2, $claim->medicaidReferralCode());
361 // Box 11d. Is There Another Health Benefit Plan
363 if (!$new_medicare_logic) {
364 put_hcfa(26, $claim->payerCount() > 1 ? 52 : 57, 1, 'X');
367 // Box 12. Patient's or Authorized Person's Signature
368 put_hcfa(29, 7, 17, 'Signature on File');
369 // Note: Date does not apply unless the person physically signs the form.
371 // Box 13. Insured's or Authorized Person's Signature
372 put_hcfa(29, 55, 17, 'Signature on File');
374 // Box 14. Date of Current Illness/Injury/Pregnancy
375 $tmp = $claim->miscOnsetDate();
376 put_hcfa(32, 2, 2, substr($tmp, 4, 2));
377 put_hcfa(32, 5, 2, substr($tmp, 6, 2));
378 put_hcfa(32, 8, 4, substr($tmp, 0, 4));
380 if (hcfa_1500_version_02_12() && !empty($tmp)) {
381 // Only include the Box 14 qualifier if using version 02/12 and there is a Box 14 date.
382 put_hcfa(32, 16, 3, $claim->box14Qualifier());
385 // Box 15. First Date of Same or Similar Illness, if applicable
386 $tmp = $claim->dateInitialTreatment();
387 if (hcfa_1500_version_02_12() && !empty($tmp)) {
388 // Only include the Box 15 qualifier if using version 02/12 and there is a Box 15 date.
389 put_hcfa(32, 31, 3, $claim->box15Qualifier());
392 put_hcfa(32, 37, 2, substr($tmp, 4, 2));
393 put_hcfa(32, 40, 2, substr($tmp, 6, 2));
394 put_hcfa(32, 43, 4, substr($tmp, 0, 4));
396 // Box 16. Dates Patient Unable to Work in Current Occupation
397 if ($claim->isUnableToWork()) {
398 $tmp = $claim->offWorkFrom();
399 put_hcfa(32, 54, 2, substr($tmp, 4, 2));
400 put_hcfa(32, 57, 2, substr($tmp, 6, 2));
401 put_hcfa(32, 60, 4, substr($tmp, 0, 4));
402 $tmp = $claim->offWorkTo();
403 put_hcfa(32, 68, 2, substr($tmp, 4, 2));
404 put_hcfa(32, 71, 2, substr($tmp, 6, 2));
405 put_hcfa(32, 74, 4, substr($tmp, 0, 4));
408 // Referring provider stuff. Reports are that for primary care doctors,
409 // Medicare forbids an entry here and other payers require one.
410 // There is still confusion over this.
412 if ($claim->referrerLastName() || $claim->billingProviderLastName() &&
413 (empty($GLOBALS['MedicareReferrerIsRenderer']) || $claim->claimType() != 'MB')) {
414 // Box 17a. Referring Provider Alternate Identifier
415 // Commented this out because UPINs are obsolete, leaving the code as an
416 // example in case some other identifier needs to be supported.
417 /*****************************************************************
418 if ($claim->referrerUPIN() && $claim->claimType() != 'MB') {
419 put_hcfa(33, 30, 2, '1G');
420 put_hcfa(33, 33, 15, $claim->referrerUPIN());
422 *****************************************************************/
423 if ($claim->claimType() == 'MC') {
424 put_hcfa(33, 30, 2, 'ZZ');
425 put_hcfa(33, 33, 14, $claim->referrerTaxonomy());
428 // 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
429 # Changed to look first at the misc hcfa billing form to complete this box if nothing on misc hcfa form use referrer
430 if (strlen($claim->billingProviderLastName()) !=0) {
431 $tmp2 = $claim->billingProviderLastName() . ', ' . $claim->billingProviderFirstName();
432 if ($claim->billingProviderMiddleName()) {
433 $tmp2 .= ', ' . substr($claim->billingProviderMiddleName(), 0, 1);
436 put_hcfa(34, 1, 3, $claim->billing_options['provider_qualifier_code']);
437 put_hcfa(34, 4, 25, $tmp2);
438 if ($claim->billingProviderNPI()) {
439 put_hcfa(34, 33, 15, $claim->billingProviderNPI());
441 } else {
442 $tmp = $claim->referrerLastName() . ', ' . $claim->referrerFirstName();
443 if ($claim->referrerMiddleName()) {
444 $tmp .= ', ' . substr($claim->referrerMiddleName(), 0, 1);
447 put_hcfa(34, 1, 3, 'DN');
448 put_hcfa(34, 4, 25, $tmp);
449 if ($claim->referrerNPI()) {
450 put_hcfa(34, 33, 15, $claim->referrerNPI());
455 // Box 18. Hospitalization Dates Related to Current Services
456 if ($claim->isHospitalized()) {
457 $tmp = $claim->hospitalizedFrom();
458 put_hcfa(34, 54, 2, substr($tmp, 4, 2));
459 put_hcfa(34, 57, 2, substr($tmp, 6, 2));
460 put_hcfa(34, 60, 4, substr($tmp, 0, 4));
461 $tmp = $claim->hospitalizedTo();
462 put_hcfa(34, 68, 2, substr($tmp, 4, 2));
463 put_hcfa(34, 71, 2, substr($tmp, 6, 2));
464 put_hcfa(34, 74, 4, substr($tmp, 0, 4));
467 // Box 19. Reserved for Local Use
468 put_hcfa(36, 1, 48, $claim->additionalNotes());
470 // Box 20. Outside Lab
471 put_hcfa(36, $claim->isOutsideLab() ? 52 : 57, 1, 'X');
472 if ($claim->isOutsideLab()) {
473 // Note here that put_hcfa strips the decimal point, as required.
474 // We right-justify this amount (ending in col. 69).
475 put_hcfa(36, 63, 8, sprintf('%8s', $claim->outsideLabAmount()));
478 if (hcfa_1500_version_02_12()) {
479 process_diagnoses_02_12($claim, $log);
480 } else {
481 // Box 21. Diagnoses
482 $tmp = $claim->diagArray();
483 $diags = array();
484 foreach ($tmp as $diag) {
485 $diags[] = $diag;
488 if (!empty($diags[0])) {
489 put_hcfa(38, 3, 3, substr($diags[0], 0, 3));
490 put_hcfa(38, 7, 2, substr($diags[0], 3));
493 if (!empty($diags[2])) {
494 put_hcfa(38, 30, 3, substr($diags[2], 0, 3));
495 put_hcfa(38, 34, 2, substr($diags[2], 3));
498 // Box 22. Medicaid Resubmission Code and Original Ref. No.
499 put_hcfa(38, 50, 10, $claim->medicaidResubmissionCode());
500 put_hcfa(38, 62, 15, $claim->medicaidOriginalReference());
502 // Box 21 continued. Diagnoses
503 if (!empty($diags[1])) {
504 put_hcfa(40, 3, 3, substr($diags[1], 0, 3));
505 put_hcfa(40, 7, 2, substr($diags[1], 3));
508 if (!empty($diags[3])) {
509 put_hcfa(40, 30, 3, substr($diags[3], 0, 3));
510 put_hcfa(40, 34, 2, substr($diags[3], 3));
513 // Box 23. Prior Authorization Number
514 put_hcfa(40, 50, 28, $claim->priorAuth());
517 $proccount = $claim->procCount(); // number of procedures
519 // Charges, adjustments and payments are accumulated by line item so that
520 // each page of a multi-page claim will stand alone. Payments include the
521 // co-pay for the first page only.
522 $clm_total_charges = 0;
523 $clm_amount_adjusted = 0;
524 $clm_amount_paid = $hcfa_proc_index ? 0 : $claim->patientPaidAmount();
526 // Procedure loop starts here.
528 for ($svccount = 0; $svccount < 6 && $hcfa_proc_index < $proccount; ++$hcfa_proc_index) {
529 $dia = $claim->diagIndexArray($hcfa_proc_index);
531 if (!$claim->cptCharges($hcfa_proc_index)) {
532 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
533 "' has no charges!\n";
536 if (empty($dia)) {
537 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
538 "' is not justified!\n";
541 $clm_total_charges += $claim->cptCharges($hcfa_proc_index);
543 // Compute prior payments and "hard" adjustments.
544 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
545 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
546 continue; // skip future payers
549 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($hcfa_proc_index));
550 $clm_amount_paid += $payerpaid[1];
551 $clm_amount_adjusted += $payerpaid[2];
554 ++$svccount;
555 $lino = $svccount * 2 + 41;
557 // Drug Information. Medicaid insurers want this with HCPCS codes.
559 $ndc = $claim->cptNDCID($hcfa_proc_index);
560 if ($ndc) {
561 if (preg_match('/^(\d\d\d\d\d)-(\d\d\d\d)-(\d\d)$/', $ndc, $tmp)) {
562 $ndc = $tmp[1] . $tmp[2] . $tmp[3];
563 } else if (preg_match('/^\d{11}$/', $ndc)) {
564 } else {
565 $log .= "*** NDC code '$ndc' has invalid format!\n";
568 put_hcfa($lino, 1, 50, "N4$ndc " . $claim->cptNDCUOM($hcfa_proc_index) .
569 $claim->cptNDCQuantity($hcfa_proc_index));
572 //Note Codes.
573 put_hcfa($lino, 25, 7, $claim->cptNotecodes($hcfa_proc_index));
575 // 24i and 24j Top. ID Qualifier and Rendering Provider ID
576 if ($claim->supervisorNumber()) {
577 // If there is a supervising provider and that person has a
578 // payer-specific provider number, then we assume that the SP
579 // must be identified on the claim and this is how we do it
580 // (but the NPI of the actual rendering provider appears below).
581 // BCBS of TN indicated they want it this way. YMMV. -- Rod
582 put_hcfa($lino, 65, 2, $claim->supervisorNumberType());
583 put_hcfa($lino, 68, 10, $claim->supervisorNumber());
584 } else if ($claim->providerNumber($hcfa_proc_index)) {
585 put_hcfa($lino, 65, 2, $claim->providerNumberType($hcfa_proc_index));
586 put_hcfa($lino, 68, 10, $claim->providerNumber($hcfa_proc_index));
587 } else if ($claim->claimType() == 'MC') {
588 put_hcfa($lino, 65, 2, 'ZZ');
589 put_hcfa($lino, 68, 14, $claim->providerTaxonomy());
592 ++$lino;
594 // 24a. Date of Service
595 $tmp = $claim->serviceDate();
596 put_hcfa($lino, 1, 2, substr($tmp, 4, 2));
597 put_hcfa($lino, 4, 2, substr($tmp, 6, 2));
598 put_hcfa($lino, 7, 2, substr($tmp, 2, 2));
599 put_hcfa($lino, 10, 2, substr($tmp, 4, 2));
600 put_hcfa($lino, 13, 2, substr($tmp, 6, 2));
601 put_hcfa($lino, 16, 2, substr($tmp, 2, 2));
603 // 24b. Place of Service
604 put_hcfa($lino, 19, 2, $claim->facilityPOS());
606 // 24c. EMG
607 // Not currently supported.
609 // 24d. Procedures, Services or Supplies
610 put_hcfa($lino, 25, 7, $claim->cptCode($hcfa_proc_index));
611 // replace colon with space for printing
612 put_hcfa($lino, 33, 12, str_replace(':', ' ', $claim->cptModifier($hcfa_proc_index)));
614 // 24e. Diagnosis Pointer
615 $tmp = '';
616 foreach ($claim->diagIndexArray($hcfa_proc_index) as $value) {
617 if (hcfa_1500_version_02_12()) {// For 02/12 Version convert number to letter.
618 // ASCII A is 65, since diagIndexArray is ones based, this will make 1->A, 2->B...
619 $value=chr($value+64);
622 $tmp .= $value;
625 put_hcfa($lino, 45, 4, $tmp);
627 // 24f. Charges
628 put_hcfa($lino, 50, 8, str_replace(
629 '.',
630 ' ',
631 sprintf('%8.2f', $claim->cptCharges($hcfa_proc_index))
634 // 24g. Days or Units
635 put_hcfa($lino, 59, 3, $claim->cptUnits($hcfa_proc_index));
637 // 24h. EPSDT Family Plan
639 if ($claim->epsdtFlag()) {
640 put_hcfa($lino, 63, 2, '03');
643 // 24j. Rendering Provider NPI
644 put_hcfa($lino, 68, 10, $claim->providerNPI($hcfa_proc_index));
647 // 25. Federal Tax ID Number
648 // FrreB hard coded EIN. Changed it to included SSN as well.
649 put_hcfa(56, 1, 15, $claim->billingFacilityETIN());
650 if ($claim->federalIdType()=='SY') {
651 put_hcfa(56, 17, 1, 'X'); // The SSN checkbox
652 } else {
653 put_hcfa(56, 19, 1, 'X'); // The EIN checkbox
656 // 26. Patient's Account No.
657 // Instructions say hyphens are not allowed.
658 put_hcfa(56, 23, 15, "$pid-$encounter");
660 // 27. Accept Assignment
661 put_hcfa(56, $claim->billingFacilityAssignment() ? 38 : 43, 1, 'X');
663 // 28. Total Charge
664 put_hcfa(56, 52, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_total_charges)));
665 if (!$clm_total_charges) {
666 $log .= "*** This claim has no charges!\n";
669 // 29. Amount Paid
670 put_hcfa(56, 62, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_amount_paid)));
672 // 30. Balance Due
673 // For secondary payers this reflects primary "contracted rate" adjustments,
674 // so in general box 30 will not equal box 28 minus box 29.
675 if (!hcfa_1500_version_02_12()) { // Box 30 Reserved for NUCC Use in 02/12
676 put_hcfa(56, 71, 8, str_replace('.', ' ', sprintf(
677 '%8.2f',
678 $clm_total_charges - $clm_amount_paid - $clm_amount_adjusted
679 )));
682 // 33. Billing Provider: Phone Number
683 $tmp = $claim->billingContactPhone();
684 put_hcfa(57, 66, 3, substr($tmp, 0, 3));
685 put_hcfa(57, 70, 3, substr($tmp, 3)); // slight adjustment for better look smw 030315
686 put_hcfa(57, 73, 1, '-');
687 put_hcfa(57, 74, 4, substr($tmp, 6));
689 // 32. Service Facility Location Information: Name
690 put_hcfa(58, 23, 25, $claim->facilityName());
692 // 33. Billing Provider: Name
693 if ($claim->federalIdType()=='SY') {
694 $tempName = $claim->billingFacilityName();
695 $partsName = explode(' ', $tempName);// entity == person
696 $num_parts = count($partsName);
697 switch ($num_parts) {
698 case "2":
699 $firstName = $partsName[0];
700 $lastName = $partsName[1];
701 $billingProviderName = $lastName . ", " . $firstName;
702 break;
703 case "3":
704 $firstName = $partsName[0];
705 $middleName = $partsName[1];
706 $lastName = $partsName[2];
707 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName;
708 break;
709 case "4":
710 $firstName = $partsName[0];
711 $middleName = $partsName[1];
712 $lastName = $partsName[2];
713 $suffixName = $partsName[3];
714 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
715 break;
716 default:
717 $log .= "*** individual name has more than 4 parts and may not be desirable on the claim form\n";
718 $firstName = $partsName[0];
719 $middleName = $partsName[1];
720 $lastName = $partsName[2];
721 $suffixName = $partsName[3];
722 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
725 put_hcfa(58, 50, 25, $billingProviderName);
726 } else {
727 put_hcfa(58, 50, 25, $claim->billingFacilityName());
730 // 32. Service Facility Location Information: Street
731 put_hcfa(59, 23, 25, $claim->facilityStreet());
733 // 33. Billing Provider: Name
734 put_hcfa(59, 50, 25, $claim->billingFacilityStreet());
736 // 31. Signature of Physician or Supplier
738 if ($GLOBALS['cms_1500_box_31_format']==0) {
739 put_hcfa(60, 1, 20, 'Signature on File');
740 } else if ($GLOBALS['cms_1500_box_31_format']==1) {
741 put_hcfa(60, 1, 22, $claim->providerFirstName()." ".$claim->providerLastName());
745 // $tmp = $claim->providerFirstName();
746 // if ($claim->providerMiddleName()) $tmp .= ' ' . substr($claim->providerMiddleName(),0,1);
747 // put_hcfa(60, 1, 20, $tmp . ' ' . $claim->providerLastName());
749 // 32. Service Facility Location Information: City State Zip
750 $tmp = $claim->facilityCity() ? ($claim->facilityCity() . ' ') : '';
751 put_hcfa(60, 23, 27, $tmp . $claim->facilityState() . ' ' .
752 $claim->facilityZip());
754 // 33. Billing Provider: City State Zip
755 $tmp = $claim->billingFacilityCity() ? ($claim->billingFacilityCity() . ' ') : '';
756 put_hcfa(60, 50, 27, $tmp . $claim->billingFacilityState() . ' ' .
757 $claim->billingFacilityZip());
759 // 31. Signature of Physician or Supplier: Date
760 if ($GLOBALS['cms_1500_box_31_date']>0) {
761 if ($GLOBALS['cms_1500_box_31_date']==1) {
762 $date_of_service= $claim->serviceDate();
763 $MDY=substr($date_of_service, 4, 2)." ".substr($date_of_service, 6, 2)." ".substr($date_of_service, 2, 2);
764 } else if ($GLOBALS['cms_1500_box_31_date']==2) {
765 $MDY=date("m/d/y");
768 put_hcfa(61, 6, 10, $MDY);
771 // 32a. Service Facility NPI
772 put_hcfa(61, 23, 10, $claim->facilityNPI());
774 // 32b. Service Facility Other ID
775 // Note that Medicare does NOT want this any more.
776 if ($claim->providerGroupNumber()) {
777 put_hcfa(61, 36, 2, $claim->providerNumberType());
778 put_hcfa(61, 38, 11, $claim->providerGroupNumber());
781 // 33a. Billing Facility NPI
782 put_hcfa(61, 50, 10, $claim->billingFacilityNPI());
784 // 33b. Billing Facility Other ID
785 // Note that Medicare does NOT want this any more.
786 if ($claim->claimType() == 'MC') {
787 put_hcfa(61, 63, 2, 'ZZ');
788 put_hcfa(61, 65, 14, $claim->providerTaxonomy());
789 } elseif ($claim->providerGroupNumber() && $claim->claimType() != 'MB') {
790 put_hcfa(61, 63, 2, $claim->providerNumberType());
791 put_hcfa(61, 65, 14, $claim->providerGroupNumber());
794 // Put an extra line here for compatibility with old hcfa text generated form
795 put_hcfa(62, 1, 1, ' ');
796 // put a couple more in so that multiple claims correctly print through the text file download
797 put_hcfa(63, 1, 1, ' ');
798 put_hcfa(64, 1, 1, ' ');
799 return;