{% set absUrl = app.request.getSchemeAndHttpHost() ~ app.request.baseUrl|replace({'/app_dev.php': ''}) %}
{% set customBody = patientLetter.customBody|replace({'{{PATIENT-FIRSTNAME}}' :  patientLetter.patient.firstName, '{{PATIENT-FULLNAME}}' : patientLetter.patient.fullName, '{{PATIENT-GENDER}}' : patientLetter.patient.gender == 'Male' ? 'he' : 'she', '{{PATIENT-GENDER2}}' : patientLetter.patient.gender == 'Male' ? 'him' : 'her', '{{PATIENT-GENDER3}}' : patientLetter.patient.gender == 'Male' ? 'his' : 'her', '{{PATIENT-GENDER4}}' : patientLetter.patient.gender == 'Male' ? 'He' : 'She'}) %}
{% set userData = app.session.get('userData') %}
<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
<head>
	<meta charset="utf-8">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>DSRC Letter</title>
</head>
<body style="font-family:'Arial'; font-size:18px;">
{% if patientLetter.patientReferringProvider %}
    {% set rp = patientLetter.patientReferringProvider.referringProvider %}
    {% set patient = patientLetter.patient %}
    {% set receiverFullName = patientLetter.patientReferringProvider.referringProvider.honorific ~ ' ' ~ patientLetter.patientReferringProvider.referringProvider.fullName|title %}
    {% if patientLetter.patientReferringProvider.referringProvider.phoneNo is null or patientLetter.patientReferringProvider.referringProvider.phoneNo == '' %}
        {% set receiverPhone = '' %}
        {% for prhf in patient.patientReferringHealthFacilities %}
            {% for rhfp in rp.referringHealthFacilityProviders %}
                {% if prhf.referringHealthFacility.id == rhfp.referringHealthFacility.id %}
                    {% set receiverPhone = prhf.referringHealthFacility.phoneNo %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% else %}
        {% set receiverPhone = patientLetter.patientReferringProvider.referringProvider.phoneNo %}
    {% endif %}
    {% if patientLetter.patientReferringProvider.referringProvider.faxNo is null or patientLetter.patientReferringProvider.referringProvider.faxNo == '' %}
        {% set receiverFax = '' %}
        {% for prhf in patient.patientReferringHealthFacilities %}
            {% for rhfp in rp.referringHealthFacilityProviders %}
                {% if prhf.referringHealthFacility.id == rhfp.referringHealthFacility.id %}
                    {% set receiverFax = prhf.referringHealthFacility.faxNo %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% else %}
        {% set receiverFax = patientLetter.patientReferringProvider.referringProvider.faxNo %}
    {% endif %}
{% endif %}
{% if patientLetter.patientReferringHealthFacility %}
    {% set receiverFullName = patientLetter.patientReferringHealthFacility.referringHealthFacility.description %}
    {% set receiverPhone = patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNo is null ? patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNoTwo : patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNo %}
    {% set receiverFax = patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNo is null ? patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNoTwo : patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNo %}
{% endif %}
{% if patientLetter.patientInsurance %}
    {% set receiverFullName = patientLetter.patientInsurance.insurance.description %}
    {% set receiverPhone = patientLetter.patientInsurance.insurance.phoneNo is null ? patientLetter.patientInsurance.insurance.phoneNoTwo : patientLetter.patientInsurance.insurance.phoneNo %}
    {% set receiverFax = patientLetter.patientInsurance.insurance.faxNo is null ? patientLetter.patientInsurance.insurance.faxNoTwo : patientLetter.patientInsurance.insurance.faxNo %}
{% endif %}
{% set patientFullName = patientLetter.patient.fullName %}
{% set patientFirstName = patientLetter.patient.firstName %}
{% set patientLastName = patientLetter.patient.lastName %}
{% set patient = patientLetter.patient %}

{% set faxCoverCustomBody = patientLetter.faxCoverCustomBody|replace({'{{RECEVIER-FULLNAME}}' :  receiverFullName, '{{PATIENT-FULLNAME}}' : patientFullName, '{{USERDATA-FULLNAME}}' : userData.fullName}) %}

{% set referringPhysicianFullName = '' %}
{% set referringPhysicianPhone = '' %}
{% set referringPhysicianFax = '' %}
{% set referringPhysicianClinic = '' %}
{% set referringPhysicianClinicAddress = '' %}
{% if patientLetter.patientReferringProvider %}

	{% set referringPhysicianPhone = patientLetter.patientReferringProvider.referringProvider.phoneNo is null ? patientLetter.patientReferringProvider.referringProvider.phoneNoTwo : patientLetter.patientReferringProvider.referringProvider.phoneNo %}
	{% set referringPhysicianFax = patientLetter.patientReferringProvider.referringProvider.faxNo is null ? patientLetter.patientReferringProvider.referringProvider.faxNoTwo : patientLetter.patientReferringProvider.referringProvider.faxNo %}
	{% set referringPhysicianFullName = patientLetter.patientReferringProvider.referringProvider.honorific ~ ' ' ~ patientLetter.patientReferringProvider.referringProvider.fullName|title %}
	{% for referringClinic in patientLetter.patient.patientReferringHealthFacilities %}
		{% if (referringClinic.endDate == null and referringClinic.startDate != null) or referringClinic.endDate|date('m/d/Y') > 'now'|date('m/d/Y')  %}
			{% set receiverClinic = referringClinic.referringHealthFacility.description|default('') %}
			{% set receiverClinicAddress = referringClinic.referringHealthFacility.getReferringAffiliateAddress|default('') %}
		{% endif %}
	{% endfor %}
{% endif %}

