<?php 
//*************************
include('functions.php');
$s=new db_query();
$user_ids=$_SESSION['UserID'];
$user_id = $_SESSION['ACG_DashBoard_User'];
$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'];//UNDER USER
else
$lowerlist="'".$_SESSION['UserID']."'";
$date=date('Y-m-d');
$back_days=calculateBackDate($_SESSION['ACGRoleId']);
$back_date=date('Y-m-d', strtotime('-'.$back_days.' day', strtotime($date)));	

$Client1=array();
$ZoneSQL=DB_query("select * from www_users where id in($lowerlist) and assigned_sites in(".$_SESSION['AssignedSites'].")",$db);

while($ZoneClient=DB_fetch_array($ZoneSQL))
{
	$Client1[]=$ZoneClient['id'];
}
$Client2=implode(",",$Client1);
?>

<!-----------------------------------SCRIPT FILE--------------START HERE-----------------------AMITSINGH--------------------->

<script type="text/javascript">
function Cal_TotalTA()
{
	var KM= document.getElementById("KM").value;
	var Rate= document.getElementById("Rate").value;
	var TotalTa=parseFloat(KM)*parseFloat(Rate);
	document.getElementById("TotalTA").value=TotalTa;
	
}
function Cal_TotalAmt()
{
	var TotalTA= document.getElementById("TotalTA").value;
	var Fooding= document.getElementById("Fooding").value;
	var OtherCharges=document.getElementById("OtherCharges").value;
	var TotalAmt=parseFloat(TotalTA)+parseFloat(Fooding)+parseFloat(OtherCharges);
	document.getElementById("TotalAmt").value=TotalAmt;
	
}
</script>

 <script language="javascript" >
function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	//alert(myCheckboxArray);
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}
</script>

<!-----------------------------------SCRIPT FILE--------------END HERE-----------------------AMITSINGH--------------------->



<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Daily Visit Entry</h1>
<div class="centre" id="slidingDiv">
<form action="" method="post" name="myform1" >
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
	<td class="tabledata1">From:</td>
	<td><input name="fromdate" value="<?php echo $_REQUEST['fromdate'] ?>" type="text" style="width:150px;"  class="datepicker"/></td>
	<td class="tabledata1">To:</td>
	<td><input name="todate" value="<?php echo $_REQUEST['todate'] ?>" type="text" style="width:150px;"  class="datepicker"/></td>
	</tr>
	<tr class="row1">
    <td class="tabledata1">Employee:</td>
	<?php
 		$EmpSql="SELECT * FROM www_users where CompanyId='".$_SESSION['ACGCompanyId']."' and id in(".$Client2.")";
	 ?>
 	<td colspan="3"><select style="width:150px;" name="EmpName">
    	    		<option  value="0">-All--</option>
    		<?php 
    		$EmpRes=DB_query($EmpSql,$db);
			while($EmpRow=DB_fetch_array($EmpRes))
			{
    		?>
			<option value="<?php echo $EmpRow['id']?>" <?php echo($_REQUEST['EmpName']==$EmpRow['id']?'selected':''); ?>><?php echo $EmpRow['realname']?></option>
			<?php }?>
			
	
	</select></td>
	</tr>
	<tr class="row1">
	<td class="tabledata1" colspan="4"><p align="center"><input name="Search" type="submit"  value="Search" class="search"/></p></td>
  </tr>
</table>
</form>
</div>

