<?php
$action=$_REQUEST['action'];
$id=$_REQUEST['clientID'];
$type=$s->getField('www_users','id',$_SESSION['UserID'],'type');
if($_REQUEST['Add'])
{
	$fstatus=true;
	if($_REQUEST['followDate']=='')
	{
		$msgErr[]="Please Enter the date";
		$fstatus=false;
	}
	if($_REQUEST['FollowType']=='0')
	{
		$msgErr[]="Please Enter The Type";
		$fstatus=false;
	}
	
}


   if($_REQUEST['Add']!='' && $fstatus && $action!=='Edit')
{
      $insert="Insert into crm_followup set
					mst_leadid='".$_REQUEST['leadid']."',
					party= '".$_REQUEST['party']."',
					followDate='".FormatDateForSQL($_REQUEST['followDate'])."',
					FollowType= '".$_REQUEST['FollowType']."',  
					comments='".$_REQUEST['comments']."',
                                        reason='".$_REQUEST['reason']."',
					nextfollowDate='".FormatDateForSQL($_REQUEST['nextfollowDate'])."',
					createdBy='".$_SESSION['UserID']."',
					companyId='".$_SESSION['ACGCompanyId']."',
					createdDate='".date('Y-m-d')."'";
					$result=DB_query($insert,$db);
	  prnMsg( _('Record Added successfully'),'success');
echo "<script>location.href='index.php?route=CRM/CrmFollowUp&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if($_REQUEST['Add']!='' && $fstatus && $action=='Edit')
{
 $data['followDate']=FormatDateForSQL($_REQUEST['followDate']);
 $data['FollowType']=$_REQUEST['FollowType'];
 $data['comments']=$_REQUEST['comments'];
 $data['reason']=$_REQUEST['reason'];
 $data['nextfollowDate']=FormatDateForSQL($_REQUEST['nextfollowDate']);
 $data['modifiedDate']=date('Y-m-d');
 $data['modifiedBy']=$_SESSION['UserID'];
 $cond="id='".$_REQUEST['id']."'";
	$update= $s->EditRecord('crm_followup', $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/CrmFollowUp&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";}

if($_REQUEST['action']=='delete')
{
	$sql=DB_query("delete from crm_followup where id='".$_REQUEST['id']."'",$db);
        prnMsg( _('Record Deleted successfully'),'success');
        echo "<script>location.href='index.php?route=CRM/CrmFollowUp&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";}


   
?>
<script>
function validation(form)
{
     if(document.getElementById('FollowType').value=='0')
	{
	alert('Select Follow Type');
	document.getElementById('FollowType').focus();
	return false;
	}
        
        if(document.getElementById('nextfollowDate').value=='')
	{
            if(document.getElementById('reason').value=='')
            {
                alert('Enter Reason');
                document.getElementById('reason').focus();
                return false;
            }
	}
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<?php  include('crm_top_menu.php'); ?>
<div class="body_contain">
<div class="tab_div">
<table align="right"><tr><td></td></tr></table>
<h3 align="center"><?php if($_REQUEST['action']=='Edit') { echo 'Party Follow Up Edit'; } else { echo 'Party Follow Up'; } ?></h3>
<form action="" method="post" onsubmit="return validation(this.form);">
<table  cellpadding="3" cellspacing="0" align="center" border="0" width="100%">
<?php if(count($msgErr)>0)
	{?>
		
	<tr><td colspan="6">
		<?php
		for($i=0;$i<count($msgErr);$i++)
		{
			echo "<div class='error'>".$msgErr[$i]."</div>";
		}
		?>
	</td></tr>
<?php }
if($action=='Edit') {
$PartySql1="Select * from crm_followup where id='".$_REQUEST['id']."'";
	$PartyRes1=DB_query($PartySql1,$db);
	$PartyRow1=DB_fetch_array($PartyRes1);
 ?>
 <tr class="row1">
	<td width="13%" align="right"><strong>Client</strong></td>
	<?php 
	$PartySql="Select * from crm_leads where leadid='".$_REQUEST['leadid']."'";
	$PartyRes=DB_query($PartySql,$db);
	$PartyRow=DB_fetch_array($PartyRes);
	?>
	<td width="20%"><input type="hidden"  style="width:100%" name="party"  value="<?php echo $PartyRow['company_name'];?>" readonly="true"/>	
	<input type="text"  style="width:98%" name="partyDisPlay"  value="<?php echo $s->GetFieldValue('clientmaster',"id='".$PartyRow['company_name']."'",'c_name');?>" readonly="true"/></td>
	<td width="13%" align="right"><strong>Follow Up Date</strong></td>
	<td width="20%"><input type="text"  style="width:98%" name="followDate" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>" readonly size="15" /></td>
	<td width="13%" align="right"><strong>Follow Type</strong></td>
	<td><select  name="FollowType" style="width:98%;">
		<option value="0">--Select--</option>
		<option value="By Phone"<?php if($PartyRow1['FollowType']=='By Phone') { echo 'selected'; } else { echo ''; } ?>>By Phone</option>
		<option value="By Mail"<?php if($PartyRow1['FollowType']=='By Mail') { echo 'selected'; } else { echo ''; } ?>>By Mail</option>
		</select>
	</td>
</tr>
<tr class="row1">
	<td align="right"><strong>Description1</strong></td>
	<td colspan="3"><textarea name="comments" rows="3" cols="40" style="width:99%;"><?php echo $PartyRow1['comments']; ?></textarea></td>	
	<td align="right"><strong>Next Follow Up Date</strong></td>
	<td><input type="text" style="width:98%" name="nextfollowDate" value="<?php echo ConvertSQLDate($PartyRow1['nextfollowDate']); ?>" class="datepicker" size="15" /></td>
</tr>

<tr class="row1">
	<td align="right"><strong>Reason</strong></td>
	<td colspan="3"><textarea name="reason" id="reason" rows="3" cols="40" style="width:99%;"><?php echo $PartyRow1['reason']; ?></textarea></td>
       <td></td><td></td>
</tr>
<?php } else { ?>
<tr class="row1">
	<td width="13%" align="right"><strong>Client</strong></td>
	<?php 
	$PartySql="Select * from crm_leads where leadid='".$_REQUEST['leadid']."'";
	$PartyRes=DB_query($PartySql,$db);
	$PartyRow=DB_fetch_array($PartyRes);
        if($PartyRow['company_name']=='')
        {
            $client=$id;
        }
 else {
     $client=$PartyRow['company_name'];
 }
       // echo $client;
	?>
	<td width="20%"><input type="hidden"  style="width:100%" name="party"  value="<?php echo $client;?>" readonly="true"/>	
	<input type="text"  style="width:98%" name="partyDisPlay"  value="<?php echo $s->GetFieldValue('clientmaster',"id='".$client."'",'c_name');?>" readonly="true"/></td>
	<td width="13%" align="right"><strong>Follow Up Date</strong></td>
	<td width="20%"><input type="text"  style="width:98%" name="followDate" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>"  readonly size="15" /></td>
	<td width="13%" align="right"><strong>Follow Type</strong> <span class="mandatory"><strong>*</strong></span></td>
	<td><select  name="FollowType" id="FollowType" style="width:98%">
		<option value="0">--Select--</option>
		<option value="By Phone">By Phone</option>
		<option value="By Mail">By Mail</option>
		</select>
	</td>
</tr>
<tr class="row1">
	<td align="right"><strong>Description</strong></td>
	<td colspan="3"><textarea name="comments" rows="3" cols="40" style="width:99%;"></textarea></td>
	
	<td align="right"><strong>Next Follow Up Date</strong></td>
	<td><input type="text"  style="width:98%" name="nextfollowDate" id="nextfollowDate" class="datepicker" size="15" /></td>
</tr>
<tr class="row1">
	<td align="right"><strong>Reason</strong></td>
	<td colspan="3"><textarea name="reason" id="reason" rows="3" cols="40" style="width:99%;"></textarea></td>
        <td></td><td></td>
</tr>
<?php } ?>
</table>
<p align="center"><input type="submit" name="Add" class="search" value="<?php if($_REQUEST['action']=='Edit') { echo 'UPDATE'; } else { echo 'SUBMIT'; } ?>" />&nbsp;<a href="index.php?route=CRM/LeadView&action=View&leadid=<?php  echo $_REQUEST['leadid']?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>"><input type="button" value="CLOSE" class="search" /></a></p>
</form>

<h3>Follow Up History</h3>
<table align="center" cellpadding="3" cellspacing="0" border="1" width="100%" style="border:#808080 1px solid;">
	<tr>
		<th width="16%">Client</th>
		<th width="10%">Follow Up Date</th>
		<th width="10%">Next Follow Up Date</th>
		<th width="16%">Follow Type</th>
		<th width="20%">Description</th>
        <th>Reason</th>
		<th colspan="2" style="text-align:center;">Action</th>
	</tr>
	<?php 
	$follow_sql=DB_query("Select * from crm_followup where mst_leadid='".$_REQUEST['leadid']."'",$db);
	while($follow_row=DB_fetch_array($follow_sql))
	{
           $_REQUEST['clientID']=$follow_row['party'];  
            ?>
	<tr bgcolor="#dfdfdf">
		<td><?php echo $s->GetFieldValue('clientmaster',"id='".$_REQUEST['clientID']."'",'c_name');?></td>
		<td><?php echo ConvertSQLDate($follow_row['followDate']);?></td>
		<td><?php echo ConvertSQLDate($follow_row['nextfollowDate']);?></td>
		<td><?php echo $follow_row['FollowType'];?></td>
		<td><?php echo $follow_row['comments'];?></td>
                <td><?php echo $follow_row['reason'];?></td>
		<td width="5%" style="text-align:center;"><?php 
                 if($type=='SuperAdmin')
                if($_SESSION['frmDelete']==1){ ?>
		<a href="index.php?route=CRM/CrmFollowUp&leadid=<?php  echo $_REQUEST['leadid']?>&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>		
		<td width="5%" style="text-align:center;"><?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/CrmFollowUp&action=Edit&leadid=<?php  echo $_REQUEST['leadid']?>&id=<?php echo $follow_row['id'].$rurl;?>">
  <img src="images/icon-edit.png" width="15" height="15" /></a>
<?php } ?>
		</td>
	</tr>
	<?php 
	}
?>	
</table>
</div>
</div>