<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
$s=new db_query();
/*$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
 if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";


$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone);
$current_time=$date->format('H:i:s');
*/
if($_REQUEST['tid']!="")
{
   $tsk_res=DB_fetch_assoc(DB_query("select * from perf_task_assign where id='".$_REQUEST['tid']."' ",$db));
}

if(isset($_REQUEST['approve']))
{

 $all_app_sql=DB_query("select * from perf_approval_list",$db);
 $all_app_count=DB_num_rows($all_app_sql);
    foreach($_REQUEST['app'] as $val)
    {
     DB_query("insert into perf_taskwise_approve(task_id,app_list_id,task_det_id,approved_by) value ('".$_REQUEST['tid']."','".$_REQUEST['appid']."','".$val."','".$_SESSION['UserID']."')" ,$db );
     
     $app_count=DB_query("select * from perf_taskwise_approve where task_id='".$_REQUEST['tid']."' and task_det_id='".$val."' ",$db);
     if(DB_num_rows($app_count)==$all_app_count)
     {
         $app_count=DB_query("update perf_task_assign_details set app_status='1' where id='".$val."' ",$db);
     }     
    }
  $detcountsql=DB_query("select * from perf_task_assign_details where task_id='".$_REQUEST['tid']."' ",$db);
  $det_appcountsql=DB_query("select * from perf_task_assign_details where task_id='".$_REQUEST['tid']."' and app_status='1' ",$db);
  if(DB_num_rows($detcountsql)==DB_num_rows($det_appcountsql))
  {
      $app_count=DB_query("update perf_task_assign set approval_status='1' where id='".$_REQUEST['tid']."' ",$db);
  }
  
  $s->pageLocation("index.php?route=TaskMgt/task_assign_approval".$rurl);   
}

?>
<script src="ui/jquery.ui.timepicker.js"></script>
<link rel="stylesheet" href="css/new_mxerp.css">
<style>
#showdata {
	display: block;
	background: url(Default.png) no-repeat;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
#back {
	display: none;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
</style>
<div class="body_contain">
<div class="tab_div">
<h1>Task Assignment</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Branch</td>
    <td><select name="branch" style="width:150px;">
            <?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$tsk_res['branch']."' ";
	$SiteRes=DB_query($Sitesql,$db);
	while($SiteRow=DB_fetch_array($SiteRes))
	{?>
            <option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo ($branch==$SiteRow['loccode']?'selected':'');?> >
            <?php  echo $SiteRow['locationname'];?>
            </option>
            <?php 
	}
	?>
          </select></td>
          <td>DOC No</td>
          <td><input type="text"  name="doc_no" id="doc_no" value="<?php echo $tsk_res['doc_no'];?>" readonly style="width:150px" ></td>
	<!--<td class="tabledata1">Task No. </td>
	<td><input type="text" name="task_no" style="width:150px;" value="<?php echo $task_no;?>"></td>-->
