<!---
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 = "" />
<cfscript>
function stripHTML(str) {
   return REReplaceNoCase(str,"<[^>]*>","","ALL");
}
</cfscript>

<cfparam name='form.patient_page' default='' />
<cfparam name="form.resolved" default="No" />
<cfparam name="form.employee_id" default="0" />
<cfparam name="form.Schedule_ID" default="0" />
<cfparam name="form.Progress_ID" default="0" />
<cfparam name="form.cancelled" default="0" />
<cfparam name="form.sendfax" default="0" /> 
<cfparam name="form.Reason_for_discharge" default="" /> 
<cfparam name="form.Discharge_Disposition" default="" /> 
<cfparam default = '' name='url.next' />
<cfparam name="form.redirectpage" default="">
<cfparam default='0' name='url.comm_id' />
 <cfif url.comm_id neq '0'><cfset form.comm_id = url.comm_id /></cfif>
<cfparam default='0' name='url.commsub_id' />
 <cfif url.commsub_id neq '0'><cfset form.commsub_id = url.commsub_id /></cfif>
<cfparam default='' name='url.docId' />
<cfparam default='0' name='url.employee_id' />
 <cfif url.employee_id neq '0'><cfset form.employee_id = url.employee_id /></cfif>
<cfparam default='0' name='url.patient_id' />
 <cfif url.patient_id neq '0'><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>

<!--- <cfset form.Memo = #stripHTML(form.Memo)# />
<cfset form.followup_memo = #stripHTML(form.followup_memo)# /> --->

<cfquery name='getpgcommunications' datasource='#Application.DataSrc#'>
         SELECT * FROM  #Request.prefix_db_agency#.Communication
          WHERE  Schedule_ID = #form.Schedule_ID#
          AND   Schedule_ID != 0
 </cfquery>
 <cfdump var="#form#">
