<?php	
	ini_set('max_execution_time', 5000);
	mysqli_query($connect,"SET SESSION group_concat_max_len = 1000000");		
	
	$currentFile1 = $_SERVER["PHP_SELF"];
	$parts1 = Explode('/', $currentFile1);
	$result1= $parts1[count($parts1) - 1];
	$parts2 = Explode('?', $result1);
	$result2= $parts2[0];
	$ip_flagg = 1; 
	
	//$st_ip = $_SERVER['REMOTE_ADDR'];
	$st_ip = '';//$_SERVER["HTTP_X_REAL_IP"];
	
	$IPStart = explode(".",$st_ip); 
	/*$IPFinish = explode(".","122.165.225.195");
	for($i=0;$i<4;$i++) 
	{ 				 
		if($IPStart[$i]==$IPFinish[$i]) 
		{ 					
			$ip_flagg = 1; 
		} 
		else
		{
			$ip_flagg = 0; 
			break;
		}
	}*/
	/*if($ip_flagg==0)
	{
		if(isset($_SESSION['admin_designation']) && $_SESSION['admin_designation']!='')
		{	
			if($_SESSION['admin_designation']!='guest_user' && $_SESSION['admin_designation']!='client')
			{
				$explod = explode(".",$result2);
				if($explod[0]!="home" && $explod[0]!="index" && $explod[0]!="login-AJAX")
				{
					echo "<script>window.location='http://thahaayaa.com/liWeImplHaaTha/home.php';</script>";
				}
			}
		}
		else
		{
			echo "<script>window.location='http://thahaayaa.com/liWeImplHaaTha/home.php';</script>";
		}
	}*/
	if(isset($_SESSION['admin_designation']))
	{
		if($_SESSION['admin_designation']=='super_admin')
		{
			$admin_flagg = 1;
		}
		else if($_SESSION['admin_designation']=='admin')
		{
			$admin_flagg = 2;
		}
	}
	else
	{
		$admin_flagg = 1;
	}
	
	function openurl($url)
	{
		$postvars ='';
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_TIMEOUT, '3'); 
		$content = trim(curl_exec($ch)); 
		curl_close($ch); 
		return $content;
		
	}
	
	
	
	$senderid	=	"THAYAA";								//use your sms api sender id	
	$workingkey	=   "A17f7fe88b1817a1aa97b571d72555b17";	//use your sms api username
	$sms_domain = 	"http://trans.kapsystem.com/api/web2sms.php?workingkey=%s&to=%s&sender=%s&message=%s&response=Y";
	
	############## get current indian timing depends upon the server ############

	$SLCT_cur_dt_time = "SELECT DATE_ADD( now( ) , INTERVAL 5.30 HOUR_MINUTE ) AS CUR_TIME";
	$QRY_cur_dt_time  = mysqli_query($connect,$SLCT_cur_dt_time);
	$FTCH_cur_dt_time = mysqli_fetch_object($QRY_cur_dt_time);
	$cur_time         = $FTCH_cur_dt_time->CUR_TIME;
	$cur_date		  = explode(" ",$cur_time);
	$cur_date		  = $cur_date[0];

    date_default_timezone_set("Asia/Kolkata");
    $cur_time 	= date("Y-m-d H:i:s");
	$cur_date 	= date("Y-m-d");

	##############################################--- SCREEN TITLES

	$ADMINPANEL_TITLE 	= ":: PROJECT NAME ^ Administrator Panel ::";

	$USERPANEL_TITLE	= ":: THAHAAYAA ::";

	$PROJECT_TITLE		= "Internal Software";

	$currentFile1 = $_SERVER["PHP_SELF"];
	$parts1 = Explode('/', $currentFile1);
	$result1= $parts1[count($parts1) - 1];
	$parts2 = Explode('?', $result1);
	$result2= $parts2[0];
	//echo $result2;

  $SLCT_ms="SELECT * FROM `menu_det` WHERE `screen_name`='$result2'";
	//echo $SLCT_ms;
	$QURY_ms=mysqli_query($connect,$SLCT_ms);
	if(mysqli_num_rows($QURY_ms)>0)
	{
		$FTCH_ms=mysqli_fetch_object($QURY_ms);
		$mani_menu_id	= $FTCH_ms->menu_id;
		$menu_id	= $FTCH_ms->screen_id;
	}
	else
	{
		$mani_menu_id = 0;
		$menu_id = 0;
	}

	function convert_number_to_words($number) {

    $hyphen      = '';
    $conjunction = ' and ';
    $separator   = ' ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'zero',
        1                   => 'one',
        2                   => 'two',
        3                   => 'three',
        4                   => 'four',
        5                   => 'five',
        6                   => 'six',
        7                   => 'seven',
        8                   => 'eight',
        9                   => 'nine',
        10                  => 'ten',
        11                  => 'eleven',
        12                  => 'twelve',
        13                  => 'thirteen',
        14                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen',
        20                  => 'twenty',
        30                  => 'thirty',
        40                  => 'fourty',
        50                  => 'fifty',
        60                  => 'sixty',
        70                  => 'seventy',
        80                  => 'eighty',
        90                  => 'ninety',
        100                 => 'hundred',
        1000                => 'thousand',
        1000000             => 'million',
        1000000000          => 'billion',
        1000000000000       => 'trillion',
        1000000000000000    => 'quadrillion',
        1000000000000000000 => 'quintillion'
    );
    
    if (!is_numeric($number)) {
        return false;
    }
    
    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }

    $string = $fraction = null;

    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen.' '.$dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }

    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }

    return $string;
}

