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

if($_REQUEST['Proceed'])
{
	$images=$_FILES['panel_img']['name'];
/*	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 approval_dot_and_panel_entry where site='".$site."'",$db);
$NoRows=DB_num_rows($qry);*/

//if($NoRows>0)
//{
//	$sel_code=DB_query("select max(form_no) AS max_id from approval_dot_and_panel_entry 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."/".'ADPE'."/".str_pad($maxid,4,"0",STR_PAD_LEFT);
//}
//else
//{
//	$finalCode=$site."/".'ADPE'."/".'0001';
//}
	
	
 	//$datarray['client_id']=$_REQUEST['client'];
	$datarray['parant_id']=$_REQUEST['id'];
	$datarray['site']=$_REQUEST['site'];
	$datarray['form_no']=$_REQUEST['from_no'];
	$datarray['status']=$_REQUEST['status'];
	$datarray['panel_in_date']=FormatDateForSQL($_REQUEST['panel_in_date']);
	$datarray['panel_out_date']=FormatDateForSQL($_REQUEST['panel_out_date']);
	$datarray['panel_image']=$images;
	$datarray['remark']=$_REQUEST['remark'];
	$datarray['request_date']=FormatDateForSQL($_REQUEST['request_date']);
	$datarray['out_date']=FormatDateForSQL($_REQUEST['out_date']);
	$datarray['remark_by_rd']=$_REQUEST['reamrk_by_rd'];


	$saved= $s->RecordInsert('pending_request_to_approval', $datarray);
	if($images!='')
	{
		$upload=move_uploaded_file($_FILES['panel_img']['tmp_name'],"CRM/pendingreq/".$images);
	}
	
$update=DB_query("update approval_dot_and_panel_entry set status='".$_REQUEST['status']."' where id='".$_REQUEST['id']."'",$db);
	
	$s->pageLocation("index.php?route=CRM/factory_detail_view".$rurl);
}

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




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

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

