Beispiel #1
0
 public static function recalculateClaims(self $visit, $newClaim = false)
 {
     $fees = $visit->calculateFees(true);
     $hasProcedure = false;
     if ($newClaim) {
         $claimId = WebVista_Model_ORM::nextSequenceId('claimSequences');
     }
     $copay = $visit->getCopay();
     $totalPaid = 0;
     $personId = (int) $visit->patientId;
     $userId = (int) Zend_Auth::getInstance()->getIdentity()->personId;
     $visitId = (int) $visit->visitId;
     $discountPayerId = InsuranceProgram::lookupSystemId('Discounts');
     // ID of System->Discounts
     $creditPayerId = InsuranceProgram::lookupSystemId('Credit');
     // ID of System->Credit
     $payerId = InsuranceProgram::lookupSystemId('Self Pay');
     // ID of System->Self Pay
     foreach ($fees['details'] as $id => $values) {
         // update claim or create if not exists
         $fee = (double) $values['fee'];
         $feeDiscounted = (double) $values['feeDiscounted'];
         $claimLine = new ClaimLine();
         $claimLine->populateWithPatientProcedure($values['orm'], $visit);
         if ($newClaim) {
             $claimLine->claimLineId = 0;
             $claimLine->claimId = $claimId;
         }
         $claimLine->baseFee = $fee;
         $claimLine->adjustedFee = $feeDiscounted;
         $claimLine->persist();
         $claimLineId = (int) $claimLine->claimLineId;
         $billable = $feeDiscounted;
         /*$discount = 0;
         		if ($feeDiscounted > 0) $discount = $fee - $feeDiscounted;
         		if ($discount < 0) $discount = 0;*/
         $discount = (double) $values['writeoff'];
         if ($newClaim && $discount > 0) {
             // add writeoffs
             $writeOff = new WriteOff();
             $writeOff->personId = $personId;
             $writeOff->claimLineId = $claimLineId;
             $writeOff->visitId = $visitId;
             $writeOff->appointmentId = $visit->appointmentId;
             $writeOff->amount = $discount;
             $writeOff->userId = $userId;
             $writeOff->timestamp = date('Y-m-d H:i:s');
             $writeOff->title = 'discount';
             $writeOff->payerId = $discountPayerId;
             $writeOff->persist();
             $billable -= $discount;
         }
         if ($newClaim && $billable > 0) {
             foreach ($copay['details'] as $paymentId => $payment) {
                 $amount = (double) $payment->unallocated;
                 if (!$amount > 0) {
                     unset($copay['details'][$paymentId]);
                     continue;
                 }
                 if ($amount > $billable) {
                     $amount = $billable;
                 }
                 $payment->allocated += $amount;
                 $payment->payerId = $payerId;
                 $payment->persist();
                 $copay['details'][$paymentId] = $payment;
                 $totalPaid += $amount;
                 $postingJournal = new PostingJournal();
                 $postingJournal->paymentId = (int) $payment->paymentId;
                 $postingJournal->patientId = $personId;
                 $postingJournal->payerId = $payerId;
                 $postingJournal->claimLineId = $claimLineId;
                 $postingJournal->visitId = $visitId;
                 $postingJournal->amount = $amount;
                 $postingJournal->note = 'copay posting';
                 $postingJournal->userId = $userId;
                 $dateTime = date('Y-m-d H:i:s');
                 $postingJournal->datePosted = $dateTime;
                 $postingJournal->dateTime = $dateTime;
                 $postingJournal->persist();
                 $billable -= $amount;
                 if ($billable <= 0) {
                     break;
                 }
             }
         }
         $hasProcedure = true;
     }
     if ($newClaim && $copay['total'] > $totalPaid) {
         // if copay is greater than all claimlines reamining dollars are posted to credit program
         foreach ($copay['details'] as $paymentId => $payment) {
             $amount = (double) $payment->unallocated;
             $payment->allocated += $amount;
             $payment->persist();
             $postingJournal = new PostingJournal();
             $postingJournal->paymentId = (int) $payment->paymentId;
             $postingJournal->patientId = $personId;
             $postingJournal->payerId = $creditPayerId;
             $postingJournal->visitId = $visitId;
             $postingJournal->amount = $amount;
             $postingJournal->note = 'remaining copay balance';
             $postingJournal->userId = $userId;
             $dateTime = date('Y-m-d H:i:s');
             $postingJournal->datePosted = $dateTime;
             $postingJournal->dateTime = $dateTime;
             $postingJournal->persist();
         }
     }
     if (!$hasProcedure) {
         $visitId = $visit->visitId;
         $payment = new Payment();
         foreach ($payment->getIteratorByVisitId($visitId) as $row) {
             // If visit has copay then at closing copay should be turned into unallocated payment (not associated with visit).
             $row->visitId = 0;
             $row->persist();
         }
     } else {
         $visit = ClaimRule::checkRules($visit, $fees);
     }
     return $visit;
 }
 public function processManualJournalAction()
 {
     $params = $this->_getParam('journal');
     $data = array('error' => 'Invalid parameters');
     if (is_array($params)) {
         $postingJournal = new PostingJournal();
         $postingJournal->populateWithArray($params);
         $postingJournal->userId = (int) Zend_Auth::getInstance()->getIdentity()->personId;
         $postingJournal->dateTime = date('Y-m-d H:i:s');
         $postingJournal->persist();
         $msg = 'Posting journal was successfully saved';
         $data = array('msg' => $msg);
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct($data);
 }
 public function processPaymentsAction()
 {
     $visitId = (int) $this->_getParam('visitId');
     $params = $this->_getParam('payment');
     $paidAmounts = $this->_getParam('paid');
     $writeOffAmounts = $this->_getParam('writeOff');
     $note = $this->_getParam('note');
     $sourceOfFunds = $this->_getParam('sourceOfFunds');
     $checkNo = $this->_getParam('checkNo');
     $userId = (int) Zend_Auth::getInstance()->getIdentity()->personId;
     // TODO: need to record payment as a postingJournal entry against the new payment row that represents the new check # or other source of unallocated funds
     if (is_array($params)) {
         $visit = new Visit();
         $visit->visitId = $visitId;
         $visit->populate();
         $payment = new Payment();
         $payment->populateWithArray($params);
         $payerId = (int) $payment->payerId;
         $personId = (int) $payment->personId;
         $visitId = (int) $payment->visitId;
         $paymentDate = $payment->paymentDate;
         $otm = new WebVista_Model_ORMTransactionManager();
         if (is_array($paidAmounts)) {
             $payments = array();
             if ($sourceOfFunds == 'checkFundsId') {
                 // check funds
                 // uniqueCheckNumbers
                 $checkFunds = Payment::listCheckFunds($checkNo);
                 $payments = $checkFunds['details'];
             } else {
                 // unallocated funds
                 $unAllocatedFunds = $visit->unallocatedFunds;
                 $payments = $unAllocatedFunds['details'];
             }
             $firstRow = strlen($note) > 0 ? false : true;
             foreach ($paidAmounts as $claimLineId => $amount) {
                 $amount = (double) $amount;
                 if (!$amount > 0) {
                     continue;
                 }
                 $billable = $amount;
                 foreach ($payments as $paymentId => $payment) {
                     $amount = (double) $payment->unallocated;
                     if (!$amount > 0) {
                         unset($payments[$paymentId]);
                         continue;
                     }
                     if ($amount > $billable) {
                         $amount = $billable;
                     }
                     $payment->allocated += $amount;
                     $payment->persist();
                     $otm->addORM($payment);
                     $payments[$paymentId] = $payment;
                     $postingJournal = new PostingJournal();
                     $postingJournal->paymentId = (int) $payment->paymentId;
                     $postingJournal->patientId = $personId;
                     $postingJournal->payerId = $payerId;
                     $postingJournal->claimLineId = $claimLineId;
                     $postingJournal->visitId = $visitId;
                     $postingJournal->amount = $amount;
                     $postingJournal->note = $note;
                     $postingJournal->userId = $userId;
                     $dateTime = date('Y-m-d H:i:s');
                     $postingJournal->datePosted = $paymentDate;
                     $postingJournal->dateTime = $dateTime;
                     $postingJournal->persist();
                     $otm->addORM($postingJournal);
                     $billable -= $amount;
                     if ($billable <= 0) {
                         break;
                     }
                 }
                 if (!$firstRow) {
                     // persist payment note on first claim
                     $claimLine = new ClaimLine();
                     $claimLine->claimLineId = (int) $claimLineId;
                     if ($claimLine->populate()) {
                         $claimLine->note = $note;
                         $claimLine->persist();
                         $otm->addORM($claimLine);
                         $firstRow = true;
                     }
                 }
             }
         }
         if (is_array($writeOffAmounts)) {
             $writeOff = new WriteOff();
             $writeOff->populateWithArray($params);
             $writeOff->userId = (int) Zend_Auth::getInstance()->getIdentity()->personId;
             $writeOff->timestamp = date('Y-m-d H:i:s');
             $writeOff->payerId = $payerId;
             foreach ($writeOffAmounts as $claimLineId => $amount) {
                 if (!$amount > 0) {
                     continue;
                 }
                 $writeOff->writeOffId = 0;
                 $writeOff->claimLineId = (int) $claimLineId;
                 $writeOff->amount = $amount;
                 $writeOff->persist();
                 $otm->addORM($writeOff);
             }
         }
         if (count($otm->getQueries()) > 0 && !$otm->persist()) {
             trigger_error(__('Failed to save.'));
         }
     }
     $unallocatedFunds = 0;
     if (isset($visit)) {
         $funds = $visit->unallocatedFunds;
         $unallocatedFunds = (double) $funds['total'];
     }
     if ($unallocatedFunds < 0) {
         $unallocatedFunds = 0;
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct(sprintf('%.2f', $unallocatedFunds));
 }