<?php ob_start();
error_reporting(0);
$PageSecurity = 4;
date_default_timezone_set('Asia/Kolkata');
$PageName = 'CanibilizationReceiptNote';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');

$dealertype=$s->getField('www_users','id',$_SESSION['UserID'],'Type');
$Prefix_code = "CLD9/PO";
$url_cancel= "index.php?route=Inventory/transaction/canbilize_note_index".$rurl;
$url_add= "index.php?route=Inventory/transaction/canbilize_note_add".$rurl;
?>
<script type="text/javascript" src="js/function-ajax.js" language="javascript"></script>
<?php 
if(isset($_POST['save']))
{

	$prefixtitle='ATL/'.$_REQUEST['site'].'/CANI';	
   					
	$SQLMax=DB_query("select can_no as CanNo from log_canbilize where site='".$_REQUEST['site']."' order by id DESC limit 0,1",$db);
	$MaxQut=DB_fetch_array($SQLMax);
	$num=DB_num_rows($SQLMax);
	if($num>0)
	{
	$explode=explode("/",$MaxQut['CanNo']);
	$FinMx=$explode[3]+1;
	$FinalCode=$prefixtitle."/".str_pad($FinMx,5,0,STR_PAD_LEFT);
	}
	else
	{
	$FinalCode=$prefixtitle."/".'00001';
	}			
	
		$data['can_no'] = $FinalCode; 
		$data['can_date'] = FormatDateForSQL1($_REQUEST['dis_date']);
		$data['site'] = $_REQUEST['site'];
		$data['store'] = $_REQUEST['storecode'];
		$data['party_id'] = $_REQUEST['party_id'];
		$data['to_site'] = $_REQUEST['to_site'];
		$data['address'] = $_REQUEST['address'];
		$data['contact_per'] = $_REQUEST['contact_per'];
		$data['contact_no'] = $_REQUEST['contact_no'];  
		$data['mrn_no'] = $_REQUEST['mrn_no'];
		$data['mrn_date'] = FormatDateForSQL1($_REQUEST['mrn_date']);
		$data['way_bill_no'] = $_REQUEST['way_bill_no'];
		$data['way_bill_amt'] = $_REQUEST['way_bill_amt'];
		$data['vehicle_no'] = $_REQUEST['vehicle_no'];
		$data['lr_no'] = $_REQUEST['lr_no'];  
		$data['lr_date'] = FormatDateForSQL1($_REQUEST['lr_date']);
		$data['remarks'] = $_REQUEST['remarks'];  
		$data['companyID'] = $_SESSION['ACGCompanyId'];
		$data['created_by']=$_SESSION['UserID'];
		$data['created_date']=date('Y-m-d');
	   $insertsql=$s->insertRecord('log_canbilize', $data);
 	   $insertResult = DB_query($insertsql,$db,$ErrMsg);
	   $Lastreq_id=DB_Last_Insert_ID($db,'log_canbilize',$ErrMsg);
 	   if($insertResult==true)
	   { 		
			$countval1= $_REQUEST['countrow'];
			for($j=0;$j<$countval1;$j++)
			{
				if($_REQUEST['disboxid'][$j]==1)
				{		
					DB_query("update log_addmrnbox set dispatch=0, dis_id=-1 where grn_id='".$_REQUEST['mrnid']."' AND package_no='".$_POST['package_no'][$j]."'",$db);
							
					$data_arr1['can_id']= $Lastreq_id;
					$data_arr1['package_no']= $_POST['package_no'][$j];
					$data_arr1['no_of_package']= $_REQUEST['no_of_package'][$j];
					$data_arr1['content']= $_POST['content'][$j];				
					$data_arr1['layout']= $_REQUEST['layout'][$j];
					$data_arr1['itemid']= $_REQUEST['itemid'][$j];
					$data_arr1['itemname']= $s->GetFieldValue('item_master','id="'.$_REQUEST['itemid'][$j].'"','item_name');
					$data_arr1['grn_id']= $_REQUEST['mrnid'];
											
					$insert_mrm1=$s->insertRecord('log_canbilizebox', $data_arr1);
					$ErrMsg1 = _('An error occurred Record for Boxes Not Save') . '. ' . _('The error says');
					$insertResult = DB_query($insert_mrm1,$db,$ErrMsg);	
			   }
			}		 
	 }		
	 $_SESSION['success_msg']="Add Successfully";
		echo "<script>location='index.php?route=LOG/transaction/canbilize_note_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
	
	
}

