<!---
This file is Copyright © 1998-2009 by Home Care Business Services. This work may not be reproduced, in whole or in part, 
using any medium, including, but not limited to, electronic transmission, CD-ROM or published in print, without the express 
permission of Home Care Business Services.

$Revision: 1.4 $
$Id: communication_action.cfm,v 1.4 2009/03/25 19:32:31 arlen Exp $
$Date: 2009/03/25 19:32:31 $
--->

<cfinclude template='/__Security-Block.cfm' />

<cfset toEmail = "" />
<cfset toText = "" />
<cfset postedBy = "" />

<cfparam name='form.patient_page' default='' />
<cfparam name="form.resolved" default="No">
<cfparam default = '' name='url.next' />

<cfparam default='' name='url.comm_id' />
 <cfif url.comm_id neq ''><cfset form.comm_id = url.comm_id /></cfif>
<cfparam default='' name='url.commsub_id' />
 <cfif url.commsub_id neq ''><cfset form.commsub_id = url.commsub_id /></cfif>
<cfparam default='' name='url.docId' />
<cfparam default='' name='url.employee_id' />
 <cfif url.employee_id neq ''><cfset form.employee_id = url.employee_id /></cfif>
<cfparam default='' name='url.patient_id' />
 <cfif url.patient_id neq ''><cfset form.patient_id = url.patient_id /></cfif>
<cfparam default='' name='url.comm_date' />
 <cfif url.comm_date neq ''><cfset form.comm_date = url.comm_date /></cfif>
<cfparam default='' name='url.memo' />
 <cfif url.memo neq ''><cfset form.memo = url.memo /></cfif>
<cfparam default='' name='url.resolved' />
 <cfif url.resolved neq ''><cfset form.resolved = url.resolved /></cfif>
<cfparam default='' name='url.followup_desc' />
 <cfif url.followup_desc neq ''><cfset form.followup_desc = url.followup_desc /></cfif>
<cfparam default='' name='url.followup_memo' />
 <cfif url.followup_memo neq ''><cfset form.followup_memo = url.followup_memo /></cfif>