<div style="width:768px; position: relative; padding:0px; margin: 0 auto; box-shadow:3px 1px 10px 1px;">
	<div style="font-size: 38px;">CPAP Prescription Form</div>
	<div>
		<div style="width: 100%; float: left;">
			<div style="width: 50%; float: left; font-size: 17px;">
				<p>Name:{{ patientLetter.patient.fullName }}</p>
				<p>PHN: {{ patientLetter.patient.personalHealthNo|default('') }}</p>
				<p>ATTENTION, DR.: {{ referringPhysicianFullName|default('') }}</p>
				<p>Interpretation Completed by Dr. {{ patientPrescription.interpretationCompletedDoctor|default('') }}</p>
			</div>
			<div style="width: 50%; float: right; font-size: 17px;">
				<p>Date: {{ patientLetter.createdAt|default('')|date('F j, Y') }}</p>
				<p>DOB: {{ patientLetter.patient.birthdate|default('')|date('F j, Y') }}</p>
				<p>Date of Study: {{ patientPrescription.dateOfStudyAsString|default('')|date('F j, Y') }}</p>
				<p>Findings Consistent With: {{ patientPrescription.findingConsistent|default('') }}</p>
			</div>
		</div>
	</div>
	<div style="width: 100%; float: left;">
		<div style="width: 70%; border: solid 1px; height: 365px; float: left; padding: 2px; font-size:13px;">
			<span>Recommendation</span>
			<div style="margin-left: 7px; margin-top: 2px;">{{ patientPrescription.recommendation|default('')|raw }}</div>
		</div>
		<div style="width: 28%; height: 350px; float: right; padding: 2px; margin-left: 5px;">
			<p style="text-align: center;"><span style="font-size: 28px;">AHI</span><br/>(Apnea-Hypopnea Index)</p>
			<p style="margin-top: 20px; text-align: center; font-size: 30px;"><strong>{{ patientPrescription.ahi|default('') }}</strong></p>
			<p style="margin-top: 70px; text-align: center;"><span style="font-size: 28px;">RDI</span><br/>(Respiratory Disturbance Index)</p>
			<p style="margin-top: 20px; text-align: center; font-size: 30px;"><strong>{{ patientPrescription.rdi|default('') }}</strong></p>
		</div>
	</div>
	<div style="width: 100%; float: left;">
		<p><strong>PHYSICIAN USE ONLY</strong></p>
		<div>
			<div><input type="checkbox" {{ patientPrescription.isCpapTheraphy|default('') == true ? 'checked' : '' }}> CPAP Therapy (Auto) <u>{{ patientPrescription.cpapTheraphyOne|default('_________') }}</u> cmH<sub>2</sub>0 to <u>{{ patientPrescription.cpapTheraphyTwo|default('_________') }}</u> cmH<sub>2</sub>0</div>
			<div style="margin-top: 14px; margin-left: 15px;">(CPAP) <u>{{ patientPrescription.cpapTheraphyThree|default('_________') }}</u> cmH<sub>2</sub>0</div>
			<div style="margin-top: 14px;"><input type="checkbox" {{ patientPrescription.isReferralToSleepSpecialist|default('') == true ? 'checked' : '' }}> Referral to Sleep Specialist / Level 1 PSG</div>
			<div style="margin-top: 14px;"><input type="checkbox" {{ patientPrescription.isOralAppliance|default('') == true ? 'checked' : '' }}> Oral Appliance</div>
		</div>
	</div>
	<div style="width: 100%; float: left;">
		<div style="width: 50%; float: left;">
			<p>Doctor:</p>
			<p>Doctor's Signature:</p>
			<p>Date:</p>
		</div>
		<div style="width: 50%; float: right;">
			<p>Stamp</p>
		</div>
	</div>
	<hr>
	<p style="font-size: 10px;">"This Facsimile transmission contains information which is confidential and/or privileged. This information is intended for use only by the addressee indicated above. If you are not the intended recipient, please be advised that any disclosure, copying, distribution or use of the contents of this information is stricly prohibited, and that any misdirected or improperly received information must be returned to this company immediately. Your cooperation in phoning us of erroneous receipt is requested."</p>
</div>
</body>
</html>