<?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['task_assign_save']!='')
{
//echo "<pre>";
//print_r($_REQUEST);

    $pre=$s->getField('prefix_tbl','prefix_name','Task Assignment','prefix_code');
  //print_r($_SESSION);
  $strt_yr= substr($_SESSION['financialYrStart'],2,2);
  $end_yr=substr($_SESSION['financialYrEnd'],2,2);
  $str= substr($_REQUEST['branch'],0,3);
   $yr=$strt_yr."-".$end_yr;
   $str=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr;
 $res=DB_fetch_array(DB_query("select id from tsk_taskassignment ORDER BY  id desc limit 1",$db));
 $finalstr='';
  if($res['id']=="")
      $finalstr=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr.'/1';
      else
      {$cntr= $res['id']+1;   $finalstr=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr.'/'.$cntr;} 
       $finalstr;
  
        $data['branch']=$_REQUEST['branch'];
	$data['task_no']=$finalstr;
	$data['start_date']=FormatDateForSQL($_REQUEST['start_date']);
	$data['start_time']=$_REQUEST['start_time'];
	$data['end_date']=FormatDateForSQL($_REQUEST['end_date']);
	$data['end_time']=$_REQUEST['end_time'];
	$data['repeat_status']=$_REQUEST['repeat_status'];
	$data['reminder_status']=$_REQUEST['reminder_status'];
	$data['reminder_time']=$_REQUEST['reminder_time'];
	$data['time_mode']=$_REQUEST['time_mode'];
	$data['assign_to']=$_REQUEST['assign_to'];
        $data['milestone']=$_REQUEST['milestone'];
	$data['user_id']=$_REQUEST['user_id'];
	$data['task_description']=$_REQUEST['task_description'];
	$data['assigned_by']=$_SESSION['UserID'];
	$data['created']=date('Y/m/d');
	$data['created_by']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
        
        $strt=FormatDateForSQL($_REQUEST['start_date'])." ".$_REQUEST['start_time'];
        $end=FormatDateForSQL($_REQUEST['end_date'])." ".$_REQUEST['end_time'] ;
	$sql1=$s->insertRecord('tsk_taskassignment', $data);
       
	DB_query("insert into evenement (title,start,end,url) values('task-".$_REQUEST['task_description']."','".$strt."','".$end."','".$_REQUEST['task_description']."')",$db );
        $result1=DB_query($sql1,$db);
        
         $last=  DB_Last_Insert_ID($db,'tsk_taskassignment','id');
        foreach($_REQUEST['subtask_description'] as $key=>$value)
        {
            $start=ConvertSqlDate($_REQUEST['substart_date'][$key]).' '.$_REQUEST['substart_time'][$key] ;
             $end=ConvertSqlDate($_REQUEST['subend_date'][$key]).' '.$_REQUEST['subend_time'][$key] ;
         DB_query("insert into tsk_taskassignment_details(task_id,task_desc,start_time,end_time) value ($last,'".$value."','".$start."','".$end."')",$db);
        }
      
	$s->pageLocation("index.php?Application=emp&modid=".$_REQUEST['modid']);
}
?>
<link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.css"/>
<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" onsubmit="javascript:return task_validate();">
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Branch</td>
    <td><select name="branch" id="branch" style="width:150px;">
            <option value="">--Select--</option>
            <?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."'";
	$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 class="tabledata1">Task No. </td>
	<td><input type="text" name="task_no" style="width:150px;" value="Auto Generate" readonly="true"></td>
</tr>
<tr class="row1">
          <td class="tabledata1">Starts</td>
          <td><input type="text" name="start_date" readonly style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>">
            <input type="text" name="start_time" readonly  style="width:45px;" class="datetimepicker1" value="" id="timepicker1"></td>
		<td class="tabledata1">Ends</td>
		<td><input type="text" readonly name="end_date" style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>">
			<input type="text" name="end_time" readonly style="width:45px;" class="datetimepicker1"  id="timepicker2"></td>
        </tr>
<tr class="row1">
	<td class="tabledata1">Repeat</td>
	<td><select name="repeat_status" style="width:150px;">
			<option value="N" <?php if($repeat_status=='N') echo "selected";?>>No</option>
			<option value="Y" <?php if($repeat_status=='Y') echo "selected";?>>Yes</option>
			  </select>			</td>
			  <td class="tabledata1">Reminder</td>
          <td>
		 
		  <input type="checkbox" name="" id="reminder_status" value="" onClick="activateTime()" <?php echo $ch;?>>
		  <input type="hidden" name="reminder_status" value="" id="reminder" />
          <input type="text" name="reminder_time" id="reminder_time" style="width:45px;" disabled="disabled" value="<?php echo $reminder_time;?>">
          <select name="time_mode" style="width:100px;" disabled="disabled" id="time_mode">
		  <option value="H" >Hours</option>
		  <option value="M" >Minutes</option>
          </select>          </td>
