Exemple #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;
 }