<?php session_start();
  
extract($_ENV);

extract($_GET);

extract($_POST);

extract($_COOKIE);

extract($_SERVER);

?>
<link rel="stylesheet" href="style.css" type="text/css" media="all">
<?


set_time_limit(0);

$db_host = 'localhost';
$db_user = "indmet_vinod";
$db_pass = "vinodbhaskar69*";
$db_name = "indmet_teachers";
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

global $db_host;
global $db_user;
global $db_pass;
global $db_name;
global $conn;
global $user;
//global $testuser;

$filename = 'templedger';
$filename2 = 'templedger1';
$branch= '101';
$branch_code = '101';
$myBranch = '101';

$myresult = mysqli_query($conn,"select compcode from company") or die(mysqli_error());
$myrow = mysqli_fetch_row($myresult);
$myCompany = $myrow[0];
$compcode = $myrow[0];
$myresult = mysqli_query($conn,"select distinct account_year from branches") or die(mysqli_error());
$myrow = mysqli_fetch_row($myresult);
$vinyear = substr($myrow[0],0,4);

$mystartdt = '01/04/'.$vinyear;
/*
if($_SESSION['userid']=='')
{
	?>
    	<script language="javascript">
		alert("Login ID or Password is wrong . Please login again ");
        </script>
    <?
	exit;
	
}
if($_SESSION['password'] =='')
{
	?>
    	<script language="javascript">
		alert("Login ID or Password is wrong . Please login again ");
        </script>
    <?
	exit;
}
*/
 $user =$_SESSION['userid']; 

 
 $filename = $_SESSION['userid'];
 $filename2 = $_SESSION['userid'].'_dj'; 
 $filename3 = $_SESSION['userid'].'_led'; 
 $filename4 = $_SESSION['userid'].'_bnk'; 
 //$frecord = $_SESSION['frecord'];  
 $password =$_SESSION['password']; 

 $ya = date('Y');
 $yb = $ya - 10;
/*
// USER ID AND PASSWORD CHECKING

$pt = dirname($_SERVER['PHP_SELF']);	
//echo("select user_id,password,path from users where user_id = '$filename' and password = '$password' and path = '$pt' <br>");
$uresult = mysqli_query($conn,"select user_id,password,path from users where user_id = '$filename' and password = '$password' and path = '$pt' ") or die(mysqli_error());
$urow = mysqli_fetch_row($uresult);
if(!is_array($urow))

{

 	?>
    <script language="javascript">
	 alert("You are not authorised to access this area.  Thank you");
	</script>
    <?
	exit;
}
else
{
  */
	$script_filename = $_SERVER['SCRIPT_FILENAME'];
	$remote_addr = $_SERVER['REMOTE_ADDR'];
	$browser = $_SERVER['HTTP_USER_AGENT'];
	
	$tod=gmmktime(date('h')+5,date('i')+30,date('s'));
	$time = date('H:i',$tod);
	$logdate = date('Y-m-d');
//	mysqli_query($conn,"insert into accesslog values('$branch','$filename','$remote_addr','$logdate','$time','$browser','$script_filename',NULL)") or die(mysqli_error());
//}



function filecreate($tempfile)
{
$filename= $_SESSION['userid'];

$result = mysqli_list_tables("irfannew");
$fileset="T";
for ($i = 0; $i < mysqli_num_rows($result); $i++) 
{
    if( mysqli_tablename($result, $i)== "$filename")
	{
	$query = "drop table $filename ";
	mysqli_query($conn,$query) or die(mysqli_error());
	}
	
}

	$query= "create table $filename  as select * from $tempfile ";
	mysqli_query($conn,$query) or die(mysqli_error());
	$query = "ALTER TABLE $filename ADD PRIMARY KEY(`record`)";
	mysqli_query($conn,$query) or die(mysqli_error());	
	$query = "ALTER TABLE $filename CHANGE `record` `record` INT( 15 ) NOT NULL AUTO_INCREMENT";
	mysqli_query($conn,$query) or die(mysqli_error());	

}



