<?php 
//****************MODIFY BY AMIT SINGH ************D&D************************23-11-2012*********************************
$table='crm_leadtodo';  include("ClassRole.php");
 $type=$s->getField('www_users','id',$_SESSION['UserID'],'type');
if($_REQUEST['DeleteRecord'])
{
	for($i=0;$i<count($_REQUEST['todo_leadid']);$i++)
	{
	$data['modified_date']=date('Y-m-d');
	$data['delete_status']='N';
	$cond="todoid='".$_REQUEST['todo_leadid'][$i]."'";
	$row= $s->RecordEdit($table, $data,$cond);
	}
}

if($_REQUEST['action']=='delete')
{
	//echo "delete from $table where todoid='".$_REQUEST['todoid']."'";exit();
    $sql=DB_query("delete from $table where todoid='".$_REQUEST['todoid']."'",$db);
    prnMsg( _('Record Deleted successfully'),'success');
        echo "<script>location.href='index.php?route=CRM/CrmLeadTODO&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
if($_REQUEST['Save'])
{
	$fstatus=true;
	if($_REQUEST['leadtodo']=='')
	{
	$msgErr[]="Please Enter The To Do";
	$fstatus=false;
	}
	if($_REQUEST['todostatus']=='0')
	{
	$msgErr[]="Please Select The Status";
	$fstatus=false;
	}
	if($_REQUEST['lead_priority']=='0')
	{
	$msgErr[]="Please Select The  Priority";
	$fstatus=false;
	}
	if($_REQUEST['tododue_date']=='')
	{
	$msgErr[]="Please Select The Due Date";
	$fstatus=false;
	}
	if($_REQUEST['tododatetime']=='')
	{
	$msgErr[]="Please Enter The To Do Date";
	$fstatus=false;
	}
}


if($_REQUEST['Save'] && $fstatus)
{
        if($_REQUEST['groupid']!='')
            {
           $string= trim(implode(',',$_REQUEST['groupid']));
            }
       
	$data['mst_leadid']=$_REQUEST['mst_leadid'];
	$data['tododue_date']=FormatDateForSQL($_REQUEST['tododue_date']);
	$data['leadtodo']=$_REQUEST['leadtodo'];
	$data['lead_type']=$_REQUEST['lead_type'];
	$data['lead_priority']=$_REQUEST['lead_priority'];
	$data['description']=$_REQUEST['description'];
	$data['tododatetime']=FormatDateForSQL($_REQUEST['tododatetime']);
	$data['todostatus']=$_REQUEST['todostatus'];
	$data['groupid']=$string;
	$data['leaduserid']=$_REQUEST['userString'];
	$data['todostart_time']=$_REQUEST['time'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	if($_REQUEST['action']=='Edit')
	{
	$cond="todoid='".$_REQUEST['todoid']."'";
	$row= $s->RecordEdit($table, $data,$cond);
	}
	else{
	$row= $s->RecordInsert($table, $data);
	}
	$s->pageLocation("index.php?route=CRM/CrmLeadTODO&leadid=".$_REQUEST['leadid'].$rurl);
}	

if($_REQUEST['Save'] && $fstatus && $_REQUEST['action']=='Edit')
{
    $data['mst_leadid']=$_REQUEST['mst_leadid'];
	$data['tododue_date']=FormatDateForSQL($_REQUEST['tododue_date']);
	$data['leadtodo']=$_REQUEST['leadtodo'];
	$data['lead_type']=$_REQUEST['lead_type'];
	$data['lead_priority']=$_REQUEST['lead_priority'];
	$data['description']=$_REQUEST['description'];
	$data['tododatetime']=FormatDateForSQL($_REQUEST['tododatetime']);
	$data['todostatus']=$_REQUEST['todostatus'];
	$data['groupid']=$string;
	$data['leaduserid']=$_REQUEST['userString'];
	$data['todostart_time']=$_REQUEST['time'];
    $cond="id='".$_REQUEST['id']."'"; 
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  prnMsg( _('Record Updated successfully'),'success');
echo "<script>location.href='index.php?route=CRM/CrmLeadTODO&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if($_REQUEST['action']=='Edit' &&  isset($_REQUEST['todoid']))
{
	$getlead=DB_query( "select * from $table where mst_leadid='".$_REQUEST['leadid']."' and todoid='".$_REQUEST['todoid']."'",$db);
	$row_lead=DB_fetch_array($getlead);
         $id=$row_lead['todoid'];
	 $mst_leadid=$row_lead['mst_leadid'];
	$tododue_date=$row_lead['tododue_date'];
	$leadtodo=$row_lead['leadtodo'];
	$lead_type=$row_lead['lead_type'];
	$description=$row_lead['description'];
	$tododatetime=$row_lead['tododatetime'];
	$todostatus=$row_lead['todostatus'];
	$tododue_date=$row_lead['tododue_date'];
	$groupid=$row_lead['groupid'];
	$lead_priority=$row_lead['lead_priority'];
	$leaduserid= $row_lead['leaduserid'];
	$todostart_time=$row_lead['todostart_time'];
	$hours=$todostart_time[0];
	$mins=$todostart_time[1];
	$AMPM=$todostart_time[2];
        
        
        
}	 


if($_REQUEST['action']=='View')
{
	$condition="todoid='".$_REQUEST['todoid']."' and mst_leadid='".$_REQUEST['leadid']."'";
	$rec=$s->GetRecordById('crm_leadtodo',  $condition);
	$recRow=DB_fetch_array($rec);
	extract($recRow);
}

 
  if(isset($_REQUEST['leadid']))
 {
     $leaduserid=array();
      $_REQUEST['leadid'];
    $getUsers=DB_query("select * from crm_lead_groups_users where parent_id='".$_REQUEST['leadid']."'",$db);
while($getUsersdata=DB_Fetch_array($getUsers)){
    $leaduserid[]=$getUsersdata['user_id'];
}
//print_r($leaduserid);

$getGroup=DB_query("select * from crm_lead_groups where parent_id='".$_REQUEST['leadid']."'",$db);
while($getGrouprow=DB_Fetch_array($getGroup)){
    $groupidd[]=$getGrouprow['group_id'];
}
//print_r($groupidd);
$userString=implode(', ', $leaduserid);

 }
?>

<script src="ui/jquery.ui.timepicker.js"></script>

<script language="javascript" >

function LeadData()
{	
	myCheckboxArray = document.myform.elements["todo_leadid[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('todo_leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('todo_leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('todo_leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('todo_leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('todo_leadid_'+i).checked=false;	
	}
	}	
}

function validation(form)
{ 
    if(document.getElementById('leadtodo').value=='')
	{
	alert('Enter To Do Type');
	document.getElementById('leadtodo').focus();
	return false;
	}
        
        if(document.getElementById('time').value=='')
	{
	alert('Enter Due Time');
	document.getElementById('time').focus();
	return false;
	}
        
         if(document.getElementById('tododue_date').value=='')
	{
	alert('Enter Due Date');
	document.getElementById('tododue_date').focus();
	return false;
	}      
        
}
</script>
<?php  
include('crm_top_menu.php'); ?>
<div style="width:100%">
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>To Do Information</h1>
<form name="form1" method="post" action="" onsubmit="return validation(this.form);">
<input name="mst_leadid" type="hidden" value="<?php  echo $_REQUEST['leadid']?>">
<input name="lead_type" type="hidden" value="Task">
<input name="userString" type="hidden" value="<?php  echo $userString;  ?>">
<input name="id" type="hidden" value="<?php  echo $id;  ?>">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
 <?php if(count($msgErr)>0){?>
	<tr class="row1">
		<td colspan="6"><?php for($i=0;$i<count($msgErr);$i++){
		echo "<div class='error'>".$msgErr[$i]."</div>";}?></td>
	</tr><?php }?>
	<tr class="row1">
	    <td width="13%" align="right"><strong>To Do Type</strong> <label><font color="#FF0000">*</font></label></td>
	    <td width="20%"><input type="text" name="leadtodo" id="leadtodo" value="<?php echo $leadtodo?>" style="width:98%;"></td>
		<td width="13%" align="right"><strong>Date</strong></td>
        <td width="20%"><input type="text" name="tododatetime" value="<?php  if($tododatetime!='') { echo ConvertSQLDate($tododatetime); } else { echo ConvertSQLDate(date('Y-m-d')); } ?>" style="width:98%;" readonly></td>
		<td width="13%" align="right"><strong>Time</strong> <label><font color="#FF0000">*</font></label></strong></td>
		<td><input type="text" name="Meettime" id="timedone" value="<?php echo $todostart_time; ?>" class="timepicker" style="width:98%;" /></td>
	    </tr>
		<tr class="row1">
	    <td width="13%" align="right"><strong>Status</strong></td>
	    <td width="20%"><select name="todostatus" style="width:98%;">		    	
		    	<option value="Pending" <?php echo  $todostatus=='Pending'?'selected':'' ;?>>Pending</option>
		    	<option value="Confirm" <?php echo  $todostatus=='Confirm'?'selected':'' ;?>>Confirm</option>		    	
		    </select></td>
		<td width="13%" align="right"><strong>Priority</strong></td>
		<td width="20%"><select style="width:98%;" name="lead_priority">					
					<option value="High" <?php  echo ($lead_priority=='High'?'selected':'')?>>High</option>
					<option value="Low" <?php  echo ($lead_priority=='Low'?'selected':'')?>>Low</option>
					<option value="Medium" <?php  echo ($lead_priority=='Medium'?'selected':'')?>>Medium</option>
		</select></td>
		<td width="13%" align="right"><strong>Due Date</strong> <label><font color="#FF0000">*</font></label></td>
        <td align="left"><input type="text" name="tododue_date" id="tododue_date" class="datepicker" value="<?php if($tododue_date!='') { echo ConvertSQLDate($tododue_date); }?>" style="width:98%;" readonly></td>
	</tr>
	<tr class="row1" valign="top">
	    <td align="right"><strong>Description</strong></td>
	    <td colspan="5" align="left"><textarea name="description" cols="50" rows="3" style="width:99%;"><?php echo $description?></textarea></td>
	</tr>
	<tr class="row1">
	     <td colspan="6" style="padding:0;">
		    <table width="100%" border="0" cellspacing="0" cellpadding="3">
		    <tr>		    	
		    	<td width="13%" align="right"><strong>Group</strong> <label><font color="#FF0000">*</font></label></td>
                <td width="20%"><?php 
			 // print_r($groupidd);
 $sql_state2=DB_query("SELECT * FROM  crm_lead_groups where parent_id ='".$_REQUEST['leadid']."' and  companyId='".$_SESSION['ACGCompanyId']."'",$db); 
	$i=0;
	while($row2=DB_fetch_array($sql_state2))
					{
					$p='';
 	 if (in_array($row2['secroleid'],  $groupidd))
	 $p='checked';
					?>
				  <input type='checkbox' name='groupid[]' disabled  id='groupid_<?php echo $i; ?>' checked onclick='getUsers(this.value,<?php echo $i; ?>);' value='<?php echo $row2['id']; ?>'><?php echo $s->GetFieldValue('deptmaster_hierarchy',"id='".$row2['group_id']."'" ,'deptname').'<br/>';
					$i++;}?>			  </td>
              	<td width="13%" align="right"><strong>User</strong> <label><font color="#FF0000">*</font></label></td>
              	<td><div id='hid-Roleid'>
<?php  
// print_r($leaduserid);
 
 //echo "SELECT id , realname FROM  www_users where rolepid ='".$groupid."'";
if($userString!='') {
 $sql_state=DB_query("SELECT id , realname FROM  www_users where id in(".$userString.")",$db);
while($row=DB_fetch_array($sql_state))
{
?>
 <input name="leaduserid[]" type="checkbox" disabled value="<?php  echo $row['id'];?>"checked/><?php  echo $row['realname'];?><br/>
 <?php 
 }
}
?>	
	</div></td>
		    </tr> 
		</table></td>
	    </tr> 		  
		<tr class="row1">
		    <td colspan="6" align="center" style="text-align:center;"><input type="submit" name="Save" value="SUBMIT" class="search"></td>
		</tr>
	</table>
</form>

 <form action="" method="post" name="myform" >
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid;">
  <tr>
  <!--<td><input name="todo_leadid" type="checkbox"   id="todo_leadid" onclick="LeadData()"    />  </td>-->
	<th width="10%">Status</th>
	<th width="14%">To Type</th>
	<th width="14%">Start Date / Time </th>
	<th width="8%">End Date</th>
	<th>Users</th>
    <th <?php if($type=='SuperAdmin') { ?>colspan="2" <?php } else { ?>colspan="1"<?php } ?>>Action</th>	
  </tr>
<?php $i=1;
      $getlead=DB_query( "select * from $table A,crm_leads B where B.leadid='".$_REQUEST['leadid']."' and A.mst_leadid=B.leadid and delete_status='Y' and A.lead_type='Task'",$db);
	 while($row_lead=DB_fetch_array($getlead))
	 {
	 ?>
  <tr>
  <!--<td>
  <input name="todo_leadid[]" type="checkbox" id="todo_leadid_<?php echo $i; ?>"   value="<?php  echo $row_lead['todoid']?>" />  
  </td>-->
  <td><?php echo $row_lead['todostatus'];// ConvertSQLDate($row_lead['created_date'])?></td> 
  <td><?php  echo $row_lead['leadtodo']?></td>	
  <td><?php  echo ConvertSQLDate($row_lead['tododatetime']).'&nbsp;&nbsp;';
  echo $row_lead['todostart_time'];?></td>
  <td><?php  echo ConvertSQLDate($row_lead['tododue_date']);?></td>
  <td><?php  $userstr=$row_lead['leaduserid'];$userArray=explode(', ', $userstr);
            //print_r($userArray);
            foreach($userArray as $users)
            {
                //echo $users;
                $query="select * from www_users where id='".$users."'";
              $getUsers=DB_query($query,$db);
              $row_user=DB_fetch_array($getUsers);
              $userName[]=$row_user['realname'];
            }
           echo  $userStr=implode(', ', $userName);
           unset($userName);
	?></td>
  
   <td>
       <?php if($_SESSION['frmEdit']==0){ ?>
  <img src="images/icon-edit.png" width="15" alt="Not Allowed" title="Not Allowed" height="15" />
<?php }else{ ?>
  
  <a href="index.php?route=CRM/CrmLeadTODO&action=Edit&leadid=<?php echo $row_lead['leadid'].$rurl;?>&todoid=<?php echo $row_lead['todoid']?>">
  <img src="images/icon-edit.png" width="15" height="15" /></a> <?php } ?>
   </td>
   <td style="text-align:center;"><?php 
    if($type=='SuperAdmin')
   if($_SESSION['frmDelete']==1){ ?>
		<a href="index.php?route=CRM/CrmLeadTODO&leadid=<?php  echo $row_lead['leadid']?>&todoid=<?php echo $row_lead['todoid']?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=delete&id=<?php echo $follow_row['id'];?>" onclick="return confirm('Are You Sure?')" ><img src="images/Icon_Delete.png" height="15" width="15"></a>
		<?php } else { ?>
		<img src="images/Icon_Delete.png" title="Not Allowed" height="15" width="15">
		<?php } ?>
		</td>
  </tr>
<?php $i++; } ?>
</table>		
</form>
</div></div>
<script>
      $('.timepicker').timepicker();
	  
      </script>