<?php
//*######################MOHAMMAD###################################
$works_state = array(1 => "فعال", 2 => "غیر فعال", 3 => "آرشیو شده");
$dayofweek_name = array(1 => "شنبه", 2 => "يكشنبه", 3 => "دوشنبه", 4 =>
    "سه شنبه", 5 => "چهارشنبه", 6 => "پنجشنبه", 7 => "جمعه");
$work_type = array(1 => "کار عادی", 2 => "اضافه کار", 3 => "غیبت");

function get_user_group2(){
    global $db;
    $sql = $db->query("SELECT userGroup from  u_admin_users  WHERE  ID ='".$_SESSION['adminID']."'");
    $res = $db->fetchNextArray($sql);
    return $res['userGroup'];
}
function name_user_m($ID){
    global $db;
    $query = "select * from u_users where ID='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['name_lastname'];
}

function mobile_user_m($ID){
    global $db;
    $query = "select * from u_users where ID='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['mobile'];
}
function name_ad_user_m($ID){
    global $db;
    $query = "select * from u_admin_users  where ID ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['first_lastName'];
}
//------------------------------------------------
function getCatUseName($ID){
    global $db;
    $query = "select * from typeusecat  where catID ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['nameCat'];
}
//-------------------------------------------------
function admin_group_name($ID){
    global $db;
    $query = "select * from u_admin_groups  where groupsID ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['groupName'];
}
//----------------------------------------------------
function get_Position_menu_user($ID){
    global $db;
    switch ($ID){
		case 1 :
		 return 'control Panel';
		break;
		case 2 :
			return 'header';
		break;	
		case 3 :
		return 'footer';
		break;
	}
}
//----------------------------------------------------
function get_available_menu_user($ID){
    global $db;
    switch ($ID){
		case 1 :
		 return 'همه وضعیتها';
		break;
		case 2 :
			return 'مشاهده فقط بعد از مجوز';
		break;	
		case 3 :
		return 'footer';
		break;
	}
}
//----------------------------------------------------
function user_group_name($ID){
    global $db;
    $query = "select * from u_groups  where groupsID ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
	if($row['groupName']){
    	return $row['groupName'];
	}else{
		return 'دسترسی عموم';
	}
}
function get_parent_menu_admin($ID){
    global $db;
    $query = "select * from m_u_admin_menu  where id ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    if (count($row) != 0)
        return $row['title'];
    else
        return 'منو اصلی';
}
function get_parent_menu_user($ID){
    global $db;
    $query = "select * from m_user_menu  where id ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    if (count($row) != 0)
        return $row['title'];
    else
        return 'منو اصلی';
}


function make_menu($adminid, $parentid){
    if ($parentid == 0){
        echo '<ul id="adminmenu" class="dropdown dropdown-vertical dropdown-vertical-rtl"   style="background-color: white;">';
    } else
        echo '<ul>';
    $result = mysql_query("SELECT * FROM m_u_admin_menu where admingroup = '" . $adminid ."'and parent = '" . $parentid . "'  order BY id ASC");
    while ($r = mysql_fetch_array($result)){
        $cn = $r["title"];
        $ci = $r["id"];
        $link = $r["link"];
        echo '<li >' .
            "<a href='$link'>$cn</a>";
        make_menu($adminid, $ci);
        echo '</li>';
    }
    echo '</ul>';
	
}
function convertdatetimeTofarsi($datetime){
    $explode_p_e = explode(' ', $datetime);
    $date_p_e = explode('-', $explode_p_e[0]);
    $h_p_e = explode(':', $explode_p_e[1]);
    $e = gregorian_to_jalali($date_p_e[0], $date_p_e[1], $date_p_e[2]);
    $edate = $e[0] . '-' . $e[1] . '-' . $e[2] . '<br>' . $h_p_e[0] . '-' . $h_p_e[1];
    return $edate;
}
function specialtyname_ad_user_m($ID){
    global $db;
    $query = "select * from m_specialtys  where id ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['name'];
}
function projectname($ID){
    global $db;
    $query = "select * from  m_projects where p_id ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['p_name'];
}
function projectstepname($ID){
    global $db;
    $query = "select * from  m_project_step where id ='" . (int)$ID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['name'];
}
function getgroupid($aID){
    global $db;
    $query = "select * from  u_admin_users where ID ='" . (int)$aID . "'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row['userGroup'];
}
function getsetting($dbField){
    global $db;
    $query = "select * from m_setting  where id ='1'";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    return $row[$dbField];

}
function getsuffix($filename){

    return end(explode('.', $filename));
}