function filecreate2($tempfile)
{
$filename2= $_SESSION['userid'].'_dj';

$result = mysqli_list_tables("irfannew");
$fileset="T";
for ($i = 0; $i < mysqli_num_rows($result); $i++) 
{
    if( mysqli_tablename($result, $i)== "$filename2")
	{
	$query = "drop table $filename2 ";
	mysqli_query($conn,$query) or die(mysqli_error());
	}
	
}

	$query= "create table $filename2  as select * from $tempfile ";
	mysqli_query($conn,$query) or die(mysqli_error());
	$query = "ALTER TABLE $filename2 ADD PRIMARY KEY(`record`)";
	mysqli_query($conn,$query) or die(mysqli_error());	
	$query = "ALTER TABLE $filename2 CHANGE `record` `record` INT( 15 ) NOT NULL AUTO_INCREMENT";
	mysqli_query($conn,$query) or die(mysqli_error());	

}



function filecreate3($tempfile)
{
$filename3= $_SESSION['userid'].'_led';

$result = mysqli_list_tables("irfannew");
$fileset="T";
for ($i = 0; $i < mysqli_num_rows($result); $i++) 
{
    if( mysqli_tablename($result, $i)== "$filename3")
	{
	$query = "drop table $filename3 ";
	mysqli_query($conn,$query) or die(mysqli_error());
	}
	
}

	$query= "create table $filename3  as select * from $tempfile ";
	mysqli_query($conn,$query) or die(mysqli_error());
	//$query = "ALTER TABLE $filename3 ADD PRIMARY KEY(`record`)";
	//mysqli_query($conn,$query) or die(mysqli_error());	
	//$query = "ALTER TABLE $filename3 CHANGE `record` `record` INT( 15 ) NOT NULL AUTO_INCREMENT";
	//mysqli_query($conn,$query) or die(mysqli_error());	

}

function filecreate4($tempfile)
{
$filename4= $_SESSION['userid'].'_bnk';

$result = mysqli_list_tables("irfannew");
$fileset="T";
for ($i = 0; $i < mysqli_num_rows($result); $i++) 
{
    if( mysqli_tablename($result, $i)== "$filename4")
	{
	$query = "drop table $filename4 ";
	mysqli_query($conn,$query) or die(mysqli_error());
	}
	
}

	$query= "create table $filename4  as select * from $tempfile ";
	mysqli_query($conn,$query) or die(mysqli_error());
	$query = "ALTER TABLE $filename4 ADD PRIMARY KEY(`record`)";
	mysqli_query($conn,$query) or die(mysqli_error());	
	$query = "ALTER TABLE $filename4 CHANGE `record` `record` INT( 15 ) NOT NULL AUTO_INCREMENT";
	mysqli_query($conn,$query) or die(mysqli_error());	

}

function british($indate)
{
	$outdate = substr($indate,8,2).'/'.substr($indate,5,2).'/'.substr($indate,0,4);
	return $outdate;
}

function us($indate)
{
	$outdate = substr($indate,6,4).'-'.substr($indate,3,2).'-'.substr($indate,0,2);
	$st = validateDate($outdate);
	if($st == 1)
	{
		return $outdate;
	}
	else
	{
		?>
        <script language="javascript">
        alert("Not a valid Date. Please correct");
        </script>
        <?
		exit;
	}
}


