<?php

function f_get_fin_period($site, $year, $date, $conn){
    $period;
	
	//1
	$sql = "SELECT 	'period' = 1
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."' 
			AND		prod_year = ".$year." 
			AND		p1beg <= '".$date."'
			AND  	p2beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 1)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//2
	$sql = "SELECT 	'period' = 2
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p2beg <= '".$date."'
			AND  	p3beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 2)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//3
	$sql = "SELECT 	'period' = 3
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p3beg <= '".$date."'
			AND  	p4beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 3)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//4
	$sql = "SELECT 	'period' = 4
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p4beg <= '".$date."'
			AND  	p5beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 4)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//5
	$sql = "SELECT 	'period' = 5
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p5beg <= '".$date."'
			AND  	p6beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 5)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//6
	$sql = "SELECT 	'period' = 6
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p6beg <= '".$date."'
			AND  	p7beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 6)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//7
	$sql = "SELECT 	'period' = 7
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p7beg <= '".$date."'
			AND  	p8beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 7)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//8
	$sql = "SELECT 	'period' = 8
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p8beg <= '".$date."'
			AND  	p9beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 8)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//9
	$sql = "SELECT 	'period' = 9
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p9beg <= '".$date."'
			AND  	p10beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 9)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//10
	$sql = "SELECT 	'period' = 10
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p10beg <= '".$date."'
			AND  	p11beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 10)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//11
	$sql = "SELECT 	'period' = 11
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p11beg <= '".$date."'
			AND  	p12beg > '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 11)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	//12
	$sql = "SELECT 	'period' = 12
			FROM	cf_acct_period (NOLOCK)
			WHERE 	site_cd = '".$site."'		
			AND		prod_year = ".$year."
			AND		p12beg <= '".$date."'
			AND  	end_of_year >= '".$date."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period - 12)";
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$period = $row['period'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	return $period;
}

?>