minor fix in hover for prior commit
[openemr.git] / library / gen_hcfa_1500.inc.php
blob1a98c3d9579e77d9ed4168c923e73abc48f7d908
1 <?php
2 // Copyright (C) 2008-2011 Rod Roark <rod@sunsetsystems.com>
3 //
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("Claim.class.php");
10 require_once("gen_hcfa_1500_02_12.inc.php");
12 $hcfa_curr_line = 1;
13 $hcfa_curr_col = 1;
14 $hcfa_data = '';
15 $hcfa_proc_index = 0;
18 /**
19 * take the data element and place it at the correct coordinates on the page
21 * @global int $hcfa_curr_line
22 * @global type $hcfa_curr_col
23 * @global type $hcfa_data
24 * @param type $line
25 * @param type $col
26 * @param type $maxlen
27 * @param type $data
28 * @param type $strip regular expression for what to strip from the data. period and has are the defaults
29 * 02/12 version needs to include periods in the diagnoses hence the need to override
31 function put_hcfa($line, $col, $maxlen, $data,$strip='/[.#]/') {
32 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data;
33 if ($line < $hcfa_curr_line)
34 die("Data item at ($line, $col) precedes current line.");
35 while ($hcfa_curr_line < $line) {
36 $hcfa_data .= "\n";
37 ++$hcfa_curr_line;
38 $hcfa_curr_col = 1;
40 if ($col < $hcfa_curr_col)
41 die("Data item at ($line, $col) precedes current column.");
42 while ($hcfa_curr_col < $col) {
43 $hcfa_data .= " ";
44 ++$hcfa_curr_col;
46 $data = preg_replace($strip, '', strtoupper($data));
47 $len = min(strlen($data), $maxlen);
48 $hcfa_data .= substr($data, 0, $len);
49 $hcfa_curr_col += $len;
52 function gen_hcfa_1500($pid, $encounter, &$log) {
53 global $hcfa_data, $hcfa_proc_index;
55 $hcfa_data = '';
56 $hcfa_proc_index = 0;
58 $today = time();
59 $claim = new Claim($pid, $encounter);
61 $log .= "Generating HCFA claim $pid-$encounter for " .
62 $claim->patientFirstName() . ' ' .
63 $claim->patientMiddleName() . ' ' .
64 $claim->patientLastName() . ' on ' .
65 date('Y-m-d H:i', $today) . ".\n";
67 while ($hcfa_proc_index < $claim->procCount()) {
68 if ($hcfa_proc_index) $hcfa_data .= "\014"; // append form feed for new page
69 gen_hcfa_1500_page($pid, $encounter, $log, $claim);
72 $log .= "\n";
73 return $hcfa_data;
76 function gen_hcfa_1500_page($pid, $encounter, &$log, &$claim) {
77 global $hcfa_curr_line, $hcfa_curr_col, $hcfa_data, $hcfa_proc_index;
79 $hcfa_curr_line = 1;
80 $hcfa_curr_col = 1;
82 // According to:
83 // http://www.ngsmedicare.com/NGSMedicare/PartB/EducationandSupport/ToolsandMaterials/CMS_ClaimFormInst.aspx
84 // Medicare interprets sections 9 and 11 of the claim form in its own
85 // special way. This flag tells us to do that. However I'm not 100%
86 // sure that it applies nationwide, and if you find that it is not right
87 // for you then set it to false. -- Rod 2009-03-26
88 $new_medicare_logic = $claim->claimType() == 'MB';
90 // Payer name, attn, street.
91 put_hcfa(2, 41, 31, $claim->payerName());
92 put_hcfa(3, 41, 31, $claim->payerAttn());
93 put_hcfa(4, 41, 31, $claim->payerStreet());
95 // Payer city, state, zip.
96 $tmp = $claim->payerCity() ? ($claim->payerCity() . ', ') : '';
97 put_hcfa(5, 41, 31, $tmp . $claim->payerState() . ' ' . $claim->payerZip());
99 // Box 1. Insurance Type
100 // claimTypeRaw() gets the integer value from insurance_companies.ins_type_code.
101 // Previous version of this code called claimType() which maps ins_type_code to
102 // a 2-character code and that was not specific enough.
103 $ct = $claim->claimTypeRaw();
104 $tmpcol = 45; // Other
105 if ($ct == 2) $tmpcol = 1; // Medicare
106 else if ($ct == 3) $tmpcol = 8; // Medicaid
107 else if ($ct == 5) $tmpcol = 15; // TriCare (formerly CHAMPUS)
108 else if ($ct == 4) $tmpcol = 24; // Champus VA
109 else if ($ct == 6) $tmpcol = 31; // Group Health Plan (only BCBS?)
110 else if ($ct == 7) $tmpcol = 39; // FECA
111 put_hcfa(8, $tmpcol, 1, 'X');
113 // Box 1a. Insured's ID Number
114 put_hcfa(8, 50, 17, $claim->policyNumber());
116 // Box 2. Patient's Name
117 $tmp = $claim->patientLastName() . ', ' . $claim->patientFirstName();
118 if ($claim->patientMiddleName())
119 $tmp .= ', ' . substr($claim->patientMiddleName(),0,1);
120 put_hcfa(10, 1, 28, $tmp);
122 // Box 3. Patient's Birth Date and Sex
123 $tmp = $claim->patientDOB();
124 put_hcfa(10, 31, 2, substr($tmp,4,2));
125 put_hcfa(10, 34, 2, substr($tmp,6,2));
126 put_hcfa(10, 37, 4, substr($tmp,0,4));
127 put_hcfa(10, $claim->patientSex() == 'M' ? 42 : 47, 1, 'X');
129 // Box 4. Insured's Name
130 $tmp = $claim->insuredLastName() . ', ' . $claim->insuredFirstName();
131 if ($claim->insuredMiddleName())
132 $tmp .= ', ' . substr($claim->insuredMiddleName(),0,1);
133 put_hcfa(10, 50, 28, $tmp);
135 // Box 5. Patient's Address
136 put_hcfa(12, 1, 28, $claim->patientStreet());
138 // Box 6. Patient Relationship to Insured
139 $tmp = $claim->insuredRelationship();
140 $tmpcol = 47; // Other
141 if ($tmp === '18') $tmpcol = 33; // self
142 else if ($tmp === '01') $tmpcol = 38; // spouse
143 else if ($tmp === '19') $tmpcol = 42; // child
144 put_hcfa(12, $tmpcol, 1, 'X');
146 // Box 7. Insured's Address
147 put_hcfa(12, 50, 28, $claim->insuredStreet());
149 // Box 5 continued. Patient's City and State
150 put_hcfa(14, 1, 20, $claim->patientCity());
151 put_hcfa(14, 26, 2, $claim->patientState());
153 // Box 8. Patient (Marital) Status
154 if(!hcfa_1500_version_02_12()) // Box 8 Reserved for NUCC Use in 02/12
156 $tmp = $claim->patientStatus();
157 $tmpcol = 47; // Other
158 if ($tmp === 'S') $tmpcol = 35; // Single
159 else if ($tmp === 'M') $tmpcol = 41; // Married
160 put_hcfa(14, $tmpcol, 1, 'X');
163 // Box 7 continued. Insured's City and State
164 put_hcfa(14, 50, 20, $claim->insuredCity());
165 put_hcfa(14, 74, 2, $claim->insuredState());
167 // Box 5 continued. Patient's Zip Code and Telephone
168 put_hcfa(16, 1, 10, $claim->patientZip());
169 $tmp = $claim->patientPhone();
170 put_hcfa(16, 15, 3, substr($tmp,0,3));
171 put_hcfa(16, 19, 7, substr($tmp,3));
173 // Box 8 continued. Patient (Employment) Status
174 if(!hcfa_1500_version_02_12()) // Box 8 Reserved for NUCC Use in 02/12
176 $tmp = $claim->patientOccupation();
177 if ($tmp === 'STUDENT' ) put_hcfa(16, 41, 1, 'X');
178 else if ($tmp === 'PT STUDENT') put_hcfa(16, 47, 1, 'X');
179 else if ($tmp !== 'UNEMPLOYED') put_hcfa(16, 35, 1, 'X');
182 // Box 7 continued. Insured's Zip Code and Telephone
183 put_hcfa(16, 50, 10, $claim->insuredZip());
184 $tmp = $claim->insuredPhone();
185 put_hcfa(16, 65, 3, substr($tmp,0,3));
186 put_hcfa(16, 69, 7, substr($tmp,3));
188 // Box 9. Other Insured's Name
189 if ($new_medicare_logic) {
190 // TBD: Medigap stuff? How do we know if this is a Medigap transfer?
192 else {
193 if ($claim->payerCount() > 1) {
194 $tmp = $claim->insuredLastName(1) . ', ' . $claim->insuredFirstName(1);
195 if ($claim->insuredMiddleName(1))
196 $tmp .= ', ' . substr($claim->insuredMiddleName(1),0,1);
197 put_hcfa(18, 1, 28, $tmp);
201 // Box 11. Insured's Group Number
202 if ($new_medicare_logic) {
203 // If this is Medicare secondary then we need the primary's policy number
204 // here, otherwise the word "NONE".
205 $tmp = $claim->payerSequence() == 'P' ? 'NONE' : $claim->policyNumber(1);
207 else {
208 $tmp = $claim->groupNumber();
210 put_hcfa(18, 50, 30, $tmp);
212 // Box 9a. Other Insured's Policy or Group Number
213 if ($new_medicare_logic) {
214 // TBD: Medigap stuff?
216 else {
217 if ($claim->payerCount() > 1) {
218 put_hcfa(20, 1, 28, $claim->policyNumber(1));
222 // Box 10a. Employment Related
223 put_hcfa(20, $claim->isRelatedEmployment() ? 35 : 41, 1, 'X');
225 // Box 11a. Insured's Birth Date and Sex
226 if ($new_medicare_logic) {
227 $tmpdob = $tmpsex = '';
228 if ($claim->payerSequence() != 'P') {
229 $tmpdob = $claim->insuredDOB(1);
230 $tmpsex = $claim->insuredSex(1);
233 else {
234 $tmpdob = $claim->insuredDOB();
235 $tmpsex = $claim->insuredSex();
237 if ($tmpdob) {
238 put_hcfa(20, 53, 2, substr($tmpdob,4,2));
239 put_hcfa(20, 56, 2, substr($tmpdob,6,2));
240 put_hcfa(20, 59, 4, substr($tmpdob,0,4));
242 if ($tmpsex) {
243 put_hcfa(20, $tmpsex == 'M' ? 68 : 75, 1, 'X');
246 // Box 9b. Other Insured's Birth Date and Sex
247 if(!hcfa_1500_version_02_12()) // Box 9b Reserved for NUCC Use in 02/12
249 if ($new_medicare_logic) {
250 // TBD: Medigap stuff?
252 else {
253 if ($claim->payerCount() > 1) {
254 $tmp = $claim->insuredDOB(1);
255 put_hcfa(22, 2, 2, substr($tmp,4,2));
256 put_hcfa(22, 5, 2, substr($tmp,6,2));
257 put_hcfa(22, 8, 4, substr($tmp,0,4));
258 put_hcfa(22, $claim->insuredSex(1) == 'M' ? 18 : 24, 1, 'X');
263 // Box 10b. Auto Accident
264 put_hcfa(22, $claim->isRelatedAuto() ? 35 : 41, 1, 'X');
265 if ($claim->isRelatedAuto())
266 put_hcfa(22, 45, 2, $claim->autoAccidentState());
268 // Box 11b. Insured's Employer/School Name
269 if ($new_medicare_logic) {
270 $tmp = $claim->payerSequence() == 'P' ? '' : $claim->groupName(1);
272 else {
273 $tmp = $claim->groupName();
275 put_hcfa(22, 50, 30, $tmp);
277 // Box 9c. Other Insured's Employer/School Name
278 if(!hcfa_1500_version_02_12()) // Box 9c Reserved for NUCC Use in 02/12
280 if ($new_medicare_logic) {
281 // TBD: Medigap stuff?
283 else {
284 if ($claim->payerCount() > 1) {
285 put_hcfa(24, 1, 28, $claim->groupName(1));
290 // Box 10c. Other Accident
291 put_hcfa(24, $claim->isRelatedOther() ? 35 : 41, 1, 'X');
293 // Box 11c. Insurance Plan Name or Program Name
294 if ($new_medicare_logic) {
295 $tmp = '';
296 if ($claim->payerSequence() != 'P') {
297 $tmp = $claim->planName(1);
298 if (!$tmp) $tmp = $claim->payerName(1);
301 else {
302 $tmp = $claim->planName();
304 put_hcfa(24, 50, 30, $tmp);
306 // Box 9d. Other Insurance Plan Name or Program Name
307 if ($new_medicare_logic) {
308 // TBD: Medigap stuff?
310 else {
311 if ($claim->payerCount() > 1) {
312 put_hcfa(26, 1, 28, $claim->planName(1));
316 # Box 10d. Claim Codes medicaid_referral_code
318 if($claim->epsdtFlag()) {
319 put_hcfa(26, 34, 2, $claim->medicaidReferralCode());
322 # Box 10d. Claim Codes medicaid_referral_code
324 if($claim->epsdtFlag()) {
325 put_hcfa(26, 34, 2, $claim->medicaidReferralCode());
328 // Box 11d. Is There Another Health Benefit Plan
329 if (!$new_medicare_logic) {
330 put_hcfa(26, $claim->payerCount() > 1 ? 52 : 57, 1, 'X');
333 // Box 12. Patient's or Authorized Person's Signature
334 put_hcfa(29, 7, 17, 'Signature on File');
335 // Note: Date does not apply unless the person physically signs the form.
337 // Box 13. Insured's or Authorized Person's Signature
338 put_hcfa(29, 55, 17, 'Signature on File');
340 // Box 14. Date of Current Illness/Injury/Pregnancy
341 $tmp = $claim->onsetDate();
342 put_hcfa(32, 2, 2, substr($tmp,4,2));
343 put_hcfa(32, 5, 2, substr($tmp,6,2));
344 put_hcfa(32, 8, 4, substr($tmp,0,4));
346 if(hcfa_1500_version_02_12() && !empty($tmp))
348 // Only include the Box 14 qualifier if there we are using version 02/12 and there is a Box 14 date.
349 put_hcfa(32, 16, 3, $claim->box14qualifier());
352 // Box 15. First Date of Same or Similar Illness, if applicable
353 $tmp = $claim->dateInitialTreatment();
354 if(hcfa_1500_version_02_12() && !empty($tmp))
356 // Only include the Box 15 qualifier if there we are using version 02/12 and there is a Box 15 date.
357 put_hcfa(32, 31, 3, $claim->box15qualifier());
361 put_hcfa(32,37, 2, substr($tmp,4,2));
362 put_hcfa(32,40, 2, substr($tmp,6,2));
363 put_hcfa(32,43, 4, substr($tmp,0,4));
366 // Box 16. Dates Patient Unable to Work in Current Occupation
367 if ($claim->isUnableToWork()) {
368 $tmp = $claim->offWorkFrom();
369 put_hcfa(32, 54, 2, substr($tmp,4,2));
370 put_hcfa(32, 57, 2, substr($tmp,6,2));
371 put_hcfa(32, 60, 4, substr($tmp,0,4));
372 $tmp = $claim->offWorkTo();
373 put_hcfa(32, 68, 2, substr($tmp,4,2));
374 put_hcfa(32, 71, 2, substr($tmp,6,2));
375 put_hcfa(32, 74, 4, substr($tmp,0,4));
378 // Referring provider stuff. Reports are that for primary care doctors,
379 // Medicare forbids an entry here and other payers require one.
380 // There is still confusion over this.
382 if ($claim->referrerLastName() || $claim->billingProviderLastName() &&
383 (empty($GLOBALS['MedicareReferrerIsRenderer']) || $claim->claimType() != 'MB'))
385 // Box 17a. Referring Provider Alternate Identifier
386 // Commented this out because UPINs are obsolete, leaving the code as an
387 // example in case some other identifier needs to be supported.
388 /*****************************************************************
389 if ($claim->referrerUPIN() && $claim->claimType() != 'MB') {
390 put_hcfa(33, 30, 2, '1G');
391 put_hcfa(33, 33, 15, $claim->referrerUPIN());
393 *****************************************************************/
394 if ($claim->claimType() == 'MC') {
395 put_hcfa(33, 30, 2, 'ZZ');
396 put_hcfa(33, 33, 14, $claim->referrerTaxonomy());
401 // 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
402 # Changed to look first at the misc hcfa billing form to complete this box if nothing on misc hcfa form use referrer
403 if (strlen($claim->billingProviderLastName()) !=0) {
404 $tmp2 = $claim->billingProviderLastName() . ', ' . $claim->billingProviderFirstName();
405 if ($claim->billingProviderMiddleName())
406 $tmp2 .= ', ' . substr($claim->billingProviderMiddleName(),0,1);
407 put_hcfa(34, 1, 3, $claim->billing_options['provider_qualifier_code']);
408 put_hcfa(34, 4, 25, $tmp2);
409 if ($claim->billingProviderNPI()) {
410 put_hcfa(34, 33, 15, $claim->billingProviderNPI());
413 else
415 $tmp = $claim->referrerLastName() . ', ' . $claim->referrerFirstName();
416 if ($claim->referrerMiddleName())
417 $tmp .= ', ' . substr($claim->referrerMiddleName(),0,1);
418 put_hcfa(34, 1, 3, 'DN');
419 put_hcfa(34, 4, 25, $tmp);
420 if ($claim->referrerNPI()) {
421 put_hcfa(34, 33, 15, $claim->referrerNPI());
426 // Box 18. Hospitalization Dates Related to Current Services
427 if ($claim->isHospitalized()) {
428 $tmp = $claim->hospitalizedFrom();
429 put_hcfa(34, 54, 2, substr($tmp,4,2));
430 put_hcfa(34, 57, 2, substr($tmp,6,2));
431 put_hcfa(34, 60, 4, substr($tmp,0,4));
432 $tmp = $claim->hospitalizedTo();
433 put_hcfa(34, 68, 2, substr($tmp,4,2));
434 put_hcfa(34, 71, 2, substr($tmp,6,2));
435 put_hcfa(34, 74, 4, substr($tmp,0,4));
438 // Box 19. Reserved for Local Use
439 put_hcfa(36, 1, 48, $claim->additionalNotes());
441 // Box 20. Outside Lab
442 put_hcfa(36, $claim->isOutsideLab() ? 52 : 57, 1, 'X');
443 if ($claim->isOutsideLab()) {
444 // Note here that put_hcfa strips the decimal point, as required.
445 // We right-justify this amount (ending in col. 69).
446 put_hcfa(36, 63, 8, sprintf('%8s', $claim->outsideLabAmount()));
449 if(hcfa_1500_version_02_12())
451 process_diagnoses_02_12($claim,$log);
453 else
455 // Box 21. Diagnoses
456 $tmp = $claim->diagArray();
457 $diags = array();
458 foreach ($tmp as $diag) $diags[] = $diag;
459 if (!empty($diags[0])) {
460 put_hcfa(38, 3, 3, substr($diags[0], 0, 3));
461 put_hcfa(38, 7, 2, substr($diags[0], 3));
463 if (!empty($diags[2])) {
464 put_hcfa(38, 30, 3, substr($diags[2], 0, 3));
465 put_hcfa(38, 34, 2, substr($diags[2], 3));
468 // Box 22. Medicaid Resubmission Code and Original Ref. No.
469 put_hcfa(38, 50, 10, $claim->medicaidResubmissionCode());
470 put_hcfa(38, 62, 15, $claim->medicaidOriginalReference());
472 // Box 21 continued. Diagnoses
473 if (!empty($diags[1])) {
474 put_hcfa(40, 3, 3, substr($diags[1], 0, 3));
475 put_hcfa(40, 7, 2, substr($diags[1], 3));
477 if (!empty($diags[3])) {
478 put_hcfa(40, 30, 3, substr($diags[3], 0, 3));
479 put_hcfa(40, 34, 2, substr($diags[3], 3));
482 // Box 23. Prior Authorization Number
483 put_hcfa(40, 50, 28, $claim->priorAuth());
485 $proccount = $claim->procCount(); // number of procedures
487 // Charges, adjustments and payments are accumulated by line item so that
488 // each page of a multi-page claim will stand alone. Payments include the
489 // co-pay for the first page only.
490 $clm_total_charges = 0;
491 $clm_amount_adjusted = 0;
492 $clm_amount_paid = $hcfa_proc_index ? 0 : $claim->patientPaidAmount();
494 // Procedure loop starts here.
496 for ($svccount = 0; $svccount < 6 && $hcfa_proc_index < $proccount; ++$hcfa_proc_index) {
497 $dia = $claim->diagIndexArray($hcfa_proc_index);
499 if (!$claim->cptCharges($hcfa_proc_index)) {
500 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
501 "' has no charges!\n";
504 if (empty($dia)) {
505 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
506 "' is not justified!\n";
509 $clm_total_charges += $claim->cptCharges($hcfa_proc_index);
511 // Compute prior payments and "hard" adjustments.
512 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
513 if ($claim->payerSequence($ins) > $claim->payerSequence())
514 continue; // skip future payers
515 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($hcfa_proc_index));
516 $clm_amount_paid += $payerpaid[1];
517 $clm_amount_adjusted += $payerpaid[2];
520 ++$svccount;
521 $lino = $svccount * 2 + 41;
523 // Drug Information. Medicaid insurers want this with HCPCS codes.
525 $ndc = $claim->cptNDCID($hcfa_proc_index);
526 if ($ndc) {
527 if (preg_match('/^(\d\d\d\d\d)-(\d\d\d\d)-(\d\d)$/', $ndc, $tmp)) {
528 $ndc = $tmp[1] . $tmp[2] . $tmp[3];
530 else if(preg_match('/^\d{11}$/', $ndc)){
533 else {
534 $log .= "*** NDC code '$ndc' has invalid format!\n";
536 put_hcfa($lino, 1, 50, "N4$ndc " . $claim->cptNDCUOM($hcfa_proc_index) .
537 $claim->cptNDCQuantity($hcfa_proc_index));
540 //Note Codes.
541 put_hcfa($lino, 25, 7, $claim->cptNotecodes($hcfa_proc_index));
543 // 24i and 24j Top. ID Qualifier and Rendering Provider ID
544 if ($claim->supervisorNumber()) {
545 // If there is a supervising provider and that person has a
546 // payer-specific provider number, then we assume that the SP
547 // must be identified on the claim and this is how we do it
548 // (but the NPI of the actual rendering provider appears below).
549 // BCBS of TN indicated they want it this way. YMMV. -- Rod
550 put_hcfa($lino, 65, 2, $claim->supervisorNumberType());
551 put_hcfa($lino, 68, 10, $claim->supervisorNumber());
553 else if ($claim->providerNumber($hcfa_proc_index)) {
554 put_hcfa($lino, 65, 2, $claim->providerNumberType($hcfa_proc_index));
555 put_hcfa($lino, 68, 10, $claim->providerNumber($hcfa_proc_index));
557 else if ($claim->claimType() == 'MC') {
558 put_hcfa($lino, 65, 2, 'ZZ');
559 put_hcfa($lino, 68, 14, $claim->providerTaxonomy());
562 ++$lino;
564 // 24a. Date of Service
565 $tmp = $claim->serviceDate();
566 put_hcfa($lino, 1, 2, substr($tmp,4,2));
567 put_hcfa($lino, 4, 2, substr($tmp,6,2));
568 put_hcfa($lino, 7, 2, substr($tmp,2,2));
569 put_hcfa($lino,10, 2, substr($tmp,4,2));
570 put_hcfa($lino,13, 2, substr($tmp,6,2));
571 put_hcfa($lino,16, 2, substr($tmp,2,2));
573 // 24b. Place of Service
574 put_hcfa($lino, 19, 2, $claim->facilityPOS());
576 // 24c. EMG
577 // Not currently supported.
579 // 24d. Procedures, Services or Supplies
580 put_hcfa($lino, 25, 7, $claim->cptCode($hcfa_proc_index));
581 // replace colon with space for printing
582 put_hcfa($lino, 33, 12, str_replace(':', ' ', $claim->cptModifier($hcfa_proc_index)));
584 // 24e. Diagnosis Pointer
585 $tmp = '';
586 foreach ($claim->diagIndexArray($hcfa_proc_index) as $value)
588 if(hcfa_1500_version_02_12())// For 02/12 Version convert number to letter.
590 // ASCII A is 65, since diagIndexArray is ones based, this will make 1->A, 2->B...
591 $value=chr($value+64);
593 $tmp .= $value;
595 put_hcfa($lino, 45, 4, $tmp);
597 // 24f. Charges
598 put_hcfa($lino, 50, 8, str_replace('.', ' ',
599 sprintf('%8.2f', $claim->cptCharges($hcfa_proc_index))));
601 // 24g. Days or Units
602 put_hcfa($lino, 59, 3, $claim->cptUnits($hcfa_proc_index));
604 // 24h. EPSDT Family Plan
606 if($claim->epsdtFlag()) {
607 put_hcfa($lino, 63, 2, '03');
610 // 24j. Rendering Provider NPI
611 put_hcfa($lino, 68, 10, $claim->providerNPI($hcfa_proc_index));
614 // 25. Federal Tax ID Number
615 // FrreB hard coded EIN. Changed it to included SSN as well.
616 put_hcfa(56, 1, 15, $claim->billingFacilityETIN());
617 if($claim->federalIdType()=='SY'){
618 put_hcfa(56, 17, 1, 'X'); // The SSN checkbox
620 else{
621 put_hcfa(56, 19, 1, 'X'); // The EIN checkbox
624 // 26. Patient's Account No.
625 // Instructions say hyphens are not allowed.
626 put_hcfa(56, 23, 15, "$pid-$encounter");
628 // 27. Accept Assignment
629 put_hcfa(56, $claim->billingFacilityAssignment() ? 38 : 43, 1, 'X');
631 // 28. Total Charge
632 put_hcfa(56, 52, 8, str_replace('.',' ',sprintf('%8.2f',$clm_total_charges)));
633 if (!$clm_total_charges) {
634 $log .= "*** This claim has no charges!\n";
637 // 29. Amount Paid
638 put_hcfa(56, 62, 8, str_replace('.',' ',sprintf('%8.2f',$clm_amount_paid)));
640 // 30. Balance Due
641 // For secondary payers this reflects primary "contracted rate" adjustments,
642 // so in general box 30 will not equal box 28 minus box 29.
643 if(!hcfa_1500_version_02_12()) // Box 30 Reserved for NUCC Use in 02/12
645 put_hcfa(56, 71, 8, str_replace('.',' ',sprintf('%8.2f',
646 $clm_total_charges - $clm_amount_paid - $clm_amount_adjusted)));
649 // 33. Billing Provider: Phone Number
650 $tmp = $claim->billingContactPhone();
651 put_hcfa(57, 66, 3, substr($tmp,0,3));
652 put_hcfa(57, 70, 3, substr($tmp,3)); // slight adjustment for better look smw 030315
653 put_hcfa(57, 73, 1, '-');
654 put_hcfa(57, 74, 4, substr($tmp,6));
656 // 32. Service Facility Location Information: Name
657 put_hcfa(58, 23, 25, $claim->facilityName());
659 // 33. Billing Provider: Name
660 if($claim->federalIdType()=='SY'){
661 $tempName = $claim->billingFacilityName();
662 $partsName = explode(' ', $tempName);// entity == person
663 $num_parts = count($partsName);
664 switch ($num_parts) {
665 case "2":
666 $firstName = $partsName[0];
667 $lastName = $partsName[1];
668 $billingProviderName = $lastName . ", " . $firstName;
669 break;
670 case "3":
671 $firstName = $partsName[0];
672 $middleName = $partsName[1];
673 $lastName = $partsName[2];
674 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName;
675 break;
676 case "4":
677 $firstName = $partsName[0];
678 $middleName = $partsName[1];
679 $lastName = $partsName[2];
680 $suffixName = $partsName[3];
681 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
682 break;
683 default:
684 $log .= "*** individual name has more than 4 parts and may not be desirable on the claim form\n";
685 $firstName = $partsName[0];
686 $middleName = $partsName[1];
687 $lastName = $partsName[2];
688 $suffixName = $partsName[3];
689 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
691 put_hcfa(58, 50, 25, $billingProviderName);
693 else {
694 put_hcfa(58, 50, 25, $claim->billingFacilityName());
696 // 32. Service Facility Location Information: Street
697 put_hcfa(59, 23, 25, $claim->facilityStreet());
699 // 33. Billing Provider: Name
700 put_hcfa(59, 50, 25, $claim->billingFacilityStreet());
702 // 31. Signature of Physician or Supplier
704 if($GLOBALS['cms_1500_box_31_format']==0)
706 put_hcfa(60, 1, 20, 'Signature on File');
708 else if($GLOBALS['cms_1500_box_31_format']==1)
710 put_hcfa(60, 1, 22, $claim->providerFirstName()." ".$claim->providerLastName());
713 // $tmp = $claim->providerFirstName();
714 // if ($claim->providerMiddleName()) $tmp .= ' ' . substr($claim->providerMiddleName(),0,1);
715 // put_hcfa(60, 1, 20, $tmp . ' ' . $claim->providerLastName());
717 // 32. Service Facility Location Information: City State Zip
718 $tmp = $claim->facilityCity() ? ($claim->facilityCity() . ' ') : '';
719 put_hcfa(60, 23, 27, $tmp . $claim->facilityState() . ' ' .
720 $claim->facilityZip());
722 // 33. Billing Provider: City State Zip
723 $tmp = $claim->billingFacilityCity() ? ($claim->billingFacilityCity() . ' ') : '';
724 put_hcfa(60, 50, 27, $tmp . $claim->billingFacilityState() . ' ' .
725 $claim->billingFacilityZip());
727 // 31. Signature of Physician or Supplier: Date
728 if($GLOBALS['cms_1500_box_31_date']>0)
730 if($GLOBALS['cms_1500_box_31_date']==1)
732 $date_of_service= $claim->serviceDate();
733 $MDY=substr($date_of_service,4,2)." ".substr($date_of_service,6,2)." ".substr($date_of_service,2,2);
735 else if($GLOBALS['cms_1500_box_31_date']==2)
737 $MDY=date("m/d/y");
739 put_hcfa(61,6,10,$MDY);
742 // 32a. Service Facility NPI
743 put_hcfa(61, 23, 10, $claim->facilityNPI());
745 // 32b. Service Facility Other ID
746 // Note that Medicare does NOT want this any more.
747 if ($claim->providerGroupNumber()) {
748 put_hcfa(61, 36, 2, $claim->providerNumberType());
749 put_hcfa(61, 38, 11, $claim->providerGroupNumber());
752 // 33a. Billing Facility NPI
753 put_hcfa(61, 50, 10, $claim->billingFacilityNPI());
755 // 33b. Billing Facility Other ID
756 // Note that Medicare does NOT want this any more.
757 if ($claim->claimType() == 'MC') {
758 put_hcfa(61, 63, 2, 'ZZ');
759 put_hcfa(61, 65, 14, $claim->providerTaxonomy());
761 if ($claim->providerGroupNumber() && $claim->claimType() != 'MB') {
762 put_hcfa(61, 63, 2, $claim->providerNumberType());
763 put_hcfa(61, 65, 14, $claim->providerGroupNumber());
766 // Put an extra line here for compatibility with old hcfa text generated form
767 put_hcfa(62, 1, 1, ' ');
768 // put a couple more in so that multiple claims correctly print through the text file download
769 put_hcfa(63, 1, 1, ' ');
770 put_hcfa(64, 1, 1, ' ');
771 return;