function validateDate( $date, $format='YYYY-MM-DD')
{
	switch( $format )
	{
		case 'YYYY/MM/DD':
		case 'YYYY-MM-DD':
		list( $y, $m, $d ) = preg_split( '/[-\.\/ ]/', $date );
		break;

		case 'YYYY/DD/MM':
		case 'YYYY-DD-MM':
		list( $y, $d, $m ) = preg_split( '/[-\.\/ ]/', $date );
		break;

		case 'DD-MM-YYYY':
		case 'DD/MM/YYYY':
		list( $d, $m, $y ) = preg_split( '/[-\.\/ ]/', $date );
		break;

		case 'MM-DD-YYYY':
		case 'MM/DD/YYYY':
		list( $m, $d, $y ) = preg_split( '/[-\.\/ ]/', $date );
		break;

		case 'YYYYMMDD':
		$y = substr( $date, 0, 4 );
		$m = substr( $date, 4, 2 );
		$d = substr( $date, 6, 2 );
		break;

		case 'YYYYDDMM':
		$y = substr( $date, 0, 4 );
		$d = substr( $date, 4, 2 );
		$m = substr( $date, 6, 2 );
		break;

		default:
		throw new Exception( "Invalid Date Format" );
	}
	return checkdate( $m, $d, $y );
}


function actdate($today)
{
	$chkday = substr($today,1,1);
	if($chkday=='-' or $chkday=='/' or $chkday=='.')
	{
		$day = substr($today,0,1);
		$rest = substr($today,1,9);
		$day = '0'.$day;
		$today = $day.$rest;
	}
	$chkday = substr($today,4,1);
	if($chkday=='-' or $chkday=='/' or $chkday=='.')
	{
		$day = substr($today,3,1);
		$firstpart = substr($today,0,3);
		$rest = substr($today,4,9);
		$day = '0'.$day;
		$today = $firstpart.$day.$rest;
	}
			$rest = substr($today,6,4);
			$str=strlen($rest);
			if($str < 4)
			{
				?>
                <script language="javascript">
                	alert("Year must be 4 digits. Please correct");
                </script>
                <?
				exit;
			}
	
	return $today;
}



function ist()
{
	$tod=gmmktime(date('h')+5,date('i')+30,date('s'));
	$ins = date('h:i a',$tod);
	return $ins;
}


function british_blank($indate)
{
	if($indate=='00-00-0000')
	{
		$retin = '';
	}
	else
	{
		$retin= $indate;
	}
	return $retin;
}




function us_blank($indate)
{
	if($indate=='0000-00-00')
	{
		$retin = '';
	}
	else
	{
		$retin= $indate;
	}
	return $retin;
}


// FUNCTION TO CHECK THE GIVEN DATE BELONGS TO THE ACCOUNT YEAR

function date_belong($date1,$account_year)
{
	$start_year = substr($account_year,0,4);
	$chkdate1 = $start_year.'-04-01';

	$end_year = $start_year +1;
	$chkdate2 = $end_year.'-03-31';

	if($date1 < $chkdate1 or $date1 > $chkdate2)
	{
	?>
		<script language="JavaScript">
		alert("Selected date does not belong to the accounting year Selected. Please recheck the date");
		</script>	
	<?
	exit;
	}

}	




// FUNCTION TO CHECK WHETHER START DATE IS LESS THAN END DATE



function date_less_grater($date1,$date2)
{
	if($date1>$date2)
	{
		?>
		<script language="JavaScript">
		alert("Start Date must be less than End Date Please recheck the date");
		</script>	
	<?
	exit;
	}

}








function datediff($endDate, $beginDate)
{
$dformat ='/';

$beginDate = substr($beginDate ,5,2).'/'.substr($beginDate ,8,2).'/'.substr($beginDate ,0,4);
$endDate = substr($endDate,5,2).'/'.substr($endDate,8,2).'/'.substr($endDate,0,4);		


$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
$mn= $end_date - $start_date;
return $mn;
}

function previous_month($sdt,$endt)
{
$date5 = $sdt;
$curr_month = substr($date5,5,2);
$prev_month = substr($date5,5,2);
while( $prev_month== $curr_month)
{ 
	$sec = mysqli_query($conn,"select '$date5' -  interval 1 day ");
	$secondday = mysqli_fetch_row($sec);
	$nextday = substr($secondday[0],0,4).'-'.substr($secondday[0],5,2).'-'.substr($secondday[0],8,2);
	$prev_month=substr($secondday[0],5,2);
	$date5= $nextday;
}

return $date5;

}

