<?php 
//******************************INSERT THE NEW DATA************START HERE************AMIT**********10-10-12**********************

if($_REQUEST['Proceed'])
{
	if($_REQUEST['client']=='new')
	{
	$data1['c_name']=$_REQUEST['crm_newclient'];
	$data1['short_name']=$_REQUEST['crm_newclient'];
	$data1['CompanyId']=$_SESSION['ACGCompanyId'];
	$data1['Site']=$_REQUEST['site'];
	//  $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'];
	$data2['CompanyId']=$_SESSION['ACGCompanyId'];
	$data2['ledgertype']='client';
	$data2['UnderGroup']='111';
	$row1= $s->RecordInsert('accountledger', $data2);
	}
	if($_REQUEST['client']=='new')
    $datarray['client']= $lastid1;
	else
	
	$site=$_REQUEST['site'];
	$qry=DB_query("select * from crm_dot_request_form where site='".$site."'",$db);
$NoRows=DB_num_rows($qry);

if($NoRows>0)
{
	$sel_code=DB_query("select max(form_no) AS max_id from crm_dot_request_form where site='".$site."'",$db);
	$sel_codeRow=DB_fetch_array($sel_code);
	$sub_code=substr($sel_codeRow['max_id'],-2);
	$maxid=$sub_code+1;
	$finalCode=$site."/".'SAM'."/".str_pad($maxid,4,"0",STR_PAD_LEFT);
}
else
{
	$finalCode=$site."/".'SAM'."/".'0001';
}
	
	
	
 	$datarray['client']=$_REQUEST['client'];
	$datarray['site']=$_REQUEST['site'];
	$datarray['form_no']=$finalCode;
	$datarray['form_date']=FormatDateForSQL($_REQUEST['form_date']);
	$datarray['executive']=$_REQUEST['executive'];
	$datarray['item_code']=$_REQUEST['item_code'];
	if($_REQUEST['shade_name']=='new')
	$datarray['shade_name']=$_REQUEST['crm_newshade'];
	else
	$datarray['shade_name']=$_REQUEST['shade_name'];	
	$datarray['dot_in_date']=FormatDateForSQL($_REQUEST['dot_in_date']);
	$datarray['dot_out_date']=FormatDateForSQL($_REQUEST['dot_out_date']);
	$datarray['status']=$_REQUEST['status'];
	$datarray['remark_by_exec']=$_REQUEST['remark_by_exec'];
	$datarray['additional_remark']=$_REQUEST['additional_remark'];
	$saved= $s->RecordInsert('crm_dot_request_form', $datarray);
	$s->pageLocation("index.php?route=CRM/view_dot_request_form".$rurl);
}

//******************************INSERT THE NEW DATA************END HERE************AMIT************10-10-12********************




//******************************EDIT PART************START HERE************AMIT************10-10-12********************

if($_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$GetRec=$s->GetRecordById('crm_dot_request_form',  $condition);
	$GetRec1=DB_fetch_array($GetRec);
	extract($GetRec1);
}

if($_REQUEST['Update'] && $_REQUEST['action']=='Edit')
{
	if($_REQUEST['client']=='new')
	{
	$data1['c_name']=$_REQUEST['crm_newclient'];
	$data1['short_name']=$_REQUEST['crm_newclient'];
	$data1['CompanyId']=$_SESSION['ACGCompanyId'];
	$data1['Site']=$_REQUEST['site'];
	//  $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'];
	$data2['CompanyId']=$_SESSION['ACGCompanyId'];
	$data2['ledgertype']='client';
	$data2['UnderGroup']='111';
	$row1= $s->RecordInsert('accountledger', $data2);
	}
	if($_REQUEST['client']=='new')
    $datarray['client']= $lastid1;
	else
 	$datarray['client']=$_REQUEST['client'];
	$datarray['site']=$_REQUEST['site'];
	$datarray['form_no']=$_REQUEST['form_no'];
	$datarray['form_date']=FormatDateForSQL($_REQUEST['form_date']);
	$datarray['executive']=$_REQUEST['executive'];
	$datarray['item_code']=$_REQUEST['item_code'];
	if($_REQUEST['shade_name']=='new')
	$datarray['shade_name']=$_REQUEST['crm_newshade'];
	else
	$datarray['shade_name']=$_REQUEST['shade_name'];
	$datarray['dot_in_date']=FormatDateForSQL($_REQUEST['dot_in_date']);
	$datarray['dot_out_date']=FormatDateForSQL($_REQUEST['dot_out_date']);
	$datarray['status']=$_REQUEST['status'];
	$datarray['remark_by_exec']=$_REQUEST['remark_by_exec'];
	$datarray['additional_remark']=$_REQUEST['additional_remark'];
	$cond="id='".$_REQUEST['id']."'";
	$saved= $s->RecordEdit('crm_dot_request_form', $datarray, $cond);
	$s->pageLocation("index.php?route=CRM/view_dot_request_form".$rurl);
}




