package My_AgencyFunctions;

use strict;
use Data::Dumper;

#--------------------------------------------------------------------------------------------------
sub new
{
  my $pclass = shift;
  my $pdbhMain = shift;
  my $pGLOBALS = shift;
  my $self = {
    dbh=>$pdbhMain,
    GLOBALS=>$pGLOBALS,
    logger=>Log::Log4perl->get_logger()
  };
  bless $self, $pclass;
  $self->{logger}->info("::DBVersions::Instance Started");
  return $self;
}

#--------------------------------------------------------------------------------------------------
#@function agency_functions_agency_setting_types_list
#@author AUTOAPI
#@description List setting_types(s) on the ssystem
#@params api_action
#api_content>session_id
#@params_description
#api_action - The name of the function   
#@return 
#status
#error_description
#message_level
#agency_setting_types
#agency_setting_types>agency_setting_type
#agency_setting_types>agency_setting_type>agency_setting_type_id
#agency_setting_types>agency_setting_type>agency_setting_type_desc
#agency_setting_types>agency_setting_type>agency_setting_type_default_value
#agency_setting_types>agency_setting_type>agency_setting_type_can_edit
#agency_setting_types>agency_setting_type>agency_setting_type_setting_type
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#agency_setting_types - container for all agency_setting_types
#agency_setting_type - container for each agency_setting_types
#agency_setting_type_id - ID of the item
#agency_setting_type_desc - Description of the item
#agency_setting_type_default_value - Default value for the item
#agency_setting_type_can_edit - Can the item be edited.
#agency_setting_type_setting_type - Type for the Item
#@document_class both
sub agency_functions_agency_setting_types_list($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;

	
	$statement = "select ".My_GenericFunctions->return_var_encode("id","tAGENCY_SETTING_TYPES")." as 'agency_setting_type_id', long_desc as 'agency_setting_type_desc',default_value as 'agency_setting_type_default_value',can_edit as 'agency_setting_type_can_edit',setting_type as 'agency_setting_type_setting_type'  from tAGENCY_SETTING_TYPES ";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"agency_setting_types","agency_setting_type");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

#--------------------------------------------------------------------------------------------------
#@function agency_functions_agency_settings_list
#@author AUTOAPI
#@description List agency setting(s) on the ssystem
#@params api_action
#api_content>session_id
#@params_description
#api_action - The name of the function   
#@return 
#status
#error_description
#message_level
#agency_settings
#agency_settings>agency_setting
#agency_settings>agency_setting>agency_setting_id
#agency_settings>agency_setting>agency_setting_type_id
#agency_settings>agency_setting>agency_setting_value
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#agency_settings - container for all items
#agency_setting - container for each item
#agency_setting_id - ID of the setting
#agency_setting_type_id - ID of the setting type
#agency_setting_value - Value of the setting
#@document_class both
sub agency_functions_agency_settings_list($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
  my $tUSER_id=$this->{GLOBALS}{tUSER_id};
  my $tUSER_TYPE_value=$this->{GLOBALS}{tUSER_TYPE_value};
  my $tAGENCY_value=$this->{GLOBALS}{tAGENCY_value};
  my $tAGENCY_id=$this->{GLOBALS}{tAGENCY_id};  
	
	$statement = "select ".My_GenericFunctions->return_var_encode("id","tAGENCY_SETTINGS")." as 'agency_setting_id', ".My_GenericFunctions->return_var_encode("tAGENCY_SETTING_TYPES_id","tAGENCY_SETTING_TYPES")." as 'agency_setting_type_id', value as 'agency_setting_value'  from tAGENCY_SETTINGS where tAGENCY_id=".$this->{GLOBALS}{tAGENCY_id}." and (select can_view from tAGENCY_SETTING_TYPES where id=tAGENCY_SETTING_TYPES_id)=1";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"agency_settings","agency_setting");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

#--------------------------------------------------------------------------------------------------
#@function agency_functions_agency_settings_list
#@author AUTOAPI
#@description List agency setting(s) on the ssystem
#@params api_action
#api_content>agency_id
#@params_description
#api_action - The name of the function   
#@return 
#status
#error_description
#message_level
#agency_settings
#agency_settings>agency_setting
#agency_settings>agency_setting>agency_setting_id
#agency_settings>agency_setting>agency_setting_type_id
#agency_settings>agency_setting>agency_setting_value
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#agency_settings - container for all items
#agency_setting - container for each item
#agency_setting_id - ID of the setting
#agency_setting_type_id - ID of the setting type
#agency_setting_value - Value of the setting
#@document_class both
sub agency_functions_agency_settings_get_setting_list($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
	
  if(($rxml=My_GenericFunctions->validate_input_field($this,"agency_id",$xml->{action_content}->{agency_id},"FK","tAGENCY")) ne "")
  {
    return($rxml);
  }
	
	$statement = "select ".My_GenericFunctions->return_var_encode("id","tAGENCY_SETTINGS")." as 'agency_setting_id', ".My_GenericFunctions->return_var_encode("tAGENCY_SETTING_TYPES_id","tAGENCY_SETTING_TYPES")." as 'agency_setting_type_id', value as 'agency_setting_value'  from tAGENCY_SETTINGS where tAGENCY_id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{agency_id},"tAGENCY")." and (select can_view from tAGENCY_SETTING_TYPES where id=tAGENCY_SETTING_TYPES_id)=1";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"agency_settings","agency_setting");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

#--------------------------------------------------------------------------------------------------
#@function agency_functions_agency_settings_edit
#@author AUTOAPI
#@description Edit an agency setting on the ssystem
#@params api_action
#action_content>agency_setting_id
#action_content>agency_setting_value
#@params_description
#api_action - The name of the function   
#agency_setting_id - ID of the Item to edit
#agency_setting_value - The new value
#@return 
#status
#error_description
#message_level
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#@document_class both
sub agency_functions_agency_settings_edit($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
  my $tUSER_id=$this->{GLOBALS}{tUSER_id};
  my $tUSER_TYPE_value=$this->{GLOBALS}{tUSER_TYPE_value};
  my $tAGENCY_value=$this->{GLOBALS}{tAGENCY_value};
  my $tAGENCY_id=$this->{GLOBALS}{tAGENCY_id};  
  
  if(($rxml=My_GenericFunctions->validate_input_field($this,"agency_setting_id",$xml->{action_content}->{agency_setting_id},"FK","tAGENCY_SETTINGS")) ne "")
  {
    return($rxml);
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"agency_setting_value",$xml->{action_content}->{agency_setting_value},"TEXT","")) ne "")
  {
    return($rxml);
  }
  
  $statement = "select can_edit,setting_type from tAGENCY_SETTING_TYPES where id=(select tAGENCY_SETTING_TYPES_id from tAGENCY_SETTINGS where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{agency_setting_id},"tAGENCY_SETTINGS").") ";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("No Such Setting.","warn"));
  }
  @row = $sth->fetchrow_array;
  if($row[0]!=1)
  {
    return(My_GenericFunctions->return_error("You are not allowed to edit this setting.","warn"));
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"agency_setting_value",$xml->{action_content}->{agency_setting_value},"$row[1]","")) ne "")
  {
    return($rxml);
  }
  
  $statement = "update tAGENCY_SETTINGS set value=\"".$xml->{action_content}->{agency_setting_value}."\" where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{agency_setting_id},"tAGENCY_SETTINGS")." and tAGENCY_id=$tAGENCY_id";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Update item.","warn"));
  }
  
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}
1;