function changecheckbox($s){
    if ($s == 1)
    {
        return 'فعال';
    } else
    {
        return 'غیر فعال';
    }
}
function getdomainstate($id){
    switch ($id)
    {
        case '1':
            return "ثبت شده";
        case '2':
            return "در انتظار ثبت";
        case '3':
            return "منقظی شده";
        case '4':
            return "حذف شده";
    }
}
function printCombobox($selectname, $selectid, $database, $value_filed, $text_filed,$selected_value = "", $condition = ""){
    global $db;
    $result = $db->query("select * from $database $condition");
    $num = mysql_num_rows($result);
    print '<select name="'.$selectname.'" dir="rtl" id="'.$selectid.'" style="width:137px">';
	print '<option value="0"  >دسترسی عموم</option>';
    for ($i = 1; $i <= $num; $i++){
        $selected = $selected_value;
        $rowx = mysql_fetch_array($result);
        if ($rowx[$value_filed] == $selected_value){
            $selected = 'selected="selected"';
        }else{
			$selected='';
		}
        print '<option value="' . $rowx[$value_filed] . '" '.$selected.' >' . $rowx[$text_filed] .'</option>';
    }
    print '</select>';
}
function getadminsmenu($selectname, $selectid, $database, $value_filed, $text_filed,$defultText = "انتخاب کنید", $selected_value = "", $condition = ""){
    global $db;
    $result = $db->query("select * from m_u_admin_menu $condition order by admingroup ,id");
    $num = mysql_num_rows($result);

    print '<select name="' . $selectname . '" dir="rtl" id="' . $selectid .
        '" style="width:100%">';
  
        print "<option value= 0  >$defultText</option>";
    
    for ($i = 1; $i <= $num; $i++)
    {
        $selected = $selected_value;
        $rowx = mysql_fetch_array($result);
        if ($rowx[$value_filed] == $selected_value)
        {
            $selected = 'selected="selected"';
        }
		print '<option value="' . $rowx[$value_filed] . '" ' . $selected . ' >' .
            		admin_group_name($rowx['admingroup']) .  getfullname_adminmenu($rowx[$value_filed]) .
				'</option>';
    }
    print '</select>';
}
function getfullname_adminmenu($id){
    global $db;
    $result = $db->query("select * from  m_u_admin_menu where id ='" . $id . "'");
    $num = mysql_num_rows($result);
    $fname = '';
    for ($i = 1; $i <= $num; $i++)
    {
        $selected = $selected_value;
        $rowx = mysql_fetch_array($result);
        $fname = getfullname_adminmenu($rowx['parent']) . '/' . $rowx['title'];
    }
    return $fname;
}
function getusermenu($selectname, $selectid, $database, $value_filed, $text_filed,$defultText = "انتخاب کنید", $selected_value = "", $condition = ""){
    global $db;
    $result = $db->query("select * from  m_user_menu $condition order by usergroup ,id");
    $num = mysql_num_rows($result);

    print '<select name="' . $selectname . '" dir="rtl" id="' . $selectid .'" style="width:100%">';
  
    print '<option value="0" >'.$defultText.'</option>';
    
    for ($i = 1; $i <= $num; $i++){
        $selected = $selected_value;
        $rowx = mysql_fetch_array($result);
        if ($rowx[$value_filed] == $selected_value)
        {
            $selected = 'selected="selected"';
        }
        print '<option value="' . $rowx[$value_filed] . '" ' . $selected . ' >' .
            	user_group_name($rowx['usergroup']) . getfullname_usermenu($rowx[$value_filed]) .
			  '</option>';
    }
    print '</select>';
}

