Index: /trunk/workflow/inc/local/classes/class.wf_orgchart.php
===================================================================
--- /trunk/workflow/inc/local/classes/class.wf_orgchart.php	(revision 2042)
+++ /trunk/workflow/inc/local/classes/class.wf_orgchart.php	(revision 2056)
@@ -139,11 +139,7 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
 				 "	(a.organizacao_id = ?) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) " .
 				 " ORDER BY a.sigla, a.descricao";
 
@@ -334,11 +330,7 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
 				 "	(a.area_id = ?) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) " .
 				 " ORDER BY a.sigla, a.descricao";
 
@@ -381,13 +373,9 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
 				 "	(UPPER(a.sigla) = UPPER(?)) " .
 				 " AND " . 
 				 "	(a.organizacao_id = ?) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) " .
 				 " ORDER BY a.sigla, a.descricao";
 
@@ -431,11 +419,7 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
 				 "	(a.area_status_id = ?) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) " .
 				 " ORDER BY a.sigla, a.descricao";
 
@@ -483,11 +467,7 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
 				 "	(a.area_id IN ($areas)) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) " .
 				 " ORDER BY a.sigla, a.descricao";
 
@@ -561,11 +541,7 @@
 				 " FROM area a " .
 				 "  LEFT OUTER JOIN substituicao s " .
-				 "  USING(area_id) " .
+				 "  ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) " .
 				 " WHERE " . 
-				 "	(a.superior_area_id = ?) " .
-				 " AND " . 
-				 "	((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) " .
-				 " 	OR " . 
-				 "	(s.substituicao_id is null)) ";
+				 "	(a.superior_area_id = ?) ";
 
 
Index: /trunk/workflow/inc/class.so_orgchart.inc.php
===================================================================
--- /trunk/workflow/inc/class.so_orgchart.inc.php	(revision 2042)
+++ /trunk/workflow/inc/class.so_orgchart.inc.php	(revision 2056)
@@ -699,5 +699,5 @@
 		$this->_checkAccess($organizationID);
 
-		$supervisors = '{' . implode(', ', $this->db->GetRow('SELECT COALESCE(titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(substituto_funcionario_id, -1) AS substituto_funcionario_id FROM area WHERE (area_id = ?) AND (organizacao_id = ?)', array($areaID, $organizationID))) . '}';
+		$supervisors = '{' . implode(', ', $this->db->GetRow('SELECT COALESCE(a.titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(s.funcionario_id, -1) AS substituto_funcionario_id FROM area a LEFT OUTER JOIN substituicao s ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) WHERE (area_id = ?) AND (organizacao_id = ?)', array($areaID, $organizationID))) . '}';
 		$query = "SELECT funcionario_id, funcionario_status_id, centro_custo_id, localidade_id, organizacao_id, area_id, cargo_id, nivel, funcionario_categoria_id, titulo FROM funcionario WHERE ((area_id = ?) AND (organizacao_id = ?)) OR (funcionario_id = ANY (?))";
 		$result = $this->db->query($query, array($areaID, $organizationID, $supervisors));
@@ -863,5 +863,5 @@
 		$this->_checkAccess($organizationID);
 
-		$query = 'SELECT sigla FROM area WHERE (? IN (titular_funcionario_id, substituto_funcionario_id, auxiliar_funcionario_id))';
+		$query = 'SELECT DISTINCT(a.sigla) FROM area a LEFT OUTER JOIN substituicao s USING (area_id) WHERE (? IN (a.titular_funcionario_id, s.funcionario_id, a.auxiliar_funcionario_id))';
 		$areas = array();
 		$resultSet = $this->db->query($query, $employeeID);