</tr>
<tr class="row1">
          <!--<td class="tabledata1">Starts</td>
          <td><input type="text" name="start_date" style="width:100px;" class="datepicker" value="<?php echo ConvertSQLDate($start_date);?>">
            <input type="text" name="start_time" style="width:45px;" value="<?php echo $start_time; ?>" id="timepicker1"></td>
		<td class="tabledata1">Ends</td>
		<td><input type="text" name="end_date" style="width:100px;" class="datepicker" value="<?php echo ConvertSQLDate($end_date);?>">
			<input type="text" name="end_time" style="width:45px;"  id="timepicker2"></td>-->
    <td>Assign To</td>
     <td>
         <input name="assign_to" type="radio" disabled="disabled" value="user"<?php if($tsk_res['assign_to']=='user'){ ?> checked="checked" <?php }?>> 
            User 
          <input name="assign_to" type="radio" disabled="disabled" value="team" <?php if($tsk_res['assign_to']=='team'){ ?> checked="checked" <?php }?>> 
          Team  </td>
     <td>User</td>
     <td>
         <?php if($tsk_res['assign_to']=='user') { ?>
        <select name="user_id" id="user_id" <?php echo $user_disable;?> style="width:150px;">
        <?php 
        $users=DB_query("Select * from www_users where id= '".$tsk_res['user_id']."' ",$db);
        while($users_row=DB_fetch_array($users))
        {?>
             <option value="<?php echo $users_row['id']?>"><?php echo $users_row['realname'];?></option>
        <?php 	
        }
        ?>
        </select>
         <?php } ?>
          </td>
        </tr>
        <tr class="row1">
            <td>Date</td>
            <td><input type="text" readonly class="" id="task_date" name="task_date" value="<?php echo ConvertSqlDate($tsk_res['task_date']) ?>" style="width:150px"></td>
            <?php 
            $realneme=DB_fetch_assoc(DB_query("Select realname from www_users where id='".$tsk_res['assigned_by']."' ",$db));
           ?>
            <td>Assign By</td>
            <td><input type="text"  name="" readonly value="<?php echo $realneme['realname']  ?>" id="assign_to" style="width:150px" ></td> 
        </tr>
        <tr><td colspan="4"><strong>Task Details</strong></td></tr>
		<tr>
		<td colspan="4" style="padding:0px;">
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
                    <tr>
                         
                        <th width="5%" align="center">Approve Task</th>
                        
                        <th width="15%" align="center">Task Description</th>
                        <th colspan="2" align="center" style="text-align:center;" width="13%">Start Date & Time</th>
                        <th  colspan="2" align="center" style="text-align:center;" width="13%">End Date & Time</th>
                      
                        <th width="5%" align="center">Internal(%)</th>
                        <th width="12%" align="center">Semi Internal(%)</th>
                        <th align="center">External(%)</th>
                        <th align="center">Total Completion(%)</th>
                        
                    </tr>
                 
    <?php
     $tsk_query=DB_query("select * from perf_task_assign_details where task_id='".$_REQUEST['tid']."' and status='close' ",$db);
     $j=1;
        while($tsk_det=DB_fetch_assoc($tsk_query)){
             $flag=0;
       $appdata=DB_fetch_assoc(DB_query("select * from perf_approval_list where id='".$_REQUEST['appid']."' ",$db));
       $apprval_check1=DB_query("select * from perf_taskwise_approve where task_id='".$_REQUEST['tid']."' and task_det_id='".$tsk_det['id']."' and app_list_id='".$appdata['id']."' ",$db);
      if(DB_num_rows($apprval_check1)>0){$flag=1;}
       
      $prev_app=$appdata['id']-1;
      $prevappdata=DB_fetch_assoc(DB_query("select * from perf_approval_list where id='".$prev_app."' ",$db));
      if($prevappdata['id']!='')
      {
         $apprval_check=DB_query("select * from perf_taskwise_approve where task_id='".$_REQUEST['tid']."' and task_det_id='".$tsk_det['id']."' and app_list_id='".$prev_app."' ",$db); 
          if(DB_num_rows($apprval_check)==0){$flag=1;}
      }
           
 ?>
                    <tr>
                        <td><input <?php if($flag==1){ echo "disabled"; } ?> type="checkbox" value="<?php echo $tsk_det['id'];?>" name="app[]"></td>
                        <td><textarea readonly style="width:99%; height:50px;"><?php echo $tsk_det['task_description'];?></textarea></td>
                      <td align="left" valign="top" style="text-align:right"><input type="text" readonly  style="width:100%;" class="" value="<?php echo ConvertSQLDate($tsk_det['start_date']);?>" ></td>
                      <td align="left" valign="top" style="text-align:left"><input type="text" readonly  style="width:100%;" value="<?php echo $tsk_det['start_time']; ?>" class="" ></td>
                      <td align="left" valign="top" style="text-align:right"><input type="text" readonly id="end_date" name="end_date[]" style="width:100%;" class="" value="<?php echo ConvertSQLDate($tsk_det['end_date']);?>"></td>
			<td align="left" valign="top" style="text-align:left"><input type="text" readonly   style="width:100%;" value="<?php echo $tsk_det['end_time']; ?>" class=""></td>
             <td align="left" valign="top"><input type="text" disabled="disabled"   style="width:100%;"  value="<?php echo $tsk_det['int_com']; ?>" ></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" style="width:100%;"  value="<?php echo $tsk_det['semi_com']; ?>"></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" style="width:100%;"  value="<?php echo $tsk_det['ext_com']; ?>" ></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" readonly style="width:100%;" value="<?php echo $tsk_det['total_com']; ?>" ></td>
                   
                    </tr>
 <?php }  ?>
  <tr>
      <td>Remarks</td>
      <td colspan="11"><textarea readonly name="remarks" cols="30" rows="5" style="width:90%;"><?php echo  $tsk_res['remarks'] ?></textarea></td>
  </tr>


