public function calculateFees($recompute = null) { // pass true or false to override visit.closed checking if ($recompute === null) { $recompute = $this->closed ? false : true; } $visitId = (int) $this->encounter_id; $total = 0; $discounted = 0; $visitFlat = 0; $visitPercentage = 0; $codeFlat = 0; $codePercentage = 0; $discountApplied = array(); if ($recompute) { $insuranceProgramId = (int) $this->activePayerId; $dateOfVisit = date('Y-m-d', strtotime($this->date_of_treatment)); $statistics = PatientStatisticsDefinition::getPatientStatistics((int) $this->patient_id); $familySize = isset($statistics['family_size']) ? $statistics['family_size'] : 0; $monthlyIncome = isset($statistics['monthly_income']) ? $statistics['monthly_income'] : 0; $retDiscount = DiscountTable::checkDiscount($insuranceProgramId, $dateOfVisit, $familySize, $monthlyIncome); if ($retDiscount !== false) { $discount = (double) $retDiscount['discount']; switch ($retDiscount['discountType']) { case DiscountTable::DISCOUNT_TYPE_FLAT_VISIT: $discountApplied[] = 'Flat Visit: $' . $discount; $visitFlat += $discount; break; case DiscountTable::DISCOUNT_TYPE_FLAT_CODE: $discountApplied[] = 'Flat Code: $' . $discount; $codeFlat += $discount; break; case DiscountTable::DISCOUNT_TYPE_PERC_VISIT: $discountApplied[] = 'Percentage Visit: ' . $discount . '%'; $visitPercentage += $discount / 100; break; case DiscountTable::DISCOUNT_TYPE_PERC_CODE: $discountApplied[] = 'Percentage Code: ' . $discount . '%'; $codePercentage += $discount / 100; break; } } } else { $claimLineFees = array(); $iterator = ClaimLine::mostRecentClaims($visitId); foreach ($iterator as $claimLine) { $code = $claimLine->procedureCode; if (!isset($claimLineFees[$code])) { $claimLineFees[$code] = array('baseFee' => 0, 'adjustedFee' => 0); } $claimLineFees[$code]['baseFee'] += (double) $claimLine->baseFee; $claimLineFees[$code]['adjustedFee'] += (double) $claimLine->adjustedFee; } } $details = array(); $iterator = new PatientProcedureIterator(); $iterator->setFilters(array('visitId' => $visitId)); $firstProcedureId = null; foreach ($iterator as $patientProcedure) { $patientProcedureId = (int) $patientProcedure->patientProcedureId; $code = $patientProcedure->code; $quantity = (int) $patientProcedure->quantity; $writeoff = 0; if ($recompute) { $fee = '-.--'; $feeDiscounted = '-.--'; $discountedRate = ''; $retFee = FeeSchedule::checkFee($insuranceProgramId, $dateOfVisit, $code); if ($retFee !== false && (double) $retFee['fee'] != 0) { $fee = (double) $retFee['fee']; $tmpFee = 0; for ($i = 1; $i <= 4; $i++) { $modifier = 'modifier' . $i; if (!strlen($patientProcedure->{$modifier}) > 0) { continue; } switch ($patientProcedure->{$modifier}) { case $retFee['modifier1']: $tmpFee += (double) $retFee['modifier1fee']; break 2; case $retFee['modifier2']: $tmpFee += (double) $retFee['modifier2fee']; break 2; case $retFee['modifier3']: $tmpFee += (double) $retFee['modifier3fee']; break 2; case $retFee['modifier4']: $tmpFee += (double) $retFee['modifier4fee']; break 2; } } if ($tmpFee > 0) { $fee = $tmpFee; } if ($quantity > 0) { $fee *= $quantity; $feeDiscounted *= $quantity; } // calculate discounts if ($codeFlat > 0) { $tmpDiscount = $fee - $codeFlat; if ($tmpDiscount < 0) { $tmpDiscount = 0; } $feeDiscounted += $tmpDiscount; $writeoff = $tmpDiscount; } if ($firstProcedureId !== null && $visitFlat > 0) { $writeoff = $fee; trigger_error('VISIT FLAT: ' . $visitFlat); trigger_error('WRITEOFF: ' . $writeoff); } if ($codePercentage > 0) { $tmpDiscount = $feeDiscounted * $codePercentage; if ($tmpDiscount < 0) { $tmpDiscount = 0; } $feeDiscounted += $tmpDiscount; $writeoff = $tmpDiscount; } } if ($firstProcedureId === null) { $firstProcedureId = $patientProcedureId; } } else { if (isset($claimLineFees[$code])) { $fee = $claimLineFees[$code]['baseFee']; $feeDiscounted = $claimLineFees[$code]['adjustedFee']; } else { $fee = $patientProcedure->baseFee; $feeDiscounted = $patientProcedure->adjustedFee; } if ($quantity > 0) { $fee *= $quantity; $feeDiscounted *= $quantity; } } /*$quantity = (int)$patientProcedure->quantity; if ($quantity > 0) { $fee *= $quantity; $feeDiscounted *= $quantity; }*/ $total += $fee; $discounted += (double) $feeDiscounted; $details[$patientProcedureId] = array(); $details[$patientProcedureId]['orm'] = $patientProcedure; $details[$patientProcedureId]['fee'] = $fee; $details[$patientProcedureId]['feeDiscounted'] = $feeDiscounted; $details[$patientProcedureId]['writeoff'] = $writeoff; } if ($visitFlat > 0) { $discounted += $visitFlat; // update the first procedure if ($firstProcedureId !== null) { $details[$firstProcedureId]['feeDiscounted'] += $visitFlat; $writeoff = $details[$firstProcedureId]['fee'] - $details[$firstProcedureId]['feeDiscounted']; if ($writeoff < 0) { $writeoff = 0; } $details[$firstProcedureId]['writeoff'] = $writeoff; } } if ($visitPercentage > 0) { $discounted += $discounted * $visitPercentage; // update the first procedure if ($firstProcedureId !== null) { $details[$firstProcedureId]['feeDiscounted'] += $details[$firstProcedureId]['feeDiscounted'] * $visitFlat; } } $row = array(); $row['discountApplied'] = $discountApplied; $row['details'] = $details; $row['total'] = $total; $row['discounted'] = $discounted; return $row; }
public function recalculateBaseFee(Visit $visit) { $fee = 0; $retFee = FeeSchedule::checkFee($this->insuranceProgramId, substr($visit->dateOfTreatment, 0, 10), $this->procedureCode); if ($retFee !== false && (double) $retFee['fee'] != 0) { $fee = (double) $retFee['fee']; $tmpFee = 0; for ($i = 1; $i <= 4; $i++) { $modifier = 'modifier' . $i; if (!strlen($this->{$modifier}) > 0) { continue; } switch ($this->{$modifier}) { case $retFee['modifier1']: $tmpFee += (double) $retFee['modifier1fee']; break; case $retFee['modifier2']: $tmpFee += (double) $retFee['modifier2fee']; break; case $retFee['modifier3']: $tmpFee += (double) $retFee['modifier3fee']; break; case $retFee['modifier4']: $tmpFee += (double) $retFee['modifier4fee']; break; } } if ($tmpFee > 0) { $fee = $tmpFee; } } $units = (int) $this->units; if ($units > 0) { $fee *= $units; } $this->baseFee = $fee; }