<?php include('includes/Class_Client.php');
?>
<script language="javascript" >
function caltotal()
{
	var rowCount = document.getElementById("dataTable").rows.length;
	for(var i=0;i<rowCount;i++)
	{
	var TotalKM=document.getElementById("total_km[]").value;
	var Rate=document.getElementById("rate_unit[]").value;
	var OtherAmount=document.getElementById("other_amt[]").value;
	totalDiscounted=parseFloat(TotalKM)*parseFloat(Rate)+parseFloat(OtherAmount);
	document.getElementById("total_amt[]").value=parseFloat(totalDiscounted);
	}
}

function addRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {
	var newcell = row.insertCell(i);
	newcell.innerHTML = table.rows[1].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) {
	case "text":
	newcell.childNodes[0].value = "";
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	break;
	}
	}
	}
	function deleteRow(tableID) {
	try {
	var table = document.getElementById(tableID);
	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 <= 2) {
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	}
	}catch(e) {
	alert(e);
	}
}
</script>
<?php 
if($_REQUEST['Save'])
{
	$data_mst['visit_date']=ConvertSQLDate($_REQUEST['visit_date']);
	$data_mst['empid']=$_SESSION['UserID'];
	$row_insert= $s->RecordInsert('crm_mst_daily_visit_entry', $data_mst );
	$lastid=$s->GetLastRecord('crm_mst_daily_visit_entry',  "1=1",'id');	
	for($i=0;$i<count($_REQUEST['ClientContact']);$i++)
	{
	$data['mst_crm_entryid']=$lastid;
	$data['client_id']=$_REQUEST['client_id'][$i];
	$data['ClientContact']=$_REQUEST['ClientContact'][$i];
	$data['quotes']=$_REQUEST['quotes'][$i];
	$data['sample']=$_REQUEST['sample'][$i];
	$data['callagain']=$_REQUEST['callagain'][$i];
	$data['callagain']=$_REQUEST['callagain'][$i];
	$data['remark']=$_REQUEST['remark'][$i];
	$row_insert= $s->RecordInsert('crm_daily_visit_entry', $data );
	}
	for($i=0;$i<count($_REQUEST['from_location']);$i++)
	{
	$data_exp['mst_crm_entryid']=$lastid;
	$data_exp['from_location']=$_REQUEST['from_location'][$i];
	$data_exp['to_location']=$_REQUEST['to_location'][$i];
	$data_exp['travelmode']=$_REQUEST['travelmode'][$i];
	$data_exp['total_km']=$_REQUEST['total_km'][$i];
	$data_exp['rate_unit']=$_REQUEST['rate_unit'][$i];
	$data_exp['other_amt']=$_REQUEST['other_amt'][$i];
	$data_exp['total_amt']=$_REQUEST['total_amt'][$i];
	$data_exp['narration']=$_REQUEST['narration'][$i];
	$row_insert= $s->RecordInsert('crm_daily_visit_expense_entry', $data_exp );
	}
	$s->pageLocation('index.php?route=CRM/field_visit_report&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}

//echo $_REQUEST['VisitId'];
if($_REQUEST['action']=='View')
{	
	//echo "select * from crm_daily_visit_expense_entry where mst_crm_entryid='".$_REQUEST['VisitId']."'";
	$Report=DB_query("select * from crm_daily_visit_expense_entry where mst_crm_entryid='".$_REQUEST['VisitId']."'",$db);
	
	//echo "select * from crm_daily_visit_entry where mst_crm_entryid='".$ReportRow['mst_crm_entryid']."'";
	$Report1=DB_query("select * from crm_daily_visit_entry where mst_crm_entryid='".$_REQUEST['VisitId']."'",$db);
	
	$report2=DB_query("select * from crm_mst_daily_visit_entry where id='".$_REQUEST['VisitId']."'",$db);
	$report2Row=DB_fetch_array($report2);
}?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>VIEW DAILY FIELD MARKETING</h1>
<form method="post" action="">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<?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 }?>
<tr class="row1">
	<td colspan="6"><h3>General Info</h3></td>
</tr>
<?php 
// $SQL="SELECT MAX(id) FROM daily_visit_entry";
//	$id=DB_query($SQL,$db);
//$Res=DB_fetch_array($id);

$ID=$Res['MAX(id)'];
$Srno=$ID+1;
?>
<tr class="row1">
	<td class="tabledata1">Date</td>
    <td colspan="2"><?php echo ConvertSQLDate($report2Row['visit_date']); ?></td>
    <td class="tabledata1">Employee Name</td>
    <td colspan="2"><?php echo $s->GetFieldValue('empmaster',"mst_www_userid='".$_SESSION['UserID']."'",'emp_name');?></td>
    </tr>
<tr>
<tr class="row1">  	 
	<td class="tabledata1">Dept.</td>
	<td colspan="2"><?php echo $s->GetFieldValue('deptmaster',"id='".$s->GetFieldValue('empmaster',"mst_www_userid='".$_SESSION['UserID']."'",'department')."'",'deptname')	;?></td>
    <td class="tabledata1">Branch</td>
    <td colspan="2"><?php echo $s->GetFieldValue('empmaster',"mst_www_userid='".$_SESSION['UserID']."'",'emp_site');	
	// echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'assigned_sites');
	 ?></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Visited Area</td>
	<td colspan="5"><?php echo $report2Row['visited_area']; ?></td>
</tr>
<tr  class="row1">
	<td colspan="6"><h3>Field Vist Report</h3></td>
</tr>

<!--<INPUT type="button" value="Add Row" onClick="addRow('dataTable')" />-->
<!--<INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable')" />-->
<tr class="row1"> 
	<td class="tabledata">Client</td>
	<td class="tabledata">Contact Officer Tel. No.</td>
	<td class="tabledata"><center>RESPONSE</center>
			 <table width="100%" cellspacing="0" cellpadding="3">
			 	<tr>
					<td>Quote</td>
					<td>Sample</td>
					<td style="white-space:nowrap">Call Again</td>
			  	</tr>
			 </table>
	</td>
    <td class="tabledata">New Client</td>
    <td class="tabledata">Remarks</td>
	<td class="tabledata">Comments</td>
  </tr>
<?php while($Report1Row=DB_fetch_array($Report1)){?>
<tr class="row1">
	<td>
	 <!--<INPUT type="checkbox" name="chk"/>-->
	<?php  echo $s->GetFieldValue('clientmaster',"id='".$Report1Row['client_id']."'",'c_name');	?>
	</td>
    <td><?php echo $Report1Row['ClientContact']; ?></td>
    <td>
	<table width="100%" border="0" cellpadding="3" cellspacing="0">
		  <tr>
			<td><center><?php if($Report1Row['quotes']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?></center></td>
			<td><center><?php if($Report1Row['sample']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?></center></td>
			<td><center><?php if($Report1Row['callagain']=='1'){echo 'Y';}else{echo '&nbsp;'; echo '&nbsp;'; } ?></center></td>
		  </tr>
		</table>
	</td>
    <td></td>
    <td><?php echo $Report1Row['remark']; ?></td>
	<td><?php echo $Report1Row['comment']; ?></td>
</tr>
  <?php } ?>

<tr class="row1">
	<td class="tabledata1"><h3>Expense Information</h3></td>
</tr>
</table>
<!--	<INPUT type="button" value="Add Row" onClick="addRow('dataTable1')" />
<INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable1')" />-->
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
    <td class="tabledata">From</td>
    <td class="tabledata">To</td>
    <td class="tabledata">Mode</td>
    <td class="tabledata">Total km</td>
    <td class="tabledata">Rate/Unit</td>
    <td class="tabledata">Other Amount</td>
    <td class="tabledata">Total Amount</td>
    <td class="tabledata">Narration</td>
  </tr>
<?php  while($ReportRow=DB_fetch_array($Report)){?>
<tr class="row1">
    <td  width="15%"><?php echo $ReportRow['from_location']; ?></td>
    <td  width="10%"><?php echo $ReportRow['to_location'] ?></td>
    <td  width="5%"><?php echo $ReportRow['travelmode'] ?></td>
    <td  width="10%"><?php echo $ReportRow['total_km'] ?></td>
    <td  width="10%"><?php echo $ReportRow['rate_unit'] ?></td>
    <td  width="10%"><?php echo $ReportRow['other_amt'] ?></td>
    <td  width="10%"><?php echo $ReportRow['total_amt'] ?></td>
    <td  width="10%"><?php echo $ReportRow['narration'] ?></td>
  </tr>
  <?php } ?>
  <tr>
  	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th style="text-align:right;">Total :</th>
	<th><?php
		$GrandTotal=DB_query("SELECT SUM(total_amt) AS TotalAmmount FROM crm_daily_visit_expense_entry",$db);
		$GrandTotalRow=DB_fetch_array($GrandTotal);
		echo $GrandTotalRow['TotalAmmount'];
	 ?></th>
	<th>&nbsp;</th>
	
  </tr>
</table>
	</form>
</div>
	</div>
 