<form action="" method="post" name="myform" >
<p align="left">
<?php if($_SESSION['frmAdd']==0){ ?> 
<input type="button" name="Create" value="Add" alt="Not Allowed" title="Not Allowed" class="search" />
<input type="button" name="Create" value="Add Event" alt="Not Allowed" title="Not Allowed" class="search" />
<input type="button" name="Create" value="Add To Do" alt="Not Allowed" title="Not Allowed" class="search" />
<input type="button" name="Create" value="Add Note" alt="Not Allowed" title="Not Allowed" class="search" />
<?php }else{?>

<a href="index.php?route=CRM/AddDailyEntry&modid=<?php echo $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid']?>">
<input type="button" name="Create" value="Add" class="search" /></a> 
<a href="index.php?route=CRM/CrmfieldvisitreportEvent&VisitId=<?php  echo $_REQUEST['leadid'].$rurl;?>"><input type="button" name="Create" value="Add Event" class="search" /></a>	  
<a href="index.php?route=CRM/CrmfieldvisitreportTODO&VisitId=<?php  echo $_REQUEST['leadid'].$rurl;?>"><input type="button" name="Create" value="Add To Do" class="search" /></a>
<a href="index.php?route=CRM/CrmfieldvisitreportAddLeadNote&VisitId=<?php  echo $_REQUEST['leadid'].$rurl;?>"><input type="button" name="Create" value="Add Note" class="search" /></a>
<?php }?>
<?php if($_SESSION['frmDelete']==0){ ?>	
<input type="button" name="Delete" value="Delete" alt="Not Allowed" title="Not Allowed" class="search" />	
<?php }else{?>  
<input type="submit" name="Delete" value="Delete" class="search" />	
<?php }?>		  
</p>

              <?php 
if($_REQUEST['leadid']!='')
{
	if($_REQUEST['Delete'])
	{
	for($i=0;$i<count($_REQUEST['leadid']);$i++)
	{
	$cond="mst_crm_entryid='".$_REQUEST['leadid'][$i]."'";
	$row= $s->DeleteRecord('crm_daily_visit_expense_entry', $cond);// DELETE FROM THIRD TABLE 
	
	
	$cond1="mst_crm_entryid='".$_REQUEST['leadid'][$i]."'";
	$row= $s->DeleteRecord('crm_daily_visit_entry', $cond1); // DELETE FROM SECOND TABLE
	
	
	$cond1="id='".$_REQUEST['leadid'][$i]."'";
	$row= $s->DeleteRecord('crm_mst_daily_visit_entry', $cond1); // DELETE FROM FIRST TABLE
	}
	}
}



if($_REQUEST['Search'])
{?>
<?php }?>
<?php
if($_REQUEST['Search'])
{
	$todate=ConvertSQLDate($_REQUEST['todate']);
	$fromdate=ConvertSQLDate($_REQUEST['fromdate']);
	$EmpName=$_REQUEST['EmpName'];
	if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='' )
	{
	$filter[]="crm_mst_daily_visit_entry.visit_date between '".$fromdate."' and '".$todate."'";
	}
	if($_REQUEST['EmpName']!=0)
	{
	$filter[]="crm_mst_daily_visit_entry.empid='".$EmpName."'";
	}
	if($_REQUEST['EmpName']==0)
	{
	$filter[]="crm_mst_daily_visit_entry.empid in($lowerlist)";
	}
	if($filter!='')
	{
	$filter1=implode(" and ",$filter);
	$getlead=DB_query( "SELECT *,crm_mst_daily_visit_entry.visit_date AS Vid FROM 
	crm_mst_daily_visit_entry 
	INNER JOIN crm_daily_visit_entry ON 
	(crm_mst_daily_visit_entry.id=crm_daily_visit_entry.mst_crm_entryid ) where   $filter1 " ,$db);
	}
}

