<cfcomponent extends="proventas31.common.formObject">
	<cfset variables.idProject=0>
	<cfset variables.project="">
	<cfset variables.idEmployee=0>
	<cfset variables.employee="">
	<cfset variables.dateStart="">
	<cfset variables.dateEnd="">

	<cfset variables.chartStyle="pie">
	<cfset module.list.orderBy="employee">

	<cffunction name="init" access="public" output="false">
		<cfset variables.idProject=0>
		<cfset variables.project="">
		<cfset variables.idEmployee=0>
		<cfset variables.employee="">
		<cfset variables.dateStart="">
		<cfset variables.dateEnd="">

		<cfset variables.chartStyle="pie">
	</cffunction>
	
	<cffunction name="getFormObjectId" access="public" output="false" returntype="numeric">
		<cfreturn getIdProject()>
	</cffunction>
	
	<cffunction name="setFormObjectId" access="public" output="false">
		<cfargument name="objectId" type="numeric" required="yes">
		<cfset setIdProject(arguments.objectId)>
	</cffunction>

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

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

	<cffunction name="getProject" access="public" returntype="proventas31.corporate.project" output="false">
		<cfif not IsObject("variables.project")><cfset variables.project=CreateObject("component", "proventas31.corporate.project")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.project.isRetrievedFromDB()) or variables.project.getIdProject() neq variables.idProject>
			<cfset variables.project=CreateObject("component", "proventas31.corporate.project")>
			<cfset variables.project.setIdProject(variables.idProject)>
			<cfset variables.project.get()>
		</cfif>
		<cfreturn variables.project>
	</cffunction>

	<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>

	<cffunction name="getEmployee" access="public" returntype="proventas31.corporate.employee" output="false">
		<cfif not IsObject("variables.employee")><cfset variables.employee=CreateObject("component", "proventas31.corporate.employee")></cfif>
		<cfif (Compare(module.form.pageAction, "add") and not variables.employee.isRetrievedFromDB()) or variables.employee.getIdEmployee() neq variables.idEmployee>
			<cfset variables.employee=CreateObject("component", "proventas31.corporate.employee")>
			<cfset variables.employee.setIdEmployee(variables.idEmployee)>
			<cfset variables.employee.get()>
		</cfif>
		<cfreturn variables.employee>
	</cffunction>
	
	<cffunction name="setChartStyle" access="public" output="false">
		<cfargument name="chartStyle" type="string" required="yes" default="pie">
		<cfset variables.chartStyle=arguments.chartStyle>
	</cffunction>

	<cffunction name="getChartStyle" access="public" returntype="string" output="false">
		<cfreturn variables.chartStyle>
	</cffunction>
	
	<cffunction name="setDateStart" access="public" output="false">
		<cfargument name="dateStart" type="string" required="yes" default="">
		<cfset variables.dateStart=arguments.dateStart>
	</cffunction>
	
	<cffunction name="getDateStart" access="public" returntype="string" output="false">
		<cfreturn variables.dateStart>
	</cffunction>

	<cffunction name="setDateEnd" access="public" output="false">
		<cfargument name="dateEnd" type="string" required="yes" default="">
		<cfset variables.dateEnd=arguments.dateEnd>
	</cffunction>
	
	<cffunction name="getDateEnd" access="public" returntype="string" output="false">
		<cfreturn variables.dateEnd>
	</cffunction>
	
	<cffunction name="getList" access="public" output="false" returntype="query">
		<cfquery name="getListQuery" datasource="#application.datasource#">
			SELECT DISTINCT 
				salesClientUnit.idEmployee, COUNT(salesClientUnit.idSalesUnit) AS numberOfUnits,
				CONCAT(TRIM(CONCAT(employee.nameFirst, ' ', employee.nameFirst1)), ' ', TRIM(CONCAT(employee.nameLast, ' ', employee.nameLast1))) AS employee
			FROM 
				salesClientUnit 
			LEFT JOIN employee ON salesClientUnit.idEmployee=employee.idEmployee
			WHERE 
				(
					salesClientUnit.idSalesClientState=30 OR salesClientUnit.idSalesClientState=40
				)
				<cfif variables.idProject gt 0>
				AND salesClientUnit.idProject=<cfqueryparam value="#variables.idProject#" cfsqltype="cf_sql_integer">
				</cfif>
			GROUP BY salesClientUnit.idEmployee
			ORDER BY employee
		</cfquery>
		<cfreturn getListQuery>
	</cffunction>

	<cffunction name="getDetailedList" access="public" output="false" returntype="query">
		<cfquery name="getDetailedListQuery" datasource="#application.datasource#">
			SELECT DISTINCT
				salesClientUnit.idSalesClientUnit,
				salesClientUnit.idSalesClient,
				CONCAT(TRIM(CONCAT(salesClient.nameFirst, ' ', salesClient.nameFirst1)), ' ', TRIM(CONCAT(salesClient.nameLast, ' ', salesClient.nameLast1))) as salesClient,
				salesClientUnit.idSalesUnit, salesUnit.number as salesUnit,
				salesClientUnit.idProject, project.name AS project,
				salesClientUnit.idEmployee, 
				CONCAT(TRIM(CONCAT(employee.nameFirst, ' ', employee.nameFirst1)), ' ', TRIM(CONCAT(employee.nameLast, ' ', employee.nameLast1))) AS employee,
				salesClientUnit.idSalesPaymentPlan, salesPaymentPlan.name as salesPaymentPlan,
				salesClientUnit.idSalesClientState, salesClientState.name as salesClientState,
				salesClientUnit.idSalesContact, salesContact.name as salesContact,
				salesClientUnit.idSalesContactMedia, salesContactMedia.name as salesContactMedia,
				salesClientUnit.dateRegistration, 
				salesClientUnit.sellPrice, salesClientUnit.price,
				salesClientUnit.idCurrency, currency.name AS currency
			FROM 
				salesClientUnit 
			LEFT JOIN salesClient ON salesClientUnit.idSalesClient=salesClient.idSalesClient
			LEFT JOIN salesUnit ON salesClientUnit.idSalesUnit=salesUnit.idSalesUnit
			LEFT JOIN project ON salesClientUnit.idProject=project.idProject
			LEFT JOIN employee ON salesClientUnit.idEmployee=employee.idEmployee
			LEFT JOIN salesPaymentPlan ON salesClientUnit.idSalesPaymentPlan=salesPaymentPlan.idSalesPaymentPlan
			LEFT JOIN salesClientState ON salesClientUnit.idSalesClientState=salesClientState.idSalesClientState
			LEFT JOIN salesContact ON salesClientUnit.idSalesContact=salesContact.idSalesContact
			LEFT JOIN salesContactMedia ON salesClientUnit.idSalesContactMedia=salesContactMedia.idSalesContactMedia
			LEFT JOIN currency ON salesClientUnit.idCurrency=currency.idCurrency
			WHERE 
				(
					salesClientUnit.idSalesClientState=30 OR salesClientUnit.idSalesClientState=40
				)
				<cfif variables.idProject gt 0>
				AND salesClientUnit.idProject=<cfqueryparam value="#variables.idProject#" cfsqltype="cf_sql_integer">
				</cfif>
			ORDER BY #module.list.orderBy#
		</cfquery>
		<cfreturn getDetailedListQuery>
	</cffunction>
	
	<cffunction name="createChartStyleOptions" access="public" output="true">
		<cfargument name="selected" required="yes" type="string" default="pie">
		<cfset var list="Pie,Bar,Curve,Line,Scatter,Step,Area">
		<cfoutput><select name="#getFormPrefix()#chartStyle" id="#getFormPrefix()#chartStyle"></cfoutput>
		<cfloop list="#list#" index="idx">
			<cfoutput><option value="#LCase(idx)#"<cfif not CompareNoCase(arguments.selected, idx)> selected</cfif>>#idx#</option></cfoutput>
		</cfloop>
		<cfoutput></select></cfoutput>
	</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 
				idProject 
			FROM 
				project 
			WHERE 
				name=<cfqueryparam value="#variables.name#" cfsqltype="cf_sql_varchar" maxlength="255">
				<cfif variables.idProject gt 0>
				AND idProject != <cfqueryparam value="#variables.idProject#" cfsqltype="cf_sql_integer">
				</cfif>
		</cfquery>
		<cfif getIdQuery.recordcount gt 0>
			<cfset result.id=getIdQuery.idProject>
		</cfif>
		<cfreturn result>
	</cffunction>
	
</cfcomponent>