?>
<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:fixed;
left:280px;
top:200px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<form action="" method="post" name="myform" id="myform">
<h1 align="center"><strong>Add Canililization Note</strong></h1>
<input name="usertype" id="usertype" type="hidden" value="<?php echo $dealertype; ?>" />
 
<h3 >GENERAL DETAILS</h3>
<table width="100%"  border="1" cellspacing="0" cellpadding="3">
<tr class="row1">
    <td class="tabledata1">Location </td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td><?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
      $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."'
    and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }?>
     <select name="site" onchange="getreqitemdis(this.value)" id="sitecode" style="width:70px"    >
      <option value="0">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($sitecode==$site_row['loccode']?'selected':'')?>><?php echo $site_row['loccode'];?></option>
      <?php } ?>
    </select></td>
	  <td><div id="txtsite">
	    <?php
	
	$site_sql="Select locationname from locations where loccode ='".$sitecode."'";
	$site_res=DB_query($site_sql,$db);
	$site_row=DB_fetch_array($site_res);
	if(isset($sitecode))
	{
	?>
	    <input name="sitename" type="text" id="sitename"  value="<?php echo $site_row['locationname'];?>" size="20" readonly="true">
	<?php } else {?>
	<input name="sitename" type="text" id="sitename"  value="<?php echo $sitename;?>" size="20" readonly="true">
	<?php } ?>
	<input type="hidden" name="hidden_site" id="hidden_site" value="<?php echo $_REQUEST['hidden_site'];?>"/></div></td>
	</tr>
	<tr></tr>
	</table>	</td>
	<td  class="tabledata1">Warehouse</td>
    <td id="storedata">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
		<td>	
		<?php
		$sql_store = "select id, StoreCode, StoreName from storetype where CompanyId='".$_SESSION['ACGCompanyId']."' and Store_Site in ('".$_SESSION['AssignedBranch']."') order by StoreCode ASC";
		$resultStkLocs = DB_query($sql_store,$db);
		?>
		<select id="storecode" name="storecode"  style="width:100%">
		<option value="0">Select</option> 
		<?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
				$p='';
				if($storecode==$myrow['id'])
				$p="selected";
		?>
			<option Value="<?php echo $myrow['id']; ?>" <?php echo $p; ?>><?php echo $myrow['StoreName']; ?></option>
		<?php }
		?>
		</select>		</td>
        <!--<td><div id="txtid"><input type="text" name="storename" id="storename" size="20" readonly="true" value="<?php echo $storename; ?>"></div></td>-->
      </tr>
    </table></td>
	<td  class="tabledata1">Canilize No. </td>
    <td><input type="text" value="Will Be Generated On Saving" style="width:150px;" name="dis_no" readonly="true"></td>
</tr>
<tr class="row1">
	<td  class="tabledata1">Canilize Date </td>
    <td><input type="text" value="" style="width:150px;" name="dis_date" readonly="true" class="datepicker"></td>
  	<td class="tabledata1">Party Name</td>
    <td><select name="party_id" id="party_id" style="width:100%;">
      <option value="0">Select Party</option>
      <?php 
      $party=DB_query("Select id,client_code,c_name from clientmaster",$db);
	  while($party_row=DB_fetch_array($party))
	  {?>
      <option value="<?php echo $party_row['id'];?>"><?php echo $party_row['c_name'];?></option>
      <?php 	
	  }
      ?>
    </select></td>  
	<td class="tabledata1">Site To</td>
    <td><select name="site_to" id="site_to"  style="width:150px;" onchange="getaddress(this.value)">
      <option value="0" >---Select---</option>
      <?php
			$sql="select id, StoreCode, StoreName from locations_to";
			$result = DB_query($sql, $db);
			while($myrow = DB_fetch_array($result))
			{ 
			?>
      <option  value="<?php echo $myrow['id']; ?>"><?php echo  $myrow['StoreName']; ?></option>
      <?php }
			?>
    </select></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Site Address</td>
    <td ><textarea name="address" id="address"></textarea></td>
	<td class="tabledata1">Contact Person</td>
    <td><input type="text" name="contact_per" id="contact_per"  style="width:150px;" /></td>
	<td class="tabledata1">Mobile</td>
    <td><input name="contact_no" id="contact_no" type="text" value="" size="25" style="width:100%;"/></td>