<cfif isDefined("form.Comm_Id")>
 <cfif form.Comm_ID EQ 0 <!--- AND getpgcommunications.recordcount EQ 0 --->>
  <cfquery name="Comm" datasource="#Application.DataSrc#">
    INSERT INTO #Request.prefix_db_agency#.Communication (
      CommSub_ID,
      Patient_ID,
      Employee_ID,
      Schedule_ID,
      Comm_Date,
      Reason_for_discharge,
      Discharge_Disposition,
      Memo,
      Memo_copy,
      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 '0'>#url.employee_id#<cfelse>#Session.EmployeeID#</cfif>,
      #form.Schedule_ID#,
      #CreateODBCDate(form.Comm_Date)#,
      <cfqueryparam value="#Trim(form.Reason_for_discharge)#"  cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#Trim(form.Discharge_Disposition)#"  cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#Trim(form.Memo)#"  cfsqltype="CF_SQL_LONGNVARCHAR">,
      <cfqueryparam value="#Trim(form.Memo_copy)#"  cfsqltype="CF_SQL_LONGNVARCHAR">,
      '#Trim(form.Resolved)#',
      #session.AgencyID#,
      <cfqueryparam value="#Trim(form.Followup_desc)#"  cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#Trim(form.Followup_memo)#"  cfsqltype="cf_sql_varchar">,
      '#myDateTime#',
      <cfif url.docId neq ''>#url.docId#<cfelseif url.employee_id neq '0'>#url.employee_id#<cfelse>#Session.EmployeeID#</cfif>,
      '#myDateTime#',
      <cfif url.docId neq ''>#url.docId#<cfelseif url.employee_id neq '0'>#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 />
    <cfif form.Schedule_ID NEQ '0'>
        <cfquery name="update_schedules" datasource="#Application.DataSrc#">
            UPDATE #Request.prefix_db_agency#.pSchedules SET
                Comm_ID = '#Trim(form.comm_id)#'
            WHERE Schedule_ID  = #form.Schedule_ID#
            AND Status =0 
        </cfquery>
    </cfif>

    <cfif form.Progress_ID NEQ '0'>
       <cfquery name="Comm" datasource="#Application.DataSrc#">
            UPDATE #Request.prefix_db_agency#.pProgress SET
            Comm_ID = '#Trim(form.Comm_ID)#'
            WHERE Progress_ID  IN  (#form.Progress_ID#)
            AND Status =0 
        </cfquery>
         <cfquery name="Comm" datasource="#Application.DataSrc#">
            DELETE FROM #Request.prefix_db_agency#.pUnstable  
            WHERE Progress_ID  IN  (#form.Progress_ID#)
        </cfquery>
    </cfif>
    <cfif isdefined("cookie.progress_Ids")>
       <cfloop list="#cookie.progress_Ids#" index="progressId">
          <cfquery name="Comm" datasource="#Application.DataSrc#">
            INSERT INTO #Request.prefix_db_agency#.Comm_Progress
             (Comm_ID, Progress_ID, Status, Date_Created, Created_by, Date_Change, Change_by )
            values
             (
             <cfqueryparam VALUE="#form.comm_id#"  cfsqltype="cf_sql_integer" >,
             <cfqueryparam VALUE="#progressId#"  cfsqltype="cf_sql_integer" >,
             <cfqueryparam VALUE="0"  cfsqltype="cf_sql_integer" >,
             <cfqueryparam VALUE="#now()#"  cfsqltype="cf_sql_timestamp">,
             <cfqueryparam VALUE="#session.employeeid#"  cfsqltype="cf_sql_integer">,
             <cfqueryparam VALUE="#now()#"  cfsqltype="cf_sql_timestamp">,
             <cfqueryparam VALUE="#session.employeeid#"  cfsqltype="cf_sql_integer">
             )

          </cfquery>
        </cfloop>
    </cfif>

   <cfif form.cancelled EQ 1 >
          <cfquery name="Comm" datasource="#Application.DataSrc#">
                UPDATE #Request.prefix_db_agency#.Communication 
                SET  Comm_record_deleted = 1,
                Status = 1
                WHERE Comm_ID = <cfqueryparam VALUE="#form.comm_id#"  cfsqltype="cf_sql_integer" >
          </cfquery>
   </cfif>

  <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>

      <cfquery name='commEmails' datasource='#Application.DataSrc#'>
         SELECT e.Emp_Email AS email, e.Emp_Telephone AS cell_phone                
          FROM   #Request.prefix_db_lookup#.pEmployee e
          WHERE  e.Emp_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 ''>
      <cfset texts = REReplace(commEmails.cell_phone, "[^0-9_]", "", "ALL") />
      <cfif toText eq ''>
       <cfset toText = #texts# />
      <cfelse>
       <cfset toText = toText & ", " & #texts# />
      </cfif>
     </cfif>
    </cfoutput>
    <!--- end Not DEV --->
   </cfloop> 

  <cfset ReplyTo = "info@MyHomeCareBiz.com" /> 
  <cfquery name='GetReplyTo' datasource='#Application.DataSrc#'>
    SELECT e.Emp_Email AS email , e.Emp_First, e.Emp_Last
    FROM   #Request.prefix_db_agency#.Communication c, #Request.prefix_db_lookup#.pEmployee e
    WHERE  c.comm_id = '#id#'
    AND    e.emp_id = c.Record_Created_By
  </cfquery>
  <cfset postedBy = GetReplyTo.Emp_First & " " & GetReplyTo.Emp_Last />
  <cfif GetReplyTo.recordcount eq 1 and GetReplyTo.email neq ''>
   <cfset ReplyTo = GetReplyTo.email />
  </cfif>

 <cfquery name='Getemployee' datasource='#Application.DataSrc#'>
    SELECT *, 1 AS communication_email FROM  #Request.prefix_db_lookup#.pEmployee WHERE Emp_ID =  '#Session.EmployeeID#'
</cfquery>

<cfif Getemployee.communication_email EQ 1 OR Session.IsAgencyLogin EQ 1 >
 <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"  cc="velmurugan@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"  cc="velmurugan@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>
  </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)#,
      Reason_for_discharge = <cfqueryparam value="#Trim(form.Reason_for_discharge)#"  cfsqltype="cf_sql_varchar">,
      Discharge_Disposition = <cfqueryparam value="#Trim(form.Discharge_Disposition)#"  cfsqltype="cf_sql_varchar">,
      Memo = <cfqueryparam value="#Trim(form.Memo)#"  cfsqltype="cf_sql_varchar">,
      Resolved = '#form.Resolved#',
      Followup_desc = <cfqueryparam value="#Trim(form.Followup_desc)#"  cfsqltype="cf_sql_varchar">,
      Followup_memo = <cfqueryparam value="#Trim(form.Followup_memo)#"  cfsqltype="cf_sql_varchar">,
      Record_Mod_Date = '#myDateTime#',
      Record_Mod_By = #Session.EmployeeID#
    WHERE  <cfif form.Schedule_ID NEQ '0'>Schedule_ID = #form.Schedule_ID#<cfelse>Comm_ID = #form.Comm_Id#</cfif>
  </cfquery>
  <cfif isdefined('form.employee_id')>
  <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>
<cfcookie name="alert_description" expires="now">
<cfcookie name="progress_Ids" expires="now">
<cfcookie name="Patient_ID" expires="now">

   <cfif form.sendfax EQ '1'>
        <cfset FORM.Emp_ID = form.employee_id  />
        <cfinclude template="senfax_communication.cfm">
   </cfif>

 <cfif form.redirectpage EQ 'pathway' OR form.redirectpage EQ 'progressnotes'>
      <cflocation url='#form.redirecturl#' addtoken='No' />
 <cfelse>
      <cflocation url='/agencyadmin/index.cfm?page=agency&cat=Communication&rpt=communication_view&id=#form.Comm_Id#&ret=1' addtoken='No' />
 </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='/agencyadmin/index.cfm?page=agency&cat=Communication&rpt=communication_view&id=#form.Comm_Id#' 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="/agencyadmin/index.cfm?page=agency&cat=Communication&rpt=communication_view&id=#form.Comm_Id#" addtoken="no">
 </cfif>


<cfelse>
 Unable to Create / Update Communication: Form Data is Missing
</cfif>

