php7 bug fix 7
[openemr.git] / library / gen_hcfa_1500.inc.php
blob7a9a2c9e71b1c8e4280f2a2dae29a29136fc9c49
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.freeb_type.
101 // Previous version of this code called claimType() which maps freeb_type 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 11d. Is There Another Health Benefit Plan
317 if (!$new_medicare_logic) {
318 put_hcfa(26, $claim->payerCount() > 1 ? 52 : 57, 1, 'X');
321 // Box 12. Patient's or Authorized Person's Signature
322 put_hcfa(29, 7, 17, 'Signature on File');
323 // Note: Date does not apply unless the person physically signs the form.
325 // Box 13. Insured's or Authorized Person's Signature
326 put_hcfa(29, 55, 17, 'Signature on File');
328 // Box 14. Date of Current Illness/Injury/Pregnancy
329 $tmp = $claim->onsetDate();
330 put_hcfa(32, 2, 2, substr($tmp,4,2));
331 put_hcfa(32, 5, 2, substr($tmp,6,2));
332 put_hcfa(32, 8, 4, substr($tmp,0,4));
334 if(hcfa_1500_version_02_12() && !empty($tmp))
336 // Only include the Box 14 qualifier if there we are using version 02/12 and there is a Box 14 date.
337 put_hcfa(32, 16, 3, $claim->box14qualifier());
340 // Box 15. First Date of Same or Similar Illness, if applicable
341 $tmp = $claim->dateInitialTreatment();
342 if(hcfa_1500_version_02_12() && !empty($tmp))
344 // Only include the Box 15 qualifier if there we are using version 02/12 and there is a Box 15 date.
345 put_hcfa(32, 31, 3, $claim->box15qualifier());
349 put_hcfa(32,37, 2, substr($tmp,4,2));
350 put_hcfa(32,40, 2, substr($tmp,6,2));
351 put_hcfa(32,43, 4, substr($tmp,0,4));
354 // Box 16. Dates Patient Unable to Work in Current Occupation
355 if ($claim->isUnableToWork()) {
356 $tmp = $claim->offWorkFrom();
357 put_hcfa(32, 54, 2, substr($tmp,4,2));
358 put_hcfa(32, 57, 2, substr($tmp,6,2));
359 put_hcfa(32, 60, 4, substr($tmp,0,4));
360 $tmp = $claim->offWorkTo();
361 put_hcfa(32, 68, 2, substr($tmp,4,2));
362 put_hcfa(32, 71, 2, substr($tmp,6,2));
363 put_hcfa(32, 74, 4, substr($tmp,0,4));
366 // Referring provider stuff. Reports are that for primary care doctors,
367 // Medicare forbids an entry here and other payers require one.
368 // There is still confusion over this.
370 if ($claim->referrerLastName() &&
371 (empty($GLOBALS['MedicareReferrerIsRenderer']) || $claim->claimType() != 'MB'))
373 // Box 17a. Referring Provider Alternate Identifier
374 // Commented this out because UPINs are obsolete, leaving the code as an
375 // example in case some other identifier needs to be supported.
376 /*****************************************************************
377 if ($claim->referrerUPIN() && $claim->claimType() != 'MB') {
378 put_hcfa(33, 30, 2, '1G');
379 put_hcfa(33, 33, 15, $claim->referrerUPIN());
381 *****************************************************************/
383 // Box 17. Name of Referring Provider or Other Source
384 $tmp = $claim->referrerLastName() . ', ' . $claim->referrerFirstName();
385 if ($claim->referrerMiddleName())
386 $tmp .= ', ' . substr($claim->referrerMiddleName(),0,1);
387 put_hcfa(34, 1, 25, $tmp);
389 // Box 17b. Referring Provider NPI
390 if ($claim->referrerNPI()) {
391 put_hcfa(34, 33, 15, $claim->referrerNPI());
395 // Box 18. Hospitalization Dates Related to Current Services
396 if ($claim->isHospitalized()) {
397 $tmp = $claim->hospitalizedFrom();
398 put_hcfa(34, 54, 2, substr($tmp,4,2));
399 put_hcfa(34, 57, 2, substr($tmp,6,2));
400 put_hcfa(34, 60, 4, substr($tmp,0,4));
401 $tmp = $claim->hospitalizedTo();
402 put_hcfa(34, 68, 2, substr($tmp,4,2));
403 put_hcfa(34, 71, 2, substr($tmp,6,2));
404 put_hcfa(34, 74, 4, substr($tmp,0,4));
407 // Box 19. Reserved for Local Use
408 put_hcfa(36, 1, 48, $claim->additionalNotes());
410 // Box 20. Outside Lab
411 put_hcfa(36, $claim->isOutsideLab() ? 52 : 57, 1, 'X');
412 if ($claim->isOutsideLab()) {
413 // Note here that put_hcfa strips the decimal point, as required.
414 // We right-justify this amount (ending in col. 69).
415 put_hcfa(36, 63, 8, sprintf('%8s', $claim->outsideLabAmount()));
418 if(hcfa_1500_version_02_12())
420 process_diagnoses_02_12($claim,$log);
422 else
424 // Box 21. Diagnoses
425 $tmp = $claim->diagArray();
426 $diags = array();
427 foreach ($tmp as $diag) $diags[] = $diag;
428 if (!empty($diags[0])) {
429 put_hcfa(38, 3, 3, substr($diags[0], 0, 3));
430 put_hcfa(38, 7, 2, substr($diags[0], 3));
432 if (!empty($diags[2])) {
433 put_hcfa(38, 30, 3, substr($diags[2], 0, 3));
434 put_hcfa(38, 34, 2, substr($diags[2], 3));
437 // Box 22. Medicaid Resubmission Code and Original Ref. No.
438 put_hcfa(38, 50, 10, $claim->medicaidResubmissionCode());
439 put_hcfa(38, 62, 10, $claim->medicaidOriginalReference());
441 // Box 21 continued. Diagnoses
442 if (!empty($diags[1])) {
443 put_hcfa(40, 3, 3, substr($diags[1], 0, 3));
444 put_hcfa(40, 7, 2, substr($diags[1], 3));
446 if (!empty($diags[3])) {
447 put_hcfa(40, 30, 3, substr($diags[3], 0, 3));
448 put_hcfa(40, 34, 2, substr($diags[3], 3));
451 // Box 23. Prior Authorization Number
452 put_hcfa(40, 50, 28, $claim->priorAuth());
454 $proccount = $claim->procCount(); // number of procedures
456 // Charges, adjustments and payments are accumulated by line item so that
457 // each page of a multi-page claim will stand alone. Payments include the
458 // co-pay for the first page only.
459 $clm_total_charges = 0;
460 $clm_amount_adjusted = 0;
461 $clm_amount_paid = $hcfa_proc_index ? 0 : $claim->patientPaidAmount();
463 // Procedure loop starts here.
465 for ($svccount = 0; $svccount < 6 && $hcfa_proc_index < $proccount; ++$hcfa_proc_index) {
466 $dia = $claim->diagIndexArray($hcfa_proc_index);
468 if (!$claim->cptCharges($hcfa_proc_index)) {
469 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
470 "' has no charges!\n";
473 if (empty($dia)) {
474 $log .= "*** Procedure '" . $claim->cptKey($hcfa_proc_index) .
475 "' is not justified!\n";
478 $clm_total_charges += $claim->cptCharges($hcfa_proc_index);
480 // Compute prior payments and "hard" adjustments.
481 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
482 if ($claim->payerSequence($ins) > $claim->payerSequence())
483 continue; // skip future payers
484 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($hcfa_proc_index));
485 $clm_amount_paid += $payerpaid[1];
486 $clm_amount_adjusted += $payerpaid[2];
489 ++$svccount;
490 $lino = $svccount * 2 + 41;
492 // Drug Information. Medicaid insurers want this with HCPCS codes.
494 $ndc = $claim->cptNDCID($hcfa_proc_index);
495 if ($ndc) {
496 if (preg_match('/^(\d\d\d\d\d)-(\d\d\d\d)-(\d\d)$/', $ndc, $tmp)) {
497 $ndc = $tmp[1] . $tmp[2] . $tmp[3];
499 else if(preg_match('/^\d{11}$/', $ndc)){
502 else {
503 $log .= "*** NDC code '$ndc' has invalid format!\n";
505 put_hcfa($lino, 1, 50, "N4$ndc " . $claim->cptNDCUOM($hcfa_proc_index) .
506 $claim->cptNDCQuantity($hcfa_proc_index));
509 //Note Codes.
510 put_hcfa($lino, 25, 7, $claim->cptNotecodes($hcfa_proc_index));
512 // 24i and 24j Top. ID Qualifier and Rendering Provider ID
513 if ($claim->supervisorNumber()) {
514 // If there is a supervising provider and that person has a
515 // payer-specific provider number, then we assume that the SP
516 // must be identified on the claim and this is how we do it
517 // (but the NPI of the actual rendering provider appears below).
518 // BCBS of TN indicated they want it this way. YMMV. -- Rod
519 put_hcfa($lino, 65, 2, $claim->supervisorNumberType());
520 put_hcfa($lino, 68, 10, $claim->supervisorNumber());
522 else if ($claim->providerNumber($hcfa_proc_index)) {
523 put_hcfa($lino, 65, 2, $claim->providerNumberType($hcfa_proc_index));
524 put_hcfa($lino, 68, 10, $claim->providerNumber($hcfa_proc_index));
527 ++$lino;
529 // 24a. Date of Service
530 $tmp = $claim->serviceDate();
531 put_hcfa($lino, 1, 2, substr($tmp,4,2));
532 put_hcfa($lino, 4, 2, substr($tmp,6,2));
533 put_hcfa($lino, 7, 2, substr($tmp,2,2));
534 put_hcfa($lino,10, 2, substr($tmp,4,2));
535 put_hcfa($lino,13, 2, substr($tmp,6,2));
536 put_hcfa($lino,16, 2, substr($tmp,2,2));
538 // 24b. Place of Service
539 put_hcfa($lino, 19, 2, $claim->facilityPOS());
541 // 24c. EMG
542 // Not currently supported.
544 // 24d. Procedures, Services or Supplies
545 put_hcfa($lino, 25, 7, $claim->cptCode($hcfa_proc_index));
546 // replace colon with space for printing
547 put_hcfa($lino, 33, 12, str_replace(':', ' ', $claim->cptModifier($hcfa_proc_index)));
549 // 24e. Diagnosis Pointer
550 $tmp = '';
551 foreach ($claim->diagIndexArray($hcfa_proc_index) as $value)
553 if(hcfa_1500_version_02_12())// For 02/12 Version convert number to letter.
555 // ASCII A is 65, since diagIndexArray is ones based, this will make 1->A, 2->B...
556 $value=chr($value+64);
558 $tmp .= $value;
560 put_hcfa($lino, 45, 4, $tmp);
562 // 24f. Charges
563 put_hcfa($lino, 50, 8, str_replace('.', ' ',
564 sprintf('%8.2f', $claim->cptCharges($hcfa_proc_index))));
566 // 24g. Days or Units
567 put_hcfa($lino, 59, 3, $claim->cptUnits($hcfa_proc_index));
569 // 24h. EPSDT Family Plan
570 // Not currently supported.
572 // 24j. Rendering Provider NPI
573 put_hcfa($lino, 68, 10, $claim->providerNPI($hcfa_proc_index));
576 // 25. Federal Tax ID Number
577 // FrreB hard coded EIN. Changed it to included SSN as well.
578 put_hcfa(56, 1, 15, $claim->billingFacilityETIN());
579 if($claim->federalIdType()=='SY'){
580 put_hcfa(56, 17, 1, 'X'); // The SSN checkbox
582 else{
583 put_hcfa(56, 19, 1, 'X'); // The EIN checkbox
586 // 26. Patient's Account No.
587 // Instructions say hyphens are not allowed, but freeb used them.
588 put_hcfa(56, 23, 15, "$pid-$encounter");
590 // 27. Accept Assignment
591 put_hcfa(56, $claim->billingFacilityAssignment() ? 38 : 43, 1, 'X');
593 // 28. Total Charge
594 put_hcfa(56, 52, 8, str_replace('.',' ',sprintf('%8.2f',$clm_total_charges)));
595 if (!$clm_total_charges) {
596 $log .= "*** This claim has no charges!\n";
599 // 29. Amount Paid
600 put_hcfa(56, 62, 8, str_replace('.',' ',sprintf('%8.2f',$clm_amount_paid)));
602 // 30. Balance Due
603 // For secondary payers this reflects primary "contracted rate" adjustments,
604 // so in general box 30 will not equal box 28 minus box 29.
605 if(!hcfa_1500_version_02_12()) // Box 30 Reserved for NUCC Use in 02/12
607 put_hcfa(56, 71, 8, str_replace('.',' ',sprintf('%8.2f',
608 $clm_total_charges - $clm_amount_paid - $clm_amount_adjusted)));
611 // 33. Billing Provider: Phone Number
612 $tmp = $claim->billingContactPhone();
613 put_hcfa(57, 66, 3, substr($tmp,0,3));
614 put_hcfa(57, 70, 3, substr($tmp,3)); // slight adjustment for better look smw 030315
615 put_hcfa(57, 73, 1, '-');
616 put_hcfa(57, 74, 4, substr($tmp,6));
618 // 32. Service Facility Location Information: Name
619 put_hcfa(58, 23, 25, $claim->facilityName());
621 // 33. Billing Provider: Name
622 if($claim->federalIdType()=='SY'){
623 $tempName = $claim->billingFacilityName();
624 $partsName = explode(' ', $tempName);// entity == person
625 $num_parts = count($partsName);
626 switch ($num_parts) {
627 case "2":
628 $firstName = $partsName[0];
629 $lastName = $partsName[1];
630 $billingProviderName = $lastName . ", " . $firstName;
631 break;
632 case "3":
633 $firstName = $partsName[0];
634 $middleName = $partsName[1];
635 $lastName = $partsName[2];
636 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName;
637 break;
638 case "4":
639 $firstName = $partsName[0];
640 $middleName = $partsName[1];
641 $lastName = $partsName[2];
642 $suffixName = $partsName[3];
643 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
644 break;
645 default:
646 $log .= "*** individual name has more than 4 parts and may not be desirable on the claim form\n";
647 $firstName = $partsName[0];
648 $middleName = $partsName[1];
649 $lastName = $partsName[2];
650 $suffixName = $partsName[3];
651 $billingProviderName = $lastName . ", " . $firstName. ", " . $middleName. ", " . $suffixName;
653 put_hcfa(58, 50, 25, $billingProviderName);
655 else {
656 put_hcfa(58, 50, 25, $claim->billingFacilityName());
658 // 32. Service Facility Location Information: Street
659 put_hcfa(59, 23, 25, $claim->facilityStreet());
661 // 33. Billing Provider: Name
662 put_hcfa(59, 50, 25, $claim->billingFacilityStreet());
664 // 31. Signature of Physician or Supplier
665 // FreeB printed the rendering provider's name and the current date here,
666 // but according to my instructions it must be a real signature and date,
667 // or else "Signature on File" or "SOF".
669 if($GLOBALS['cms_1500_box_31_format']==0)
671 put_hcfa(60, 1, 20, 'Signature on File');
673 else if($GLOBALS['cms_1500_box_31_format']==1)
675 put_hcfa(60, 1, 22, $claim->providerFirstName()." ".$claim->providerLastName());
678 // $tmp = $claim->providerFirstName();
679 // if ($claim->providerMiddleName()) $tmp .= ' ' . substr($claim->providerMiddleName(),0,1);
680 // put_hcfa(60, 1, 20, $tmp . ' ' . $claim->providerLastName());
682 // 32. Service Facility Location Information: City State Zip
683 $tmp = $claim->facilityCity() ? ($claim->facilityCity() . ' ') : '';
684 put_hcfa(60, 23, 27, $tmp . $claim->facilityState() . ' ' .
685 $claim->facilityZip());
687 // 33. Billing Provider: City State Zip
688 $tmp = $claim->billingFacilityCity() ? ($claim->billingFacilityCity() . ' ') : '';
689 put_hcfa(60, 50, 27, $tmp . $claim->billingFacilityState() . ' ' .
690 $claim->billingFacilityZip());
692 // 31. Signature of Physician or Supplier: Date
693 if($GLOBALS['cms_1500_box_31_date']>0)
695 if($GLOBALS['cms_1500_box_31_date']==1)
697 $date_of_service= $claim->serviceDate();
698 $MDY=substr($date_of_service,4,2)." ".substr($date_of_service,6,2)." ".substr($date_of_service,2,2);
700 else if($GLOBALS['cms_1500_box_31_date']==2)
702 $MDY=date("m/d/y");
704 put_hcfa(61,6,10,$MDY);
707 // 32a. Service Facility NPI
708 put_hcfa(61, 23, 10, $claim->facilityNPI());
710 // 32b. Service Facility Other ID
711 // Note that Medicare does NOT want this any more.
712 if ($claim->providerGroupNumber()) {
713 put_hcfa(61, 36, 2, $claim->providerNumberType());
714 put_hcfa(61, 38, 11, $claim->providerGroupNumber());
717 // 33a. Billing Facility NPI
718 put_hcfa(61, 50, 10, $claim->billingFacilityNPI());
720 // 33b. Billing Facility Other ID
721 // Note that Medicare does NOT want this any more.
722 if ($claim->providerGroupNumber() && $claim->claimType() != 'MB') {
723 put_hcfa(61, 63, 2, $claim->providerNumberType());
724 put_hcfa(61, 65, 14, $claim->providerGroupNumber());
727 // Put an extra line here for compatibility with old hcfa text generated form
728 put_hcfa(62, 1, 1, ' ');
729 // put a couple more in so that multiple claims correctly print through the text file download
730 put_hcfa(63, 1, 1, ' ');
731 put_hcfa(64, 1, 1, ' ');
732 return;