function current_month($sdt)
{
global $conn;    
$date5 = $sdt;
$curr_month = substr($date5,5,2);
$next_month = substr($date5,5,2);
while( $next_month== $curr_month)
{ 
	$sec = mysqli_query($conn,"select '$date5' +  interval 1 day ");
	$secondday = mysqli_fetch_row($sec);
	$nextday = substr($secondday[0],0,4).'-'.substr($secondday[0],5,2).'-'.substr($secondday[0],8,2);
	$curr_month=substr($secondday[0],5,2);
	$date5= $nextday;
	//echo("DAT5 : $date5<br>");
}

return $date5;

}


function lastyear($dt)
{
	$sec = mysqli_query($conn,"select '$dt' -  interval 365 day ");
	$secondday = mysqli_fetch_row($sec);
	$nextday = substr($secondday[0],0,4).'-'.substr($secondday[0],5,2).'-'.substr($secondday[0],8,2);	
	return $nextday;
}


function no_of_month($sdt,$endt)
{
$date5 = $sdt;
$first_month = substr($sdt,5,2);
$last_month = substr($endt,5,2);
$count_month =0;
while($date5 < $endt)
{ 
	$sec = mysqli_query($conn,"select '$date5' +  interval 1 day ");
	$secondday = mysqli_fetch_row($sec);
	$nextday = substr($secondday[0],0,4).'-'.substr($secondday[0],5,2).'-'.substr($secondday[0],8,2);
	$curr_month=substr($secondday[0],5,2);
	if($first_month != $curr_month)
	{
		$count_month++;
		$first_month = $curr_month;
	}
	$date5= $nextday;
}

return $count_month;

}