//******************************EDIT PART************END HERE************AMIT************10-10-12********************




//*******************************VIEW PART***************START HERE****************AMIT*********10-10-12*************

if($_REQUEST['action']=='View')
{
	$condition="id='".$_REQUEST['id']."'";
	$Rec_For_View=$s->GetRecordById('crm_dot_request_form', $condition);
	$Rec12=DB_fetch_array($Rec_For_View);
	extract($Rec12);
}

//*******************************VIEW PART***************END HERE****************AMIT*********10-10-12*************





?>

<!-----------------------AJAX FILES----------------------------START HERE------AMITSINGH--------------------------------->

<script type="text/javascript" language="javascript">
function autogeneratecode(str)
{
	//alert(str);
	if(str.length==0)
	{
	document.getElementById("autono").innerHTML="";
	return;
	}
	if(window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	document.getElementById("autono").innerHTML=xmlhttp.responseText;
	}
	xmlhttp.open("GET","auto_generate_no.php?AUTO_NO="+str,true);
	xmlhttp.send();
}


function getitemcode(str)
{
	
	if(str.length==0)
	{
	document.getElementById("ITEMCODE").innerHTML="";
	return;
	}
	if(str=='new')
	{
	document.getElementById("hid-newclient1").style.display='block';	
	}
	else
	{
	document.getElementById("hid-newclient1").style.display='none';	
	}
	if(window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	document.getElementById("ITEMCODE").innerHTML=xmlhttp.responseText;
	}
	xmlhttp.open("GET","getitemcodeforDRF.php?shadecode="+str,true);
	xmlhttp.send();
 
}



<!-----------------------AJAX FILES----------------------------END HERE------AMITSINGH--------------------------------->



<!-----------------------VALIDATION----------------------------START HERE------AMITSINGH--------------------------------->

function validate_field()
{
	if(document.getElementById("site").value=='0')
	{
		alert("Please Select The Site First");
		return false;
	}
	if(document.getElementById("form_date").value=='')
	{
		alert("Please Enter The From Date First");
		return false;
	}
	if(document.getElementById("client").value=='new')
	{
		alert("Please Enter The Name Of New Client First");
		return false;
	}
	
	if(document.getElementById("client").value=='0')
	{
		alert("Please Select The client First");
		return false;
	}
	
	if(document.getElementById("executive").value=='0')
	{
		alert("Please Select The Executive First");
		return false;
	}
	if(document.getElementById("item_code").value=='0')
	{
		alert("Please Select The Item Code First");
		return false;
	}
	if(document.getElementById("shade_name").value=='0')
	{
		alert("Please Select The Item Shade Name First");
		return false;
	}
	if(document.getElementById("dot_in_date").value=='')
	{
		alert("Please Enter The Dot In Date First");
		return false;
	}
	if(document.getElementById("dot_out_date").value=='')
	{
		alert("Please Enter The Dot Out Date First");
		return false;
	}
}

<!-----------------------VALIDATION----------------------------END HERE------AMITSINGH--------------------------------->


</script>



