protected static function _generate4010A1($claimId, array $claim) { static $ctr = 0; static $visits = array(); static $practices = array(); static $insurancePrograms = array(); static $providers = array(); static $patients = array(); $claimId = (int) $claimId; $claimLine = new ClaimLine(); $claimLine->populateByClaimId($claimId); $visitId = (int) $claimLine->visitId; if (!isset($visits[$visitId])) { $visit = new Visit(); $visit->visitId = $visitId; $visit->populate(); $visits[$visitId] = $visit; } $visit = $visits[$visitId]; $patientId = (int) $visit->patientId; if (!isset($patients[$patientId])) { $patient = new Patient(); $patient->personId = $patientId; $patient->populate(); $patients[$patientId] = $patient; } $patient = $patients[$patientId]; $practiceId = (int) $visit->practiceId; if (!isset($practices[$practiceId])) { $practice = new Practice(); $practice->practiceId = $practiceId; $practice->populate(); $practices[$practiceId] = $practice; } $practice = $practices[$practiceId]; $insuranceProgramId = (int) $visit->activePayerId; if (!isset($insurancePrograms[$insuranceProgramId])) { $insurance = new InsuranceProgram(); $insurance->insuranceProgramId = $insuranceProgramId; $insurance->populate(); $insurancePrograms[$insuranceProgramId] = $insurance; } $insuranceProgram = $insurancePrograms[$insuranceProgramId]; $providerId = (int) $visit->treatingPersonId; if (!isset($providers[$providerId])) { $provider = new Provider(); $provider->personId = $providerId; $provider->populate(); $providers[$providerId] = $provider; } $provider = $providers[$providerId]; $billAs = (int) $provider->billAs; if ($billAs > 0) { $providerId = $billAs; if (!isset($providers[$providerId])) { $provider = new Provider(); $provider->personId = $providerId; $provider->populate(); $providers[$providerId] = $provider; } $provider = $providers[$providerId]; } $subscribers = array(); $enumeration = new Enumeration(); $enumeration->populateByUniqueName(InsuranceProgram::INSURANCE_ENUM_NAME); $enumerationClosure = new EnumerationClosure(); foreach ($enumerationClosure->getAllDescendants($enumeration->enumerationId, 1, true) as $enum) { $rowset = $enumerationClosure->getAllDescendants($enum->enumerationId, 1, true); if ($enum->key == InsuranceProgram::INSURANCE_SUBSCRIBER_ENUM_KEY) { foreach ($rowset as $row) { $subscribers[$row->key] = $row->name; } break; } } $insuredRelationship = new InsuredRelationship(); $db = Zend_Registry::get('dbAdapter'); $sqlSelect = $db->select()->from($insuredRelationship->_table)->where('insurance_program_id = ?', (int) $insuranceProgram->insuranceProgramId)->where('person_id = ?', (int) $patientId)->where('active = 1')->order('program_order')->limit(1); if ($row = $db->fetchRow($sqlSelect)) { $insuredRelationship->populateWithArray($row); } $subs = $insuredRelationship->subscriber; $subscriberAddr = $subs->address; $relationship = null; $relationshipCode = $insuredRelationship->subscriberToPatientRelationship; if (isset($subscribers[$relationshipCode])) { $relationship = $subscribers[$relationshipCode]; } if ($relationship === null) { $relationship = 'Self'; $relationshipCode = 18; $subs = new Person(); $subs->personId = $insuredRelationship->personId; $subs->populate(); } $subscriber = array('id' => (int) $subs->personId, 'relationship_code' => $relationshipCode, 'group_number' => $insuredRelationship->groupNumber, 'group_name' => $insuredRelationship->groupName, 'relationship' => $relationship, 'last_name' => $subs->lastName, 'first_name' => $subs->firstName, 'middle_name' => $subs->middleName, 'address' => array('line1' => $subscriberAddr->line1, 'line2' => $subscriberAddr->line2, 'city' => $subscriberAddr->city, 'state' => $subscriberAddr->state, 'zip' => $subscriberAddr->zipCode), 'date_of_birth' => date('Ymd', strtotime($subs->dateOfBirth)), 'gender' => $subs->gender, 'contract_type_code' => '', 'contract_amount' => '', 'contract_percent' => '', 'contract_code' => '', 'contract_discount_percent' => '', 'contract_version' => ''); $practiceAddr = $practice->primaryAddress; $room = new Room(); $room->roomId = (int) $visit->roomId; $room->populate(); $facility = $room->building; $phoneNumber = PhoneNumber::autoFixNumber($practice->mainPhone->number); $phoneLen = strlen($phoneNumber); if ($phoneLen < 10) { $phoneNumber = str_pad($phoneNumber, 10, '0', STR_PAD_LEFT); } else { if ($phoneLen > 10) { $phoneNumber = substr($phoneNumber, -10); } } $identifierType = ''; $identifier = $practice->identifier; if (strlen($identifier) > 0) { $identifierType = 'XX'; } //24'; $data = array('hlCount' => ++$ctr); $data['practice'] = array('name' => $practice->name, 'identifier_type' => $identifierType, 'identifier' => $identifier, 'address' => array('line1' => $practiceAddr->line1, 'line2' => $practiceAddr->line2, 'city' => $practiceAddr->city, 'state' => $practiceAddr->state, 'zip' => $practiceAddr->zipCode), 'phoneNumber' => $phoneNumber); $data['treating_facility'] = array('identifier' => $facility->identifier); $dateOfTreatment = date('Ymd', strtotime($visit->dateOfTreatment)); $payer2Id = $insuranceProgram->insuranceProgramId; if (strlen($payer2Id) < 2) { $payer2Id = str_pad($payer2Id, 2, '0', STR_PAD_LEFT); } $identifierType = ''; $identifier = $provider->person->identifier; if (strlen($identifier) > 0) { $identifierType = 'XX'; } //34'; $claimData = array('claim' => $claim, 'patient' => array('date_of_initial_treatment' => $dateOfTreatment, 'date_of_onset' => $dateOfTreatment, 'comment_type' => '', 'comment' => ''), 'treating_facility' => array('facility_code' => $facility->facilityCodeId, 'name' => $facility->name, 'address' => array('line1' => $facility->line1, 'line2' => $facility->line2, 'city' => $facility->city, 'state' => $facility->state, 'zip' => $facility->zipCode)), 'provider' => array('signature_on_file' => 'Y', 'accepts_assignment' => 'A', 'last_name' => $provider->person->lastName, 'first_name' => $provider->person->firstName, 'identifier_type' => $identifierType, 'identifier' => $identifier, 'identifier_2' => ''), 'billing_facility' => array('clia_number' => ''), 'subscriber' => $subscriber, 'clearing_house' => array('credit_max_amount' => '', 'repricing_method' => '', 'allowed_amount' => '', 'savings_amount' => '', 'identifier' => '', 'rate' => '', 'apg_code' => '', 'apg_amount' => '', 'reject_code' => '', 'compliance_code' => '', 'exception_code' => ''), 'referring_provider' => array('last_name' => '', 'first_name' => '', 'referral_type' => '', 'identifier_type' => '', 'identifier' => '', 'taxonomy_code' => ''), 'supervising_provider' => array('last_name' => '', 'first_name' => '', 'identifier_type' => '', 'identifier' => ''), 'payer2' => array('id' => $payer2Id, 'name' => $insuranceProgram->name)); $clm = array(); $iterator = new ClaimLineIterator(); $iterator->setFilters(array('claimId' => $claimId)); foreach ($iterator as $row) { $baseFee = (double) $row->baseFee; $adjustedFee = (double) $row->adjustedFee; $paid = (double) $row->paid; $billed = $row->totalMiscCharge; if ($baseFee > 0) { $billed += $baseFee - $adjustedFee; } $balance = abs($billed) - $paid; $clm[] = array('claim' => $claimData['claim'], 'patient' => $claimData['patient'], 'claim_line' => array('amount' => $balance, 'diagnosis1' => preg_replace('/[\\.]/', '', $row->diagnosisCode1), 'diagnosis2' => preg_replace('/[\\.]/', '', $row->diagnosisCode2), 'diagnosis3' => preg_replace('/[\\.]/', '', $row->diagnosisCode3), 'diagnosis4' => preg_replace('/[\\.]/', '', $row->diagnosisCode4), 'diagnosis5' => preg_replace('/[\\.]/', '', $row->diagnosisCode5), 'diagnosis6' => preg_replace('/[\\.]/', '', $row->diagnosisCode6), 'diagnosis7' => preg_replace('/[\\.]/', '', $row->diagnosisCode7), 'diagnosis8' => preg_replace('/[\\.]/', '', $row->diagnosisCode8), 'procedure' => $row->procedureCode, 'modifier1' => $row->modifier1, 'modifier2' => $row->modifier2, 'modifier3' => $row->modifier3, 'modifier4' => $row->modifier4, 'units' => str_replace('.00', '', $row->units), 'date_of_treatment' => $dateOfTreatment, 'clia_number' => ''), 'treating_facility' => $claimData['treating_facility'], 'provider' => $claimData['provider'], 'billing_facility' => $claimData['billing_facility'], 'subscriber' => $subscriber, 'clearing_house' => $claimData['clearing_house'], 'referring_provider' => $claimData['referring_provider'], 'supervising_provider' => $claimData['supervising_provider'], 'payer2' => $claimData['payer2']); } $hl2 = array(); $hl2[] = array('hlCount' => $ctr, 'hlCount2' => ++$ctr, 'payer' => array('responsibility' => 'P'), 'subscriber' => $subscriber, 'patient' => array('weight' => '', 'last_name' => $patient->lastName, 'first_name' => $patient->firstName), 'responsible_party' => array('last_name' => '', 'first_name' => '', 'address' => array('line1' => '', 'line2' => '', 'city' => '', 'state' => '', 'zip' => '')), 'CLM' => $clm); $data['HL2'] = $hl2; return $data; }
protected function _populateCMS1500XML(SimpleXMLElement $xml, ClaimLine $claimLine) { $claimId = (int) $claimLine->claimId; $visitId = (int) $claimLine->visitId; $payerId = (int) $claimLine->insuranceProgramId; $visit = new Visit(); $visit->visitId = $visitId; $visit->populate(); $dateOfTreatment = $visit->dateOfTreatment; $patientId = (int) $visit->patientId; $providerId = (int) $visit->treatingPersonId; $insuranceProgram = new InsuranceProgram(); $insuranceProgram->insuranceProgramId = $payerId; $insuranceProgram->populate(); $insuranceAddress = $insuranceProgram->address; $insuredRelationship = InsuredRelationship::filterByPayerPersonIds($payerId, $patientId); $patient = new Patient(); $patient->personId = $patientId; $patient->populate(); $patientAddress = $patient->address; $patientPhone = $patient->phoneNumber; $provider = new Provider(); $provider->personId = $providerId; $provider->populate(); $providerAddress = $provider->person->address; $providerPhone = $provider->phoneNumber; $buildingId = (int) $visit->buildingId; $building = new Building(); $building->buildingId = $buildingId; $building->populate(); $address = $building->practice->primaryAddress; $amountTotal = 0; $claimLines = array(); $iterator = new ClaimLineIterator(); $iterator->setFilters(array('claimId' => $claimId)); foreach ($iterator as $orm) { $amount = (double) $orm->baseFee; $amountTotal += $amount; $row = array(); $row['procedure'] = $orm->procedureCode; $modifiers = array(); for ($i = 1; $i <= 4; $i++) { $field = 'modifier' . $i; $key = $orm->{$field}; if (strlen($key) > 0) { $modifiers[$i] = $key; } } $row['modifier'] = implode(',', $modifiers); $row['amount'] = $amount; $row['units'] = $orm->units; $diagnoses = array(); for ($i = 1; $i <= 8; $i++) { $field = 'diagnosisCode' . $i; $key = $orm->{$field}; if (strlen($key) > 0) { $diagnoses[$key] = $key; } } $row['diagnoses'] = $diagnoses; $row['diagnosis_pointer'] = ''; // TODO: need to resolve on the issue of multiple diagnoses per claimLine $claimLines[] = $row; } $amountPaid = $claimLine->paid; $netAmountTotal = $amountTotal - $amountPaid; if ($payerId > 0) { $xmlPayer = $xml->addChild('payer'); $this->_addChild($xmlPayer, 'name', InsuranceProgram::getInsuranceProgram($payerId)); if ($insuranceAddress->addressId > 0) { $xmlFbaddress = $xmlPayer->addChild('fbaddress'); $this->_addChild($xmlFbaddress, 'line1', $insuranceAddress->line1); $this->_addChild($xmlFbaddress, 'line2', $insuranceAddress->line2); $this->_addChild($xmlFbaddress, 'city', $insuranceAddress->city); $this->_addChild($xmlFbaddress, 'state', $insuranceAddress->state); $this->_addChild($xmlFbaddress, 'zip', $insuranceAddress->zipCode); } } $xmlPatient = $xml->addChild('patient'); $this->_addChild($xmlPatient, 'record_number', $patient->recordNumber); $this->_addChild($xmlPatient, 'last_name', $patient->lastName); $this->_addChild($xmlPatient, 'first_name', $patient->firstName); $this->_addChild($xmlPatient, 'middle_name', $patient->middleName); if ($patientAddress->addressId > 0) { $xmlFbaddress = $xmlPatient->addChild('fbaddress'); $this->_addChild($xmlFbaddress, 'line1', $patientAddress->line1); $this->_addChild($xmlFbaddress, 'line2', $patientAddress->line2); $this->_addChild($xmlFbaddress, 'city', $patientAddress->city); $this->_addChild($xmlFbaddress, 'state', $patientAddress->state); $this->_addChild($xmlFbaddress, 'zip', $patientAddress->zipCode); } $this->_addChild($xmlPatient, 'phone_number', $patientPhone->number); $this->_addChild($xmlPatient, 'date_of_birth', $patient->dateOfBirth); $subscriber = $insuredRelationship->subscriber; $subscriberAddress = $subscriber->address; if ($subscriber->personId > 0) { $xmlSubscriber = $xml->addChild('subscriber'); $this->_addChild($xmlSubscriber, 'phone_number', $subscriber->phoneNumber->number); if ($subscriberAddress->addressId > 0) { $xmlFbaddress = $xmlSubscriber->addChild('fbaddress'); $this->_addChild($xmlFbaddress, 'line1', $subscriberAddress->line1); $this->_addChild($xmlFbaddress, 'line2', $subscriberAddress->line2); $this->_addChild($xmlFbaddress, 'city', $subscriberAddress->city); $this->_addChild($xmlFbaddress, 'state', $subscriberAddress->state); $this->_addChild($xmlFbaddress, 'zip', $subscriberAddress->zipCode); } $this->_addChild($xmlSubscriber, 'last_name', $subscriber->lastName); $this->_addChild($xmlSubscriber, 'first_name', $subscriber->firstName); $this->_addChild($xmlSubscriber, 'middle_name', $subscriber->middleName); $this->_addChild($xmlSubscriber, 'group_number', $insuredRelationship->groupNumber); $this->_addChild($xmlSubscriber, 'gender', $subscriber->gender); $this->_addChild($xmlSubscriber, 'date_of_birth', $subscriber->dateOfBirth); } if ($building->buildingId > 0) { $xmlPractice = $xml->addChild('practice'); $this->_addChild($xmlPractice, 'identifier', $building->practice->identifier); $xmlTreatingFacility = $xml->addChild('treating_facility'); $this->_addChild($xmlTreatingFacility, 'identifier', $building->identifier); $this->_addChild($xmlTreatingFacility, 'name', $building->name); $xmlFbaddress = $xmlTreatingFacility->addChild('fbaddress'); $this->_addChild($xmlFbaddress, 'line1', $building->line1); $this->_addChild($xmlFbaddress, 'line2', $building->line2); $this->_addChild($xmlFbaddress, 'city', $building->city); $this->_addChild($xmlFbaddress, 'state', $building->state); $this->_addChild($xmlFbaddress, 'zip', $building->zipCode); } $xmlProvider = $xml->addChild('provider'); $this->_addChild($xmlProvider, 'identifier', $provider->identifier); $this->_addChild($xmlProvider, 'first_name', $provider->firstName); $this->_addChild($xmlProvider, 'last_name', $provider->lastName); if ($providerAddress->addressId > 0) { $xmlFbaddress = $xmlProvider->addChild('fbaddress'); $this->_addChild($xmlFbaddress, 'line1', $providerAddress->line1); $this->_addChild($xmlFbaddress, 'line2', $providerAddress->line2); $this->_addChild($xmlFbaddress, 'city', $providerAddress->city); $this->_addChild($xmlFbaddress, 'state', $providerAddress->state); $this->_addChild($xmlFbaddress, 'zip', $providerAddress->zipCode); } $xmlDiagnoses = null; foreach ($claimLines as $row) { foreach ($row['diagnoses'] as $diagnosis) { if ($xmlDiagnoses === null) { $xmlDiagnoses = $xml->addChild('diagnoses'); } $this->_addChild($xmlDiagnoses, 'array', $diagnosis); } } $xmlClaimLine = null; $dateOfService = date('m d y', strtotime($dateOfTreatment)); foreach ($claimLines as $row) { if ($xmlClaimLine === null) { $xmlClaimLine = $xml->addChild('claim_lines'); } // 0 - 5 ONLY $xmlClaimLineArr = $xmlClaimLine->addChild('array'); $this->_addChild($xmlClaimLineArr, 'date_of_treatment', $dateOfService); $this->_addChild($xmlClaimLineArr, 'procedure', $row['procedure']); $this->_addChild($xmlClaimLineArr, 'modifier', $row['modifier']); $this->_addChild($xmlClaimLineArr, 'diagnosis_pointer', $row['diagnosis_pointer']); $this->_addChild($xmlClaimLineArr, 'amount', $row['amount']); $this->_addChild($xmlClaimLineArr, 'units', $row['units']); } $xmlClaim = $xml->addChild('claim'); $this->_addChild($xmlClaim, 'amount_total', $amountTotal); $this->_addChild($xmlClaim, 'amount_paid', $amountPaid); $this->_addChild($xmlClaim, 'net_amount_total', $netAmountTotal); //$doc = new DOMDocument(); //$doc->formatOutput = true; //$doc->loadXML($xml->asXML()); //file_put_contents('/tmp/claims.xml',$doc->saveXML()); }