</tr>
<tr class="row1">
	<td class="tabledata1" colspan="2">Assign To </td>
		  
          <td style="width:250px;" colspan="2"><input name="assign_to" checked type="radio" value="user" onClick=""> User </tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">User</td>
          
          <td colspan="2">
		  <select name="user_id" id="user_id" style="width:150px;">
                      <option value="">Select</option>
		  <?php 
                 
		  $users=DB_query("Select * from www_users where companyid='".$_SESSION['ACGCompanyId']."' and vendorid='0' AND dealer IS NULL ",$db);
		  while($users_row=DB_fetch_array($users))
		  {?>
		  	<option value="<?php echo $users_row['id']?>"><?php echo $users_row['realname'];?></option>
		  <?php 	
		  }
		  ?>
          </select>
          </td>
</tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">Task Description</td>
          <td colspan="2"><textarea name="task_description" style="width:100%; height:50px;" id="task_description" placeholder="Enter Task Description"></textarea></td>
</tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">Milestone</td>
          <td colspan="2"><select name="milestone">
                  <option value="">Select</option>
                <?php $sql=DB_query("select * from tsk_milestone_master",$db); while($res=DB_fetch_assoc($sql)) {  ?>
                <option value="<?php echo $res['id']; ?>"><?php echo $res['mile_name']?></option>
                <?php } ?> </select></td>
</tr>
<tr class="row1">
    <td class="tabledata1"><strong>Sub Task Details</strong></td>
    <td class="tabledata1" colspan="3" style="text-align: left"><input type="button" class="search" value="Add Sub task" onclick="addRow('dataTable')"  >&nbsp;&nbsp;<input type="button" class="search" value="Delete" onclick="deleteRowOrder('dataTable')"  ></td>
</tr>
<tr class="row1">
    <td class="tabledata1"></td>
          <td colspan="3">
              <table width="100%">
                  <input type="hidden" name="hiditemcount" id="hiditemcount" value="">
                  <input type="hidden" name="childcounter" id="childcounter" value="0">
                  <tr>
                      <th>Select</th>
                        <th style="width:50%">Task Description</th>
                        <th colspan="2" style="text-align:center">Start Date & Time</th>
                        <th  colspan="2" style="text-align:center">End Date & Time</th>
                        
                    </tr>
                    <tbody id="dataTable">
                    <tr>
                       
                        <td class="tabledata1"><input type="checkbox" id="delete"></td>
                        <td class="tabledata1"><textarea id="task_description" name="subtask_description[]" style="width:99%; height:50px;"><?php echo $task_description;?></textarea></td>
                        <td class="tabledata1" ><input type="text" id="start_date" name="substart_date[]" style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>" ></td>
                        <td class="tabledata1" ><input type="text" id="start_time" class="datetimepicker1" name="substart_time[]" style="width:45px;" value="<?php echo $start_time; ?>" ></td>
                        <td class="tabledata1" ><input type="text" id="end_date" name="subend_date[]" style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>"></td>
			<td class="tabledata1" ><input type="text" id="end_time" name="subend_time[]" style="width:45px;"  class="datetimepicker1"></td>
                        
                    </tr>
                </table>            </td>
        </tr>
  </tbody>
              </table>
          </td>
</tr>
    
</table>
	  
<p align="center"><input type="submit" name="task_assign_save" value="Assign" class="search" /> 
    <a href="index.php?Application=emp&modid=<?php echo $_REQUEST['modid'] ?>"><input type="button"  value="Cancel" class="search" /></a>

</p>	  
    </form>
  </div>
</div>
<script src="js/jquery111.js"></script>
<script src="js/jquery.datetimepicker.js"></script>
<script type="text/javascript" language="javascript">
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 task_validate()
{
   if($('#branch').val()=='')
    {
        $('#branch').focus()
        alert("Please Select Branch")
        return false;
    }
    if($('#user_id').val()=='')
    {
        $('#user_id').focus()
        alert("Please Select User")
        return false;
    }
    if($('#task_description').val()=='')
        {
            $('#task_description').focus()
            alert("Please Enter Task Description")
            return false;
        }
}
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 childcounter=document.getElementById("childcounter").value=parseInt(document.getElementById("childcounter").value)+1;
	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='tabledata1';
        
  
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
           if(newcell.childNodes[0].className=="datetimepicker2")
            {
                var nod_id = newcell.childNodes[0].id;
                newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
                newcell.childNodes[0].className="datetimepicker2";    
                newcell.childNodes[0].setAttribute('style','width:100px');
            }
            else
            {
               var nod_id = newcell.childNodes[0].id;
               newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter; 
               newcell.childNodes[0].className="datetimepicker1";
               newcell.childNodes[0].setAttribute('style','width:45px');
            }
	break;
        case "textarea":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+childcounter;
	break;	
	}
		//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);
	}
}
$('.datetimepicker1').datetimepicker({
	datepicker:false,
	format:'H:i',
	step:5
});
$('.datetimepicker2').datetimepicker({
	timepicker:false,
	format:'d/m/Y',
	formatDate:'Y/m/d',
	//minDate:'-1970/01/02', // yesterday is minimum date
	//maxDate:'+1970/01/02' // and tommorow is maximum date calendar
});

function datetime1()
{
$('.datetimepicker1').datetimepicker({
	datepicker:false,
	format:'H:i',
	step:5
});
$('.datetimepicker2').datetimepicker({
	timepicker:false,
	format:'d/m/Y',
	formatDate:'Y/m/d',
	//minDate:'-1970/01/02', // yesterday is minimum date
	//maxDate:'+1970/01/02' // and tommorow is maximum date calendar
});
}
</script>