Slimmed down version of class OemrUI - Take 4 (#2044)
[openemr.git] / library / billing / src / HCFA_1500.php
blob81c3985d8bbc84f2714bebe84bff0395cbd5dcea
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) 2018 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 namespace OpenEMR\Billing;
16 require_once(dirname(__FILE__) . "/../../invoice_summary.inc.php");
18 use OpenEMR\Billing\Claim;
19 use OpenEMR\Billing\HCFA_Info;
21 class HCFA_1500
23 protected $hcfa_curr_line;
24 protected $hcfa_curr_col;
25 protected $hcfa_data;
26 protected $hcfa_proc_index;
27 /**
28 * HCFA_1500 constructor.
29 * @param int $hcfa_curr_line
30 * @param int $hcfa_curr_col
31 * @param type $hcfa_data
32 * @param int $hcfa_proc_index
34 public function __construct()
36 $this->hcfa_curr_line = 1;
37 $this->hcfa_curr_col = 1;
38 $this->hcfa_data = '';
39 $this->hcfa_proc_index = 0;
42 /**
43 * take the data element and place it at the correct coordinates on the page
45 * @global int $hcfa_curr_line
46 * @global int $hcfa_curr_col
47 * @global type $hcfa_data
48 * @param type $line
49 * @param type $col
50 * @param type $maxlen
51 * @param type $data
52 * @param type $strip regular expression for what to strip from the data. period and has are the defaults
53 * 02/12 version needs to include periods in the diagnoses hence the need to override
55 private function put_hcfa($line, $col, $maxlen, $data, $strip = '/[.#]/')
57 if ($line < $this->hcfa_curr_line) {
58 die("Data item at ($line, $col) precedes current line $this->hcfa_curr_line and $data");
61 while ($this->hcfa_curr_line < $line) {
62 $this->hcfa_data .= "\n";
63 ++$this->hcfa_curr_line;
64 $this->hcfa_curr_col = 1;
67 if ($col < $this->hcfa_curr_col) {
68 die("Data item at ($line, $col) precedes current column.");
71 while ($this->hcfa_curr_col < $col) {
72 $this->hcfa_data .= " ";
73 ++$this->hcfa_curr_col;
76 $data = preg_replace($strip, '', strtoupper($data));
77 $len = min(strlen($data), $maxlen);
78 $this->hcfa_data .= substr($data, 0, $len);
79 $this->hcfa_curr_col += $len;
82 /**
83 * Process the diagnoses for a given claim. log any errors
85 * @param type $claim
86 * @param string $log
88 private function process_diagnoses_02_12($claim, &$log)
90 $hcfa_entries=array();
91 $diags = $claim->diagArray(false);
92 if ($claim->diagtype=='ICD10') {
93 $icd_indicator='0';
94 } else {
95 $icd_indicator='9';
98 $hcfa_entries[]=new HCFA_Info(37, 42, 1, $icd_indicator);
100 // Box 22. Medicaid Resubmission Code and Original Ref. No.
101 $hcfa_entries[]=new HCFA_Info(38, 50, 10, $claim->medicaidResubmissionCode());
102 $hcfa_entries[]=new HCFA_Info(38, 62, 15, $claim->medicaidOriginalReference());
104 // Box 23. Prior Authorization Number
105 $hcfa_entries[]=new HCFA_Info(40, 50, 28, $claim->priorAuth());
107 $diag_count=0;
108 foreach ($diags as $diag) {
109 if ($diag_count<12) {
110 $this->add_diagnosis($hcfa_entries, $diag_count, $diag);
111 } else {
112 $log.= "***Too many diagnoses ".($diag_count+1).":".$diag;
115 $diag_count++;
117 // Sort the entries to put them in the page base sequence.
118 usort($hcfa_entries, array('OpenEMR\Billing\HCFA_Info', 'cmp_hcfa_info'));
120 foreach ($hcfa_entries as $hcfa_entry) {
121 $this->put_hcfa($hcfa_entry->getRow(), $hcfa_entry->getColumn(), $hcfa_entry->getWidth(), $hcfa_entry->getInfo(), '/#/');
125 * calculate where on the form a given diagnosis belongs and add it to the entries
127 * @param array $hcfa_entries
128 * @param type $number
129 * @param type $diag
131 private function add_diagnosis(&$hcfa_entries, $number, $diag)
134 * The diagnoses go across the page.
135 * Positioned
136 * A B C D
137 * E F G H
138 * I J K L
140 $column_num = ($number%4);
141 $row_num = (int)($number / 4);
143 // First column is at location 3, each column is 13 wide
144 $col_pos=3+13*$column_num;
146 // First diagnosis row is 38
147 $strip='/[.#]/';
148 $diag = preg_replace($strip, '', strtoupper($diag));
149 $row_pos=38+$row_num;
150 $hcfa_entries[]=new HCFA_Info($row_pos, $col_pos, 8, $diag);
153 public function gen_hcfa_1500($pid, $encounter, &$log)
155 $this->hcfa_data = '';
156 $this->hcfa_proc_index = 0;
158 $today = time();
160 $claim = new Claim($pid, $encounter);
162 $log .= "Generating HCFA claim $pid-$encounter for " .
163 $claim->patientFirstName() . ' ' .
164 $claim->patientMiddleName() . ' ' .
165 $claim->patientLastName() . ' on ' .
166 date('Y-m-d H:i', $today) . ".\n";
168 while ($this->hcfa_proc_index < $claim->procCount()) {
169 if ($this->hcfa_proc_index) {
170 $this->hcfa_data .= "\014"; // append form feed for new page
173 $this->gen_hcfa_1500_page($pid, $encounter, $log, $claim);
176 $log .= "\n";
177 return $this->hcfa_data;
180 private function gen_hcfa_1500_page($pid, $encounter, &$log, $claim)
183 $this->hcfa_curr_line = 1;
184 $this->hcfa_curr_col = 1;
186 // According to:
187 // http://www.ngsmedicare.com/NGSMedicare/PartB/EducationandSupport/ToolsandMaterials/CMS_ClaimFormInst.aspx
188 // Medicare interprets sections 9 and 11 of the claim form in its own
189 // special way. This flag tells us to do that. However I'm not 100%
190 // sure that it applies nationwide, and if you find that it is not right
191 // for you then set it to false. -- Rod 2009-03-26
192 $new_medicare_logic = $claim->claimType() == 'MB';
194 // Payer name, attn, street.
195 $this->put_hcfa(2, 41, 31, $claim->payerName());
196 $this->put_hcfa(3, 41, 31, $claim->payerAttn());
197 $this->put_hcfa(4, 41, 31, $claim->payerStreet());
199 // Payer city, state, zip.
200 $tmp = $claim->payerCity() ? ($claim->payerCity() . ', ') : '';
201 $this->put_hcfa(5, 41, 31, $tmp . $claim->payerState() . ' ' . $claim->payerZip());
203 // Box 1. Insurance Type
204 // claimTypeRaw() gets the integer value from insurance_companies.ins_type_code.
205 // Previous version of this code called claimType() which maps ins_type_code to
206 // a 2-character code and that was not specific enough.
207 $ct = $claim->claimTypeRaw();
208 $tmpcol = 45; // Other
209 if ($ct == 2) {
210 $tmpcol = 1; // Medicare
211 } else if ($ct == 3) {
212 $tmpcol = 8; // Medicaid
213 } else if ($ct == 5) {
214 $tmpcol = 15; // TriCare (formerly CHAMPUS)
215 } else if ($ct == 4) {
216 $tmpcol = 24; // Champus VA
217 } else if ($ct == 6) {
218 $tmpcol = 31; // Group Health Plan (only BCBS?)
219 } else if ($ct == 7) {
220 $tmpcol = 39; // FECA
223 $this->put_hcfa(8, $tmpcol, 1, 'X');
225 // Box 1a. Insured's ID Number
226 $this->put_hcfa(8, 50, 17, $claim->policyNumber());
228 // Box 2. Patient's Name
229 $tmp = $claim->patientLastName() . ', ' . $claim->patientFirstName();
230 if ($claim->patientMiddleName()) {
231 $tmp .= ', ' . substr($claim->patientMiddleName(), 0, 1);
234 $this->put_hcfa(10, 1, 28, $tmp);
236 // Box 3. Patient's Birth Date and Sex
237 $tmp = $claim->patientDOB();
238 $this->put_hcfa(10, 31, 2, substr($tmp, 4, 2));
239 $this->put_hcfa(10, 34, 2, substr($tmp, 6, 2));
240 $this->put_hcfa(10, 37, 4, substr($tmp, 0, 4));
241 $this->put_hcfa(10, $claim->patientSex() == 'M' ? 42 : 47, 1, 'X');
243 // Box 4. Insured's Name
244 $tmp = $claim->insuredLastName() . ', ' . $claim->insuredFirstName();
245 if ($claim->insuredMiddleName()) {
246 $tmp .= ', ' . substr($claim->insuredMiddleName(), 0, 1);
249 $this->put_hcfa(10, 50, 28, $tmp);
251 // Box 5. Patient's Address
252 $this->put_hcfa(12, 1, 28, $claim->patientStreet());
254 // Box 6. Patient Relationship to Insured
255 $tmp = $claim->insuredRelationship();
256 $tmpcol = 47; // Other
257 if ($tmp === '18') {
258 $tmpcol = 33; // self
259 } else if ($tmp === '01') {
260 $tmpcol = 38; // spouse
261 } else if ($tmp === '19') {
262 $tmpcol = 42; // child
265 $this->put_hcfa(12, $tmpcol, 1, 'X');
267 // Box 7. Insured's Address
268 $this->put_hcfa(12, 50, 28, $claim->insuredStreet());
270 // Box 5 continued. Patient's City and State
271 $this->put_hcfa(14, 1, 20, $claim->patientCity());
272 $this->put_hcfa(14, 26, 2, $claim->patientState());
274 // Box 8. Reserved for NUCC Use in 02/12
276 // Box 7 continued. Insured's City and State
277 $this->put_hcfa(14, 50, 20, $claim->insuredCity());
278 $this->put_hcfa(14, 74, 2, $claim->insuredState());
280 // Box 5 continued. Patient's Zip Code and Telephone
281 $this->put_hcfa(16, 1, 10, $claim->patientZip());
282 $tmp = $claim->patientPhone();
283 $this->put_hcfa(16, 15, 3, substr($tmp, 0, 3));
284 $this->put_hcfa(16, 19, 7, substr($tmp, 3));
286 // Box 7 continued. Insured's Zip Code and Telephone
287 $this->put_hcfa(16, 50, 10, $claim->insuredZip());
288 $tmp = $claim->insuredPhone();
289 $this->put_hcfa(16, 65, 3, substr($tmp, 0, 3));
290 $this->put_hcfa(16, 69, 7, substr($tmp, 3));
292 // Box 9. Other Insured's Name
293 if ($new_medicare_logic) {
294 // TBD: Medigap stuff? How do we know if this is a Medigap transfer?
295 } else {
296 if ($claim->payerCount() > 1) {
297 $tmp = $claim->insuredLastName(1) . ', ' . $claim->insuredFirstName(1);
298 if ($claim->insuredMiddleName(1)) {
299 $tmp .= ', ' . substr($claim->insuredMiddleName(1), 0, 1);
302 $this->put_hcfa(18, 1, 28, $tmp);
306 // Box 11. Insured's Group Number
307 if ($new_medicare_logic) {
308 // If this is Medicare secondary then we need the primary's policy number
309 // here, otherwise the word "NONE".
310 $tmp = $claim->payerSequence() == 'P' ? 'NONE' : $claim->policyNumber(1);
311 } else {
312 $tmp = $claim->groupNumber();
315 $this->put_hcfa(18, 50, 30, $tmp);
317 // Box 9a. Other Insured's Policy or Group Number
318 if ($new_medicare_logic) {
319 // TBD: Medigap stuff?
320 } else {
321 if ($claim->payerCount() > 1) {
322 $this->put_hcfa(20, 1, 28, $claim->policyNumber(1));
326 // Box 10a. Employment Related
327 $this->put_hcfa(20, $claim->isRelatedEmployment() ? 35 : 41, 1, 'X');
329 // Box 11a. Insured's Birth Date and Sex
330 if ($new_medicare_logic) {
331 $tmpdob = $tmpsex = '';
332 if ($claim->payerSequence() != 'P') {
333 $tmpdob = $claim->insuredDOB(1);
334 $tmpsex = $claim->insuredSex(1);
336 } else {
337 $tmpdob = $claim->insuredDOB();
338 $tmpsex = $claim->insuredSex();
341 if ($tmpdob) {
342 $this->put_hcfa(20, 53, 2, substr($tmpdob, 4, 2));
343 $this->put_hcfa(20, 56, 2, substr($tmpdob, 6, 2));
344 $this->put_hcfa(20, 59, 4, substr($tmpdob, 0, 4));
347 if ($tmpsex) {
348 $this->put_hcfa(20, $tmpsex == 'M' ? 68 : 75, 1, 'X');
351 // Box 9b. Reserved for NUCC Use in 02/12
353 // Box 10b. Auto Accident
354 $this->put_hcfa(22, $claim->isRelatedAuto() ? 35 : 41, 1, 'X');
355 if ($claim->isRelatedAuto()) {
356 $this->put_hcfa(22, 45, 2, $claim->autoAccidentState());
359 // Box 11b. Insured's Employer/School Name
360 if ($new_medicare_logic) {
361 $tmp = $claim->payerSequence() == 'P' ? '' : $claim->groupName(1);
362 } else {
363 $tmp = $claim->groupName();
366 $this->put_hcfa(22, 50, 30, $tmp);
368 // Box 9c. Reserved for NUCC Use in 02/12
370 // Box 10c. Other Accident
371 $this->put_hcfa(24, $claim->isRelatedOther() ? 35 : 41, 1, 'X');
373 // Box 11c. Insurance Plan Name or Program Name
374 if ($new_medicare_logic) {
375 $tmp = '';
376 if ($claim->payerSequence() != 'P') {
377 $tmp = $claim->planName(1);
378 if (!$tmp) {
379 $tmp = $claim->payerName(1);
382 } else {
383 $tmp = $claim->planName();
386 $this->put_hcfa(24, 50, 30, $tmp);
388 // Box 9d. Other Insurance Plan Name or Program Name
389 if ($new_medicare_logic) {
390 // TBD: Medigap stuff?
391 } else {
392 if ($claim->payerCount() > 1) {
393 $this->put_hcfa(26, 1, 28, $claim->planName(1));
397 // Box 10d. Claim Codes medicaid_referral_code
399 if ($claim->epsdtFlag()) {
400 $this->put_hcfa(26, 34, 2, $claim->medicaidReferralCode());
403 // Box 11d. Is There Another Health Benefit Plan
405 if (!$new_medicare_logic) {
406 $this->put_hcfa(26, $claim->payerCount() > 1 ? 52 : 57, 1, 'X');
409 // Box 12. Patient's or Authorized Person's Signature
410 $this->put_hcfa(29, 7, 17, 'Signature on File');
411 // Note: Date does not apply unless the person physically signs the form.
413 // Box 13. Insured's or Authorized Person's Signature
414 $this->put_hcfa(29, 55, 17, 'Signature on File');
416 // Box 14. Date of Current Illness/Injury/Pregnancy
417 // this will cause onsetDate in Encounter summary to override misc billing so not perfect yet but fine for now
418 $tmp = ($claim->onsetDate()) ? $claim->onsetDate() : $claim->miscOnsetDate();
419 if (!empty($tmp)) {
420 $this->put_hcfa(32, 2, 2, substr($tmp, 4, 2));
421 $this->put_hcfa(32, 5, 2, substr($tmp, 6, 2));
422 $this->put_hcfa(32, 8, 4, substr($tmp, 0, 4));
423 // Include Box 14 Qualifier
424 $this->put_hcfa(32, 16, 3, $claim->box14Qualifier());
427 // Box 15. First Date of Same or Similar Illness, if applicable
428 $tmp = $claim->dateInitialTreatment();
429 if (!empty($tmp)) {
430 // Only include the Box 15 qualifier if using version 02/12 and there is a Box 15 date.
431 $this->put_hcfa(32, 31, 3, $claim->box15Qualifier());
434 $this->put_hcfa(32, 37, 2, substr($tmp, 4, 2));
435 $this->put_hcfa(32, 40, 2, substr($tmp, 6, 2));
436 $this->put_hcfa(32, 43, 4, substr($tmp, 0, 4));
438 // Box 16. Dates Patient Unable to Work in Current Occupation
439 if ($claim->isUnableToWork()) {
440 $tmp = $claim->offWorkFrom();
441 $this->put_hcfa(32, 54, 2, substr($tmp, 4, 2));
442 $this->put_hcfa(32, 57, 2, substr($tmp, 6, 2));
443 $this->put_hcfa(32, 60, 4, substr($tmp, 0, 4));
444 $tmp = $claim->offWorkTo();
445 $this->put_hcfa(32, 68, 2, substr($tmp, 4, 2));
446 $this->put_hcfa(32, 71, 2, substr($tmp, 6, 2));
447 $this->put_hcfa(32, 74, 4, substr($tmp, 0, 4));
450 // Referring provider stuff. Reports are that for primary care providers,
451 // Medicare forbids an entry here and other payers require one.
452 // There is still confusion over this.
453 if ($claim->referrerLastName() || $claim->billingProviderLastName() &&
454 (empty($GLOBALS['MedicareReferrerIsRenderer']) || $claim->claimType() != 'MB')) {
455 // Box 17a. Referring Provider Alternate Identifier
456 // Commented this out because UPINs are obsolete, leaving the code as an
457 // example in case some other identifier needs to be supported.
458 /*****************************************************************
459 * if ($claim->referrerUPIN() && $claim->claimType() != 'MB') {
460 * $this->put_hcfa(33, 30, 2, '1G');
461 * $this->put_hcfa(33, 33, 15, $claim->referrerUPIN());
463 *****************************************************************/
464 if ($claim->claimType() == 'MC') {
465 $this->put_hcfa(33, 30, 2, 'ZZ');
466 $this->put_hcfa(33, 33, 14, $claim->referrerTaxonomy());
469 // Box 17. Name of Referring Provider or Other Source
470 if (strlen($claim->billingProviderLastName()) != 0) {
471 $tmp2 = $claim->billingProviderLastName() . ', ' . $claim->billingProviderFirstName();
472 if ($claim->billingProviderMiddleName()) {
473 $tmp2 .= ', ' . substr($claim->billingProviderMiddleName(), 0, 1);
476 $this->put_hcfa(34, 1, 3, $claim->billing_options['provider_qualifier_code']);
477 $this->put_hcfa(34, 4, 25, $tmp2);
478 if ($claim->billingProviderNPI()) {
479 $this->put_hcfa(34, 33, 15, $claim->billingProviderNPI());
481 } else {
482 $tmp = $claim->referrerLastName() . ', ' . $claim->referrerFirstName();
483 if ($claim->referrerMiddleName()) {
484 $tmp .= ', ' . substr($claim->referrerMiddleName(), 0, 1);
487 $this->put_hcfa(34, 1, 3, 'DN');
488 $this->put_hcfa(34, 4, 25, $tmp);
489 if ($claim->referrerNPI()) {
490 $this->put_hcfa(34, 33, 15, $claim->referrerNPI());
495 // Box 18. Hospitalization Dates Related to Current Services
496 if ($claim->isHospitalized()) {
497 $tmp = $claim->hospitalizedFrom();
498 $this->put_hcfa(34, 54, 2, substr($tmp, 4, 2));
499 $this->put_hcfa(34, 57, 2, substr($tmp, 6, 2));
500 $this->put_hcfa(34, 60, 4, substr($tmp, 0, 4));
501 $tmp = $claim->hospitalizedTo();
502 $this->put_hcfa(34, 68, 2, substr($tmp, 4, 2));
503 $this->put_hcfa(34, 71, 2, substr($tmp, 6, 2));
504 $this->put_hcfa(34, 74, 4, substr($tmp, 0, 4));
507 // Box 19. Reserved for Local Use
508 $this->put_hcfa(36, 1, 48, $claim->additionalNotes());
510 // Box 20. Outside Lab
511 $this->put_hcfa(36, $claim->isOutsideLab() ? 52 : 57, 1, 'X');
512 if ($claim->isOutsideLab()) {
513 // Note here that $this->put_hcfa strips the decimal point, as required.
514 // We right-justify this amount (ending in col. 69).
515 $this->put_hcfa(36, 63, 8, sprintf('%8s', $claim->outsideLabAmount()));
518 // Box 21. Diagnoses
519 $this->process_diagnoses_02_12($claim, $log);
521 $proccount = $claim->procCount(); // number of procedures
523 // Charges, adjustments and payments are accumulated by line item so that
524 // each page of a multi-page claim will stand alone. Payments include the
525 // co-pay for the first page only.
526 $clm_total_charges = 0;
527 $clm_amount_adjusted = 0;
528 $clm_amount_paid = $this->hcfa_proc_index ? 0 : $claim->patientPaidAmount();
530 // Procedure loop starts here.
531 for ($svccount = 0; $svccount < 6 && $this->hcfa_proc_index < $proccount; ++$this->hcfa_proc_index) {
532 $dia = $claim->diagIndexArray($this->hcfa_proc_index);
534 if (!$claim->cptCharges($this->hcfa_proc_index)) {
535 $log .= "*** Procedure '" . $claim->cptKey($this->hcfa_proc_index) .
536 "' has no charges!\n";
539 if (empty($dia)) {
540 $log .= "*** Procedure '" . $claim->cptKey($this->hcfa_proc_index) .
541 "' is not justified!\n";
544 $clm_total_charges += floatval($claim->cptCharges($this->hcfa_proc_index));
546 // Compute prior payments and "hard" adjustments.
547 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
548 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
549 continue; // skip future payers
552 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($this->hcfa_proc_index));
553 $clm_amount_paid += $payerpaid[1];
554 $clm_amount_adjusted += $payerpaid[2];
557 ++$svccount;
558 $lino = $svccount * 2 + 41;
560 // Drug Information. Medicaid insurers want this with HCPCS codes.
562 $ndc = $claim->cptNDCID($this->hcfa_proc_index);
563 if ($ndc) {
564 if (preg_match('/^(\d\d\d\d\d)-(\d\d\d\d)-(\d\d)$/', $ndc, $tmp)) {
565 $ndc = $tmp[1] . $tmp[2] . $tmp[3];
566 } else if (preg_match('/^\d{11}$/', $ndc)) {
567 } else {
568 $log .= "*** NDC code '$ndc' has invalid format!\n";
571 $this->put_hcfa($lino, 1, 50, "N4$ndc " . $claim->cptNDCUOM($this->hcfa_proc_index) .
572 $claim->cptNDCQuantity($this->hcfa_proc_index));
575 //Note Codes.
576 $this->put_hcfa($lino, 25, 7, $claim->cptNotecodes($this->hcfa_proc_index));
578 // 24i and 24j Top. ID Qualifier and Rendering Provider ID
579 if ($claim->supervisorNumber()) {
580 // If there is a supervising provider and that person has a
581 // payer-specific provider number, then we assume that the SP
582 // must be identified on the claim and this is how we do it
583 // (but the NPI of the actual rendering provider appears below).
584 // BCBS of TN indicated they want it this way. YMMV. -- Rod
585 $this->put_hcfa($lino, 65, 2, $claim->supervisorNumberType());
586 $this->put_hcfa($lino, 68, 10, $claim->supervisorNumber());
587 } else if ($claim->providerNumber($this->hcfa_proc_index)) {
588 $this->put_hcfa($lino, 65, 2, $claim->providerNumberType($this->hcfa_proc_index));
589 $this->put_hcfa($lino, 68, 10, $claim->providerNumber($this->hcfa_proc_index));
590 } else if ($claim->claimType() == 'MC') {
591 $this->put_hcfa($lino, 65, 2, 'ZZ');
592 $this->put_hcfa($lino, 68, 14, $claim->providerTaxonomy());
595 ++$lino;
597 // 24a. Date of Service
598 $tmp = $claim->serviceDate();
599 $this->put_hcfa($lino, 1, 2, substr($tmp, 4, 2));
600 $this->put_hcfa($lino, 4, 2, substr($tmp, 6, 2));
601 $this->put_hcfa($lino, 7, 2, substr($tmp, 2, 2));
602 $this->put_hcfa($lino, 10, 2, substr($tmp, 4, 2));
603 $this->put_hcfa($lino, 13, 2, substr($tmp, 6, 2));
604 $this->put_hcfa($lino, 16, 2, substr($tmp, 2, 2));
606 // 24b. Place of Service
607 $this->put_hcfa($lino, 19, 2, $claim->facilityPOS());
609 // 24c. EMG
610 // Not currently supported.
612 // 24d. Procedures, Services or Supplies
613 $this->put_hcfa($lino, 25, 7, $claim->cptCode($this->hcfa_proc_index));
614 // replace colon with space for printing
615 $this->put_hcfa($lino, 33, 12, str_replace(':', ' ', $claim->cptModifier($this->hcfa_proc_index)));
617 // 24e. Diagnosis Pointer
618 $tmp = '';
619 foreach ($claim->diagIndexArray($this->hcfa_proc_index) as $value) {
620 $value = chr($value + 64);
621 $tmp .= $value;
624 $this->put_hcfa($lino, 45, 4, $tmp);
626 // 24f. Charges
627 $this->put_hcfa($lino, 50, 8, str_replace(
628 '.',
629 ' ',
630 sprintf('%8.2f', $claim->cptCharges($this->hcfa_proc_index))
633 // 24g. Days or Units
634 $this->put_hcfa($lino, 59, 3, $claim->cptUnits($this->hcfa_proc_index));
636 // 24h. EPSDT Family Plan
638 if ($claim->epsdtFlag()) {
639 $this->put_hcfa($lino, 63, 2, '03');
642 // 24j. Rendering Provider NPI
643 $this->put_hcfa($lino, 68, 10, $claim->providerNPI($this->hcfa_proc_index));
646 // 25. Federal Tax ID Number
647 $this->put_hcfa(56, 1, 15, $claim->billingFacilityETIN());
648 if ($claim->federalIdType() == 'SY') {
649 $this->put_hcfa(56, 17, 1, 'X'); // The SSN checkbox
650 } else {
651 $this->put_hcfa(56, 19, 1, 'X'); // The EIN checkbox
654 // 26. Patient's Account No.
655 // Instructions say hyphens are not allowed.
656 $this->put_hcfa(56, 23, 15, "$pid-$encounter");
658 // 27. Accept Assignment
659 $this->put_hcfa(56, $claim->billingFacilityAssignment() ? 38 : 43, 1, 'X');
661 // 28. Total Charge
662 $this->put_hcfa(56, 52, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_total_charges)));
663 if (!$clm_total_charges) {
664 $log .= "*** This claim has no charges!\n";
667 // 29. Amount Paid
668 $this->put_hcfa(56, 62, 8, str_replace('.', ' ', sprintf('%8.2f', $clm_amount_paid)));
670 // 30. Reserved for NUCC use.
672 // 33. Billing Provider: Phone Number
673 $tmp = $claim->billingContactPhone();
674 $this->put_hcfa(57, 66, 3, substr($tmp, 0, 3));
675 $this->put_hcfa(57, 70, 3, substr($tmp, 3)); // slight adjustment for better look smw 030315
676 $this->put_hcfa(57, 73, 1, '-');
677 $this->put_hcfa(57, 74, 4, substr($tmp, 6));
679 // 32. Service Facility Location Information: Name
680 $this->put_hcfa(58, 23, 25, $claim->facilityName());
682 // 33. Billing Provider: Name
683 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
684 $firstName = $claim->providerFirstName();
685 $lastName = $claim->providerLastName();
686 $middleName = $claim->providerMiddleName();
687 $suffixName = $claim->providerSuffixName();
688 $billingProviderName = $lastName . ", " . $firstName . ", " . $middleName . ", " . $suffixName;
689 $this->put_hcfa(58, 50, 25, $billingProviderName);
690 } else {
691 $this->put_hcfa(58, 50, 25, $claim->billingFacilityName());
694 // 32. Service Facility Location Information: Street
695 $this->put_hcfa(59, 23, 25, $claim->facilityStreet());
697 // 33. Billing Provider: Name
698 $this->put_hcfa(59, 50, 25, $claim->billingFacilityStreet());
700 // 31. Signature of Physician or Supplier
702 if ($GLOBALS['cms_1500_box_31_format'] == 0) {
703 $this->put_hcfa(60, 1, 20, 'Signature on File');
704 } else if ($GLOBALS['cms_1500_box_31_format'] == 1) {
705 $this->put_hcfa(60, 1, 22, $claim->providerFirstName() . " " . $claim->providerLastName());
708 // 32. Service Facility Location Information: City State Zip
709 $tmp = $claim->facilityCity() ? ($claim->facilityCity() . ' ') : '';
710 $this->put_hcfa(60, 23, 27, $tmp . $claim->facilityState() . ' ' .
711 $claim->facilityZip());
713 // 33. Billing Provider: City State Zip
714 $tmp = $claim->billingFacilityCity() ? ($claim->billingFacilityCity() . ' ') : '';
715 $this->put_hcfa(60, 50, 27, $tmp . $claim->billingFacilityState() . ' ' .
716 $claim->billingFacilityZip());
718 // 31. Signature of Physician or Supplier: Date
719 if ($GLOBALS['cms_1500_box_31_date'] > 0) {
720 if ($GLOBALS['cms_1500_box_31_date'] == 1) {
721 $date_of_service = $claim->serviceDate();
722 $MDY = substr($date_of_service, 4, 2) . " " . substr($date_of_service, 6, 2) . " " . substr($date_of_service, 2, 2);
723 } else if ($GLOBALS['cms_1500_box_31_date'] == 2) {
724 $MDY = date("m/d/y");
727 $this->put_hcfa(61, 6, 10, $MDY);
730 // 32a. Service Facility NPI
731 $this->put_hcfa(61, 23, 10, $claim->facilityNPI());
733 // 32b. Service Facility Other ID
734 // Note that Medicare does NOT want this any more.
735 if ($claim->providerGroupNumber()) {
736 $this->put_hcfa(61, 36, 2, $claim->providerNumberType());
737 $this->put_hcfa(61, 38, 11, $claim->providerGroupNumber());
740 // 33a. Billing Facility NPI
741 $this->put_hcfa(61, 50, 10, $claim->billingFacilityNPI());
743 // 33b. Billing Facility Other ID
744 // Note that Medicare does NOT want this any more.
745 if ($claim->claimType() == 'MC') {
746 $this->put_hcfa(61, 63, 2, 'ZZ');
747 $this->put_hcfa(61, 65, 14, $claim->providerTaxonomy());
748 } elseif ($claim->providerGroupNumber() && $claim->claimType() != 'MB') {
749 $this->put_hcfa(61, 63, 2, $claim->providerNumberType());
750 $this->put_hcfa(61, 65, 14, $claim->providerGroupNumber());
753 // Put an extra line here for compatibility with old hcfa text generated form
754 $this->put_hcfa(62, 1, 1, ' ');
755 // put a couple more in so that multiple claims correctly print through the text file download
756 $this->put_hcfa(63, 1, 1, ' ');
757 $this->put_hcfa(64, 1, 1, ' ');
758 return;