<!---
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.5 $
$Id: telephonyid.cfm,v 1.5 2010/02/16 15:42:50 melissa Exp $
$Date: 2010/02/16 15:42:50 $
--->
<cfinclude template="../__Security-Block.cfm">

<!---=========|=====================================================    

TagName:        <cf_telephony>

Notes:		This custom tag encodes or decodes a telephonyid

Variables:      - Required Fields -

		action   - encode or decode
		visitdate   - visit date
		employeeid - employee id

		<cf_telephony action="encode|decode"
			visitdate="myDate" 
			employeeid="myemployeeid"
			id = id to decode
			result= attributename
			item= item number (visit number)

		>
=============|=================================================--->

<cfparam name="attributes.result" default="result" type="variableName">
<cfparam name="attributes.action" default="">
<cfparam name="attributes.id" default=0>
<cfparam name="attributes.visitdate" default=#now()#>
<cfparam name="attributes.employeeid" default=0>
<cfparam name="attributes.item" default=0>

<cfif 0>
<cfoutput>
#attributes.action#<br>
#attributes.visitdate#<br>
#attributes.employeeid#<br>
#attributes.id#<br>
</cfoutput>
</cfif>

<cfif attributes.action eq "encode">

	<cfif attributes.visitdate lt '2009-11-21'>
		<cfset final="">
	<cfelse>

		<cfset startdate=#CreateDate(dateformat(attributes.visitdate,"YYYY"),dateformat(attributes.visitdate,"m"),1)#>
		<cfset enddate= (DateAdd( "m", 1, startdate ) - 1 ) >

		<cfquery  datasource="#Application.DataSrc#" name="e2">
			select Agency.telephony_agencynumber,Employee.telephony_employeenumber
			from #Request.prefix_db_lookup#.Employee
			left outer join #Request.prefix_db_lookup#.Agency on Employee.Agency_ID = Agency.Agency_ID
			where employee_id = #attributes.employeeid# 
		</cfquery>


		<cfset telephonyid_value1 = 0>
		<cfset telephonyid_value1 = telephonyid_value1 * 16 + #month(attributes.visitdate)#>
		<cfset telephonyid_value1 = telephonyid_value1 * 1024 + e2.telephony_agencynumber>
		<cfset telephonyid_value2 = 0>
		<cfset telephonyid_value2 = telephonyid_value2 * 1024 + e2.telephony_employeenumber>
		<cfset telephonyid_value2 = telephonyid_value2 * 256 + attributes.item>
		<cfset tmp1 = '#numberformat(telephonyid_value1,"00009")##numberformat(telephonyid_value2,"00009")#'>

		<cfset tmp2="">
		<cfloop index=ind list="1,7,2,8,4,5,10,3,6,9">
			<cfset tmp2="#tmp2##mid(tmp1,ind,1)#">
		</cfloop>

		<cfset tmp2=tmp1>

		<!---<cfset final="#reverse(mid(tmp2,8,2))##reverse(mid(tmp2,1,6))##mid(tmp2,10,1)##mid(tmp2,7,1)#">--->
        <cfset final="000">
	
	</cfif>
	<cfset caller[attributes.result] = final>

<cfelseif attributes.action eq "decode">

	<cfset tmp1="#reverse(mid(attributes.id,3,6))##mid(attributes.id,10,1)##reverse(mid(attributes.id,1,2))##mid(attributes.id,9,1)#">
	<cfset tmp2="">
	<cfloop index=ind list="1,3,8,5,6,9,2,4,10,7">
		<cfset tmp2="#tmp2##mid(tmp1,ind,1)#">
	</cfloop>
	<cfset tmp2=tmp1>

	<cfset tmp3 = val(mid(tmp2,1,5))>
	<cfset tmp4 = val(mid(tmp2,6,5))>
	<cfset f_month=#val(numberformat(tmp3/1024,"9"))#>
	<cfset f_agency=#val(numberformat(tmp3 mod 1024,"9"))#>

<!--- JDB: Change this to do Int() b/c we should Never Round UP here!?!?... 
	<cfset f_employee=#val(numberformat(tmp4/256,"9"))#>
--->
  <cfset f_employee = Int(tmp4/256) />
  <cfset f_visitid = #val(numberformat(tmp4 mod 256,"9"))#>

  <cfset caller[attributes.result] = "#f_agency#:#f_employee#:#f_month#:#f_visitid#">

<!---
   <cfmail
    from="info@MyHomeCareBiz.com"
    to="John@JNetDev.com, melissa@myhomecarebiz.com"
    subject="decode"
    type="text"
   >
    tmp2: #tmp2#
     <br/>
    tmp4: #tmp4#
     <br/>
    div: #(tmp4/256)#
     <br/>
    f_employee: #f_employee#
	 <br/>
    attributes.result: #f_agency#:#f_employee#:#f_month#:#f_visitid#
   </cfmail>
--->

<cfelseif attributes.action eq 'updateitem'>
	<cfset startdate=#CreateDate(dateformat(attributes.visitdate,"YYYY"),dateformat(attributes.visitdate,"m"),1)#>
	<cfset enddate= (DateAdd( "m", 1, startdate ) - 1 ) >
	<cfquery  name="NewTelephony" datasource="#Application.DataSrc#">
		SELECT MAX(Telephony_ItemNumber) as maxnum from #Request.prefix_db_agency#.ScheduleItems
		where agency_id = #session.agencyid#
		and employee_id = #attributes.employeeid#
		and visit_date >= '#dateformat(startdate,"YYYY-MM-DD")#' and visit_date <= '#dateformat(enddate,"YYYY-MM-DD")#'
	</cfquery>
	<cfset nextnum=NewTelephony.maxnum+1>
	<cfquery  name="Update" datasource="#Application.DataSrc#">
		Update #Request.prefix_db_agency#.ScheduleItems set Telephony_ItemNumber = #nextnum#
		where scheduleitems_id = #attributes.id#
	</cfquery>
	<cfset caller[attributes.result] = #nextnum#>
</cfif>
