<cfcomponent hint="Calc Medicare RAP Amount">

<cffunction name="getRAP" access="public" output="No" returntype="string">

 <cfargument name="Assmt_ID" type="string" required="Yes" />

 <cfset NatStandardPayment=2270.32>    <!--- Always this value --->
 <cfset LaborA=.77082>                 <!--- Always this value --->
 <cfset NonLaborB=.22918>              <!--- Always this value --->
 <cfset LaborNonLaborSupply=0>

 <cfset amt = #LTrim(NumberFormat(NatStandardPayment, "_,___,___.__"))# />

 <cfset Today = Now() />
 <cfquery  datasource="#Application.DataSrc#" name="Assessments">
	SELECT  pPatients.Loc_ID AS Agency
		,pAssessments.Patient_ID ,pAssessments.F26  AS Assessment_Reason_M0100 ,pAssessments.F179 AS Start_of_Care_M0030
		,pAssessments.F25 AS Admission_Status ,pAssessments.F176 AS Assessment_Completed_M0090
		,pAssessments.F178 AS Certification_End ,pPatients.Patient_ID
		,pPatients.Pt_Last AS PatientLast ,pPatients.Pt_First AS PatientFirst
		,pAssessments.F300 AS HIPPS_Code ,Agency.Wage_Index ,casmix_lookup.Weights AS Weight
	FROM  #Request.prefix_db_agency#.pPatients
	INNER JOIN  #Request.prefix_db_agency#.pAssessments ON pPatients.Patient_ID = pAssessments.Patient_ID
	INNER JOIN #Request.prefix_db_lookup#.Agency ON pPatients.Loc_ID = Agency.Agency_ID
	LEFT JOIN  #Request.prefix_db_lookup#.casmix_lookup ON LEFT(pAssessments.F300, 4) = casmix_lookup.HIPPS_Code
	WHERE pPatients.Loc_ID = #session.AgencyID#
    AND   pAssessments.Assmt_ID = '#Assmt_ID#'
	AND   pAssessments.status = 0
	AND   pAssessments.F300 != ""
	AND  (pAssessments.F26 LIKE '1%' OR pAssessments.F26 LIKE '4%')
	AND   pAssessments.F176 >= DATE_ADD(NOW(), INTERVAL -90 DAY)
	ORDER BY pPatients.Pt_Last, pAssessments.F176 DESC 
 </cfquery>

<!--- <cfdump var="#Assessments#"> --->

 <cfoutput query="Assessments" group="Patient_ID">

  <cfset PatientHippsCode=#HIPPS_Code#> <!--- pAssessments.HIPPS_Code --->
  <cfset CaseMixWeight=#Weight#>        <!--- Matching first 4 digits of Patient HIPPS to case_mix table --->
  <cfset AgencyWageIndex=#Wage_Index#>  <!--- Agency.Wage_Index --->

  <cfif #CaseMixWeight# neq "">
   <cfset C=#CaseMixWeight#*#NatStandardPayment#>
   <cfset CAWage=#NumberFormat((#C#*#LaborA#)*#AgencyWageIndex#, "_______.__")#>
   <cfset CB=#NumberFormat(#C#*#NonLaborB#, "_______.__")#>
   <cfset SupplyBase=#MID(PatientHippsCode,5,1)#>
   <cfset SupplyAmount=0>
   <cfif SupplyBase eq "S">
    <cfset SupplyAmount=14.12>
   <cfelseif SupplyBase eq "T">
    <cfset SupplyAmount=51.00>
   <cfelseif SupplyBase eq "U">
    <cfset SupplyAmount=139.84>
   <cfelseif SupplyBase eq "V">
    <cfset SupplyAmount=207.76>
   <cfelseif SupplyBase eq "W">
    <cfset SupplyAmount=320.37>
   <cfelseif SupplyBase eq "X">
    <cfset SupplyAmount=551.00>
   </cfif>
   <cfset LaborNonLaborSupply=#NumberFormat(#CAWage#+#CB#+#SupplyAmount#, "_,___,___.__")#>
		<!---
			!!!!!!#HIPPS_Code#::#Wage_Index#::#Weight#::#CAWage#::#CB#::#SupplyBase#::#SupplyAmount#::#LaborNonLaborSupply#!!!!!
		--->
   <cfset amt = LTRIM(RTRIM(LaborNonLaborSupply)) />
  </cfif>
 </cfoutput>
 <cfreturn amt />
</cffunction>
</cfcomponent>