function getfullname_usermenu($id){
    global $db;
    $result = $db->query("select * from  m_user_menu where id ='" . $id . "'");
    $num = mysql_num_rows($result);
    $fname = '';
    for ($i = 1; $i <= $num; $i++){
        $selected = $selected_value;
        $rowx = mysql_fetch_array($result);
        $fname = getfullname_usermenu($rowx['parent']) . '/' . $rowx['title'];
    }
    return $fname;
}
function printDropdownWithArray($selectname, $dataAarry, $selected_value = "", $startarr =
    0, $count = 0)
{
    print '<select name="' . $selectname . '" dir="rtl" id="' . $selectname .
        '" style="width:137px">';
    if ($count == 0)
        $count = $startarr + count($dataAarry);
    else
        $count = $count + $startarr;
    for ($i = $startarr; $i < $count; $i++)
    {
        $selected = $selected_value;
        if ($i == $selected_value)
        {
            $selected = 'selected="selected"';
        }
        print '<option value="' . $i . '" ' . $selected . ' >' . $dataAarry[$i] .
            '</option>';
    }
    print '</select>';
}
function SelectfromArray($selectkey, $dataAarry){
    return $dataAarry[$selectkey];
}
function validip($ip){
    $query = "SELECT * FROM `m_valide_ip` WHERE ip = '" . $ip . "'";
    $result = mysql_query($query);
    $numrow = mysql_num_rows($result);
    if ($numrow == 1)
        return true;
    else
    {
        echo " شما نامعتبر است IP";
        header("location: error00.php");
    }
}
function getstarttimework($dayid){
    $query = "SELECT * FROM `m_working_hours` WHERE id = '" . $dayid . "'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    return $row['w_from'];
}
function getendtimework($dayid){
    $query = "SELECT * FROM `m_working_hours` WHERE id = '" . $dayid . "'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    return $row['w_to'];
}
function gettypework($sday, $eday, $nowtime, $eid, $ndate){
    $s = (int)str_replace(':', '', $sday);
    $e = (int)str_replace(':', '', $eday);
    $n = (int)str_replace(':', '', $nowtime);

    //$work_type = array(1 => "کار عادی", 2 => "اضافه کار", 3 => "غیبت" );

    if ($s == $e) //روز  اتعطیل =  اضافه کاری

    {
        return 2;
    }
    if ($n < $s) //ثبت ساعت قبل از ساعت کاری = اضافه کاری

    {
        return 2;
    }
    if ($n > $e) //ثبت ساعت  بعد از ساعت کاری = اضافه کاری

    {
        return 2;
    }
    if ($e > $n && $n >= $s) //کار عادی

    {
        $query = "SELECT * FROM m_project_times WHERE emploee_id = '" . $eid .
            "' AND date ='" . $ndate . "'AND start >='" . $sday . "'";
        $result = mysql_query($query);
        $numrow = mysql_num_rows($result); //تعداد کارهای انجام شده در امروز
        if ($numrow == 0)
        {
            //ثبت غیبت در صورتی که کاری انجام نداده باشد
            //$sday تا $nowtime
            absence($eid, $ndate, $sday, $nowtime);
        } else
        {
            //اگر کارمند بیشتر از 5 دقیق بعد از بستن کار کاری باز کند برای او تاخیری ثبت می شود
            $lastlogout = lastEndWork($eid, $ndate, $sday);
            if (defrencetime_minut($lastlogout, $nowtime) > 5)
                absence($eid, $ndate, $lastlogout, $nowtime);
        }
        return 1;
    }
}
function lastEndWork($EID, $DATE, $startdaytime){
    global $db;
    $query = "select MAX(end_project) as maxtime from m_project_times  WHERE date ='" .
        date("Y-m-d") . "' AND start >= '" . $startdaytime . "'  AND emploee_id ='" . $EID .
        "' AND finished = 1";
    echo $query . "<br>";
    $result = $db->query($query);
    $row = $db->fetchNextArray($result);
    if (count($row) >= 1)
        return $row['maxtime'];
    else
        return $startdaytime;
}
//ثبت تاخیری
function absence($EID, $DATE, $STARTTIME, $FINISHTIME) {
    $defrencetime = defrencetime_minut($STARTTIME, $FINISHTIME, $DATE);
    $query = "insert into m_project_times set 
									 project_id ='" . -1 . "',
									 emploee_id ='" . $EID . "',
									 date ='" . $DATE ."',                                                    
                                     start = '" . $STARTTIME . "', 
                                     end_project ='" . $FINISHTIME . "', 
                                     finished = 1,
                                     step_id ='" . -1 . "',
                                     time =$defrencetime,
                                     comment ='غیبت',												
                                     jtype = 3 ,
				                     state_job_id ='" . getsetting('statejob_notconfirmed') .
        "'";
    $result = mysql_query($query);
}
//اختلاف بین دوزمان را بر می گرداند
function defrencetime_minut($from, $to, $DATE){
    $from_time = strtotime($DATE . " " . $from);
    $to_time = strtotime($DATE . " " . $to);
    $defrencetime = round(($to_time - $from_time) / 60, 0);
    return $defrencetime;
}
/*$Dates = date("Y-m-d", time());
    $stime = date('H:i:s');
    $wType = gettypework(getstarttimework(jalali_week_day_1to7(date("Y-m-d"))),
    getendtimework(jalali_week_day_1to7(date("Y-m-d"))), $stime, $_SESSION['adminID'],
    $Dates);*/
	//شروع کار