</tr>
<tr class="row1">
	<td class="tabledata1">MRN No.</td>
    <td><input type="text" style="width:150px;" name="mrn_no" id="mrn_no" /></td>
    <td  class="tabledata1">MRN Date</td>
    <td><input name="mrn_date" type="text" id="mrn_date" style="width:150px;" value="" readonly="true" class="datepicker" /></td>
    <td class="tabledata1">Way bill No.</td>
    <td><input name="way_bill_no" type="text" id="way_bill_no" style="width:150px;"  value=""/></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Way Bill Amount</td>
    <td><input name="way_bill_amt" type="text" id="way_bill_amt" style="width:150px;"  value=""/></td>
	<td class="tabledata1">Vehicle no.</td>
    <td><input name="vehicle_no" type="text" id="vehicle_no" style="width:150px;"  value=""/></td>
	<td class="tabledata1">LR no.</td>
    <td><input name="lr_no" type="text" id="lr_no" style="width:150px;"  value=""/></td>
</tr>
<tr class="row1">
	<td class="tabledata1">LR Date</td>
    <td><input type="text" name="lr_date" id="lr_date"  style="width:150px;" class="datepicker" /></td>
    <td class="tabledata1">&nbsp;</td>
    <td>&nbsp;</td>
	<td class="tabledata1">&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr class="row1">
    <td  class="tabledata1">Remarks If Any</td>
    <td colspan="5"><textarea name="remarks"  id="remarks" style="width:830px;height:50px;"></textarea></td>
  </tr>
</table>
<div align="right">	 		
	<table width="100%">
		<tr>
			<td>
			 
			 <br /><br />
			 <div id="box_block"><!--style="display:none;"-->
			 <hr>
			 <h1 align="center" style="text-align:center;">Canibilization Boxes</h1>
			<br />
			<div style="height: auto" id="ItemListDiv">
			<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
			  <tr>
				<td class="tabledata1" colspan="3"><h3 style="color:#000000; font-size:16px;"><strong>Canibilization Note Boxes Details</strong></h3></td>
				 <td class="tabledata1" colspan="2">&nbsp;</td>
			  </tr>
			  <tr>
			  	<td class="tabledata"><input name="disboxid" type="checkbox"   id="disboxid" onclick="DisBoxData()"/></td>
				<td class="tabledata">S. No.</td>
				<td class="tabledata">Package No.</td>
				<td class="tabledata">No. of Packages</td>
				<td class="tabledata">Content</td>
				<td class="tabledata">Layout</td>
				<td class="tabledata">Item Name</td>
			  </tr>
			   <tbody id="dataTablerows">
			  <?php  $j=0;
