<?php
if($_REQUEST['Submit'])
{
	$SQLMax=DB_query("SELECT form_no FROM dbl_sale_return order by id DESC limit 0,1",$db);
	$MaxQut=DB_fetch_array($SQLMax);
	$num=DB_num_rows($SQLMax);
	if($num>0)
	{
	$explode=explode("/",$MaxQut['form_no']);
	$FinMx=$explode[1]+1;
	$FinalCode="SRF"."/".str_pad($FinMx,5,0,STR_PAD_LEFT);
	}
	else
	{
	$FinalCode="SRF"."/".'00001';
	}										 
	$data['form_no']=$FinalCode;
	$data['form_date']=FormatDateFORSQL($_REQUEST['form_date']);
	$data['return_typep']=$_REQUEST['return_typep'];
	$data['customer']=$_REQUEST['customer'];
	$data['pln']=$_REQUEST['pln'];
	$data['pln_date']=FormatDateFORSQL($_REQUEST['pln_date']);
	$data['area']=$_REQUEST['area'];
	$data['sub_area']=$_REQUEST['sub_area'];
	$data['engineer']=$_REQUEST['engineer'];
	$data['soi_no']=$_REQUEST['soi_no'];
	$data['soi_date']=FormatDateFORSQL($_REQUEST['soi_date']);
	$data['po_no']=$_REQUEST['po_no'];
	$data['mistake_type']=$_REQUEST['mistake_type'];
	$data['responsible']=$_REQUEST['responsible'];
	$data['Inc_Reco']=$_REQUEST['Inc_Reco'];
	$data['resto_chrg']=$_REQUEST['resto_chrg'];
	$data['Excise']=$_REQUEST['Excise'];
	$data['party_ref_no']=$_REQUEST['party_ref_no'];
	$data['saletype']=$_REQUEST['saletype'];
	$data['dealer']=$_REQUEST['dealer1'];
	$data['company_id']=$_SESSION['ACGCompanyId'];
	$data['created_by']=$_SESSION['UserID'];
	$data['created_date']=date('Y-m-d');
	//echo "<pre>";
	//print_r($data);
	$s->RecordInsert('dbl_sale_return', $data);
	$lastid=DB_Last_Insert_ID($db,'dbl_sale_return', 'id');
	
	for($i=0;$i<count($_REQUEST['mst_itemid']);$i++)
	{
		$dataArr['parant_id']=$lastid;
		$dataArr['mst_itemid']=$_REQUEST['mst_itemid'][$i];
		$dataArr['invoice_qty']=$_REQUEST['invoice_qty'][$i];
		$dataArr['return_qty']=$_REQUEST['return_qty'][$i];
		$dataArr['accepted_qty']=$_REQUEST['accepted_qty'][$i];
		$dataArr['rate']=$_REQUEST['rate'][$i];
		$dataArr['dblstore']=$_REQUEST['dblstore'][$i];
		$dataArr['return_reason']=$_REQUEST['return_reason'][$i];
		$dataArr['return_type']=$_REQUEST['return_type'][$i];
		//echo "<pre>";
		//print_r($dataArr);
		$s->RecordInsert('dbl_sale_return_details', $dataArr);
	}
	
	$s->pageLocation("index.php?route=DealerDistrbutions/Transaction/sales_return_index".$rurl);
}

if(!empty($_SESSION['dealerid']))
$SQLDealer=DB_query("select * from dbl_dealer where id='".$_SESSION['dealerid']."'",$db);
else
$SQLDealer=DB_query("select * from dbl_dealer where company='".$_SESSION['ACGCompanyId']."'",$db);
$DealerName=DB_fetch_array($SQLDealer);
extract($DealerName);


?>
<script type="text/javascript" language="javascript">
function getpicklist(str)
{
	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("pln").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","GetPicklistno.php?custID="+str,true);
	xmlhttp.send();
}



function Getpicklistdata(str)
{
	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		var data=xmlhttp.responseText.split("__");
		document.getElementById("soi_noS").innerHTML=data[0];
		document.getElementById("soi_dates").innerHTML=data[1];
		document.getElementById("AllDetaildata").innerHTML=data[2];
	}
	}
	xmlhttp.open("GET","GetPicklistData.php?PickListId="+str,true);
	xmlhttp.send();
}

function chkretutnqty(val,id)
{
	var n=id.split("_");
	var i=n[2];
	var invoiceQty=parseFloat(document.getElementById("invoice_qty_"+i).value);
	var returnQty=parseFloat(document.getElementById("return_qty_"+i).value);
	if(returnQty>invoiceQty)
	{
		alert("Return Qty Always Less Than Invoice Qty");
		document.getElementById("return_qty_"+i).focus();
		document.getElementById("return_qty_"+i).value='';
		return false;
	}
}