@@ -871,5 +871,5 @@
 		{
 			$errors = array(
-				"O funcionário \"" . $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getName($employeeID) . "\" é titular, substituto ou auxiliar administrativo das seguintes áreas: " . implode(", ", $areas),
+				"O funcionário \"" . $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getName($employeeID) . "\" é titular, substituto, já participou de substituição ou é auxiliar administrativo das seguintes áreas: " . implode(", ", $areas),
 				'-----------------',
 				'Se você quiser excluir este funcionário, precisa removê-lo dos "cargos" que ele possui nas áreas citadas.'
@@ -901,9 +901,9 @@
 		$area_condition = "";
 		if (($areaID != -1) && !empty($areaID)) {
-			$area_condition = " AND area_id = ? ";
+			$area_condition = " AND a.area_id = ? ";
 			$values[]= $areaID;
 		}
 
-		$query = "SELECT area_id, centro_custo_id, organizacao_id, area_status_id, titular_funcionario_id, superior_area_id, sigla, descricao, ativa, auxiliar_funcionario_id FROM area WHERE organizacao_id = ? " . $area_condition . " ORDER BY sigla";
+		$query = "SELECT a.area_id, a.centro_custo_id, a.organizacao_id, a.area_status_id, a.titular_funcionario_id, a.superior_area_id, a.sigla, a.descricao, a.ativa, a.auxiliar_funcionario_id, s.funcionario_id as substituto_funcionario_id FROM area a LEFT OUTER JOIN substituicao s ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) WHERE organizacao_id = ? " . $area_condition . " ORDER BY sigla";
 		$result = $this->db->query($query, $values);
 		$this->_checkError($result);
Index: /trunk/workflow/inc/class.so_userinterface.inc.php
===================================================================
--- /trunk/workflow/inc/class.so_userinterface.inc.php	(revision 2048)
+++ /trunk/workflow/inc/class.so_userinterface.inc.php	(revision 2056)
@@ -234,5 +234,5 @@
 
 		/* gather some info from the area */
-		$areaInfo = $this->db->query('SELECT COALESCE(a.titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(s.funcionario_id, -1) AS substituto_funcionario_id FROM area a LEFT OUTER JOIN substituicao s USING(area_id) WHERE (a.organizacao_id = ?) AND (a.area_id = ?) AND ((CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim) OR (s.substituicao_id IS NULL))', array($organizationID, $areaID))->GetArray(-1);
+		$areaInfo = $this->db->query('SELECT COALESCE(a.titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(s.funcionario_id, -1) AS substituto_funcionario_id FROM area a LEFT OUTER JOIN substituicao s ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) WHERE (a.organizacao_id = ?) AND (a.area_id = ?)', array($organizationID, $areaID))->GetArray(-1);
 		if (empty($areaInfo))
 			return false;
@@ -509,5 +509,5 @@
 	function getAreaWithSubtituteBoss( $organizationID )
 	{
-		$result = $this -> db -> query( "SELECT a.sigla as area, a.titular_funcionario_id as titular, s.funcionario_id as substituto, s.data_inicio, s.data_fim FROM area a INNER JOIN substituicao s USING (area_id) WHERE (organizacao_id = ?) and CURRENT_DATE BETWEEN data_inicio AND data_fim ORDER BY area", array( $organizationID ) );
+		$result = $this -> db -> query( "SELECT a.sigla as area, a.titular_funcionario_id as titular, s.funcionario_id as substituto, s.data_inicio, s.data_fim FROM area a INNER JOIN substituicao s ON ((a.area_id = s.area_id) AND (CURRENT_DATE BETWEEN s.data_inicio AND s.data_fim)) WHERE (organizacao_id = ?) ORDER BY area", array( $organizationID ) );
 
 		$cachedLDAP = $GLOBALS[ 'workflow' ][ 'factory' ] -> newInstance( 'CachedLDAP' );
Index: /trunk/workflow/js/orgchart/organization.js
===================================================================
--- /trunk/workflow/js/orgchart/organization.js	(revision 2042)
+++ /trunk/workflow/js/orgchart/organization.js	(revision 2056)
@@ -884,5 +884,5 @@
 
 				// get the list of substitutions
-				objSubstitution.generateUpdateTable({'organization_id': organizationID, 'area_id': $(area_id).value}, divLB);
+				objSubstitution.generateUpdateTable({'organizacao_id': organizationID, 'area_id': $(area_id).value}, divLB);
 			}
 			objArea.list(info_callback, {'organizacao_id': organizationID, 'area_id': $(area_id).value});
