<?php

date_default_timezone_set("Asia/Kolkata");

function LastFinancialYr($date='')
	{
	/*	if ((gmdate("y")) < 10)
			{
			$FnYear =(gmdate("y"));
			}
		else
			{
			$FnYear = (gmdate("y"));
			}*/
		  $LastFinancialYear = (gmdate("Y")-1)."-".(gmdate("Y"));
		  return $LastFinancialYear;
	}
	
		
	function FinancialYrRangesInShort($date='',$noofyearsback='0',$shortsecondyearformat='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	
		if ($monthtofind < 4)
		{
			// jan feb or march month so deduct one from current year
			$FnYear = ($yeartofind-1);
			if ($shortsecondyearformat=='1')
				{
					//$FinancialYear= $yeartofind."-".substr($FnYear, 2, 2);
					$FinancialYear= $FnYear."-".substr($yeartofind,2,2);				
				}
				else 
				{				
					$FinancialYear= $FnYear."-".$yeartofind;
				}
			  
		}
		else
		{
			$FnYear = ($yeartofind+1);
			
				if ($shortsecondyearformat=='1')
				{
					$FinancialYear= $yeartofind."-".substr($FnYear, 2, 2);				
				}
				else 
				{				
					$FinancialYear= $yeartofind."-".$FnYear;
				}
			
		}
		
		return $FinancialYear;
	}
	// Current Financial year
