<?xml version="1.0" ?>


<!-- JNCC  Records for selected Locations with Designations Short Summary 
     XML Report by Mike Weideli of Littlefield Consultancy for JNCC
     http://www.lfield.co.uk
     Version 3 - April 17  2009  

   
     QUICK REPORT - on Location
     Confidential - By parameter 
     Zero Abundance - By paramater
     Validated - By parameter
     Invalid = By parameter	
   
 --> 


 
<CustomReport menupath="Geographic Area/Location Reports\Location hierarchy reports"  title="L2 Location Report (inc. designations (by set name))" 
description="Records for a specified Location. Includes a designation summary based on a set name.  
" >

<SQL>
DECLARE @set char(16)
DECLARE @Level int
DECLARE @Restrict Int

<Where keytype="Default">



SET @SET  = 
(SELECT TDS.Taxon_Designation_Set_Key FROM Taxon_designation_set TDS Where   
<Condition field= "TDS.Title" operator="Equal" type="text"  name="Taxon Designation Set Title"  />)


Set @Level 
<Condition field="" operator="equal" type="OptionSet"  name="Status output" >
        <Option name="Short name" value = "1" />
        <Option name="Long Name" value = "2" />
        <Option name="Abbreviation" value = "4" />
        <Option name="Kind" value = "3" />
        <Option name="Yes/No" value = "5" />

</Condition>



Set @Restrict
<Condition field="" operator="equal" type="OptionSet"  name="Restrict to Designated Taxa" >
        <Option name="No" value = "2" />
        <Option name="Yes" value = "1" />
       

</Condition>

SELECT TOCC.Taxon_Occurrence_key, L.Spatial_ref,S.Spatial_ref as grid_ref,LN.Item_name,s.vague_date_start,s.vague_date_end,s.vague_date_type,dbo.formateventRecorders
(s.sample_key) as Recorders, dbo.FormatIndividual (I.Title, I.Initials, I.Forename, I.Surname) as Determiner,
ITN.Preferred_name, ITN.Authority,ITN.Common_name, ITN2.Actual_name as Recommended_name, ITN2.Common_Name as Recommended_Common,
TG.Taxon_Group_Name,
dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null) as Status


From Location L
INNER JOIN Location_Name LN ON LN.Location_key = L.Location_key
INNER JOIN Sample S ON S.Location_key = L.Location_key
INNER JOIN Taxon_Occurrence TOCC ON TOCC.Sample_key = S.Sample_key
INNER JOIN Taxon_Determination TDET on TDET.Taxon_Occurrence_key = TOCC.Taxon_Occurrence_key
INNER JOIN Taxon_List_Item TLI ON TLI.Taxon_List_Item_key = TDET.Taxon_List_Item_Key
INNER JOIN Taxon_Version TV ON TV.Taxon_version_Key = TLI.Taxon_Version_Key
LEFT JOIN Taxon_Group TG ON TV.Output_group_key = TG.Taxon_group_Key
INNER JOIN Index_Taxon_Name ITN ON ITN.Taxon_List_Item_Key = TDET.Taxon_List_Item_Key
INNER JOIN Index_taxon_Name ITN2 ON ITN2.Taxon_List_Item_key = ITN.Recommended_Taxon_List_item_key
INNER JOIN Individual I on I.name_key = TDET.Determiner  
INNER JOIN Survey_Event SE ON SE.Survey_Event_Key = S.Survey_Event_Key
LEFT JOIN User_Survey_Restriction USR
ON  USR.Survey_Key = SE.Survey_Key AND 
<Condition field="USR.Name_Key" type="CurrentUserID" name="Current User ID"     operator="equal" />


where  
USR.Name_Key IS NULL

AND
 
 ((dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null)  is not null and @Level &#60; 5) OR 
    (dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null)  ='Yes' AND @Level = 5) 
    OR @RESTRICT = 2)

AND 

LN.Preferred = 1

AND 

TDET.Preferred = 1 

AND  

<Condition field= "L.Location_Key" operator="equal" type="Location"  name="Location"  />
  
and

<Condition field= "TOCC.Verified" operator="not equal" type="OptionSet"  name="Failed/Pending verification" >

	
        <Option name="Include" value = "3" />
        <Option name="Exclude" value = "1" />
</Condition>
AND 
<Condition field= "TOCC.Confidential" operator="less than" type="OptionSet"  name="Confidential" >
	<Option name="Include" value = "2" />
        <Option name="Exclude" value = "1" />

</Condition>
AND

<Condition field= "TOCC.Checked" operator="greater than" type="OptionSet"  name="Unchecked Records" >
	<Option name="Include" value = "-1" />
        <Option name="Exclude" value = "0" />

</Condition> 
AND        
<Condition field= "TOCC.Zero_Abundance" operator="less than" type="OptionSet"  name="Zero Abundance" >
	<Option name="Include" value = "2" />
        <Option name="Exclude" value = "1" />

</Condition> 

AND

ITN.System_Supplied_data = 1	




</Where>
<Where keytype="Location">


SET @SET  =
(SELECT Taxon_designation_set_key FROM Taxon_designation_set TDS Where   
<Condition field= "TDS.Title" operator="equal" type="text"  name="Taxon Designation Set Title"  />)


Set @Level 
<Condition field="" operator="equal" type="OptionSet"  name="Status output" >
        <Option name="Short name" value = "1" />
        <Option name="Long Name" value = "2" />
        <Option name="Abbreviation" value = "4" />
        <Option name="Kind" value = "3" />
        <Option name="Yes/No" value = "5" />

