<?php include('includes/Class_Client.php');
include('functions.php');
$BackDays=calculateBackDate($_SESSION['ACGRoleId']);
$days_ago = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - $BackDays, date("Y")));
?>
 <script language="javascript" type="text/javascript">
function caltotal(k)
{
		var TotalKM=document.getElementById("total_km_"+k).value;
		
 	    var Rate=document.getElementById("rate_unit_"+k).value;
 	    var OtherAmount=document.getElementById("other_amt_"+k).value;
 	    totalDiscounted=parseFloat(TotalKM)*parseFloat(Rate)+parseFloat(OtherAmount);
		document.getElementById("totalamt_"+k).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.style.background='#ffffff';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	//alert(newcell.childNodes[0].type);
	switch(newcell.childNodes[0].type) {
	case "text":
	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;	
	}
	}
	}
	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 <= 1) {
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	
	}
	}catch(e) {
	alert(e);
	}
}
 
    </script>
<?php 
if(isset($_REQUEST['Update']))
{
	//echo $days_ago;
	$visitDate1=FormatDateForSQL($_REQUEST['visit_date']);
	$date1_array = explode("/",$visitDate1);//ENTER DATE
	$date2_array = explode("-",$days_ago);//FIXDATE
	//print_r($date1_array);
	//print_r($date2_array);
	$timestamp1 = mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);
	$timestamp2 = mktime(0,0,0,$date2_array[1],$date2_array[2],$date2_array[0]); 
	if($timestamp2<=$timestamp1)
	{
	$cond="id='".$_REQUEST['VisitId']."'";
	$data_mst['visit_date']=ConvertSQLDate($_REQUEST['visit_date']);
	$data_mst['visited_area']=$_REQUEST['visited_area'];
	$rowupdate=$s->RecordEdit('crm_mst_daily_visit_entry', $data_mst, $cond);
	
	for($i=0;$i<count($_REQUEST['ClientContact']);$i++)
	{
	if($_REQUEST['client_id'][$i]=='new')
	{
	$data1['c_name']=$_REQUEST['crm_newclient'][$i];
	$data1['short_name']=$_REQUEST['crm_newclient'][$i];
	$data1['CompanyId']=$_SESSION['ACGCompanyId'];
	$data1['Site']=$_REQUEST['site'][$i];
	//echo "<pre>";
	//print_r($data1);
	//$data1['Zone']=$_SESSION['ACGCompanyId'];
	$data1['ledger_id']='111';
	$row1= $s->RecordInsert('clientmaster', $data1);
	$lastid1=DB_Last_Insert_ID($db,'clientmaster', 'id');
	$data2['vender_client']=$lastid1;
	$data2['AccountName']=$_REQUEST['crm_newclient'][$i];
	$data2['CompanyId']=$_SESSION['ACGCompanyId'];
	$data2['ledgertype']='client';
	$data2['UnderGroup']='111';
	$row1= $s->RecordInsert('accountledger', $data2);
	}
	
	if($_REQUEST['client_id'][$i]=='new')
	$data['client_id']=$lastid1;
	else
	$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['remark']=$_REQUEST['remark'][$i];
	$data['comment']=$_REQUEST['comment'][$i];
	$cond="mst_crm_entryid='".$_REQUEST['VisitId']."' and id='".$_REQUEST['fiedvisit'][$i]."'";
	$rowupdate=$s->RecordEdit('crm_daily_visit_entry', $data, $cond);
	}
	
	for($i=0;$i<count($_REQUEST['from_location']);$i++)
	{
	$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];
	$cond="mst_crm_entryid='".$_REQUEST['VisitId']."' and id='".$_REQUEST['EcpenceInfor'][$i]."'";
	$rowupdate=$s->RecordEdit('crm_daily_visit_expense_entry', $data_exp, $cond);
	}
	$s->pageLocation('index.php?route=CRM/field_visit_report&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	}
	else
	{
	echo "<script>alert('Back Dating Not Allowed')</script>";
	}
}