// THIS IS FOR ADMIN PANEL CONDITION
if(isset($_SESSION['admin_login_id']))
{
	$results= $parts1[count($parts1) - 2];
	if($results != 'client'  && $results != 'bl' && $results != 'ajax')
	{
		if($_SESSION['admin_login_id'] != '')
		{
			$SLCT_ad = "SELECT * FROM admin WHERE admin_id = '$_SESSION[admin_login_id]'";
			$QURY_ad = mysqli_query($connect,$SLCT_ad);
			$FTCH_ad = mysqli_fetch_object($QURY_ad);
			
			if($FTCH_ad->designation == 'client' || $FTCH_ad->designation == 'guest_user')
			{
				unset($_SESSION['admin_login_id']);
				unset($_SESSION['admin_username']);
				unset($_SESSION['admin_designation']);			
				session_destroy();			
				echo "<script>window.location='index.php';</script>";
			}
				
		}		
		
	}	
}

// THIS IS FOR CLIENT PANEL CODITIONS

if(isset($_SESSION['admin_login_id']))
{
	$results= $parts1[count($parts1) - 2];
	if($results == 'client')
	{
		if($_SESSION['admin_login_id'] != '')
		{
			$SLCT_ad = "SELECT * FROM admin WHERE admin_id = '$_SESSION[admin_login_id]'";
			$QURY_ad = mysqli_query($connect,$SLCT_ad);
			$FTCH_ad = mysqli_fetch_object($QURY_ad);
			
			if($FTCH_ad->designation != 'client' && $FTCH_ad->designation != 'guest_user')
			{
				unset($_SESSION['admin_login_id']);
				unset($_SESSION['admin_username']);
				unset($_SESSION['admin_designation']);			
				session_destroy();			
				echo "<script>window.location='../index.php';</script>";
			}
		}

	}
}
// THIS FOR CC MAIL ID
function total_acc_call($pa_id)
{
	global $connect;
	$SLCT = "SELECT * FROM acc_rep_temp WHERE party_det_id = '$pa_id' ORDER BY STR_TO_DATE(entry_date,'%d-%m-%Y') ASC";
	$QURY = mysqli_query($connect,$SLCT);
	$open_amt = 0;
	$tot_bil_amt = 0;
	$tot_rec_amt = 0;
	$tot_dis_amt = 0;
	$bill_amt_vr=0;
	while($FTCH = mysqli_fetch_object($QURY))
	{
		if($FTCH->data_type=="billing")
		{
			$SLCT_tpb = "SELECT * FROM billing_det WHERE billing_det_id = '$FTCH->billing_det_id'";
			$QURY_tpb = mysqli_query($connect,$SLCT_tpb);
			$rows_tpb = mysqli_num_rows($QURY_tpb);
			if($rows_tpb>0){
			$FTCH_tpb = mysqli_fetch_object($QURY_tpb);
			$bill_amt_vr	= $FTCH_tpb->bill_amt;
			$received_amt  = '';
			$dis_amt  = '';
			$tot_bil_amt = $tot_bil_amt + $bill_amt_vr;
			}
		}
		else
		{
			$bill_amt	= '';
			
			$SLCT_t = "SELECT * FROM ac_received_det WHERE ac_received_det_id = '$FTCH->ac_received_det_id'";
			$QURY_t = mysqli_query($connect,$SLCT_t);
			$rows_t = mysqli_num_rows($QURY_t);
			if($rows_t>0){
			$FTCH_t = mysqli_fetch_object($QURY_t);
			
			$re_am = ($FTCH_t->received_amt!=0)?$FTCH_t->received_amt:$FTCH_t->cred_amt;
			
			$tot_rec_amt = $tot_rec_amt + $re_am;
			$tot_dis_amt = $tot_dis_amt + $FTCH_t->dis_amt;
			
			$received_amt  = number_format($re_am,2);
			$dis_amt  = number_format($FTCH_t->dis_amt,2);
			}
		}
	}
	$tot_re_amt = $tot_rec_amt + $tot_dis_amt; 
	$pen = $tot_bil_amt - $tot_re_amt ; 
	if($pen<0)
	{
		$tot_adv_amt = trim($pen,"-");
		$tot_pen_amt = 0;
	}
	else if($pen>=0)
	{
		$tot_pen_amt = $pen;
		$tot_adv_amt = 0;
	}
	$amountArray	=array($tot_bil_amt,$tot_re_amt,$tot_adv_amt,$tot_pen_amt,$tot_dis_amt);
	return $amountArray;
}
$cc_id = 'tcdadmi@gmail.com';
//$new_po_dt = '01-04-'.date("Y");
//$new_po_dt = '01-'.date("m").'-'.date("Y");
//$new_po_dt = '01-06-2020';
//$new_po_dt = date("Y").'-04-01 00:00:00';
$new_po_dt = '2024-04-01 00:00:00';
$Accmnth = date("m",strtotime($cur_date));
$Accyer = date("y",strtotime($cur_date));
if($Accmnth>3)
{										
	$Acc_acd_yer = $Accyer."-".($Accyer+1);
}
else
{										
	$Acc_acd_yer = ($Accyer-1)."-".$Accyer;
}
function time_addition($time1, $time2)
{
	$times = array($time1, $time2);
	$seconds = 0;
	foreach ($times as $time)
	{
		list($hour,$minute,$second) = explode(':', $time);
		$seconds += $hour*3600;
		$seconds += $minute*60;
		$seconds += $second;
	}
	$hours = floor($seconds/3600);
	$seconds -= $hours*3600;
	$minutes  = floor($seconds/60);
	$seconds -= $minutes*60;
	if($seconds < 9)
	{
		$seconds = "0".$seconds;
	}
	if($minutes < 9)
	{
		$minutes = "0".$minutes;
	}
	if($hours < 9)
	{
		$hours = "0".$hours;
	}
	return "{$hours}:{$minutes}:{$seconds}";
}
$adm_login_id='';
$dept_id_new='';
if($_SESSION){
 $adm_login_id = $_SESSION['admin_login_id'];

// Get emp id 
$login_sql = mysqli_query($connect,"SELECT * FROM `login_details` WHERE `admin_id`='".$adm_login_id."'");
$login_rows= mysqli_num_rows($login_sql);
$emp_det_id='';
if($login_rows>0){
	$login_det = mysqli_fetch_array($login_sql,MYSQLI_ASSOC);
	$emp_det_id = $login_det['emp_det_id'];
}
if($emp_det_id!=''){
$emp_det = mysqli_fetch_array(mysqli_query($connect,"SELECT * FROM `emp_det` WHERE `emp_det_id`='".$emp_det_id."'"),MYSQLI_ASSOC);

$dept_id_new = $emp_det['master_dept_id'];
}
}