if($_REQUEST['Update'] && $_REQUEST['action']=='Edit')
{

	if($_FILES['panel_img']['name']=='')
	{
		$images=$_REQUEST['panel_img1'];
	}
	else
	{
		$image=DB_query("select panel_image from approval_dot_and_panel_entry where id='".$_REQUEST['id']."'",$db);
		$ImageName=DB_fetch_array($image);
		@unlink("CRM/ApprovalDotEntryForm/".$ImageName['panel_image']);
		$images=$_FILES['panel_img']['name'];
		$upload=move_uploaded_file($_FILES['panel_img']['tmp_name'],"CRM/ApprovalDotEntryForm/".$images);
	}
	

	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_id']=$_REQUEST['client'];
	$datarray['site']=$_REQUEST['site'];
	$datarray['document_date']=FormatDateForSQL($_REQUEST['document_date']);
	$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['panel']=$_REQUEST['panel'];	
	$datarray['panel_image']=$images;
	$datarray['approval_dot']=$_REQUEST['approval_dot'];	
	$datarray['panel_in_date']=FormatDateForSQL($_REQUEST['panel_in_date']);
	$datarray['panel_out_date']=FormatDateForSQL($_REQUEST['panel_out_date']);
	$datarray['remark']=$_REQUEST['remark'];
	$datarray['request_date']=FormatDateForSQL($_REQUEST['request_date']);
	$datarray['in_date']=FormatDateForSQL($_REQUEST['in_date']);
	$datarray['out_date']=FormatDateForSQL($_REQUEST['out_date']);
	$datarray['status']=$_REQUEST['status'];
	$datarray['reamrk_by_executive']=$_REQUEST['reamrk_by_executive'];
	$datarray['remark_by_cmmercial']=$_REQUEST['remark_by_cmmercial'];
	$cond="id='".$_REQUEST['id']."'";
	

	
	
	$saved= $s->RecordEdit('approval_dot_and_panel_entry', $datarray, $cond);
	$s->pageLocation("index.php?route=CRM/view_approval_dot_panel_entry_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('approval_dot_and_panel_entry', $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 Getformno(str)
{
	//alert(str);
	var xmlhttp;
	if(str=='')
	{
	document.getElementById("FORMNO").innerHTML='';
	return false;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("FORMNO").innerHTML=xmlhttp.responseText
	}
	}
	xmlhttp.open("GET","GetFormNO.php?q="+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 Approval Dot and Panel Entry 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">Document Date</td>
  	    <td><? echo ConvertSQLDate($document_date);?></td>
	  </tr>
	  <tr class="row1">
  	    <td class="tabledata1" align="left">Client Name</td>
  	    <td><?php echo $s->GetFieldValue('clientmaster',"id='".$client_id."'",'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">Shade Name</td>
  	    <td><? if(is_int($shade_name)){ echo $s->GetFieldValue('item_master',"id='".$shade_name."'",'item_name');} else { echo $shade_name; } ?></td>
  	    <td class="tabledata1" align="left">Item Code</td>
  	    <td><? echo $s->GetFieldValue('item_master',"id='".$item_code."'",'StockID'); ?></td>
	  </tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Panel</td>
	    <td><input type="checkbox" name="panel" id="panel" value="panel"<?php echo($panel=='panel'?'checked':''); ?>></td>
  	    <td class="tabledata1">Approve Dot</td>
	    <td><input type="checkbox" name="approval_dot" id="approval_dot" value="approval_dot"<?php echo($approval_dot=='approvalDot'?'checked':''); ?>></td>
	  </tr>
	 <tr class="row1">
  	   <td colspan="4">
	   <table width="100%" border="0" cellspacing="0" cellpadding="0">
	   	<tr class="row1">
			<td class="tabledata1" colspan="4"><strong>Panel Details</strong></td>
			</tr>
		<tr class="row1">
			<td class="tabledata1">Panel In Date</td>
			<td><?php echo ConvertSQLDate($panel_in_date); ?></td>
			<td class="tabledata1">Panel Out Date</td>
			<td><?php echo ConvertSQLDate($panel_out_date); ?></td>
		</tr>
		<tr class="row1">
			<td class="tabledata1">Panel Images</td>
			<td><?php echo '<img src="CRM/ApprovalDotEntryForm/'.$panel_image.'" style=" border:2px solid grey;width:100px;"/>'; ?></td>
		</tr>
		<tr class="row1">
			<td class="tabledata1">Remark</td>
			<td><?php echo $remark; ?></td>
		</tr>
	   </table>
	   </td>
	  </tr>	
	  <tr class="row1">
  	   <td colspan="4">
	   <table width="100%" border="0" cellspacing="0" cellpadding="0">
	   	<tr class="row1">
			<td class="tabledata1" colspan="4"><strong>Approval Dot Details</strong></td>
			</tr>
		<tr class="row1">
			<td class="tabledata1">Request Date</td>
			<td><?php echo ConvertSQLDate($request_date); ?></td>
			<td class="tabledata1">In Date</td>
			<td><?php echo ConvertSQLDate($in_date); ?></td>
		</tr>
		<tr class="row1">
			<td class="tabledata1">Out Date</td>
			<td><?php echo ConvertSQLDate($out_date); ?></td>
			<td class="tabledata1">Status</td>
			<td><?php echo $status; ?></td>
		</tr>
		  <tr class="row1">
  	    <td class="tabledata1" align="left">Remark by Executive</td>
  	    <td colspan="3">
			<? echo $reamrk_by_executive; ?>
		</td>
	  </tr>
  	  <tr class="row1">
  	    <td class="tabledata1" align="left">Remark By Commericial</td>
  	    <td colspan="3">
			<? echo $remark_by_cmmercial; ?>
		</td>
	  </tr>
	   </table>
	   </td>
	  </tr>
    </table>
<?php }else {?>
<h1>Pending Request To Approval Dot </h1>
<form name="" method="post" action="" enctype="multipart/form-data">
  	<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;" onchange="Getformno(this.value)">
	<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" align="left">Form No.<font color="#FF0000">*</font></td>
  	    <td id="FORMNO"><input type="text" name="from_no" id="from_no" value="<?php echo $form_no ?>" style="width:180px;" /></td>
		</tr>
		
		
		<tr class="row1">
		<td class="tabledata1">Status</td>
			<td><select name="status" id="status" style="width:180px;">
					<option value="0">Select Staus</option>
					<option value="approved"<?php echo($status=='approved'?'selected':''); ?>>approved</option>
					<option value="unapproved"<?php echo($status=='unapproved'?'selected':''); ?>>Unapproved</option>
					<option value="requesttoad"<?php echo($status=='requesttoad'?'selected':''); ?>>Request To AD</option>
					<option value="forfutureReference"<?php echo($status=='forfutureReference'?'selected':''); ?>>For Future Reference</option>
					<option value="dispatchtoad"<?php echo($status=='dispatchtoad'?'selected':''); ?>>Dispatch To HO</option>
					<option value="underdevelopment"<?php echo($status=='underdevelopment'?'selected':''); ?>>Under Development</option>
					<option value="sentforapproval"<?php echo($status=='sentforapproval'?'selected':''); ?>>Sent For Approval</option>
					<option value="2ndtimerequest"<?php echo($status=='2ndtimerequest'?'selected':''); ?>>2nd Time Request</option>
					<option value="resend"<?php echo($status=='resend'?'selected':''); ?>>Resend</option>
				</select>
			</td>
		</tr>
		
		
		
  	  <tr class="row1">
  	    <td colspan="4" class="tabledata1"><strong>
			By Factory		</strong></td>
		</tr>
	 <tr class="row1">
	 <td colspan="4">
  	    <table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr class="row1">
			<td class="tabledata1" colspan="4"><strong>Panel Out Details</strong></td>
			</tr>
			<tr class="row1">
			<td class="tabledata1" width="19%">Panel In Date</td>
			<td  width="31%"><input type="text" name="panel_in_date" class="datepicker" value="" style="width:180px;"></td>
			<td class="tabledata1" width="17%">Panel Out Date</td>
			<td width="33%"><input type="text" name="panel_out_date" class="datepicker" value="" style="width:180px;"></td>
			</tr>
			<tr class="row1">
			<td class="tabledata1">Panel Image</td>
			<td><input type="file" name="panel_img" style="width:180px;"  value="">
			<input type="hidden" name="panel_img1" value="">
			</td>
			</tr>
			 <tr class="row1">
  	    <td class="tabledata1">Remark</td>
  	    <td colspan="3">
			<textarea name="remark" style="width:83%"></textarea>
		</td>
		</tr>
		</table>
		</td>
		</tr>
		 <tr class="row1">
  	    <td colspan="4" class="tabledata1"><strong>
			By Office		</strong></td>
		</tr>
		 <tr class="row1">
	 <td colspan="4">
  	    <table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr class="row1">
			<td class="tabledata1" colspan="4"><strong>Approval Dot Details</strong></td>
			</tr>
			<tr class="row1">
			<td class="tabledata1" width="19%">Request Date</td>
			<td  width="31%"><input type="text" name="request_date" class="datepicker" value="" style="width:180px;"></td>
			<td class="tabledata1">Out Date</td>
			<td><input type="text" name="out_date" class="datepicker" value="" style="width:180px;"></td>
			</tr>
			 <tr class="row1">
  	    <td class="tabledata1">Remark by R&D</td>
  	    <td colspan="3">
			<textarea name="reamrk_by_rd" style="width:83%"></textarea>
		</td>
		</tr>
		</table>
		</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>