<?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['Save']!='')
{
    $brnch=substr($_REQUEST['branch'],0,3);
    $prefix=$s->GetFieldValue('prefix_tbl'," prefix_name='task assignment' " ,'prefix_code');
    $maxid=DB_query("select max(id) from `perf_task_assign`",$db);
    if(DB_num_rows($maxid)==0)
    {
        $lst=1;
    }
    else{
        $res=DB_fetch_assoc($maxid);
        $lst=$res['max(id)'];
    }
     $fnl_str=$brnch."/".$prefix."/".$lst;
    
	$data['branch']=$_REQUEST['branch'];
	$data['doc_no']=$fnl_str;
	$data['assign_to']=$_REQUEST['assign_to'];
        if($_REQUEST['assign_to']=="user"){
	$data['user_id']=$_REQUEST['user_id'];
        }
        if($_REQUEST['assign_to']=="team"){
	$data['teamid']=$_REQUEST['teamid'];
        }
        $data['task_date']= FormatDateForSQL($_REQUEST['task_date']);
        $data['remarks']=$_REQUEST['remarks'];
	$data['assigned_by']=$_SESSION['UserID'];
	$data['created']=date('Y/m/d');
	$data['created_by']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];

       // print_r($data);
        
	$sql1=$s->insertRecord('perf_task_assign', $data);
	$result1=DB_query($sql1,$db);
	$last_id=DB_Last_Insert_ID($db,'perf_task_assign','id');
	
        for($i=0;$i<count($_REQUEST['task_description']);$i++)
        {
            $st_time= $_REQUEST['start_time'][$i];
            $end_time= $_REQUEST['end_time'][$i];
            $data1['task_id']=$last_id;
            $data1['task_description']=$_REQUEST['task_description'][$i];
            $data1['start_date']=FormatDateForSQL($_REQUEST['start_date'][$i]);
            $data1['start_time']=$st_time;
            $data1['end_date']=FormatDateForSQL($_REQUEST['end_date'][$i]);
            $data1['end_time']=$end_time;
            //$data1['doc_no']=$_REQUEST['doc_no'];
            $data1['repeat_status']=$_REQUEST['repeat_status'][$i];
            //$data1['remarks_time']=$current_time;

            $sql2=$s->insertRecord('perf_task_assign_details', $data1);
            $result2=DB_query($sql2,$db);
        }
	$s->pageLocation("index.php?route=TaskMgt/perf_task_assign_index".$rurl);
}

?>
<link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.css" />
<!--<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"><strong>Branch</strong></td>
    <td><select name="branch" style="width:150px;">
            
            <?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	$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><strong>DOC No</strong></td>
          <td><input type="text" name="doc_no" id="doc_no" readonly value="Auto Generate" style="width:150px" ></td>
	      <td><strong>Date</strong></td>
	      <td><input type="text" class="datetimepicker2" id="task_date" name="task_date" value="<?php echo date('d/m/Y'); ?>" 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><strong>Assign To</strong></td>
     <td><?php 
		  if($_REQUEST['action']=='AddRem'){if($assign_to=='user'){$user_ch="checked"; $team_ch=''; $user_disable="";}else{$user_ch=""; $team_ch='checked'; $user_disable="disabled";}}else{$user_ch="checked"; $team_ch=''; $user_disable="";}
		  ?>
          <input name="assign_to" type="radio" value="user" onClick="checkAssignTo(this.value)" <?php echo $user_ch; ?>> 
            User 
          <input name="assign_to" type="radio" value="team" onClick="checkAssignTo(this.value)" <?php echo $team_ch; ?>> 
          Team  </td>
		   <?php 
            $realneme=DB_fetch_assoc(DB_query("Select realname from www_users where id='".$_SESSION['UserID']."' ",$db));
           ?>
     <td><strong>Assign By</strong></td>
     <td ><input type="text"  name="Input" readonly value="<?php echo $realneme['realname']  ?>" id="assign_to" style="width:150px" ></td>
        <td ><strong>User/Team</strong></td>
        <td ><select name="user_id" id="user_id" <?php echo $user_disable;?> style="width:150px;">
          <?php 
        $users=DB_query("Select * from www_users where id in ($lowerlist)",$db);
        while($users_row=DB_fetch_array($users))
        {?>
          <option value="<?php echo $users_row['id']?>" <?php if($user_id!=''){if($user_id==$users_row['id']) echo "selected";}?>><?php echo $users_row['realname'];?></option>
          <?php 	
        }
        ?>
        </select>
          <?php
         $usersql=DB_query("select * from perf_team_master where companyid='".$_SESSION['ACGCompanyId']."' ",$db);
