<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Telephonic Order Memo');
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($_REQUEST['Save']!='' && $_REQUEST['rep']=="true")
{
    //echo "<pre>";
    //print_r($_REQUEST);
    //count($_REQUEST['taskid']);
  
    for($i=0;$i<count($_REQUEST['taskid']);$i++)
    {
        
          $tsk=$_REQUEST['taskid'][$i];
          $brk=implode(",",$_REQUEST['task_break'][$tsk]);
         // echo 
          if($_REQUEST['reply'][$tsk][0]!=""){
          DB_query("insert  into perf_tsk_reply (task_det_id,reply) value ('".$tsk."','".$_REQUEST['reply'][$tsk][0]."')",$db);
          DB_query("update perf_task_assign_details set status='process' where id='".$tsk."' ",$db);
          } 
          if($_REQUEST['task_break'][$tsk]!=""){
           DB_query("update perf_task_assign_details set status='process' ,tsk_partition='".$brk."' where id='".$tsk."' ",$db);
          }
    }
      $s->pageLocation("index.php?route=TaskMgt/perf_task_exec_index".$rurl);     
}
 if($_REQUEST['Save']!='' && $_REQUEST['close']=="true")
    { 
    
      for($i=0;$i<count($_REQUEST['close_tsk']);$i++)
        {
          $tsk=$_REQUEST['close_tsk'][$i];
          if($_REQUEST['reply'][$tsk][0]!=""){
          DB_query("insert  into perf_tsk_reply (task_det_id,reply) value ('".$tsk."','".$_REQUEST['reply'][$tsk][0]."')",$db);  
          }
           $tsk=$_REQUEST['close_tsk'][$i];
           DB_query("update perf_task_assign_details  set int_com='".$_REQUEST['int'][$tsk][0]."',semi_com='".$_REQUEST['semi_int'][$tsk][0]."',ext_com='".$_REQUEST['ext'][$tsk][0]."',total_com='".$_REQUEST['total'][$tsk][0]."',status='close' where id='".$_REQUEST['taskid'][$i]."' ",$db)  ;   
        
    }
     $s->pageLocation("index.php?route=TaskMgt/perf_task_exec_index".$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/Team</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 } 
         if($tsk_res['assign_to']=='team') { ?>
        <select name="" id="" <?php echo $user_disable;?> style="width:150px;">
        <?php 
        $users=DB_query("Select * from perf_team_master where id= '".$tsk_res['teamid']."' ",$db);
        while($users_row=DB_fetch_array($users))
        {?>
             <option value="<?php echo $users_row['id']?>"><?php echo $users_row['team_name'];?></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>
                         <?php if($_REQUEST['close']=="true"){  ?>
                        <th width="5%" align="center">Close Task</th>
                         <?php } ?>
                        <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 align="center" width="4%">Repeat</th>
                        <th align="center" width="5%">Reply</th>
                        <?php if($_REQUEST['close']=="true"){  ?>
                        <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>
                        <?php } if($_REQUEST['rep']=="true"){  ?>
                        <th width="5%" align="center">Internal</th>
                        <th width="5%" align="center">Semi Internal</th>
                        <th width="5%" align="center">External</th>
                        <?php } ?>
                    </tr>
                 
    <?php 
     $tsk_query=DB_query("select * from perf_task_assign_details where task_id='".$_REQUEST['tid']."' ",$db);
     $j=1;
        while($tsk_det=DB_fetch_assoc($tsk_query)){ 
            if($tsk_det['status']=='pending' || $tsk_det['status']=='process'){
            ?>
                    <tr>
                        <?php if($_REQUEST['close']=="true"){  ?>
                        <td><input type="checkbox" id="close_tsk_<?php echo $j ?>" onclick="javascript:closing_task(<?php echo $j ?>)" name="close_tsk[]" value="<?php echo $tsk_det['id'] ?>"></td>
                        <?php } ?>
                        <td><input type="hidden" name="taskid[]" value="<?php echo $tsk_det['id'] ?>" /><textarea id="task_description" readonly name="task_description[]" 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 id="start_date" name="start_date[]" 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 id="start_time" name="start_time[]" 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 id="end_time" name="end_time[]" style="width:100%;" value="<?php echo $tsk_det['end_time']; ?>" class=""></td>
                        <td align="left" valign="top"><select name="repeat_status[]" id="repeat_status" style="width:100%;">
			<?php if($tsk_det['repeat_status']=='N'){?>
                                <option>No</option>
                        <?php } else { ?>
                                <option>Yes</option>
                        <?php } ?>
			  </select></td>
                          <td align="left" valign="top"><table cellpadding="0" cellspacing="0" border="0">
                    <?php $rpl=DB_query("select * from perf_tsk_reply where task_det_id ='".$tsk_det['id']."' ",$db);
                    while($res=DB_fetch_assoc($rpl)){?>
                                  <tr><td>
                    <input type="text" readonly value="<?php echo $res['reply']; ?>" style="width:100%;"   >   
                    <?php }?> 
                          <input type="text" style="width:100%;" id="reply_<?php echo $j ?>" <?php if($_REQUEST['close']=="true"){ ?> disabled="disabled" <?php }?> name="reply[<?php echo $tsk_det['id'] ?>][]">
                      </td></tr></table></td>
                       <?php if($_REQUEST['close']=="true") { $appcnt1=explode(',',$tsk_det['tsk_partition']); $appcount=count($appcnt1)?>
                      <input type="hidden" value="<?php echo $appcount ?>" id="appcnt_<?php echo $j; ?>">
                      <input type="hidden" value="<?php echo $tsk_det['tsk_partition'] ?>" id="appval_<?php echo $j; ?>">
                      <td align="left" valign="top"><input type="text" disabled="disabled" onkeyup="javascript:calc_tot_completion(this,'int')"  style="width:100%;" id="internal_<?php echo $j; ?>" onkeypress="return isNumberKey(event)" name="int[<?php echo $tsk_det['id'] ?>][]" ></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" style="width:100%;" id="semi_<?php echo $j; ?>" onkeypress="return isNumberKey(event)" name="semi_int[<?php echo $tsk_det['id'] ?>][]" onkeyup="javascript:calc_tot_completion(this,'semi')"></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" style="width:100%;" id="ext_<?php echo $j; ?>" onkeypress="return isNumberKey(event)" name="ext[<?php echo $tsk_det['id'] ?>][]" onkeyup="javascript:calc_tot_completion(this,'ext')" ></td>
                <td align="left" valign="top"><input type="text" disabled="disabled" readonly style="width:100%;" onblur="javascript:calc_total(<?php //echo $j ?>)" id="total_<?php echo $j ?>" name="total[<?php echo $tsk_det['id'] ?>][]" ></td>
       <?php $j++; } elseif($_REQUEST['rep']=="true") { 
            if($tsk_det['tsk_partition']==""){
                $flg=1;
            } else{
                $flg=0;
            }   
            if($flg==0)
            {
             $arr=explode(',',$tsk_det['tsk_partition']);
             
            }
            ?>
        <td><input type="checkbox" <?php if($flg==0){ echo "disabled"; }?>  checked  name="task_break[<?php echo $tsk_det['id'] ?>][]" value="i"></td>
        <td><input type="checkbox" <?php if(in_array('s',$arr)){ echo "checked"; } ?>  <?php if($flg==0){ echo "disabled"; }?> name="task_break[<?php echo $tsk_det['id'] ?>][]" value="s"></td>
        <td><input type="checkbox" <?php if(in_array('e',$arr)){ echo "checked"; } ?> name="task_break[<?php echo $tsk_det['id'] ?>][]" <?php if($flg==0){ echo "disabled"; }?> value="e"></td>
       <?php } ?>
                    </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"><?php if($_REQUEST['rep']=='true'){?><input type="submit" name="Save" value="Reply" class="search" /> <?php } elseif($_REQUEST['close']=='true' ){?><input type="submit" name="Save" value="Close" class="search" /><?php }?>
</p>	  
    </form>
  </div>
</div>
<script type="text/javascript" language="javascript">
    function calc_tot_completion(str,type)
    {
        var tid=str.id;
//        alert(val);
        var data=tid.split("_");
        var cntr=data[1];
            if(type=='int')
            {
                var int=str.value;
                if(document.getElementById('semi_'+cntr).value=='')
                   var semi=0;
                else
                   var semi=document.getElementById('semi_'+cntr).value;

                if(document.getElementById('ext_'+cntr).value=='')
                    var ext=0;
                else
                    var ext=parseFloat(document.getElementById('ext_'+cntr).value);
            }
            else if(type=='semi')
            {
                    var semi=str.value;
                 if(document.getElementById('internal_'+cntr).value=='')
                    var int=0;
                 else
                    var int=document.getElementById('internal_'+cntr).value;

                 if(document.getElementById('ext_'+cntr).value=='')
                     var ext=0;
                 else
                     var ext=document.getElementById('ext_'+cntr).value;   
            }   
            else if(type=='ext')
            {
                var ext=str.value;
                 if(document.getElementById('semi_'+cntr).value=='')
                    var semi=0;
                 else
                    var semi=document.getElementById('semi_'+cntr).value;

                 if(document.getElementById('internal_'+cntr).value=='')
                     var int=0;
                 else
                     var int=document.getElementById('internal_'+cntr).value;  
            }
           
        var partition=parseInt(document.getElementById('appcnt_'+cntr).value);
        var part=parseFloat(100/partition).toFixed(2);
        var int1=parseFloat((int*part)/100).toFixed(2);
        var semi1=parseFloat((semi*part)/100).toFixed(2);
        var ext1=parseFloat((ext*part)/100).toFixed(2);
        
        var tot=0;
             tot=parseFloat(int1)+parseFloat(ext1)+parseFloat(semi1); 
        document.getElementById('total_'+cntr).value=tot.toFixed(2);
    }
    
    
    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)
           {
              var data=document.getElementById("appval_"+j).value.split(",");
               
                if(data.indexOf("i")!=-1)
                    document.getElementById("internal_"+j).disabled="";
                if(data.indexOf("s")!=-1)
                    document.getElementById("semi_"+j).disabled="";
                if(data.indexOf("e")!=-1)
                    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>

<SCRIPT language=Javascript>
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </SCRIPT>