</Condition>



Set @Restrict
<Condition field="" operator="equal" type="OptionSet"  name="Restrict to Designated Taxa" >
        <Option name="No" value = "2" />
        <Option name="Yes" value = "1" />
       

</Condition>

SELECT TOCC.Taxon_Occurrence_key, L.Spatial_ref,S.Spatial_ref as grid_ref,LN.Item_name,s.vague_date_start,s.vague_date_end,s.vague_date_type,dbo.formateventRecorders
(s.sample_key) as Recorders, dbo.FormatIndividual (I.Title, I.Initials, I.Forename, I.Surname) as Determiner,
ITN.Preferred_name, ITN.Authority,ITN.Common_name, ITN2.Actual_name as Recommended_name, ITN2.Common_Name as Recommended_Common,
TG.Taxon_Group_Name,
dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null) as Status


From Location L
INNER JOIN Location_Name LN ON LN.Location_key = L.Location_key
INNER JOIN Sample S ON S.Location_key = L.Location_key
INNER JOIN Taxon_Occurrence TOCC ON TOCC.Sample_key = S.Sample_key
INNER JOIN Taxon_Determination TDET on TDET.Taxon_Occurrence_key = TOCC.Taxon_Occurrence_key
INNER JOIN Taxon_List_Item TLI ON TLI.Taxon_List_Item_key = TDET.Taxon_List_Item_Key
INNER JOIN Taxon_Version TV ON TV.Taxon_version_Key = TLI.Taxon_Version_Key
INNER JOIN Taxon_Group TG ON TV.Output_group_key = TG.Taxon_group_Key
INNER JOIN Index_Taxon_Name ITN ON ITN.Taxon_List_Item_Key = TDET.Taxon_List_Item_Key
INNER JOIN Index_taxon_Name ITN2 ON ITN2.Taxon_List_Item_key = ITN.Recommended_Taxon_List_item_key
INNER JOIN Individual I on I.name_key = TDET.Determiner  
INNER JOIN Survey_Event SE ON SE.Survey_Event_Key = S.Survey_Event_Key
LEFT JOIN User_Survey_Restriction USR
ON  USR.Survey_Key = SE.Survey_Key AND 
<Condition field="USR.Name_Key" type="CurrentUserID" name="Current User ID"     operator="equal" />


where  
USR.Name_Key IS NULL

AND
 
 ((dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null)  is not null and @Level &#60; 5) OR 
    (dbo.ufn_getdesignations(ITN.taxon_List_Item_Key,@level,@set,null)  ='Yes' AND @Level = 5) 
    OR @RESTRICT = 2)

AND 

LN.Preferred = 1

AND 

TDET.Preferred = 1 

AND  

L.Location_Key = '%s'
  
and

<Condition field= "TOCC.Verified" operator="not equal" type="OptionSet"  name="Failed/Pending verification" >

	
        <Option name="Include" value = "3" />
        <Option name="Exclude" value = "1" />
</Condition>
AND 
<Condition field= "TOCC.Confidential" operator="less than" type="OptionSet"  name="Confidential" >
	<Option name="Include" value = "2" />
        <Option name="Exclude" value = "1" />

</Condition>
AND

<Condition field= "TOCC.Checked" operator="greater than" type="OptionSet"  name="Unchecked Records" >
	<Option name="Include" value = "-1" />
        <Option name="Exclude" value = "0" />

</Condition> 
AND        
<Condition field= "TOCC.Zero_Abundance" operator="less than" type="OptionSet"  name="Zero Abundance" >
	<Option name="Include" value = "2" />
        <Option name="Exclude" value = "1" />

</Condition> 

AND

ITN.System_Supplied_data = 1	



</Where>

<Orderby name="by Taxon Group" SQL="Order By tg.taxon_group_name, itn.preferred_name,s.vague_date_start"/>
 
<Orderby name="by Scientific Name" SQL="Order By itn.preferred_name, s.vague_date_start"/>

<Orderby name="Taxonomic Order" SQL="Order By itn2.Sort_Order, s.vague_date_start"/>

</SQL>
<Columns>
  <Column name="Taxon_Occurrence_key" visible="false"  tablename= "Taxon_Occurrence" />
  <Column name="Spatial_ref" width="150" position="1" caption="Location Spatial Ref" keycolumn ="Taxon_Occurrence_key"/>   
  <Column name="Grid_Ref" width="150" position="2" caption="Sample Spatial Ref" /> 
  <Column name="Item_name" width="200" position="3" caption="Location Name" /> 
  <Column name="Vague_date_start" width="100" position="4" caption="Date" /> 
  <Column name="Recorders" width="200" position="5" caption="Recorders" /> 
  <Column name="Determiner" width="120" position="6" caption="Determiner" /> 
  <Column name="Taxon_Group_name" width="200" position="7" caption="Taxon Group" /> 
  <Column name="Preferred_Name" width="200" position="8" caption="Scientific_Name" /> 
  <Column name="Authority" width="200" position="9" caption="Authority" /> 
  <Column name="Common_Name" width="200" position="10" caption="Common Name" /> 
  <Column name="Recommended_name" width="200" position="11" caption="Recommended Name" /> 
  <Column name="Recommended_Common" width="200" position="12" caption="Preferred Common Name" /> 
  
  <Column name="Status" width="300" position="13" caption="Designation" /> 

  
   

 
</Columns>
</CustomReport>