else
{

	$getlead=DB_query("SELECT *,crm_mst_daily_visit_entry.visit_date AS Vid,crm_mst_daily_visit_entry.id AS IDS FROM 
	crm_mst_daily_visit_entry 
	INNER JOIN crm_daily_visit_entry ON 
	(crm_mst_daily_visit_entry.id=crm_daily_visit_entry.mst_crm_entryid ) and crm_mst_daily_visit_entry.empid in($Client2) GROUP BY crm_mst_daily_visit_entry.id ORDER BY crm_mst_daily_visit_entry.visit_date DESC",$db);
}
?>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="row1">
        <td class="tabledata"><input name="leadid" type="checkbox"   id="leadid" onclick="LeadData()"    /></td>
        <td class="tabledata">Sr No</td>
        <td class="tabledata">Visit Date</td>
        <td class="tabledata">Client Visited</td>
        <td class="tabledata">Contact Person</td>
        <td class="tabledata" style="width:100px;">
			 <center>RESPONSE</center>
            <table width="100%" cellspacing="0" cellpadding="3">  
              <tr>
                <td class="tabledata" align="center">Quote</td>
                <td class="tabledata" align="center">Sample</td>
                <td class="tabledata" align="center" style="white-space:nowrap">Call Again</td>
              </tr>
          </table>
		</td>
        <td class="tabledata">Executive's Remarks</td>
		<td class="tabledata">Comments by Superior</td>
		<td class="tabledata">Created By</td>
      </tr>
<?php $i=0;
//echo  $_SESSION['UserID'];
if(DB_num_rows($getlead)>0){
	while($row_lead=DB_fetch_array($getlead)){?>
      <tr class="row1">
        <td valign="top" style="white-space:nowrap"><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="<?php  echo $row_lead['IDS']?>" />
          <?php if($row_lead['Vid'] >= $back_date){?>
		  <?php if($_SESSION['frmEdit']==0){ ?>
		  <img src="images/icon_edit.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
		  <?php } else {?>
		  
		  <a href="index.php?route=CRM/EditDailyEntry&action=Edit&VisitId=<?php echo $row_lead['mst_crm_entryid']?>&frmid=<?php echo $_REQUEST['frmid'] ?>&modid=<?php echo $_REQUEST['modid']?>"> <img src="images/icon_edit.gif" width="10px"  height="10" /></a>
	<?php } ?>	  
		  <?php } else {?>
		  <img src="images/icon_edit.gif" width="10px"  height="10" />
<?php } ?> 
		  <?php if($row_lead['Vid'] >= $back_date){?>
		  <?php if($_SESSION['frmView']==0){ ?>
		  <img src="images/search.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
		  <?php } else {?>
		  
		   <a href="index.php?route=CRM/ViewDailyEntry&action=View&VisitId=<?php echo $row_lead['mst_crm_entryid']?>&frmid=<?php echo $_REQUEST['frmid'] ?>&modid=<?php echo $_REQUEST['modid']?>"> <img src="images/search.gif" width="10px"  height="10" /></a>
		  
		  <?php } ?>
		  <?php } else {?><img src="images/search.gif" width="10px"  height="10" /><?php } ?> </td>
        <td valign="top" style="padding-left:5px; padding-right:5px; width:"><?php echo $i+1; // ConvertSQLDate($row_lead['created_date'])?></td>
        <td valign="top" style="padding-left:5px; padding-right:5px;"><?php  echo ConvertSQLDate($row_lead['Vid']) ?></td>
        <td valign="top" style="padding-left:5px; padding-right:5px; width:350px;"><?php  
		//echo $row_lead['empid'];
		$EmpName=$s->getField('empmaster','mst_www_userid',$row_lead['empid'],'emp_name');?>
            <?php  //echo $EmpName?>
            <?php  echo  $s->GetFieldValue('clientmaster',"id='".$row_lead['client_id']."'" ,'c_name');?>
        </td>
        <td valign="top" style="padding-left:5px; padding-right:5px; width:150px;"><?php $Dept=$s->GetFieldValue('empmaster',"mst_www_userid='".$_SESSION['UserID']."'",'department') ?>
            <?php $DepartmenName=$s->getField('deptmaster','id',$Dept,'deptname')?>
            <?php  //echo $DepartmenName?>
            <?php  echo $row_lead['ClientContact']?>
        </td>
        <td valign="top"> <table width="100%" border="0" cellspacing="1" cellpadding="2" >
            <tr>
              <td><center>
                <?php if($row_lead['quotes']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?>
              </center></td>
              <td><center>
                <?php if($row_lead['sample']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?>
              </center></td>
              <td><center>
                <?php if($row_lead['callagain']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?>
              </center></td>
            </tr>
        </table></td>
        <td valign="top" width="5000"><?php  echo $row_lead['remark']?></td>
		<td valign="top" width="500"><?php  echo $row_lead['comment']?></td>
		 <td valign="top" width="500"><?php  echo$s->GetFieldValue('www_users',"id='".$row_lead['empid']."'" ,'realname');?></td>	
      </tr>
<?php $i++; } }else{?>
 <tr>
 	<td colspan="8"><font color="#FF0000"><?php echo "No Records Found"; ?></font></td>
 </tr>
 <?php }?>
	</table>
</form>
	</div>
  </div>