<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']=='View') {?>
<h1>View Dot Request Form</h1>
  	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Branch</td>
  	    <td><? echo $s->GetFieldValue('locations',"loccode='".$site."'",'locationname'); ?></td>
  	    <td class="tabledata1">&nbsp;</td>
  	    <td>&nbsp;</td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Form No.</td>
  	    <td id="autono"><? echo $form_no ?></td>
  	    <td class="tabledata1">Form Date</td>
  	    <td><? echo ConvertSQLDate($form_date);?></td>
		</tr>
	  <tr class="row1">
  	    <td class="tabledata1" align="left">Client Name</td>
  	    <td><?php echo $s->GetFieldValue('clientmaster',"id='".$client."'",'c_name'); ?></td>
  	    <td class="tabledata1">Executive Name</td>
  	    <td><?php echo $s->GetFieldValue('www_users',"id='".$executive."'",'userid'); ?></td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Item Code</td>
  	    <td><? echo $s->GetFieldValue('item_master',"id='".$item_code."'",'StockID'); ?></td>
  	    <td class="tabledata1">Shade Name</td>
  	    <td><? if(is_int($shade_name)){ echo $s->GetFieldValue('item_master',"id='".$shade_name."'",'item_name');} else { echo $shade_name; } ?></td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Dot In Date</td>
	    <td><? echo ConvertSQLDate($dot_in_date); ?></td>
  	    <td class="tabledata1">Dot Out Date</td>
	    <td><? echo ConvertSQLDate($dot_out_date); ?></td>
		</tr>
		
	 <tr class="row1">
  	    <td class="tabledata1" align="left">Status</td>
	    <td><?php echo $status; ?></td>
  	    <td class="tabledata1">&nbsp;</td>
	    <td>&nbsp;</td>
		</tr>		
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Remark by Executive</td>
  	    <td colspan="3">
			<? echo $remark_by_exec; ?>
		</td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Additional Remark</td>
  	    <td colspan="3">
			<? echo $additional_remark; ?>
		</td>
		</tr>
    </table>
