protected function _createVisit($appointmentId)
 {
     $ret = 0;
     if ($appointmentId > 0) {
         $appointment = new Appointment();
         $appointment->appointmentId = $appointmentId;
         $appointment->populate();
         $personId = (int) $appointment->patientId;
         $insuredRelationship = new InsuredRelationship();
         $insuredRelationship->personId = $personId;
         $visit = new Visit();
         $visit->patientId = $personId;
         $visit->activePayerId = $insuredRelationship->defaultActivePayer;
         $visit->roomId = $appointment->roomId;
         $visit->practiceId = $appointment->practiceId;
         $room = new Room();
         $room->roomId = $visit->roomId;
         $room->populate();
         $visit->buildingId = $room->buildingId;
         $visit->createdByUserId = $appointment->creatorId;
         $visit->lastChangeUserId = $appointment->lastChangeId;
         $visit->treatingPersonId = $appointment->providerId;
         $visit->encounterReason = $appointment->reason;
         $visit->dateOfTreatment = $appointment->start;
         $visit->timestamp = date('Y-m-d H:i:s');
         $visit->appointmentId = $appointment->appointmentId;
         $visit->persist();
         $visitId = (int) $visit->visitId;
         $payment = new Payment();
         foreach ($payment->getIteratorByAppointmentId($appointmentId) as $row) {
             $row->visitId = $visitId;
             $row->persist();
         }
         $miscCharge = new MiscCharge();
         foreach ($miscCharge->getIteratorByAppointmentId($appointmentId) as $row) {
             $row->visitId = $visitId;
             $row->persist();
         }
         $ret = $visitId;
     }
     return $ret;
 }
 public function processEditChargeAction()
 {
     $params = $this->_getParam('app');
     $charge = new MiscCharge();
     $chargeId = 0;
     if (isset($params['id'])) {
         $chargeId = (int) $params['id'];
         unset($params['id']);
     }
     if ($chargeId > 0) {
         $charge->miscChargeId = $chargeId;
         $charge->populate();
     }
     $this->_processEditPaymentCharge($charge, $params);
 }