function chkacceptedqty(val,id)
{
	var n=id.split("_");
	var i=n[2];
	var returnQty=parseFloat(document.getElementById("return_qty_"+i).value);
	var acceptedQty=parseFloat(document.getElementById("accepted_qty_"+i).value);
	
	if(returnQty<acceptedQty)
	{
		alert("Accepted Qty Always Less Than Return Qty");
		document.getElementById("accepted_qty_"+i).focus();
		document.getElementById("accepted_qty_"+i).value='';
		return false;
	}
}


</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Sales Return Form</h1>
<form name="form1" action="" method="post" >
<input type="hidden" name="dealer1" value="<?php echo $id; ?>" />
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
    <td class="tabledata1"><strong>Form No.</strong></td>
    <td><input name="form_no" id="form_no" type="text" style="width:150px;" value="Auto Genrated" /></td>
    <td class="tabledata1"><strong>Form Date</strong></td>
    <td><input name="form_date" id="form_date" type="text" style="width:150px;" class="datepicker" value="" /></td>
    <td class="tabledata1"><strong>Return Type</strong></td>
    <td><select style="width:150px;" name="return_typep" id="return_typep">
			<option>--Select--</option>
    		<option value="Replacement">Replacement</option>
			<option value="Rejection">Rejection</option>
    	</select></td>