$sqlQtybox = "SELECT * From log_addmrnbox Where grn_id='".$_REQUEST['mrnid']."' AND dispatch=0 AND dis_id=0";
$Qty_resultbox = DB_query($sqlQtybox, $db);
while($row2box=DB_fetch_array($Qty_resultbox))
{
$k=$j+1;
$data10.='<tr>';
if($row2box['dispatch']==1){
$data10.='<td class="tabledata"><input name="" type="checkbox" id="disboxid_'.$j.'" value="1" checked="checked"  disabled="disabled" /></td>';
}else{
$data10.='<td class="tabledata"><input name="disboxid['.$j.']" type="checkbox" id="disboxid_'.$k.'" value="1" /></td>';
}
$data10.='<td class="tabledata">'.$k.'<input type="hidden" name="box_id['.$j.']" id="box_id_'.$j.'" value="'.$row2box['id'].'"/></td>';
$data10.='<td class="tabledata"><input type="text" name="package_no['.$j.']" id="package_no_'.$j.'" value="'.$row2box['package_no'].'" readonly="true"/></td>';
$data10.='<td class="tabledata"><input type="text" name="no_of_package['.$j.']" id="no_of_package_'.$j.'" value="'.$row2box['no_of_package'].'" readonly="true"/></td>';
$data10.='<td class="tabledata"><input type="text" name="content['.$j.']" id="content_'.$j.'" value="'.$row2box['content'].'" readonly="true" ></td>';
$data10.='<td class="tabledata">';
$data10.='<select name="layout['.$j.']" id="layout_'.$j.'" disabled="disabled">';
$data10.='<option value="0">Select</option>';
$sql1=DB_query("select * from log_floor where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($sql_floor=DB_fetch_array($sql1))
{
	if($sql_floor['id']==$row2box['layout']){
	$data10.='<option value="'.$sql_floor['id'].'" selected="selected">'.$sql_floor['floor_name'].'</option>';
	}else{
	$data10.='<option value="'.$sql_floor['id'].'">'.$sql_floor['floor_name'].'</option>';
	}
}
$data10.='</select>';
$data10.='</td>';
$data10.='<td class="tabledata">';
$data10.='<select name="itemid['.$j.']" id="itemid_'.$j.'" disabled="disabled">';
$data10.='<option value="0">Select</option>';
$sql2=DB_query("select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($sql_floor2=DB_fetch_array($sql2))
{
	if($sql_floor2['id']==$row2box['itemid']){
	$data10.='<option value="'.$sql_floor2['id'].'" selected="selected">'.$sql_floor2['item_name'].'</option>';
	}else{
	$data10.='<option value="'.$sql_floor2['id'].'">'.$sql_floor2['item_name'].'</option>';
	}
}
$data10.='</select>';
$data10.='</td></tr>';
$j++;}
echo $data10.='<input type="hidden" name="countrow" id="countrow" value="'.$j.'" />';
?>
			   
			   </tbody>
			</table>
			<br />
			</div>
			</div>
		</td></tr>
    </table>
		    </td></tr>
			 
		</table>
		
			
		<div align="center">
		<input type="submit" name="save" value="Save" onclick="return confirmPost()" />
		<input type="submit" name="reset" value="Reset" />
		</div>
</form>
<script language="javascript" type="text/javascript">
function get_vendor(str)
  {
	var site=document.getElementById("site").value;
	if (site==0)
	{
	alert("Please select a site");
	return;
	}
	if (str==0)
	{
	alert("Please select a warehouse");
	return;
	}
	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("grn_no").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","get_grnblank.php",true);
	xmlhttp.send();
}
 function claculateExcise(i,str)
  {
	var discounted=document.getElementById("basic_"+i).value;
	if (str.length==0)
	{
	document.getElementById("exciseId_"+i).innerHTML="";
	return;
	}
	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("exciseId_"+i).innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","calculateExcise.php?mrnTaxId="+str+"&data="+i,true);
	xmlhttp.send();
}


function calculateExciseDuty(i)
{

	var basic=document.getElementById("basic_"+i).value;
	var ExcisePercentage=document.getElementById("ExcisePercentage_"+i).value;
	
	var exciseVal=basic*(ExcisePercentage/100);
	document.getElementById("exciseValue_"+i).value=exciseVal.toFixed(2);
}










  


function showsitename158(str)
{

var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");	
	
	document.getElementById("hid-Godown-div").innerHTML=data[0];
    
	
    }
  }
  xmlhttp.open("GET","GoDownIssueSlip_mrn.php?site="+str,true);
	xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

	
}
 

function confirmPost()
{
if(document.getElementById("site").value=='0')
{
alert("Must Select Site Name");
document.getElementById("site").focus();
return false;
}
if(document.getElementById("storecode").value=='0' || document.getElementById("storecode").value=='000')
{
alert("Must Select Warehouse Name");
document.getElementById("storecode").focus();
return false;
}
if(document.getElementById("type").value=='0')
{
alert("Must Select Type");
document.getElementById("type").focus();
return false;
}
if(document.getElementById("grn_no").value=='0')
{
alert("Must Select  GRN No.");
document.getElementById("grn_no").focus();
return false;
}
if(document.getElementById("party_id").value=='0')
{
alert("Must Select  Party");
document.getElementById("party_id").focus();
return false;
}

var cou=document.getElementById("edit_cnt").value;
for(var i=1;i<=cou;i++)
{	
	if(document.getElementById("dispatchqty_"+i)){
		if(document.getElementById("dispatchqty_"+i).value=='')
		{
			alert("Please Fill dispatch Qty of All item.");
			return false;
		}	
	}
}
return true;
}




function getreqitemdis(str)
{
var xmlhttp;
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)
    {
	 var data=xmlhttp.responseText.split("__");	
	
	document.getElementById("txtsite").innerHTML=data[0];
	document.getElementById("storedata").innerHTML=data[1];
	//document.getElementById("grnno").innerHTML=data[2];
	
    }
  }
  xmlhttp.open("GET","getsitenamedis.php?site="+str,true);
xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

}




 



 

function getaddress(str)
{
	if(str==0){
		alert("Please select atleast one Site To.");
		return;
	}
	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)
	{
	var data = xmlhttp.responseText.split("__");
	document.getElementById("address").value=data[0];
	document.getElementById("contact_per").value=data[1];
	document.getElementById("contact_no").value=data[2];
	}
	}
	xmlhttp.open("GET","siteaddress.php?sid="+str,true);
	xmlhttp.send();
}
function DisBoxData()
{	
	var t=document.getElementById("countrow").value;
	if(document.getElementById('disboxid').checked==true)
	{	
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('disboxid_'+i).checked=true;	
	}	
	}
	else
	{
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('disboxid_'+i).checked=false;	
	}
	}
}

</script>