<cfif isDefined("form.Comm_Id")>
 <cfif form.Comm_ID EQ 0>
  <cfquery  name="Comm" datasource="#Application.DataSrc#">
	 INSERT INTO #Request.prefix_db_agency#.Communication (
		CommSub_ID,
		Patient_ID,
		Employee_ID,
		Comm_Date,
		Memo,
		Resolved,
		Agency_ID,
		Followup_desc,
		Followup_memo,
		Record_Creation_Date,
		Record_Created_By,
		Record_Mod_Date,
		Record_Mod_By
	 ) VALUES (
		'#Trim(form.CommSub_Id)#',
		'#Trim(form.Patient_Id)#',
		<cfif url.docId neq ''>#url.docId#<cfelseif url.employee_id neq ''>#url.employee_id#<cfelse>#Session.EmployeeID#</cfif>,
		#CreateODBCDate(form.Comm_Date)#,
		'#Trim(form.Memo)#',
		'#Trim(form.Resolved)#',
		#session.AgencyID#,
		'#Trim(form.Followup_desc)#',
		'#Trim(form.Followup_memo)#',
		'#myDateTime#',
		<cfif url.docId neq ''>#url.docId#<cfelseif url.employee_id neq ''>#url.employee_id#<cfelse>#Session.EmployeeID#</cfif>,
		'#myDateTime#',
		<cfif url.docId neq ''>#url.docId#<cfelseif url.employee_id neq ''>#url.employee_id#<cfelse>#Session.EmployeeID#</cfif>
   )
  </cfquery>
  <cfset id = 0 />
  <cfquery  name='getId' datasource='#Application.DataSrc#'>
   Select LAST_INSERT_ID() AS id
  </cfquery>
  <cfset id = getId.id>
  <cfset form.comm_id = id />
  <cfset q = #ListLen(form.employee_id, ",")# />
  <cfset a = #ListToArray(form.employee_id, ",")# />
  <cfloop index="i" from="1" to="#q#" step="1">
   <cfquery  name='commInsert' datasource='#Application.DataSrc#'>
    INSERT INTO #Request.prefix_db_agency#.communication_to (
       comm_id, employee_id
    ) VALUES (
      '#id#', '#a[i]#'
    )
   </cfquery>

   <cfif CGI.SERVER_NAME NEQ "76.12.35.143">
	  <cfquery  name='commEmails' datasource='#Application.DataSrc#'>
     SELECT e.email, e.cell_phone
           ,(select domain from #Request.prefix_db_lookup#.mobile_domains where id = e.mobiledomain) AS provider
		 FROM #Request.prefix_db_lookup#.Employee e
		 WHERE  e.employee_id = '#a[i]#'
    </cfquery>

    <cfoutput query='commEmails'>
     <cfif commEmails.email neq ''>
      <cfif toEmail eq ''>
       <cfset toEmail = #commEmails.email# />
      <cfelse>
       <cfset toEmail = toEmail & ", " & #commEmails.email# />
      </cfif>
     </cfif>
     <cfif commEmails.cell_phone neq '' and commEmails.provider neq ''>
      <cfset texts = REReplace(commEmails.cell_phone, "[^0-9_]", "", "ALL")>
      <cfset texts = texts & commEmails.provider />
      <cfif toText eq ''>
       <cfset toText = #texts# />
      <cfelse>
       <cfset toText = toText & ", " & #texts# />
      </cfif>
     </cfif>
    </cfoutput>
   </cfif> <!--- end Not DEV --->
	</cfloop> 

  <cfset ReplyTo = "info@MyHomeCareBiz.com" />
  <cfquery  name='GetReplyTo' datasource='#Application.DataSrc#'>
    SELECT e.email, e.first_name, e.last_name
    FROM   #Request.prefix_db_agency#.Communication c, #Request.prefix_db_lookup#.Employee e
    WHERE  c.comm_id = '#id#'
    AND    e.employee_id = c.Record_Created_By
  </cfquery>
  <cfset postedBy = GetReplyTo.first_name & " " & GetReplyTo.last_name />
  <cfif GetReplyTo.recordcount eq 1 and GetReplyTo.email neq ''>
   <cfset ReplyTo = GetReplyTo.email />
  </cfif>




  <cfif toEmail neq ''>
    <cfset valid = REFind("^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", 
Trim(#toText#), 1) gt 0>
	<!---<cfif valid EQ 'YES'>--->
		 <cfmail to = "#toEmail#"
				 from = "info@myhomecarebiz.com" 
				 replyto = "#ReplyTo#"
			 subject = "MyHomeCareBiz.com New Communication..."
				 type = "html">
				<br><br>
			   A New Communication has been Assigned to You at MyHomeCareBiz.com by #postedBy#.
				<br><br>
			   Please <a href='https://secure.myhomecarebiz.com/agencyadmin/comm.cfm?cid=#id#'>click here</a> to View the New Communication.
				<br><br>
				<br><br>
			   <i>
				This is an automated system notification from MyHomeCareBiz.com. Therefore, there is no Unsubscribe option.
			   </i>
				<br><br><br><br><br><br>
	   </cfmail>
  <!---	</cfif>--->
  </cfif>
  <cfif toText neq ''>
  <cfset valid = REFind("^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", 
Trim(#toText#), 1) gt 0>
	<cfif valid EQ 'YES'>
		 <cfmail to = "#toText#"
				 from = "info@myhomecarebiz.com" 
				 replyto = "#ReplyTo#"
			 subject = "MyHomeCareBiz.com New Communication..."
				 type = "text">
				A New Communication has been Assigned to You at MyHomeCareBiz.com by #postedBy#. Please <a href='https://secure.myhomecarebiz.com/agencyadmin/comm.cfm?cid=#id#'>click here</a> to View the New Communication.
	   </cfmail>
   </cfif>
  </cfif>
 <cfelse>
  <cfquery  name="Comm" datasource="#Application.DataSrc#">
	 UPDATE #Request.prefix_db_agency#.Communication SET
		CommSub_ID = '#Trim(form.CommSub_ID)#',
		Patient_ID = '#Trim(form.Patient_ID)#',
		Employee_ID = '#Trim(session.EmployeeId)#',
		Comm_Date = #CreateODBCDate(form.Comm_Date)#,
		Memo = '#Trim(form.Memo)#',
		Resolved = '#form.Resolved#',
		Followup_desc = '#Trim(form.Followup_desc)#',
		Followup_memo = '#Trim(form.Followup_memo)#',
		Record_Mod_Date = '#myDateTime#',
		Record_Mod_By = #Session.EmployeeID#
	 WHERE Comm_ID = #form.Comm_Id#
  </cfquery>
  <cfquery  name='delCommTo' datasource='#Application.DataSrc#'>
   DELETE FROM #Request.prefix_db_agency#.communication_to
	 WHERE  comm_id = '#form.Comm_Id#'
   AND    employee_id NOT IN (#form.employee_id#)
  </cfquery>
  <cfset q = #ListLen(form.employee_id, ",")# />
  <cfset a = #ListToArray(form.employee_id, ",")# />
  <cfloop index="i" from="1" to="#q#" step="1">
   <cfquery  name='checkEmp'  datasource='#Application.DataSrc#'>
    SELECT id FROM #Request.prefix_db_agency#.communication_to WHERE comm_id = '#form.Comm_Id#' AND employee_id = '#a[i]#'
   </cfquery>
   <cfif checkEmp.RecordCount lt 1>
    <cfquery  name='commInsert' datasource='#Application.DataSrc#'>
     INSERT INTO #Request.prefix_db_agency#.communication_to (
        comm_id, employee_id
     ) VALUES (
       '#form.Comm_ID#', '#a[i]#'
     )
    </cfquery>
   </cfif>
	</cfloop> 
 </cfif>

 <cfif url.next neq ''>
  <cfif url.next eq 'physician_portal'>
   <cflocation url='/physicianportal/index.cfm?page=physicianportal&cat=&rpt=physician_portal' addtoken='no' />
  <cfelse>
   <cflocation url='/dashboard/index.cfm?page=dashboard&cat=chartcompliance&review=yes&rpt=dash_no_review.cfm' addtoken='No' />
  </cfif>
 <cfelseif form.patient_page neq '' and form.patient_id eq form.patient_page>
  <cflocation url="/patientadmin_new/index.cfm?page=patient&cat=Patient" addtoken="no">
 <cfelseif form.patient_page neq '' and form.patient_id gt 0>
  <cflocation url="/patientadmin_new/index.cfm?page=patient&cat=Patient" addtoken="no">
 <cfelse>
  <cflocation url="/dashboard/index.cfm?page=dashboard&cat=chartcompliance&review=yes&rpt=dash_no_review.cfm" addtoken="no">
 </cfif>

<cfelse>
 Unable to Create / Update Communication: Form Data is Missing
</cfif>