//echo $_REQUEST['VisitId'];
if($_REQUEST['action']=='Edit' && $_REQUEST['VisitId']!='')
{	
	//echo "select * from crm_daily_visit_entry where mst_crm_entryid='".$_REQUEST['VisitId']."'";
	$Report=DB_query("select * from crm_daily_visit_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_expense_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>EDIT DAILY FIELD MARKETING</h1>
<form method="post" action="">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<?php if(count($msgErr)>0){?>
<tr class="row1">
	<td class="tabledata1" colspan="6">
<?php for($i=0;$i<count($msgErr);$i++){
		echo "<div class='error'>".$msgErr[$i]."</div>";}?>
	</td>
</tr>
<?php }?>
<tr class="row1">
	<td class="tabledata1" 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"><input type="text" style="width:130px;" value="<?php echo ConvertSQLDate($report2Row['visit_date']); ?>" name="visit_date" id="visit_date" class="datepicker" /></td>
     <td class="tabledata1"><label>Employee Name</label></td>
    <td colspan="2"><?php echo $s->GetFieldValue('empmaster',"mst_www_userid='".$_SESSION['UserID']."'",'emp_name');?></td>
</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"><input type="text" style="width:130px;" name="visited_area" id="visited_area" value="<?php echo $report2Row['visited_area']; ?>" /></td>
<tr class="row1">
	<td class="tabledata1" 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>
			 <div align="center">
			   <table width="100%" cellspacing="0" cellpadding="3">
			     <tr>
			       <td class="tabledata">Quote</td>
				  <td class="tabledata">Sample</td>
				  <td class="tabledata" style="white-space:nowrap;">Call Again</td>
			    </tr>
		        </table>
		    </div></td>
    <td class="tabledata"><div align="center">New Client</div></td>
     <td width="20%" class="tabledata">Remark</td>
	 <td width="17%" class="tabledata">Comments</td>
  </tr>
<tbody  id="dataTable">
<?php $i=0;
while($Report1Row=DB_fetch_array($Report)){?>
<tr class="row1">
     <td style="">
	 <input type="hidden" name="fiedvisit[<?php echo  $i; ?>]" value="<?php echo $Report1Row['id'] ?>" />
	 <select style="width:120px"  name="client_id[<?php echo $i; ?>]"  onchange="ShowDiv(this.value,'hid-newclient_<?php echo $i; ?>','CRM_fieldvisitclient.php?q1=<?php echo $i; ?>')" >
		<option value="000">---</option>
		<option value="new" <?php echo  $_SESSION['crm_company_name']=='new' ?'selected':''?>>--New Client--</option>
		<?php
		  $ClientSQL="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site in('".$_SESSION['AssignedBranch']."') 
		and Zone in(".$_SESSION['AssignedSites'].")";
		$ClientRes=DB_query($ClientSQL,$db);
		while($ClientRow=DB_fetch_array($ClientRes))
		{
 		?>
		<option value="<?php echo $ClientRow['id']?>" <?php echo ($Report1Row['client_id']==$ClientRow['id']?'selected':'');?>><?php echo $ClientRow['c_name']?></option>
		<?php  } ?>
 	</select><div id='hid-newclient_<?php echo $i;?>'><input  name="crm_newclient[<?php echo $i; ?>]" type="text" style="width:120px;" value="<?php  echo $_SESSION['crm_newclient']?>"/> </div>
	<?php  //echo $Class_Client->Client('client_id[]',$Report1Row['client_id']);	?>
 </td>
    <td>
	<textarea style="width:auto; height:50px;" name="ClientContact[<?php echo $i; ?>]"><?php echo $Report1Row['ClientContact']; ?></textarea>
	 
	</td>
    <td>
		<table width="100%" border="0" cellpadding="3" cellspacing="0">
		  <tr>
			<td><center><input name="quotes[<?php echo $i; ?>]" type="checkbox" value="1"<?php echo($Report1Row['quotes']==1?'checked':''); ?>/></center></td>
			<td><center><input name="sample[<?php echo $i; ?>]" type="checkbox" value="1"<?php echo($Report1Row['sample']==1?'checked':''); ?> /></center></td>
			<td><center><input name="callagain[<?php echo $i; ?>]" type="checkbox" value="1"<?php echo($Report1Row['callagain']==1?'checked':''); ?> /></center></td>
		  </tr>
		</table>	</td>
    <td> <center><input name="newclient[]" type="checkbox" value="<?php $Report1Row['newclient'] ?>" <?php echo($Report1Row['newclient']==1?'checked':''); ?> /></center> </td>
    <td><textarea name="remark[]" style="width:auto; height:60px;"><?php echo $Report1Row['remark'] ?></textarea></td>
	<td><textarea name="comment[]" style="width:140px; height:60px;"><?php echo $Report1Row['comment'] ?></textarea></td>
  </tr>
   <?php $i++;} ?>
  </tbody>

<tr class="row1">
	<td class="tabledata1" colspan="6"><h3>Expense Information</h3></td>
</tr>
</table>
<!--Table 1 Start -->
<!--<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>
  <td class="tabledata"></td>
    <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>
<tbody>
<?php  $j=0;
while($Report2Row=DB_fetch_array($Report1)){?>
<tr class="row1">
    <td><INPUT type="checkbox" name="chk"/>
	<input type="hidden" name="EcpenceInfor[<?php echo  $j; ?>]" value="<?php echo $Report2Row['id'] ?>" />
    <td  width="15%"><input style="width:95px;" type="text" name="from_location[<?php echo $j; ?>]" value="<?php echo $Report2Row['from_location'] ?>"  /></td>
    <td  width="10%"><input style="width:95px;" type="text" name="to_location[<?php echo $j; ?>]" value="<?php echo $Report2Row['to_location'] ?>" /></td>
    <td  width="5%">
	<select  name="travelmode[]"  style="width:100px">
	<option style="width:95px;" value="bus"<?php echo($Report2Row['travelmode']=='bus'?'selected':'') ?>>BY Bus</option>
	<option style="width:95px;" value="Bike" <?php echo($Report2Row['travelmode']=='Bike'?'selected':'') ?>>BY Bike</option>
	<option style="width:95px;" value="Rail" <?php echo($Report2Row['travelmode']=='Rail'?'selected':'') ?>>BY Rail</option>
	<option style="width:95px;" value="Air" <?php echo($Report2Row['travelmode']=='Air'?'selected':'') ?>>BY Air</option>
	<option style="width:95px;" value="other" <?php echo($Report2Row['travelmode']=='other'?'selected':'') ?>>Other</option>
 	</select>
	 </td>
    <td  width="10%"><input style="width:95px;" type="text" name="total_km[<?php echo $j; ?>]" id="total_km_<?php echo $j; ?>" value="<?php echo $Report2Row['total_km'] ?>" /></td>
    <td  width="10%"><input style="width:95px;" type="text" name="rate_unit[<?php echo $j; ?>]" id="rate_unit_<?php echo $j; ?>" value="<?php echo $Report2Row['rate_unit'] ?>" /></td>
    <td  width="10%"><input style="width:95px;" type="text" name="other_amt[<?php echo $j; ?>]"  id="other_amt_<?php echo $j; ?>" value="<?php echo $Report2Row['other_amt'] ?>"/></td>
    <td  width="10%"><input style="width:95px;" type="text" name="total_amt[<?php echo $j; ?>]" id="totalamt_<?php echo $j; ?>" onfocus="caltotal(<?php echo $j; ?>)" value="<?php echo $Report2Row['total_amt'] ?>" /></td>
    <td  width="10%"> <input style="width:95px;" type="text" name="narration[<?php echo $j; ?>]" id="narration_<?php echo $j; ?>" value="<?php echo $Report2Row['narration'] ?>" /></td>
  </tr>
 
  
  <?php $j++; } ?>
  </tbody>
</table>
	<br />
<div align="center">
		<input name="Update" type="submit" value="Save" class="bottonsview"  onclick="return validatedata();" />&nbsp;&nbsp;&nbsp;		</div>
</form>
</div>
	</div>
 