</table>
		</td>
		</tr>
</table>
<?php //}?>
	  
<p align="center"><input type="submit" name="approve" value="Approve" class="search" /> &nbsp;
    <a href="index.php?route=TaskMgt/task_assign_approval<?php echo $rurl; ?>"><input type="button" name="cancel" value="Cancel" class="search" /></a>
</p>	  
    </form>
  </div>
</div>
<script type="text/javascript" language="javascript">
    function calc_total(j)
    {
      var int=document.getElementById("internal_"+j).value;
      var semi=document.getElementById("semi_"+j).value;
      var ext=document.getElementById("ext_"+j).value;
      if(int=="")
        int=0;
       if(semi=="")
        semi=0;
       if(ext=="")
        ext=0;
        var total=parseFloat(int)+parseFloat(semi)+parseFloat(ext);
        if(total>100)
            {alert("Total can not tbe More than 100%");
                document.getElementById("internal_"+j).value="";
                document.getElementById("semi_"+j).value="";
                document.getElementById("ext_"+j).value="";
                document.getElementById("total_"+j).value="";
                document.getElementById("internal_"+j).focus();
            }
            else
            { document.getElementById("total_"+j).value=total+"%" ; }
       
    }
    function closing_task(j)
    {
       if(document.getElementById("close_tsk_"+j).checked==true)
           {
                document.getElementById("internal_"+j).disabled="";
                document.getElementById("semi_"+j).disabled="";
                document.getElementById("ext_"+j).disabled="";
                document.getElementById("total_"+j).disabled="";
                document.getElementById("reply_"+j).disabled="";  
           }
           else
               {
                document.getElementById("internal_"+j).value="";
                document.getElementById("semi_"+j).value="";
                document.getElementById("ext_"+j).value="";
                document.getElementById("total_"+j).value="";
                document.getElementById("internal_"+j).disabled="disabled";
                document.getElementById("semi_"+j).disabled="disabled";
                document.getElementById("ext_"+j).disabled="disabled";
                document.getElementById("total_"+j).disabled="disabled";
                document.getElementById("reply_"+j).disabled="disabled";
               }
               
    }
$('.timepicker').timepicker();
 
 function datetime1()
 {
     $('.timepicker').timepicker();
     
    $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
 }
 
function checkAssignTo(str)
{
	if(str=='user')
	{document.getElementById("user_id").disabled=false;}
	else{document.getElementById("user_id").disabled=true;}
}
function activateTime()
{
	var stat=document.getElementById("reminder_status").checked;
	if(stat==true){document.getElementById("reminder_time").disabled=false; document.getElementById("time_mode").disabled=false; document.getElementById("reminder").value='Y';}
	else{document.getElementById("reminder_time").disabled=true; document.getElementById("time_mode").disabled=true; document.getElementById("reminder").value='N';}
}
function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
           if(newcell.childNodes[0].className=="datepicker hasDatepicker")
            {
                var nod_id = newcell.childNodes[0].id;
                newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
                newcell.childNodes[0].className="datepicker";              
            }
            else
            {
               var nod_id = newcell.childNodes[0].id;
               newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount; 
               newcell.childNodes[0].className="timepicker";
            }
	break;
        case "textarea":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	//document.getElementById("stock_perunit_"+rowCount).value='1';
        datetime1();
}

function deleteRowOrder(tableID) 
{
	try 
	{
	var table = document.getElementById(tableID);
	//alert(table);
	var rowCount = table.rows.length;
	//alert(rowCount);
	for(var i=0; i<rowCount; i++) 
	{
	var row = table.rows[i];
	var chkbox = row.cells[0].childNodes[0];
	if(null != chkbox && true == chkbox.checked) 
	{
	if(rowCount <= 1) 
	{
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
	alert(e);
	}
}


</script>