$data='<select name="teamid" id="teamid" style="display:none;"> <option value="">select</option>';
while($user1=DB_fetch_assoc($usersql))
{ 
    $data.='<option value="'.$user1['id'].'">'.$user1['team_name'].'-'.$user1['team_code'].'</option>';
}
$data.='</select>';    
echo $data;
         ?></td>
</tr>
        
        <tr class="row1"><td colspan="6"><strong>Task Details</strong></td></tr>
  <tr class="row1">
<td colspan="6" style="background-color:#e7e7e7"><p align="left" style="padding-top:5px; padding-bottom:5px;">
  <input name="button" type="button" class="search" accesskey="A" onclick="addRow('dataTable')" value="Add Rows" /><input type="button" class="search"  onclick="deleteRowOrder('dataTable')" value="Delete" />
</p></td>
  </tr>
   <input type="hidden" name="hiditemcount" id="hiditemcount" value="">
        <tr>
            <td colspan="6">
                <table width="100%">
                    <tr>
                        <th></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>
                        <th style="width:10%">Repeat</th>
                    </tr>
                    <tbody id="dataTable">
                    <tr>
                        <td><input type="checkbox" id="delete"></td>
                        <td><textarea id="task_description" name="task_description[]" style="width:99%; height:50px;"><?php echo $task_description;?></textarea></td>
                        <td style="text-align:right"><input type="text" id="start_date" name="start_date[]" style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>" ></td>
                        <td style="text-align:left"><input type="text" id="start_time" class="datetimepicker1" name="start_time[]" style="width:45px;" value="<?php echo $start_time; ?>" ></td>
                        <td style="text-align:right"><input type="text" id="end_date" name="end_date[]" style="width:100px;" class="datetimepicker2" value="<?php echo date('d/m/Y');?>"></td>
			<td style="text-align:left"><input type="text" id="end_time" name="end_time[]" style="width:45px;"  class="datetimepicker1"></td>
                        <td><select name="repeat_status[]" id="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>
                    </tr>
                </table>            </td>
        </tr>
  </tbody>
  <tr>
      <td>Remarks</td>
      <td colspan="5"><textarea name="remarks" cols="100" rows="5"></textarea></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>
		  <?php if($reminder_status=='Y'){$ch="checked";}else{$ch='';}?>
		  <input type="checkbox" name="" id="reminder_status" value="" onClick="activateTime()" <?php echo $ch;?>>
		  <input type="hidden" name="reminder_status" value="<?php if($_REQUEST['action']=='AddRem') {echo $reminder_status;} else {echo "N";} ?>" 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" <?php if($time_mode=='H') echo "selected";?>>Hours</option>
		  <option value="M" <?php if($time_mode=='M') echo "selected";?>>Minutes</option>
          </select>          </td>
</tr> -->
</table>
<?php //}?>
	  
<p align="center"><?php if($_REQUEST['action']=='AddRem'){?><input type="submit" name="Remarks" value="Add Remarks" class="search" /> <?php } elseif($_REQUEST['action']!='SHRem'  && $_REQUEST['CL']!='1'){?><input type="submit" name="Save" value="Save" class="search" /><?php }?>
<?php if($_REQUEST['CL']=='1'){?><input type="submit" name="Close" value="Close" class="search" /><?php } ?>
</p>	  
    </form>
  </div>
</div>
<script type="text/javascript" language="javascript">
//$('.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").style.display='block'; 
            document.getElementById("teamid").style.display='none';  
        }
	else
        {
            document.getElementById("user_id").style.display='none';
            document.getElementById("teamid").style.display='block';  
        }
}
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='tabledata1';
	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 src="js/jquery111.js"></script>
<script src="js/jquery.datetimepicker.js"></script>
<script type="text/javascript">

$('.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>