function check_rights($page_id,$option_type1){
	global $connect,$dept_id_new,$admin_flagg;
	if($admin_flagg!='1'){
	$chk_sql = mysqli_query($connect,"SELECT * FROM `menu_rights` where dept_id='".$dept_id_new."' and page_id='".$page_id."'");
	$chk_rgt = mysqli_fetch_array($chk_sql,MYSQLI_ASSOC);
	if(mysqli_num_rows($chk_sql)>0){
		$add_option=0;
		if($option_type1=='add'){
			$add_option=$chk_rgt['add_rights'];
		}
		if($option_type1=='update'){
			$add_option=$chk_rgt['edit_rights'];
		}

		if($option_type1=='view'){
			$add_option=$chk_rgt['view_rights'];
		}

		if($option_type1=='status'){
			$add_option=$chk_rgt['status_rights'];
		}
	}
	} else {
		$add_option=1;
	}
	return $add_option;
}

define("ACCESS_MSG","You are not authorized to access this page. Please contact Administrator.");
function check_access($pge){
	global $connect,$dept_id_new,$admin_flagg;
	if($admin_flagg=='1'){
		$acc_rghts ='1';
	}else {
		$sel_m = mysqli_fetch_array(mysqli_query($connect,"select view_rights from menu_rights where dept_id='".$dept_id_new."' and page_id='".$pge."'"),MYSQLI_ASSOC);
		$acc_rghts = @$sel_m['view_rights'];
	}
	return $acc_rghts;
}
?>