<cfcomponent extends="components.common.formObject">
	<cfset variables.idEmployee=0>
	<cfset variables.idEmployeeType=0>
	<cfset variables.employeeType="">
	<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="namesFirst, namesLast">
	
	<cfset variables.employeeCompany="">
	
	<cffunction name="init" access="remote" output="false">
		<cfset variables.idEmployee=0>
		<cfset variables.idEmployeeType=0>
		<cfset variables.employeeType="">
		<cfset variables.nameFirst="">
		<cfset variables.nameFirst1="">
		<cfset variables.nameLast="">
		<cfset variables.nameLast1="">
		<cfset variables.documentNumber="">
		<cfset variables.dateRegistered="">
		<cfset variables.dateBirthday="">

		<cfset variables.employeeCompany="">
	</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>

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

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

	<cffunction name="setIdEmployeeType" access="remote" output="false">
		<cfargument name="idEmployeeType" type="numeric" required="yes" default="0">
		<cfset variables.idEmployeeType=arguments.idEmployeeType>
	</cffunction>

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

	<cffunction name="getEmployeeType" access="remote" returntype="components.corporate.employeeType" output="false">
		<cfif not IsObject("variables.employeeType")><cfset variables.employeeType=CreateObject("component", "components.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", "components.corporate.employeeType")>
			<cfset variables.employeeType.setIdEmployeeType(variables.idEmployeeType)>
			<cfset variables.employeeType.get()>
		</cfif>
		<cfreturn variables.employeeType>
	</cffunction>

	<cffunction name="setNameFirst" access="remote" output="false">
		<cfargument name="nameFirst" type="string" required="yes" default="">
		<cfset variables.nameFirst=arguments.nameFirst>
	</cffunction>

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

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

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

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

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

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

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

	<cffunction name="getNameFull" access="remote" 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>

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

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

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

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

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

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

	<cffunction name="getEmployeeCompany" access="remote" returntype="components.corporate.employeeCompany" output="false">
		<cfif not IsObject("variables.employeeCompany")><cfset variables.employeeCompany=CreateObject("component", "components.corporate.employeeCompany")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employeeCompany.isRetrievedFromDB()) or variables.employeeCompany.getIdEmployeeCompany() neq variables.idEmployeeCompany>
			<cfset variables.employeeCompany=CreateObject("component", "components.corporate.employeeCompany")>
			<cfset variables.employeeCompany.setIdEmployee(variables.idEmployee)>
			<cfset variables.employeeCompany.get()>
		</cfif>
		<cfreturn variables.employeeCompany>
	</cffunction>

	<cffunction name="setVariablesFromQuery" access="private" output="false">
		<cfargument name="getQuery" type="query" required="yes">
		<cfif getQuery.recordcount gt 0>
			<cfset variables.idEmployeeType=getQuery.idEmployeeType>
			<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="remote" 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="remote" 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 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)>#URLDecode(arguments.orderBy)#<cfelse>#URLDecode(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.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,
				company.name AS company,
				companyDepartment.name AS companyDepartment
			FROM 
				employee
			LEFT JOIN employeeType ON employee.idEmployeeType=employeeType.idEmployeeType
			LEFT JOIN employeeCompany ON employee.idEmployee=employeeCompany.idEmployee
			LEFT JOIN company ON employeeCompany.idCompany=company.idCompany
			LEFT JOIN companyDepartment ON employeeCompany.idCompanyDepartment=companyDepartment.idCompanyDepartment 
			">
		<cfreturn result>
	</cffunction>
	
	<cffunction name="createOptions" access="remote" 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="remote" 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="remote" 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 esos datos ya esta registrado en el sistema">
			<cfreturn result>
		</cfif>
		<!--- variables ok --->
		<cftry>
			<cfquery name="createQuery" datasource="#application.datasource#">
				INSERT INTO employee
					(
					idEmployee, idEmployeeType, 
					nameFirst, nameFirst1, 
					nameLast, nameLast1,
					documentNumber,
					dateRegistered,
					dateBirthday
					) 
				VALUES 
					(
					<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">, 
					<cfqueryparam value="#variables.idEmployeeType#" cfsqltype="cf_sql_integer">, 
					<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>
			<cfset result=getId()>
			<cfset variables.idEmployee=result.id>
			<!--- 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="remote" 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 esos datos 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">, 
					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="remote" 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>