function connum($num) 
{

$ones = array( "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", " Ten",  " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen");


$tensr = array( "","", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety");

$triplets = array( "", " thousand",  " million", " billion", " trillion", " quadrillion", " quintillion", " sextillion", " septillion", " octillion", " nonillion");


 // global $ones, $tensr, $triplets;

 
  $number = intval($num);
  $crore = (int) ($number/10000000);
  $reminder = (int) ($number%10000000);  
  $lakhs = (int) ($number/100000);
  $reminder = (int) ($number%100000);
  $thousand = (int) ($reminder / 1000);
  $reminder = (int) ($reminder % 1000);
  $hundred =  (int) ($reminder / 100);
  $reminder = (int) ($reminder % 100);
  $tens = intval($reminder);
  $paise     = round(($num - $number) * 100,0);

  $str = "";


if ($crore > 0 && $crore < 20)
 	 {
  	 $str .= $ones[$crore]." crore ";
   	 }
	 elseif($crore >=20)
  	 {
	   $str .= $tensr[(int) ($crore / 10)] . $ones[$crore % 10]." crore";
	 }


if ($lakhs > 0 && $lakhs < 20)
 	 {
  	 $str .= $ones[$lakhs]." lakhs ";
   	 }
	 elseif($lakhs >=20)
  	 {
	   $str .= $tensr[(int) ($lakhs / 10)] . $ones[$lakhs % 10]." lakhs";
	 }




  if ($thousand > 0 and $thousand < 20)
  	{
	$str .= $ones[$thousand]." thousand ";
   	}
  	elseif($thousand >=20)
  	{
   	$str .= $tensr[(int) ($thousand / 10)] . $ones[$thousand % 10]." thousand";
  	}


if ($hundred > 0)
  	{
   	$str.= $ones[$hundred] . " hundred";
  	}
  
  
if ($tens >0 and $tens < 20)
   	$str .= $ones[$tens];
else
 	$str .= $tensr[(int) ($tens / 10)] . $ones[$tens % 10];


if($paise > 0)
{
if ($paise < 20)
   	$str .= ' and ' .$ones[$paise] . ' paise';
else
   	$str .= ' and '.$tensr[(int) ($paise / 10)] . $ones[$paise % 10] . ' paise';
}
if($num > 0)
	{
	return($str);
	}
else
	{
	return(" ");
	}
}

function gl_balance($compcode,$branch,$acctcode,$account_year)
	{
	    global $conn;
	    global $user;
	    
       // $ac_year = $account_year;
        //$fattach = str_replace("-","_",$ac_year);
        //$ledgerfile = 'ledger'.$fattach;
	    
	    $ledgerfile = 'ledger';

		$tdbamt=0;
		$tcramt=0;
		 $filename =$_SESSION['userid']; 		
		$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = '$acctcode' and branch_code = '$branch' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
		$oprow = mysqli_fetch_row($opresult);
		$amt = $oprow[0];
		if($oprow[1] =='DR')
		{
			$tdbamt = $amt;
		}
		elseif($oprow[1] == 'CR')
		{
			$tcramt = $amt;
		}
		$msresult = mysqli_query($conn,"select sum(dbamt),sum(cramt) from `$ledgerfile` where compcode = '$compcode' && acctcode = '$acctcode'   and branch_code = '$branch' and account_year ='$account_year' order by date ");
		$msrow = mysqli_fetch_row($msresult);
		$tdbamt = $tdbamt + $msrow[0];
		$tcramt = $tcramt + $msrow[1];
		$msresult = mysqli_query($conn,"select sum(dbamt),sum(cramt) from `templedger` where compcode = '$compcode' && acctcode = '$acctcode'   and branch_code = '$branch'  order by date ") or die(mysqli_error());
		$msrow = mysqli_fetch_row($msresult);
		$tdbamt = $tdbamt + $msrow[0];
		$tcramt = $tcramt + $msrow[1];		
		
		$net = $tdbamt - $tcramt;
		if($net>0)
		{
			$tp = 'Dr';
		}
		elseif($net <0)
		{
			$net = $net*-1;
			$tp = 'Cr';
		}
			$cl = $net.'&nbsp;'.$tp;
		return $cl;
	}





function sendsms($mobile,$message)
{
	$flag = 0;    
    $username="RAAJAM";
    $password="irfannew2017**";
    $sender="RAAJAM";
	if(strlen($mobile) == 10)
	{
	$message=urlencode($message);
	 $parameters="username=$username&password=$password&to=$mobile&message=$message";
	echo("$parameters<br>");
			if ($flag == 0) 
			{
				//echo("http://sms.empromedia.com/pushsms.php?$parameters<br>");
				$fp = fopen("http://sms.empromedia.com/pushsms.php?$parameters", "r");
				fclose($fp);

				$date = date('Y-m-d');
				//$time = ist(); 							
				echo("SMS Sent to $name,$designation ,$office,$mobile on $date @ $time<br>");

				//mysqli_query($conn,"insert into sendmsg values('$name','$date','$time','$message','$mobile','','$designation','$office','$scode','$bcode','$group',NULL)") or die(mysqli_error());
				$flag=1;
			}
	}

}

    function indian_number_format($num){
        $num=explode('.',$num);
        $dec=(count($num)==2)?'.'.$num[1]:'.00';
        $num = (string)$num[0];
        if( strlen($num) < 4) return $num;
        $tail = substr($num,-3);
        $head = substr($num,0,-3);
        $head = preg_replace("/\B(?=(?:\d{2})+(?!\d))/",",",$head);
        return $head.",".$tail;
    }
    
    
    function ind($amount)
{
    list ($number, $decimal) = explode('.', sprintf('%.2f', floatval($amount)));

    $sign = $number < 0 ? '-' : '';

    $number = abs($number);

    for ($i = 3; $i < strlen($number); $i += 3)
    {
        $number = substr_replace($number, ',', -$i, 0);
    }

    return $sign . $number . '.' . $decimal;

}

?>


<script language="JavaScript">
function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}
</script>

<script language="JavaScript">
function closeit()
{
close();
}
</script>