Example #3
0
 public function getAccountSummary()
 {
     $visitId = (int) $this->visitId;
     $total = 0;
     $billed = 0;
     $writeoff = 0;
     $balance = 0;
     $baseFee = 0;
     $adjustedFee = 0;
     $miscCharge = MiscCharge::total(array('visitId' => $visitId));
     $payment = Payment::total(array('visitId' => $visitId));
     $payment += Payment::unpostedTotal($visitId);
     $writeoff += WriteOff::total(array('visitId' => $visitId));
     if ($this->closed) {
         $iterator = ClaimLine::mostRecentClaims($visitId);
         foreach ($iterator as $claimLine) {
             $baseFee += (double) $claimLine->baseFee;
             $adjustedFee += (double) $claimLine->adjustedFee;
         }
     } else {
         $fees = $this->calculateFees();
         $baseFee += $fees['total'];
         $adjustedFee = $fees['discounted'];
         if (!$writeoff > 0 && $adjustedFee > 0) {
             $writeoff += $baseFee - $adjustedFee;
         }
     }
     $total = $baseFee + $miscCharge;
     $billed = $miscCharge;
     if ($adjustedFee > 0) {
         $billed += $adjustedFee;
     }
     if (!$billed > 0) {
         $billed = $total;
     }
     $balance = abs($total) - ($payment + $writeoff);
     if ($balance == $total) {
         $balance = $billed;
     }
     // use total billed if balance equals to total
     $ret = array();
     $ret['total'] = $total;
     $ret['billed'] = $billed;
     $ret['payment'] = $payment;
     $ret['balance'] = $balance;
     $ret['writeoff'] = $writeoff;
     $ret['baseFee'] = $baseFee;
     $ret['adjustedFee'] = $adjustedFee;
     $ret['miscCharge'] = $miscCharge;
     if (isset($claimLine)) {
         $ret['claimLine'] = $claimLine;
     }
     return $ret;
 }
 protected function _generateXMLStatements(SimpleXMLElement $xml, $claimId)
 {
     $iterator = new ClaimLineIterator();
     $iterator->setFilters(array('claimId' => $claimId));
     $visitId = null;
     $totalPayments = 0;
     $totalCharges = 0;
     $claims = array();
     foreach ($iterator as $claimLine) {
         $claimLineId = (int) $claimLine->claimLineId;
         $visitId = (int) $claimLine->visitId;
         $payerId = (int) $claimLine->insuranceProgramId;
         $procedureCode = $claimLine->procedureCode;
         $charges = (double) $claimLine->baseFee;
         $totalCharges += $charges;
         if (!isset($visit)) {
             $visit = new Visit();
             $visit->visitId = $visitId;
             $visit->populate();
         }
         if (!isset($patientName)) {
             $patientId = (int) $visit->patientId;
             $patient = new Patient();
             $patient->personId = $patientId;
             $patient->populate();
             $patientName = $patient->person->firstName . ' ' . $patient->person->middleName . ' ' . $patient->person->lastName;
         }
         if (!isset($providerName)) {
             $providerId = (int) $visit->providerId;
             $provider = new Provider();
             $provider->personId = $providerId;
             $provider->populate();
             $providerName = $provider->person->firstName . ' ' . $provider->person->middleName . ' ' . $provider->person->lastName;
         }
         $claim = array();
         $claim['date'] = date('m/d/Y', strtotime($claimLine->dateTime));
         $claim['patient'] = $patientName;
         $claim['provider'] = $providerName;
         $claim['description'] = $claimLine->procedure;
         $claim['charges'] = number_format($charges, 2);
         $claim['credits'] = '';
         if (!isset($claims[$visitId])) {
             $claims[$visitId] = array();
         }
         $claims[$visitId][] = $claim;
     }
     if ($visitId === null) {
         return;
     }
     $miscCharge = new MiscCharge();
     $iterator = $miscCharge->getIteratorByVisitId($visitId);
     foreach ($iterator as $charge) {
         $amount = (double) $charge->amount;
         $totalCharges += $amount;
         $claim = array();
         $claim['date'] = date('m/d/Y', strtotime($charge->chargeDate));
         $claim['patient'] = $patientName;
         $claim['provider'] = $providerName;
         $description = $charge->chargeType;
         $title = $charge->title;
         if (strlen($title) > 0) {
             $description .= ': ' . $title;
         }
         $claim['description'] = $description;
         $claim['charges'] = number_format($amount, 2);
         $claim['credits'] = '';
         $id = (int) $charge->miscChargeId;
         if (!isset($claims[$id])) {
             $claims[$id] = array();
         }
         $claims[$id][] = $claim;
     }
     $iterator = new PostingJournalIterator();
     $iterator->setFilters(array('visitId' => $visitId));
     foreach ($iterator as $journal) {
         $amount = (double) $journal->amount;
         $totalPayments += $amount;
         $claim = array();
         $claim['date'] = date('m/d/Y', strtotime($journal->datePosted));
         $claim['patient'] = $patientName;
         $claim['provider'] = $providerName;
         $claim['description'] = $journal->note;
         $claim['charges'] = '';
         $claim['credits'] = number_format($amount, 2);
         $id = (int) $journal->postingJournalId;
         if (!isset($claims[$id])) {
             $claims[$id] = array();
         }
         $claims[$id][] = $claim;
     }
     $iterator = new WriteOffIterator();
     $iterator->setFilters(array('visitId' => $visitId));
     foreach ($iterator as $writeoff) {
         $amount = (double) $writeoff->amount;
         $totalPayments += $amount;
         $claim = array();
         $claim['date'] = date('m/d/Y', strtotime($writeoff->timestamp));
         $claim['patient'] = $patientName;
         $claim['provider'] = $providerName;
         $claim['description'] = $writeoff->title;
         $claim['charges'] = '';
         $claim['credits'] = number_format($amount, 2);
         $id = (int) $writeoff->writeOffId;
         if (!isset($claims[$id])) {
             $claims[$id] = array();
         }
         $claims[$id][] = $claim;
     }
     $practiceId = (int) $visit->practiceId;
     if (!$practiceId > 0) {
         $practiceId = (int) $provider->person->primaryPracticeId;
     }
     $practice = new Practice();
     $practice->practiceId = $practiceId;
     $practice->populate();
     $practiceName = $practice->name;
     $practicePrimaryLine1 = $practice->primaryAddress->line1;
     $practicePrimaryCityStateZip = $practice->primaryAddress->city . ', ' . $practice->primaryAddress->state . ' ' . $practice->primaryAddress->postalCode;
     $practiceMainPhone = $practice->mainPhone->number;
     $xmlPractice = $xml->addChild('practice');
     $this->_addChild($xmlPractice, 'name', $practiceName);
     $this->_addChild($xmlPractice, 'primaryLine1', $practicePrimaryLine1);
     $this->_addChild($xmlPractice, 'primaryCityStateZip', $practicePrimaryCityStateZip);
     $this->_addChild($xmlPractice, 'mainPhone', $practiceMainPhone);
     $insuredRelationship = InsuredRelationship::filterByPayerPersonIds($payerId, $patientId);
     $subscriberId = (int) $insuredRelationship->subscriberId;
     if ($subscriberId > 0) {
         $subscriber = $insuredRelationship->subscriber;
     } else {
         $subscriber = $insuredRelationship->person;
     }
     $xmlSubscriber = $xml->addChild('subscriber');
     $subscriberName = $subscriber->firstName . ' ' . $subscriber->middleName . ' ' . $subscriber->lastName;
     $this->_addChild($xmlSubscriber, 'name', $subscriberName);
     $address = $subscriber->address;
     $this->_addChild($xmlSubscriber, 'line1', $address->line1);
     $subscriberCityStateZip = $address->city . ', ' . $address->state . ' ' . $address->postalCode;
     $this->_addChild($xmlSubscriber, 'cityStateZip', $subscriberCityStateZip);
     $xmlBiller = $xml->addChild('biller');
     $this->_addChild($xmlBiller, 'name', $practiceName);
     $this->_addChild($xmlBiller, 'line1', $practicePrimaryLine1);
     $this->_addChild($xmlBiller, 'cityStateZip', $practicePrimaryCityStateZip);
     $xmlClaims = $xml->addChild('claims');
     $claim = array();
     $claim['date'] = ' ';
     $claim['patient'] = ' ';
     $claim['provider'] = ' ';
     $claim['description'] = ' ';
     $claim['charges'] = ' ';
     $claim['credits'] = ' ';
     ksort($claims);
     $claims = array_pad($claims, 12, array($claim));
     foreach ($claims as $key => $values) {
         foreach ($values as $claim) {
             $xmlClaimsDetails = $xmlClaims->addChild('details');
             $this->_addChild($xmlClaimsDetails, 'date', $claim['date']);
             $this->_addChild($xmlClaimsDetails, 'patientName', $claim['patient']);
             $this->_addChild($xmlClaimsDetails, 'providerName', $claim['provider']);
             $this->_addChild($xmlClaimsDetails, 'description', $claim['description']);
             $this->_addChild($xmlClaimsDetails, 'charges', $claim['charges']);
             $this->_addChild($xmlClaimsDetails, 'credits', $claim['credits']);
         }
     }
     $date = date('m/d/Y');
     $accountNumber = $patient->recordNumber;
     $totalOutstanding = $totalCharges - $totalPayments;
     $pendingInsurance = 0;
     $amount = $totalOutstanding - $pendingInsurance;
     $patientResponsibility = $amount;
     $current = $amount;
     $pastDue = 0;
     $amountDue = $amount;
     $xmlClaimsInfo = $xmlClaims->addChild('info');
     $this->_addChild($xmlClaimsInfo, 'statement', $date);
     $this->_addChild($xmlClaimsInfo, 'accountNo', $accountNumber);
     $this->_addChild($xmlClaimsInfo, 'amount', number_format($amountDue, 2));
     $xmlClaimsSummary = $xmlClaims->addChild('summary');
     $this->_addChild($xmlClaimsSummary, 'accountNumber', $accountNumber);
     $this->_addChild($xmlClaimsSummary, 'totalOutstanding', number_format($totalOutstanding, 2));
     $this->_addChild($xmlClaimsSummary, 'patientResponsibility', number_format($patientResponsibility, 2));
     $this->_addChild($xmlClaimsSummary, 'pendingInsurance', number_format($pendingInsurance, 2));
     $this->_addChild($xmlClaimsSummary, 'current', number_format($current, 2));
     $this->_addChild($xmlClaimsSummary, 'pastDue', number_format($pastDue, 2));
     $this->_addChild($xmlClaimsSummary, 'amountDue', number_format($amountDue, 2));
     file_put_contents('/tmp/claims.xml', $xml->asXML());
 }