<?php }else {?>
<h1>Dot Request Form</h1>
<form name="" action="" method="post">
  	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Branch<font color="#FF0000">*</font></td>
  	    <td><select name="site" id="site" style="width: 180px;" >
	<option value="0">Select</option>
	<?php     
	$site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
	while($site_row=DB_fetch_array($site_sql))
	{?>
	<option value="<?php echo $site_row['loccode']?>"<?php echo($site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname']?></option>
	<?php 	
	}
	?>
	</select></td>
  	    <td class="tabledata1">&nbsp;</td>
  	    <td>&nbsp;</td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Form No.<font color="#FF0000">*</font></td>
  	    <td id="autono"><input type="text" name="form_no" id="form_no" value="<? if($_REQUEST['action']=='Edit'){echo $form_no; }else{echo 'Auto Generated';}?>" readonly="" style="width:180px;" /></td>
  	    <td class="tabledata1">Form Date<font color="#FF0000">*</font></td>
  	    <td><input type="text" name="form_date" id="form_date" value="<? if($_REQUEST['action']=='Edit'){ echo ConvertSQLDate($form_date);}else{echo date('d/m/Y');}?>" class="datepicker" style="width:180px;" /></td>
		</tr>
  	
	  <tr class="row1">
  	    <td class="tabledata1" align="left">Client Name<font color="#FF0000">*</font></td>
  	    <td><select style="width:180px;"  name="client" id="client"  onchange="ShowDiv(this.value,'hid-newclient','CRM_leadclient.php')" >
		<option value="0">---</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 ($client==$ClientRow['id']?'selected':'');?>><?php echo $ClientRow['c_name']?></option>
		<?php  } ?>
 	</select>
	<div id='hid-newclient'><input style="width:180px;" name="crm_newclient" type="text" value="<?php  echo $_SESSION['crm_newclient']?>"/> </div></td>
  	    <td class="tabledata1">Executive Name<font color="#FF0000">*</font></td>
  	    <td><?php 
						$Dept=DB_query("SELECT id FROM deptmaster WHERE deptcode='MKT' OR deptcode='sal'",$db);
						if(DB_num_rows($Dept)>0)
						{
	while($DeptRow=DB_fetch_array($Dept))
	{
		$DCode[]=$DeptRow['id'];
	}
	 $Dept_id=implode(',',$DCode);
							$Sale_key_Exe=DB_query("select * from www_users where companyid='".$_SESSION['ACGCompanyId']."' and department in($Dept_id)",$db);
							
						?>
						<select name="executive" id="executive" style="width:180px;;">
						<option value="0">Select</option>
						<?php while($Sale_keyRow=DB_fetch_array($Sale_key_Exe))
						{
						 ?>
						 <option value="<?php echo $Sale_keyRow['id'] ?>"<?php echo ($executive==$Sale_keyRow['id']?'selected':'')?>><?php echo $Sale_keyRow['userid']; ?></option>
						 <?php } }?>
						</select></td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Item Code<font color="#FF0000">*</font></td>
  	    <td id="ITEMCODE"><select name="item_code" id="item_code" style="width:180px;">
  	      		<option value="0">Select</option>
				<?php $itemcode=DB_query("Select id,StockID,item_name from item_master ",$db); 
					  while($itemcodeRow=DB_fetch_array($itemcode)){
					  $sel='';
					  if($item_code==$itemcodeRow['id'])
					  $sel='selected';
					  ?>
				<option value="<? echo $itemcodeRow['id']; ?>"<?php echo $sel; ?>><? echo $itemcodeRow['StockID']; ?></option>
				<?php } ?>
	      </select></td>
  	    <td class="tabledata1">Shade Name<font color="#FF0000">*</font></td>
  	    <td><select name="shade_name" id="shade_name" style="width:180px;" onchange="getitemcode(this.value)">
  	      		<option value="0">Select</option>
  	      		<option value="new" <?php echo  $_SESSION['crm_company_name']=='new' ?'selected':''?>>--New Shade--</option>
  	      					<?php $itemname=DB_query("Select id,StockID,item_name from item_master ",$db); 
					  while($itemnameRow=DB_fetch_array($itemname)){
					  $selc='';
					  if($shade_name==$itemnameRow['id'])
					  $selc='selected';
					  ?>
				<option value="<? echo $itemnameRow['id']; ?>"<?php echo $selc; ?>><? echo $itemnameRow['item_name']; ?></option>
				<?php } ?>
	      </select>
	      <div id='hid-newclient1' style="display: none;"   ><input style="width:180px;" name="crm_newshade" type="text" value="<?php  echo $_SESSION['crm_newclient']?>"/> </div>
	      </td>
		</tr>

  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Dot In Date<font color="#FF0000">*</font></td>
	    <td><input type="text" name="dot_in_date" id="dot_in_date" value="<? if($_REQUEST['action']=='Edit'){echo ConvertSQLDate($dot_in_date);}else{echo date('d/m/Y');} ?>" class="datepicker" style="width:180px;" /></td>
  	    <td class="tabledata1">Dot Out Date<font color="#FF0000">*</font></td>
	    <td><input type="text" name="dot_out_date" id="dot_out_date" value="<? if($_REQUEST['action']=='Edit'){echo ConvertSQLDate($dot_out_date);}else{echo date('d/m/Y');} ?>" class="datepicker" style="width:180px;" /></td>
		</tr>

	 <tr class="row1">
  	    <td class="tabledata1" align="left">Status</td>
	    <td><select name="status" id="status" style="width:180px;">
		<option id="Approved"<?php echo($status=='Approved'?'selected':''); ?>>Approved</option>
		<option id="Unapproved"<?php echo($status=='Unapproved'?'selected':''); ?>>Unapproved</option>
		</select>
		</td>
  	    <td class="tabledata1">&nbsp;</td>
	    <td>&nbsp;</td>
		</tr>		
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Remark by Executive</td>
  	    <td colspan="3">
			<textarea name="remark_by_exec" style="width:98%"><? echo $remark_by_exec; ?></textarea>
		</td>
		</tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Additional Remark</td>
  	    <td colspan="3">
			<textarea name="additional_remark" style="width:98%"><? echo $additional_remark; ?></textarea>
		</td>
		</tr>
    </table>
<p align="center"><input type="submit" name="<? if($_REQUEST['action']=='Edit'){echo 'Update'; }else{echo 'Proceed';}?>" id="button" value="<? if($_REQUEST['action']=='Edit'){echo 'Update'; }else{echo 'Save';}?>" onclick="return validate_field();" /></p>
</form>
<?php } ?>
</div></div>