function startwork($PID, $EID, $DATE, $STARTTIME, $SID, $WTYPE) {
    $res = mysql_query("insert into m_project_times set 
											project_id ='" . $PID . "',
											emploee_id ='" . $EID . "',
											date ='" . $DATE . "',
                                            start ='" . $STARTTIME . "',
											step_id ='" . $SID . "',
                                            jtype ='" . $WTYPE . "',
											state_job_id ='" . getsetting('statejob_notconfirmed') .
        "',finished ='" . 0 . "'");
}
//اتمام کار
function finishwork($JID, $DATE, $STARTTIME, $FINISHTIME, $COMMENT){
    $defrencetime = defrencetime_minut($STARTTIME, $FINISHTIME, $DATE);
    $res = mysql_query("update  m_project_times set  
													end_project ='" . $FINISHTIME . "',
													time =$defrencetime,	
                                                    comment ='" . $COMMENT .
        "',	
                                                    finished = 1
                                                    where id = " . $JID);


}

function explodeTime($time, $part = "H"){
    $t = explode(':', $time);
    switch ($part)
    {
        case 'H':
            return $t[0];

        case 'i':
            return $t[1];

        case 's':
            return $t[2];

        case 'H:i':
            return $t[0] . ':' . $t[2];

    }
}
//================================================================
function get_Group_file($file){
	$db=con();
	$group='';
	$sql=$db->query("select * from u_admin_file_to_groups where fileName = '".$file."'");
	$num=$db->numRows($sql);
	while($res=$db->fetchNextArray($sql)){
		if(admin_group_name($res['groupID'])){
			$group .= admin_group_name($res['groupID']).'<br />';
		}
	}
	return $group;	
}
//================================================================
function get_fileName($id){
	$db=con();
	$group='';
	$sql=$db->query("select * from u_admin_files where id = '".$id."'");
	$res=$db->fetchNextArray($sql);
	return $res['fileName'];	
}
//================================================================
function get_title_pageIndex($id){
	$db=con();
	$group='';
	$sql=$db->query("select * from page_index_group where p_id = '".$id."'");
	$res=$db->fetchNextArray($sql);
	return $res['title'];	
}
?>