</tr>
<tr class="row1"> <td class="tabledata1"><strong>Customer Name</strong></td>
    <td>
	<select style="width:150px;" name="customer" id="customer" onchange="getpicklist(this.value)">
		<option value="">--Select--</option>
		<?php 
		if(!empty($_SESSION['dealerid']))
		{
		$SQLC=DB_query("SELECT * FROM clientmaster where dealer='".$_SESSION['dealerid']."'",$db);
		}
		else
		{
		$SQLC=DB_query("SELECT * FROM clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
		}
		?>
		
		<?php while($Customer=DB_fetch_array($SQLC)){?>
		<option value="<?php echo $Customer['id']; ?>"><?php echo $Customer['c_name']; ?></option>
		<?php } ?>
    </select>	
	</td>
    <td class="tabledata1"><strong>Pick List No</strong></td>
    <td><select style="width:150px;" name="pln" id="pln" onchange="Getpicklistdata(this.value)">
			<option>--Select--</option>
    	</select>
	</td>
    <td class="tabledata1"><strong>Pick List Date</strong></td>
    <td><input name="pln_date" id="pln_date" type="text" style="width:150px;" class="datepicker" value=""/></td>
</tr>

<tr class="row1">
    <td class="tabledata1"><strong> Area </strong></td>
    <td>
	<input name="area" id="area" type="hidden" style="width:150px;" value="<?php echo $area_code; ?>" />
	<input name="area1" id="area1" type="text" style="width:150px;" value="<?php echo $s->GetFieldValue('zone_master',"id='".$area_code."'",'ZoneTitle'); ?>" /></td>
    <td class="tabledata1"><strong>Sub Area</strong></td>
    <td>
	<input name="sub_area" id="sub_area" type="hidden" style="width:150px;" value="<?php echo $subarea_code; ?>" />
	<input name="sub_area1" id="sub_area1" type="text" style="width:150px;" value="<?php echo $s->GetFieldValue('subzone_master',"id='".$subarea_code."'",'subzone'); ?>" /></td>
    <td class="tabledata1"><strong>Engineer</strong></td>
    <td>
	<input name="engineer" id="engineer" type="hidden" style="width:150px;" value="<?php echo $engineer_code ?>" />
	<input name="engineer1" id="engineer1" type="text" style="width:150px;" value="<?php echo $s->GetFieldValue('dbl_engineer_master',"id='".$engineer_code."'",'engineer_name'); ?>" /></td>
</tr>

<tr class="row1">
    <td class="tabledata1"><strong>SOI No</strong></td>
    <td id="soi_noS"><input name="soi_no" id="soi_no" type="text" style="width:150px;" value="" /></td>
    <td class="tabledata1"><strong> SOI Date</strong></td>
    <td id="soi_dates"><input name="soi_date" id="soi_date" type="text" style="width:150px;" class="datepicker" value="" /></td>
    <td class="tabledata1"><strong>PO No.</strong></td>
    <td><input name="po_no" id="po_no" type="text" style="width:150px;" value="" /></td>
</tr>

<tr class="row1">
    <td class="tabledata1"><strong>Mistake Type</strong></td>
    <td><select style="width:150px;" name="mistake_type" id="mistake_type">
			<option value="">--Select--</option>
    		<option value="EngineerMistake">Engineer Mistake</option>
			<option value="CustomerMistake">Customer Mistake</option>
    	</select>
	</td>
    <td class="tabledata1"><strong>Responsible</strong></td>
    <td><input name="responsible" id="responsible" type="text" style="width:150px;" value="" /></td>
    <td class="tabledata1"><strong>Incentive Reco</strong></td>
    <td><select style="width:150px;" name="Inc_Reco" id="Inc_Reco">
    		<option value="N">N</option>
			<option value="Y">Y</option>
    	</select>
	</td>
</tr>
 
 <tr class="row1">
    <td class="tabledata1"><strong>Restocking Charges</strong></td>
    <td><input type="text" name="resto_chrg" id="resto_chrg" style="width:150px;" /></td>

	
    <td class="tabledata1"><strong>Excise</strong></td>
    <td><select style="width:150px;" name="Excise" id="Excise">
			<option value="N">N</option>
			<option value="Y">Y</option>
    	</select>
	</td>
    <td class="tabledata1"><strong>Party Ref No</strong></td>
    <td>
		<input name="party_ref_no" id="party_ref_no" type="text" style="width:150px;" value="" />
	</td>
 </tr>
 <tr class="row1">
    <td class="tabledata1" colspan="6">
	<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="10%"><strong>Sales</strong></td>
    <td width="10%">
		<input type="radio" name="saletype" id="saletype" value="sales" />
	</td>
    <td width="10%"><strong>Sample</strong></td>
    <td width="10%">
		<input type="radio" name="saletype" id="saletype" value="sample" />
	</td>
    <td width="10%"><strong>warranty</strong></td>
    <td width="10%">
		<input type="radio" name="saletype" id="saletype" value="warranty" />
	</td>
    <td width="10%"><strong>Invoice</strong></td>
    <td>
		<input type="radio" name="saletype" id="saletype" value="invoice" />
	</td>
  </tr>
</table>

	</td>
    </tr>
</table> 
<br />
<div id="AllDetaildata">
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td class="tabledata"><strong>Sr. No.</strong></td>
    <td class="tabledata"><strong>Part No.</strong></td>
    <td class="tabledata"><strong>Part name</strong></td>
    <td class="tabledata"><strong>Invoice Qty</strong></td>
	<td class="tabledata"><strong>Returned Qty</strong></td>
	<td class="tabledata"><strong>Accepted Qty </strong></td>
	<td class="tabledata"><strong>Rate Amount</strong></td>
	<td class="tabledata"><strong>Return Reason</strong></td>
    <td class="tabledata"><strong>Return Type</strong></td>
	<td class="tabledata"><strong>Store</strong></td>
  </tr>

  <tr class="row1">
    <td></td>
	<td><input name="mst_itemid[]" id="mst_itemid" type="text" style="width:70px;" value="" readonly="" /></td>
    <td><input name="mst_itemid[]" id="mst_itemid" type="text" style="width:70px;" value="" readonly="" /></td>
    <td><input name="invoice_qty[]" id="invoice_qty" type="text" style="width:70px;" value="" readonly="" /></td>
    <td><input name="return_qty[]" id="return_qty"  type="text" style="width:70px;" value="" readonly="" /></td>
	<td><input name="accepted_qty[]" id="accepted_qty" type="text" style="width:70px;" value="" readonly="" /></td>
	<td><input name="rate[]" id="rate" type="text" style="width:70px;" value="" readonly="" /></td>
	<td><input name="return_reason[]" id="return_reason" type="text" style="width:80px;" value="" readonly="" /></td>
	<td><select style="width:150px;" name="return_type[]" id="return_type">
			<option>--Select--</option>
    		<option value="Replacement">Replacement</option>
			<option value="Rejection">Rejection</option>
    	</select>
	</td>
	<td><select name="dblstore[]" id="dblstore" style="width:70px;" >
        <option value="0">Select</option>
<?php
$sql1=DB_query("select * from dbl_dealer_store  where company='".$_SESSION['ACGCompanyId']."'",$db);
while($dbrow=DB_fetch_array($sql1))
{ 
?>
<option value="<?php echo $dbrow['id']; ?>"><?php echo $dbrow['dealer_store_name']; ?></option>
 <?php } ?>
 </select></td>
  </tr>

</table>	
 </div>
<p align="center">
  <input type="submit" name="Submit" value="Save" class="search"  onclick="return validate()"/>
  <!--<input type="reset" name="reset" value="View" class="search" />-->
  <input type="button" value="Cancel" name="Back" class="search" />
</p>
</form>