function FinancialYr($date='',$noofyearsback='0')
	{
	
	//$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	//$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	// major change by rajiv removed gmdate and changed it to date	
	$monthtofind = ($date=='')?date("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):date("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	$yeartofind = ($date=='')?date("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):date("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	
		if ($monthtofind < 4)
		{
			// jan feb or march month so deduct one from current year
			$FnYear = ($yeartofind-1);
			$FinancialYear= $FnYear."-".$yeartofind;  
		}
		else
		{
			$FnYear = ($yeartofind+1);
			$FinancialYear= $yeartofind."-".$FnYear;
		}
		
		return $FinancialYear;
	}
	
	function FinancialYrRanges($date='',$noofyearsback='0',$shortsecondyearformat='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	
		if ($monthtofind < 4)
		{
			// jan feb or march month so deduct one from current year
			$FnYear = ($yeartofind-1);
			if ($shortsecondyearformat=='1')
				{
					//$FinancialYear= $yeartofind."-".substr($FnYear, 2, 2);
					//$FinancialYear= $FnYear."-".substr($yeartofind,2,2);
					$FinancialYear= $FnYear."-".substr($yeartofind,2,2);				
				}
				else 
				{				
					$FinancialYear= $FnYear."-".$yeartofind;
				}
			  
		}
		else
		{
			$FnYear = ($yeartofind+1);
			for($i=0;$i<3;$i++)
			{
				if ($shortsecondyearformat=='1')
				{
					//$FinancialYear= $yeartofind."-".substr($FnYear, 2, 2);
					$FinancialYear= $yeartofind."-".substr($FnYear, 2, 2);				
				}
				else 
				{				
					$FinancialYear= $yeartofind."-".$FnYear;
				}
			}
		}
		
		return $FinancialYear;
	}
	
	
	//get last 3 years from current financial year
	function ThreeFinancialYr()
	{
		$currentfinancialyear = FinancialYr();
		
		$financialinfo			= explode('-',$currentfinancialyear);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];	
		$previousyear1 = ($year1-1)."-".($year2-1);
		$previousyear2 = ($year1-2)."-".($year2-2);
		$previousyear3 = ($year1-3)."-".($year2-3);
		$yearMenu		.=	''.$previousyear1.'||'.$previousyear2.'||'.$previousyear3.'';
		return $yearMenu;
	}
	//get financial year after 5 year
	function CalculateYearAfterFiveYear()
	{
		$currentfinancialyear = FinancialYr();
				
		$financialinfo			= explode('-',$currentfinancialyear);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];	
		$yearafter5yearinrange= ($year1+5)."-".($year2+5);
		$yearafter5year = $year1+5;
		//$previousyear2 = ($year1-2)."-".($year2-2);
		//$previousyear3 = ($year1-3)."-".($year2-3);
		//$yearMenu		.=	''.$previousyear1.'||'.$previousyear2.'||'.$previousyear3.'';
		return '31-03-'.$yearafter5year;
	}
	//get first date of month from selected date
	function CalculateStartDateOfMonth($date='')
	{
		
		$first_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", first day of this month");
		$first_date = date("Y-m-d",$first_date_find);

		return $first_date;
	}	
	
	//get last date of month from selected date
	function CalculateEndDateOfMonth($date='')
	{
		
		$last_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", last day of this month");
		$last_date = date("Y-m-d",$last_date_find);
	
	   return $last_date;
	}	
	
	function CalculateYearInRangesBeforeSevenYear()
	{
		$currentfinancialyear = FinancialYr();
		
		$financialinfo			= explode('-',$currentfinancialyear);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];	
		$currentyear1    = ($year1).'-04-01'."|".($year2).'-03-31';
		for($i=0;$i<10;$i++) 
		{
			$years 			= ($year1-$i).'-04-01'."|".($year2-$i).'-03-31';
			$yearinrange 	= "April ".($year1-$i)."-March ".($year2-$i);
			if($years == $currentyear1)
			{
			$options 	  .='<option value="'.$years.'" selected="selected">'.$yearinrange.'</option>';
		    }
		    else
		    {
		        $options 	  .='<option value="'.$years.'">'.$yearinrange.'</option>';

		    }
		}
	
		return $options;
	}
	
	function DisplayFinancialYearForReceiptAndInvoices($selectedyear='')
	{
		$baseyear = "2007-2008";
		$baseyearinfo = explode('-',$baseyear);
		$baseyear1 = $baseyearinfo[0];
		$baseyear2 = $baseyearinfo[1];	
		$currentfinancialyear = FinancialYr();
		$financialinfo			= explode('-',$currentfinancialyear);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];	
		$diff1 = $year1-$baseyear1;
		$diff2 = $year2-$baseyear2;
		for($i=0;$i<=$diff1;$i++) 
		{
			$years 			= ($year1-$i).'-04-01'."|".($year2-$i).'-03-31';
			$yearinrange 	= "April ".($year1-$i)."-March ".($year2-$i);
			if($selectedyear == $years)
			{
				$options 	  .='<option value="'.$years.'" selected="selected">'.$yearinrange.'</option>';
			}
			else
			{
				$options 	  .='<option value="'.$years.'">'.$yearinrange.'</option>';
			}
		}
		//return $baseyear;
		return $options; 
	}
	
	function CalculateYearInRangesBeforeFourYear()
	{
		$currentfinancialyearinranges = LastFinancialYr();
		$financialinfo			= explode('-',$currentfinancialyearinranges);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];	
		for($i=0;$i<4;$i++) 
		{
			$years 			= ($year1-$i)."-".($year2-$i);
			//$yearinrange 	= "April ".($year1-$i)."-March ".($year2-$i);
			$options 	  .='<th>'.$years.'</th>';
		}
		return $options;
	}
	function YearInRangesForMonthlyServiceTaxReport()
	{
		$currentfinancialyearinranges = FinancialYr();
		$baseyearinfo = explode('-',$currentfinancialyearinranges);
		$year1 = $baseyearinfo[0];
		$year2 = $baseyearinfo[1];
		$baseyear1 = $year1 + 4;
		$baseyear2 = $year2 + 4;
		//$year1 = $baseyear1-1;
		//$year2 = $baseyear2-1;	
		for($i=0;$i<8;$i++)
		{
			$years 			= ($baseyear1-$i)."-".($baseyear2-$i);
			$options 	  .=$years.',';
		}
		//$finalyear = $year1.'-'.$year2;
		return $options;
	}
	
	function YearInRangesForReconciliationReport()
	{
		$currentfinancialyearinranges = FinancialYr();
		$baseyearinfo = explode('-',$currentfinancialyearinranges);
		$year1 = $baseyearinfo[0];
		$year2 = $baseyearinfo[1];
		$baseyear1 = $year1 - 3;
		$baseyear2 = $year2 - 3;
		//$year1 = $baseyear1-1;
		//$year2 = $baseyear2-1;	
		for($i=5;$i>=0;$i--)
		{
			$years 			= ($baseyear1+$i)."-".($baseyear2+$i);
			$options 	  .=$years.',';
		}
		//$finalyear = $year1.'-'.$year2;
		return $options;
	}
	
	function FinancialYrStarAndEndDate($date='',$noofyearsback='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	
		if ($monthtofind < 4)
		{
			// jan feb or march month so deduct one from current year
			$FnYear = ($yeartofind-1);
			//$FinancialYear= $FnYear."-".$yeartofind;  
			$FinancialYear= '01-04-'.$FnYear."||".'31-03-'.$yeartofind; 
		}
		else
		{
			$FnYear = ($yeartofind+1);
			$FinancialYear= '01-04-'.$yeartofind."||".'31-03-'.$FnYear;
		}
		
		return $FinancialYear;
	}
	
	
	function LastFinancialYrStarAndEndDate($date='',$noofyearsback='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	//$monthtofind='5';
		if ($monthtofind < 4)
		{
			// jan feb or march month so deduct one from current year
			$FnYear = ($yeartofind-2);
			 $FnYear1 = ($yeartofind-1);
			//$FinancialYear= $FnYear."-".$yeartofind;  
			$FinancialYear= '01-04-'.$FnYear."||".'31-03-'.$FnYear1; 
		}
		else
		{
			$FnYear = ($yeartofind-1);
	
			$FinancialYear= '01-04-'.$FnYear."||".'31-03-'.$yeartofind;
		}
		
		return $FinancialYear;
	}
	
	
	function YearInRangesForExportTurnoverReport()
	{
		$currentfinancialyearinranges = FinancialYr();
		$baseyearinfo = explode('-',$currentfinancialyearinranges);
		$year1 = $baseyearinfo[0];
		$year2 = $baseyearinfo[1];
		$baseyear1 = $year1 - 6;
		$baseyear2 = $year2 - 6;
		//$year1 = $baseyear1-1;
		//$year2 = $baseyear2-1;	
		for($i=0;$i<7;$i++)
		{
			$years 			= ($baseyear1+$i)."-".($baseyear2+$i);
			$options 	  .=$years.',';
		}
		//$finalyear = $year1.'-'.$year2;
		return $options;
	}	
	
	function DisplayYearAndInvoiceTotalInRangesBeforeFourYear($memberid = '')
	{
		$invoice			=	new Invoice();
		$sqlforderegister = "select * from memberderegistrationdetails where memberid = $memberid order by deregisterid desc limit 0,1";
		$deregisterlist		=	$invoice->getInvoice("",$sqlforderegister);
		$deregistercount = count($deregisterlist);
		//$memberid			=		$deregisterlist[0]['memberid'];
		$deregisterdate	=		$deregisterlist[0]['deregisterdate'];
		$currentfinancialyearinranges = LastFinancialYr();
		$financialinfo			= explode('-',$currentfinancialyearinranges);
		$year1			= $financialinfo[0];
		$year2			= $financialinfo[1];
		$last_fin_year_range1 = ($year1 - 0)."-".($year2 - 0);
		$last_fin_year_range2 = ($year1 - 1)."-".($year2 - 1);
		$last_fin_year_range3 = ($year1 - 2)."-".($year2 - 2);
		$last_fin_year_range4 = ($year1 - 3)."-".($year2 - 3);		
		
		$yearsdisplay = '<tr>
								<td style="border:1px solid #000;width:2%;border-top:none;">&nbsp;</td>
								<td style="border:1px solid #000;width:1%;border-top:none;border-right:1px solid #000;">&nbsp;</td>
								<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> '.$last_fin_year_range1.' </strong></td>
								<td style="border:1px solid #000;width:10%;border-top:none;text-align:center;"><strong> '.$last_fin_year_range2.' </strong></td>
								<td style="border:1px solid #000;width:10%;border-top:none;text-align:center;"><strong> '.$last_fin_year_range3.' </strong></td>
								<td style="border:1px solid #000;width:15%;border-top:none;text-align:center;"><strong> '.$last_fin_year_range4.' </strong></td>	
							</tr>';
		/*
		//$sql = "select * from mstmember a inner join trninvoicetomember b on a.memberid = b.memberid 
			where b.memberid = $membrid and b.invoicesrno > 0 and b.iscancelled = 0 and b.invoicedate between '$startdate' and '$enddate'";
			// $sql;
			*/
		if($deregistercount > 0)
		{
			$sql = "select  head_receipt_srno,invoicesrno,invoicetotalamount,month(b.invoicedate) as invmonth, year(b.invoicedate) as invyear,membershipforyear from mstmember a inner join trninvoicetomember b on a.memberid = b.memberid 
			INNER JOIN trninvoicetomemberdetails c on b.memberinvoicesid=c.memberinvoicesid
			INNER JOIN membership_packages d on c.membershippackageid=d.packageid 
			INNER JOIN member_payment_header e on b.memberinvoicesid=e.memberinvoicesid
			where b.memberid = $memberid and b.invoicesrno > 0 and b.iscancelled = 0 and b.invoicedate > '$deregisterdate'
			group by d.membershipforyear";
		}
		else 
		{
			$sql = "select  head_receipt_srno,invoicesrno,invoicetotalamount,month(b.invoicedate) as invmonth, year(b.invoicedate) as invyear,membershipforyear from mstmember a inner join trninvoicetomember b on a.memberid = b.memberid 
			INNER JOIN trninvoicetomemberdetails c on b.memberinvoicesid=c.memberinvoicesid
			INNER JOIN membership_packages d on c.membershippackageid=d.packageid 
			INNER JOIN member_payment_header e on b.memberinvoicesid=e.memberinvoicesid
			where b.memberid = $memberid and b.invoicesrno > 0 and b.iscancelled = 0 
			group by d.membershipforyear";
		}
//echo $sql;

			$GST_start_date = '2017-06-30';
			$invoicelist			=	$invoice->getInvoice("",$sql);
			//$InvoiceTotalAmount	=	$invoicelist[0]['InvoiceTotalAmount'];
			//$invoicesrno			=	$invoicelist[0]['invoicesrno'];
			$rowcount= count($invoicelist);
			$receiptnodetails = '<tr>
										<td style="border:1px solid #000;width:2%;border-top:none;"></td>
										<td style="border:1px solid #000;width:1%;border-top:none;border-right:1px solid #000;">Receipt No</td>';	
			for($i=0;$i<$rowcount;$i++) 
			{
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range1)
				{
					$year1 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>'.(($invoicelist[$i]['head_receipt_date'] > $GST_start_date)?'substr('.$invoicelist[$i]['head_receipt_srno'].',1);':''.$invoicelist[$i]['head_receipt_srno'].'').' </strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year1 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			for($i=0;$i<$rowcount;$i++) 
			{				
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range2)
				{
					$year2 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> '.$invoicelist[$i]['head_receipt_srno'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year2 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			for($i=0;$i<$rowcount;$i++) 
			{				
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range3)
				{
					$year3 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> '.$invoicelist[$i]['head_receipt_srno'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year3 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}			
			for($i=0;$i<$rowcount;$i++) 
			{			
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range4)
				{
					$year4 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> '.$invoicelist[$i]['head_receipt_srno'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year4 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			$retval1 = $yearsdisplay.$receiptnodetails.$year1.$year2.$year3.$year4.'</tr>'; 				
			
			
			$invoicedetails = '<tr>
										<td style="border:1px solid #000;width:2%;border-top:none;"></td>
										<td style="border:1px solid #000;width:1%;border-top:none;border-right:1px solid #000;">Amount</td>';	
			for($i=0;$i<$rowcount;$i++) 
			{
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range1)
				{
					$year1 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> Rs.'.$invoicelist[$i]['invoicetotalamount'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year1 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			for($i=0;$i<$rowcount;$i++) 
			{				
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range2)
				{
					$year2 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> Rs.'.$invoicelist[$i]['invoicetotalamount'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year2 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			for($i=0;$i<$rowcount;$i++) 
			{				
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range3)
				{
					$year3 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> Rs.'.$invoicelist[$i]['invoicetotalamount'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year3 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}			
			for($i=0;$i<$rowcount;$i++) 
			{			
				if($invoicelist[$i]['membershipforyear']==$last_fin_year_range4)
				{
					$year4 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong> Rs.'.$invoicelist[$i]['invoicetotalamount'].'</strong></td>';
					break;				
				}
				else if($i==$rowcount-1)
				{
					$year4 = '<td style="border:1px solid #000;width:22.7%;border-top:none;text-align:center;"><strong>&nbsp;</strong></td>';				
				}
			}
			$retval2 = $invoicedetails.$year1.$year2.$year3.$year4.'</tr>';
			$retval = $retval1.$retval2;
			 		
		return $retval;
	}
	
/*	function YearComboToAddTurnover()
	{
		$CurFinancialYrDates = FinancialYrStartEndDates(date("m d Y"),0);
		$LastFinancialYrDates = FinancialYrStartEndDates(date("m d Y"),-1);
		
		$CurFinDates = explode('|',$CurFinancialYrDates);
		$LastFinDates = explode('|',$LastFinancialYrDates);
		
		//if($fromdate=='' && $todate=='') {
		$CurFinStartDate = $CurFinDates[0];
		$CurFinEndDate = $CurFinDates[1];
	
		$LastFinEndDate ;
		
		$LastFinStartDate = $LastFinDates[0];
		$LastFinStartDate = $LastFinDates[1];
		
		
	
		$cur_fin_start_year = strtok($CurFinStartDate, '-');
		$cur_fin_end_year   = strtok($CurFinEndDate, '-');
		
		$cur_fin_year_range = $cur_fin_start_year."-".$cur_fin_end_year;
		//echo "current".$cur_fin_year_range."<br>";
		$last_fin_year_range1 = ($cur_fin_start_year - 1)."-".($cur_fin_end_year - 1);
		$last_fin_year_range2 = ($cur_fin_start_year - 2)."-".($cur_fin_end_year - 2);
		$last_fin_year_range3 = ($cur_fin_start_year - 3)."-".($cur_fin_end_year - 3);
		$last_fin_year_range4 = ($cur_fin_start_year - 4)."-".($cur_fin_end_year - 4);	
		$menu = '<option value="'.$last_fin_year_range1.'">'.$last_fin_year_range1.'</option>
					<option value="'.$last_fin_year_range2.'">'.$last_fin_year_range2.'</option>
					<option value="'.$last_fin_year_range3.'">'.$last_fin_year_range3.'</option>
					<option value="'.$last_fin_year_range4.'">'.$last_fin_year_range4.'</option>';
		return $menu;
	}
	*/
	function YearComboToAddTurnover($selectedyear='')
	{
		/*
		$baseyear = "2007-2008";
		$baseyearinfo = explode('-',$baseyear);
		$baseyear1 = $baseyearinfo[0];
		$baseyear2 = $baseyearinfo[1];	
		*/
		$currentfinancialyear = FinancialYr();
		//$nextfinyear = $currentfinancialyear + 1;
		$financialinfo			= explode('-',$currentfinancialyear);
		$year1			= $financialinfo[0]-5;
		$year2			= $financialinfo[1]-5;
			//$base = $year1.'-'.$year2;
		$options = "";
		//$diff1 = $year1-$baseyear1;
		//$diff2 = $year2-$baseyear2;
		
			for($i=2;$i<5;$i++) 
			{
				$yearinrange = ($year1+$i).'-'.($year2+$i);
				if($selectedyear == $yearinrange)
				{
					$options 	  .= '<option value="'.$yearinrange.'" selected="selected">'.$yearinrange.'</option>';
				}
				else
				{
					$options 	  .= '<option value="'.$yearinrange.'">'.$yearinrange.'</option>';
				}
				//echo $yeartodisplay;
			}
		//return $baseyear;
		return $options; 
	}
	
   function ThisQuarter($date='',$noofyearsback='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	//$monthtofind='5';
		if ($monthtofind <= 3)
		{
			$quarter = '1';
			$display_quarter_month ="Jan-Feb-Mar";
			$Year = ($yeartofind);
		}
		elseif ($monthtofind <= 6)
		{
			$quarter = '2';
			$display_quarter_month ="Apr-May-Jun";
			$Year = ($yeartofind);
		}
		elseif ($monthtofind <= 9)
		{
			$quarter = '3';
			$display_quarter_month ="Jul-Aug-Sept";
			$Year = ($yeartofind);
		}
		elseif ($monthtofind <= 12)
		{
			$quarter = '4';
			$display_quarter_month ="Oct-Nov-Dec";
			$Year = ($yeartofind);
		}
		else
		{
			$quarter = '4';
			$display_quarter_month ="Oct-Nov-Dec";
			$Year = ($yeartofind);
		}
		
		return $quarter."||".$display_quarter_month."||".$Year;
	}
	
	function LastQuarter($date='',$noofyearsback='0')
	{
	
	$monthtofind = ($date=='')?gmdate("n",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("n",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)));
	
	$yeartofind = ($date=='')?gmdate("Y",mktime(null,null,null,date("n"),date("d"),date("Y"))):gmdate("Y",mktime(null,null,null,substr($date,0,2),substr($date,3,2),substr($date,6,4)))-$noofyearsback;
	//$monthtofind='5';
		if ($monthtofind <= 3)
		{
			$quarter = '4';
			$display_quarter_month ="Oct-Nov-Dec";
			$Year = ($yeartofind -1);
		}
		elseif ($monthtofind <= 6)
		{
			$quarter = '1';
			$display_quarter_month ="Jan-Feb-Mar";
			$Year = ($yeartofind);
		}
		elseif ($monthtofind <= 9)
		{
			$quarter = '2';
			$display_quarter_month ="Apr-May-Jun";
			$Year = ($yeartofind);
		}
		elseif ($monthtofind <= 12)
		{
			$quarter = '3';
			$display_quarter_month ="Jul-Aug-Sept";
			$Year = ($yeartofind);
		}
		else
		{
			$quarter = '4';
			$display_quarter_month ="Oct-Nov-Dec";
			$Year = ($yeartofind -1);
		}
		
		return $quarter."||".$display_quarter_month."||".$Year;
	}
	
	function DateRenge($optvalue,$tableColname)
	{	
		$day = $optvalue;

		$this_year 				= date("Y",strtotime("now"));
		$today						=	date('Y-m-d',strtotime("now"));
		$yesterday				=	date('Y-m-d',strtotime("last days"));	
		$this_month			=	date("m",strtotime("now"));
		$lastmonth      		= date("m",strtotime("-1 month"));
     	$lastmonthyear  = date("Y",strtotime("-1 month"));
     	
     	
		$ThisQuarter = ThisQuarter();
		$ThisQuarter_and_Month =  explode('||',$ThisQuarter);
		$this_quarter 						= $ThisQuarter_and_Month[0];
		$this_quarter_year	= $ThisQuarter_and_Month[2];
		
		$LastQuarter = LastQuarter();
		$LastQuarter_and_Month =  explode('||',$LastQuarter);
		$last_quarter							= $LastQuarter_and_Month [0];
		$last_quarter_year 	= $LastQuarter_and_Month [2];

		
		//current financial Year Start And End Date
		$FinancialYrStarAndEndDate		=	FinancialYrStarAndEndDate();
		$CurFinancialYrDates 									= explode('||',$FinancialYrStarAndEndDate);
		$CurFinancialYrStartDate 			= $CurFinancialYrDates[0];
		$CurFinancialYrEndDate 				= $CurFinancialYrDates[1];
		$cur_fy_startdate                      = date("Y-m-d", strtotime($CurFinancialYrStartDate));
		$cur_fy_enddate                      = 	date("Y-m-d", strtotime($CurFinancialYrEndDate));
		//last financial Year Start And End Date
		$LastFinancialYrStarAndEndDate		=	LastFinancialYrStarAndEndDate();
		$LastFinancialYrDates 							= explode('||',$LastFinancialYrStarAndEndDate);
		$lastFinancialYrStartDate 					= $LastFinancialYrDates[0];
		$lastFinancialYrEndDate 						= $LastFinancialYrDates[1];
		$last_fy_startdate                      = date("Y-m-d", strtotime($lastFinancialYrStartDate));
		$last_fy_enddate                      = 	date("Y-m-d", strtotime($lastFinancialYrEndDate));

		switch ($day) 
		{
		    case "Today":
		        $where_day .= "$tableColname = '$today'  and YEAR (".$tableColname.") = '$this_year '";
		        break;
		    case "Yesterday":
		        $where_day .= "$tableColname = '$yesterday'  and YEAR (".$tableColname.") = '$this_year '";
		        break;
		    case "Last_7_days":
		     	$where_day .= "$tableColname >= CURRENT_DATE()-7  and YEAR (".$tableColname.") = '$this_year '";
		        break;
		    case "Last_30_days":
		   		$where_day .= "$tableColname >= (CURDATE() - INTERVAL 1 MONTH )";
		        break;
		    case "This_month":
		        $where_day .= "MONTH(".$tableColname.") in ('$this_month')  and YEAR (".$tableColname.") = '$this_year '";
		        break;
		    case "Last_month":
					$where_day .= "MONTH(".$tableColname.") in ('$lastmonth')  and YEAR (".$tableColname.") = '$lastmonthyear' ";
		        break;
		    case "This_quarter":
				$where_day .= "QUARTER(".$tableColname.")='$this_quarter' and YEAR (".$tableColname.") = '$this_quarter_year'";
		    break;
		   	case "Last_quarter":
				$where_day .= "QUARTER(".$tableColname.")='$last_quarter' and YEAR (".$tableColname.") = '$last_quarter_year'";
		    break;
		   case "This_year":
					$where_day ="date(".$tableColname.") BETWEEN '$cur_fy_startdate' and '$cur_fy_enddate'";
					break;
			case "Last_year":
					$where_day ="date(".$tableColname.") BETWEEN '$last_fy_startdate' and '$last_fy_enddate '";
					break;
		    case "All_time":
		    	$where_day .= "";
		    break;
		     default:
		        //	$where_day ="date(".$tableColname.") BETWEEN '$today' and '$today'";
		        		$where_day .= "";
		}
		return $where_day;
	}
	
	

	?>