<cfcomponent extends="proventas31.common.formObject">
	<cfset variables.idEmployee=0>
	<cfset variables.idEmployeeType=0>
	<cfset variables.employeeType=CreateObject("component", "proventas31.corporate.employeeType")>
	<cfset variables.idSupervisor=0>
	<cfset variables.supervisor="">
	<cfset variables.nameFirst="">
	<cfset variables.nameFirst1="">
	<cfset variables.nameLast="">
	<cfset variables.nameLast1="">
	<cfset variables.documentNumber="">
	<cfset variables.dateRegistered="">
	<cfset variables.dateBirthday="">
	<cfset module.list.orderBy="namesLast, namesFirst">
	
	<cffunction name="init" access="public" output="false">
		<cfset variables.idEmployee=0>
		<cfset variables.idEmployeeType=0>
		<cfset variables.employeeType=CreateObject("component", "proventas31.corporate.employeeType")>
		<cfset variables.idSupervisor=0>
		<cfset variables.supervisor="">
		<cfset variables.nameFirst="">
		<cfset variables.nameFirst1="">
		<cfset variables.nameLast="">
		<cfset variables.nameLast1="">
		<cfset variables.documentNumber="">
		<cfset variables.dateRegistered="">
		<cfset variables.dateBirthday="">
	</cffunction>

	<cffunction name="getFormObjectId" access="public" output="false" returntype="numeric">
		<cfreturn getIdEmployee()>
	</cffunction>
	
	<cffunction name="setFormObjectId" access="public" output="false">
		<cfargument name="objectId" type="numeric" required="yes">
		<cfset setIdEmployee(arguments.objectId)>
	</cffunction>

	<!--- //////////////////////////////////////idEmployee\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setIdEmployee" access="public" output="false">
		<cfargument name="idEmployee" type="numeric" required="yes" default="0">
		<cfset variables.idEmployee=arguments.idEmployee>
	</cffunction>

	<cffunction name="getIdEmployee" access="public" returntype="numeric" output="false">
		<cfreturn variables.idEmployee>
	</cffunction>

	<!--- //////////////////////////////////////employeeType\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setIdEmployeeType" access="public" output="false">
		<cfargument name="idEmployeeType" type="numeric" required="yes" default="0">
		<cfset variables.idEmployeeType=arguments.idEmployeeType>
	</cffunction>

	<cffunction name="getIdEmployeeType" access="public" returntype="numeric" output="false">
		<cfreturn variables.idEmployeeType>
	</cffunction>

	<cffunction name="getEmployeeType" access="public" returntype="proventas31.corporate.employeeType" output="false">
		<cfif not IsObject("variables.employeeType")><cfset variables.employeeType=CreateObject("component", "proventas31.corporate.employeeType")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employeeType.isRetrievedFromDB()) or variables.employeeType.getIdEmployeeType() neq variables.idEmployeeType>
			<cfset variables.employeeType=CreateObject("component", "proventas31.corporate.employeeType")>
			<cfset variables.employeeType.setIdEmployeeType(variables.idEmployeeType)>
			<cfset variables.employeeType.get()>
		</cfif>
		<cfreturn variables.employeeType>
	</cffunction>

	<cffunction name="setIdSupervisor" access="public" output="false">
		<cfargument name="idSupervisor" type="numeric" required="yes" default="0">
		<cfset variables.idSupervisor=arguments.idSupervisor>
	</cffunction>

	<cffunction name="getIdSupervisor" access="public" returntype="numeric" output="false">
		<cfreturn variables.idSupervisor>
	</cffunction>

	<cffunction name="getSupervisor" access="public" returntype="proventas31.corporate.employee" output="false">
		<cfif not IsObject("variables.supervisor")><cfset variables.supervisor=CreateObject("component", "proventas31.corporate.employee")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.supervisor.isRetrievedFromDB()) or variables.supervisor.getIdEmployee() neq variables.idSupervisor>
			<cfset variables.supervisor=CreateObject("component", "proventas31.corporate.employee")>
			<cfset variables.supervisor.setIdEmployee(variables.idSupervisor)>
			<cfset variables.supervisor.get()>
		</cfif>
		<cfreturn variables.supervisor>
	</cffunction>

	<!--- //////////////////////////////////////names\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setNameFirst" access="public" output="false">
		<cfargument name="nameFirst" type="string" required="yes" default="">
		<cfset variables.nameFirst=arguments.nameFirst>
	</cffunction>

	<cffunction name="getNameFirst" access="public" returntype="string" output="false">
		<cfreturn variables.nameFirst>
	</cffunction>

	<cffunction name="setNameFirst1" access="public" output="false">
		<cfargument name="nameFirst1" type="string" required="yes" default="">
		<cfset variables.nameFirst1=arguments.nameFirst1>
	</cffunction>

	<cffunction name="getNameFirst1" access="public" returntype="string" output="false">
		<cfreturn variables.nameFirst1>
	</cffunction>

	<cffunction name="setNameLast" access="public" output="false">
		<cfargument name="nameLast" type="string" required="yes" default="">
		<cfset variables.nameLast=arguments.nameLast>
	</cffunction>

	<cffunction name="getNameLast" access="public" returntype="string" output="false">
		<cfreturn variables.nameLast>
	</cffunction>

	<cffunction name="setNameLast1" access="public" output="false">
		<cfargument name="nameLast1" type="string" required="yes" default="">
		<cfset variables.nameLast1=arguments.nameLast1>
	</cffunction>

	<cffunction name="getNameLast1" access="public" returntype="string" output="false">
		<cfreturn variables.nameLast1>
	</cffunction>

	<cffunction name="getNameFull" access="public" returntype="string" output="false">
		<cfset var name=variables.nameFirst>
		<cfif Len(variables.nameFirst1)><cfset name=name & " " & variables.nameFirst1></cfif>
		<cfif Len(variables.nameLast)><cfset name=name & " " & variables.nameLast></cfif>
		<cfif Len(variables.nameLast1)><cfset name=name & " " & variables.nameLast1></cfif>
		<cfreturn name>
	</cffunction>

	<!--- //////////////////////////////////////documentNumber\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setDocumentNumber" access="public" output="false">
		<cfargument name="documentNumber" type="string" required="yes" default="">
		<cfset variables.documentNumber=arguments.documentNumber>
	</cffunction>

	<cffunction name="getDocumentNumber" access="public" returntype="string" output="false">
		<cfreturn variables.documentNumber>
	</cffunction>
	
	<!--- //////////////////////////////////////dateRegistered\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setDateRegistered" access="public" output="false">
		<cfargument name="dateRegistered" type="string" required="yes" default="">
		<cfset variables.dateRegistered=arguments.dateRegistered>
	</cffunction>

	<cffunction name="getDateRegistered" access="public" returntype="string" output="false">
		<cfreturn variables.dateRegistered>
	</cffunction>

	<!--- //////////////////////////////////////dateBirthday\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--->
	<cffunction name="setDateBirthday" access="public" output="false">
		<cfargument name="dateBirthday" type="string" required="yes" default="">
		<cfset variables.dateBirthday=arguments.dateBirthday>
	</cffunction>

	<cffunction name="getDateBirthday" access="public" returntype="string" output="false">
		<cfreturn variables.dateBirthday>
	</cffunction>

	<cffunction name="getEmployeeData" access="public" returntype="proventas31.corporate.employeeData" output="false">
		<cfif not IsObject("variables.employeeData")><cfset variables.employeeData=CreateObject("component", "proventas31.corporate.employeeData")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employeeData.isRetrievedFromDB()) or variables.employeeData.getIdEmployee() neq variables.idEmployee>
			<cfset variables.employeeData=CreateObject("component", "proventas31.corporate.employeeData")>
			<cfset variables.employeeData.setIdEmployee(variables.idEmployee)>
			<cfset variables.employeeData.get()>
		</cfif>
		<cfreturn variables.employeeData>
	</cffunction>
	
	<cffunction name="getEmployeeUser" access="public" returntype="proventas31.corporate.employeeUser" output="false">
		<cfif not IsObject("variables.employeeUser")><cfset variables.employeeUser=CreateObject("component", "proventas31.corporate.employeeUser")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employeeUser.isRetrievedFromDB()) or variables.employeeUser.getIdEmployee() neq variables.idEmployee>
			<cfset variables.employeeUser=CreateObject("component", "proventas31.corporate.employeeUser")>
			<cfset variables.employeeUser.setIdEmployee(variables.idEmployee)>
			<cfset variables.employeeUser.get()>
		</cfif>
		<cfreturn variables.employeeUser>
	</cffunction>
	
	<cffunction name="getEmployeeAccessProfile" access="public" returntype="proventas31.corporate.employeeAccessProfile" output="false">
		<cfif not IsObject("variables.employeeUser")><cfset variables.employeeUser=CreateObject("component", "proventas31.corporate.employeeUser")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employeeUser.isRetrievedFromDB()) or variables.employeeUser.getIdEmployee() neq variables.idEmployee>
			<cfset variables.employeeAccessProfile=CreateObject("component", "proventas31.corporate.employeeAccessProfile")>
			<cfset variables.employeeAccessProfile.setIdEmployee(variables.idEmployee)>
			<cfset variables.employeeAccessProfile.get()>
		</cfif>
		<cfreturn variables.employeeAccessProfile>
	</cffunction>

	<cffunction name="setVariablesFromQuery" access="private" output="false">
		<cfargument name="query" type="query" required="yes">
		<cfif query.recordcount gt 0>
			<cfset variables.idEmployee=getQuery.idEmployee>
			<cfset variables.idEmployeeType=getQuery.idEmployeeType>
			<cfif Len(query.idSupervisor)><cfset variables.idSupervisor=getQuery.idSupervisor><cfelse><cfset variables.idSupervisor=0></cfif>
			<cfset variables.nameFirst=getQuery.nameFirst>
			<cfset variables.nameFirst1=getQuery.nameFirst1>
			<cfset variables.nameLast=getQuery.nameLast>
			<cfset variables.nameLast1=getQuery.nameLast1>
			<cfset variables.documentNumber=getQuery.documentNumber>
			<cfset variables.dateRegistered=LSDateFormat(getQuery.dateRegistered,"dd/mm/yyyy")>
			<cfset variables.dateBirthday=LSDateFormat(getQuery.dateBirthday,"dd/mm/yyyy")>
		</cfif>
	</cffunction>

	<cffunction name="get" access="public" output="false">
		<cfquery name="getQuery" datasource="#application.datasource#">
			SELECT 
				#getQueryString()#
			WHERE
				employee.idEmployee=<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
		</cfquery>
		<cfif getQuery.recordcount gt 0>
			<cfset setVariablesFromQuery(getQuery)>
		</cfif>
		<!--- ADDED: set that object was retrieved from DB --->
		<cfset module.retrievedFromDB=true>
	</cffunction>

	<cffunction name="getList" access="public" output="false" returntype="query">
		<cfargument name="orderBy" required="no" type="string" default="">

		<cfquery name="getListQuery" datasource="#application.datasource#">
			SELECT DISTINCT 
				#getQueryString()#
			WHERE 1=1
				<cfif variables.idEmployeeType gt 0>
				AND employee.idEmployeeType=<cfqueryparam value="#variables.idEmployeeType#" cfsqltype="cf_sql_integer">
				</cfif>
				<cfif Len(variables.nameFirst)>
				AND (
					employee.nameFirst LIKE <cfqueryparam value="%#variables.nameFirst#%" cfsqltype="cf_sql_varchar" maxlength="255">
					OR 
					employee.nameFirst1 LIKE <cfqueryparam value="%#variables.nameFirst#%" cfsqltype="cf_sql_varchar" maxlength="255">
				)
				</cfif>
				<cfif Len(variables.nameLast)>
				AND (
					employee.nameLast LIKE <cfqueryparam value="%#variables.nameLast#%" cfsqltype="cf_sql_varchar" maxlength="255">
					OR 
					employee.nameLast1 LIKE <cfqueryparam value="%#variables.nameLast#%" cfsqltype="cf_sql_varchar" maxlength="255">
				)
				</cfif>
			ORDER BY <cfif Len(arguments.orderBy)>#arguments.orderBy#<cfelse>#module.list.orderBy#</cfif>
		</cfquery>
		<cfreturn getListQuery>
	</cffunction>
	
	<cffunction name="getQueryString" access="private" output="false" returntype="string">
		<cfset var result="
				employee.idEmployee, 
				employee.idEmployeeType, employeeType.name AS employeeType,
				employee.idSupervisor, 
				CONCAT(TRIM(CONCAT(supervisor.nameFirst, ' ', supervisor.nameFirst1)), ' ', TRIM(CONCAT(supervisor.nameLast, ' ', supervisor.nameLast1))) AS supervisor,
				employee.nameFirst, employee.nameFirst1, TRIM(CONCAT(employee.nameFirst, ' ', employee.nameFirst1)) AS namesFirst,
				employee.nameLast, employee.nameLast1, TRIM(CONCAT(employee.nameLast, ' ', employee.nameLast1)) AS namesLast,
				CONCAT(TRIM(CONCAT(employee.nameFirst, ' ', employee.nameFirst1)), ' ', TRIM(CONCAT(employee.nameLast, ' ', employee.nameLast1))) AS nameFull,
				employee.documentNumber,
				employee.dateRegistered,
				employee.dateBirthday,
				employeeData.phone1, employeeData.phone2, employeeData.phone3, employeeData.email
			FROM 
				employee
			LEFT JOIN employee AS supervisor ON employee.idSupervisor=supervisor.idEmployee
			LEFT JOIN employeeType ON employee.idEmployeeType=employeeType.idEmployeeType
			LEFT JOIN employeeData ON employee.idEmployee=employeeData.idEmployee
			">
		<cfreturn result>
	</cffunction>
	
	<cffunction name="createOptions" access="public" returntype="numeric" output="true">
		<cfargument name="selected" required="yes" type="numeric" default="0">
		<cfset var list=getList()>
		<cfset var result=0>
		<cfset var name="">
		<cfif list.idEmployee gt 0><cfset result=list.idEmployee></cfif>
		<cfloop query="list">
			<cfif list.idEmployee eq arguments.selected><cfset _selected=" selected"><cfset result=list.idEmployee><cfelse><cfset _selected=""></cfif>
			<cfset name=list.nameFirst>
			<cfif Len(list.nameFirst1)><cfset name=name & " " & list.nameFirst1></cfif>
			<cfif Len(list.nameLast)><cfset name=name & " " & list.nameLast></cfif>
			<cfif Len(list.nameLast1)><cfset name=name & " " & list.nameLast1></cfif>
			<cfoutput><option value="#list.idEmployee#"#_selected#>#name#</option></cfoutput>
		</cfloop>
		<cfreturn result>
	</cffunction>

	<cffunction name="getId" access="public" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<cfquery name="getIdQuery" datasource="#application.datasource#">
			SELECT 
				idEmployee 
			FROM 
				employee 
			WHERE 
				nameFirst=<cfqueryparam value="#variables.nameFirst#" cfsqltype="cf_sql_varchar" maxlength="255">
				AND nameFirst1=<cfqueryparam value="#variables.nameFirst1#" cfsqltype="cf_sql_varchar" maxlength="255">
				AND nameLast=<cfqueryparam value="#variables.nameLast#" cfsqltype="cf_sql_varchar" maxlength="255">
				AND nameLast1=<cfqueryparam value="#variables.nameLast1#" cfsqltype="cf_sql_varchar" maxlength="255">
				<cfif variables.idEmployee gt 0>
				AND idEmployee != <cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
				</cfif>
		</cfquery>
		<cfif getIdQuery.recordcount gt 0>
			<cfset result.id=getIdQuery.idEmployee>
		</cfif>
		<cfreturn result>
	</cffunction>
	
	<cffunction name="create" access="public" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<!--- check parameters --->
		<cfif not Len(variables.nameFirst)>
			<cfset result.message="Nombre es un campo obligatorio">
			<cfreturn result>
		</cfif>
		<cfif not Len(variables.nameLast)>
			<cfset result.message="Apellido es un campo obligatorio">
			<cfreturn result>
		</cfif>
		<cfif variables.idEmployeeType lt 1>
			<cfset result.message="Tipo de empleado es un campo obligatorio">
			<cfreturn result>
		</cfif>

		<cfset result=getId()>
		<cfif result.id gt 0>
			<cfset result.message="Empleado con ese nombre ya esta registrado en el sistema">
			<cfreturn result>
		</cfif>
		<!--- variables ok --->
		<cftry>
			<cfquery name="createQuery" datasource="#application.datasource#">
				INSERT INTO employee
					(
					idEmployee, idEmployeeType, 
					idSupervisor,
					nameFirst, nameFirst1, nameLast, nameLast1,
					documentNumber,
					dateRegistered,
					dateBirthday
					) 
				VALUES 
					(
					<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">, 
					<cfqueryparam value="#variables.idEmployeeType#" cfsqltype="cf_sql_integer">, 
					<cfif variables.idSupervisor gt 0><cfqueryparam value="#variables.idSupervisor#" cfsqltype="cf_sql_integer"><cfelse>NULL</cfif>,
					<cfqueryparam value="#variables.nameFirst#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.nameFirst1#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.nameLast#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.nameLast1#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.documentNumber#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfif Len(variables.dateRegistered)><cfqueryparam value="#parseDate(variables.dateRegistered)#" cfsqltype="cf_sql_date"><cfelse>NULL</cfif>,
					<cfif Len(variables.dateBirthday)><cfqueryparam value="#parseDate(variables.dateBirthday)#" cfsqltype="cf_sql_date"><cfelse>NULL</cfif>
					)
			</cfquery>
			<cfif variables.idEmployee lt 1>
				<cfset result=getId()>
				<cfset variables.idEmployee=result.id>
			</cfif>
			<!--- catch any error --->
			<cfcatch type="any">
				<cfset result.id=-1>
				<cfset result.message="Error [employee.create()]: " & cfcatch.Message & "<br>Details: " & cfcatch.Detail>
			</cfcatch>
		</cftry>
		<cfreturn result>
	</cffunction>

	<cffunction name="update" access="public" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<!--- check parameters --->
		<cfif not Len(variables.nameFirst)>
			<cfset result.message="Nombre es un campo obligatorio">
			<cfreturn result>
		</cfif>
		<cfif not Len(variables.nameLast)>
			<cfset result.message="Apellido es un campo obligatorio">
			<cfreturn result>
		</cfif>
		<cfif variables.idEmployeeType lt 1>
			<cfset result.message="Tipo de empleado es un campo obligatorio">
			<cfreturn result>
		</cfif>
		<cfset result=getId()>
		<cfif result.id gt 0>
			<cfset result.message="Empleado con ese nombre ya esta registrado en el sistema">
			<cfreturn result>
		</cfif>
		<!--- variables ok --->
		<cftry>
			<cfquery name="updateQuery" datasource="#application.datasource#">
				UPDATE employee
				SET 
					idEmployeeType=<cfqueryparam value="#variables.idEmployeeType#" cfsqltype="cf_sql_integer">, 
					idSupervisor=<cfif variables.idSupervisor gt 0><cfqueryparam value="#variables.idSupervisor#" cfsqltype="cf_sql_integer"><cfelse>NULL</cfif>,
					nameFirst=<cfqueryparam value="#variables.nameFirst#" cfsqltype="cf_sql_varchar" maxlength="255">,
					nameFirst1=<cfqueryparam value="#variables.nameFirst1#" cfsqltype="cf_sql_varchar" maxlength="255">,
					nameLast=<cfqueryparam value="#variables.nameLast#" cfsqltype="cf_sql_varchar" maxlength="255">,
					nameLast1=<cfqueryparam value="#variables.nameLast1#" cfsqltype="cf_sql_varchar" maxlength="255">,
					documentNumber=<cfqueryparam value="#variables.documentNumber#" cfsqltype="cf_sql_varchar" maxlength="255">,
					dateRegistered=<cfif Len(variables.dateRegistered)><cfqueryparam value="#parseDate(variables.dateRegistered)#" cfsqltype="cf_sql_date"><cfelse>NULL</cfif>,
					dateBirthday=<cfif Len(variables.dateBirthday)><cfqueryparam value="#parseDate(variables.dateBirthday)#" cfsqltype="cf_sql_date"><cfelse>NULL</cfif>
				WHERE 
					idEmployee=<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
			</cfquery>
			<cfset result.id=variables.idEmployee>
			<!--- catch any error --->
			<cfcatch type="any">
				<cfset result.id=-1>
				<cfset result.message="Error [employee.update()]: " & cfcatch.Message & "<br>Details: " & cfcatch.Detail>
			</cfcatch>
		</cftry>
		<cfreturn result>
	</cffunction>

	<cffunction name="delete" access="public" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<!--- check parameters --->
		<cftry>
			<cfquery name="deleteQuery" datasource="#application.datasource#">
				DELETE FROM employee
				WHERE 
					idEmployee=<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
			</cfquery>
			<cfset result.id=0>
			<cfset init()>
			<!--- catch any error --->
			<cfcatch type="any">
				<cfset result.id=-1>
				<cfset result.message="Error [employee.delete()]: " & cfcatch.Message & "<br>Details: " & cfcatch.Detail>
			</cfcatch>
		</cftry>
		<cfreturn result>
	</cffunction>
</cfcomponent>