<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>


<?php 

      //********************************************************************//
					
					function get($site,$store,$date1,$item,$closqty,$closerate,$closevalue)
					{
					global $db;
					  $check_current_opening="Select * from stock_summary where date='".$date1."'
					 and site='".$site."' and store='".$store."'
					  and item='".$item."'";
					
					$check_current_op_result=DB_query($check_current_opening,$db);
					if(DB_num_rows($check_current_op_result)=='0')
					{
					$closing_qty=$closqty;		
					$closing_value=$closevalue;
					$closing_rate=$closerate;
					}
					else
					{
						$check_current_op_row=DB_fetch_array($check_current_op_result);
						$id=$check_current_op_row['id'];
						$opening_qty=$check_current_op_row['opening_qty']+$closqty;
						$opening_value=$check_current_op_row['opening_value']+$closevalue;    
						$opening_rate=round($opening_value/$opening_qty);
						$closing_qty=$opening_qty+$check_current_op_row['inwards_qty']-$check_current_op_row['outwards_qty'];
					$closing_value=$opening_value+$check_current_op_row['inwards_value']-$check_current_op_row['outwards_value'];
						$closing_rate= round($closing_value/$closing_qty);
					
						$update_summary="Update  stock_summary set 
													opening_qty='".$opening_qty."',    
													opening_rate='".round($opening_rate,2)."',    
													opening_value='".$opening_value."', 								   
													closing_qty='".$closing_qty."',    
													closing_rate='".$closing_rate."',    
													closing_value='".$closing_value."' where id=$id ";
						$result2=DB_query($update_summary,$db);
						
						
					}

                               $date2= strtotime(date("Y-m-d", strtotime($date1)) . " +1 day");
                                $date11= date("Y-m-d",$date2);
							 
							    if($date11<=date('Y-m-d'))
							   {
 										get($site,$store,$date11,$item,$closing_qty,$closing_rate,$closing_value);
								}		

								}	
					//**********************************************************************************//


                                       $Prefix_date = date("y");
							           $Prefix_date++;									   
							           $CurrentFinancialYear = date("y")."-".$Prefix_date;
							           $year=date('y');
							            $Afteryear=$year+1;
										
if(isset($_POST['Back']))
{
header('location:index.php?route=Inventory/transation/return_rejection_index&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	
}



if(isset($_POST['Submit']))
	{	
	
	

 	    $prefixcode="RRV";	 
	      $siteCode = $_REQUEST['issuesite'];
	        $site_Code=substr($siteCode,0,3);
              $sql="select * from rejection_tbl where issue_branch = '".$_REQUEST['issuesite']."'";
      
	             $result = DB_query($sql,$db);
	              $numrows = DB_num_rows($result);
				  
	 if($numrows>0)
	 {
			
			if($numrows>0)	{ $maxval = $numrows;	}
			else	{ 	$maxval=0;	}
		{
			$strChar = $site_Code."/".$prefixcode."/".$year.$Afteryear."/";
		}
	  }
	  else
	  {
	  	$strChar = $site_Code."/".$prefixcode."/".$year.$Afteryear."/";
	  }
	  
	  
	               $strFinal = $maxval+1;
	               $strBothFinal = $strChar.str_pad($strFinal,6,"0",STR_PAD_LEFT); 
				   
				    $data['voucher_no'] = $strBothFinal; 
					$data['date'] = FormatDateForSQL($_REQUEST['date']); 
					$data['issue_branch'] = $_REQUEST['issuesite']; 
					$data['issue_stroe'] =  $_REQUEST['issue_store'];
					$data['receive_branch'] =  $_REQUEST['receive_branch'];
					$data['receive_store'] =  $_REQUEST['receive_store'];
					$data['issue_slip_no']= $_REQUEST['issue_slip_no'];
					$data['rejection_type']= $_REQUEST['return_type'];
					$data['get_pass ']= $_REQUEST['pass_no'];
					$insertsql1=$s->insertRecord('rejection_tbl', $data);
					$insertResult1 = DB_query($insertsql1,$db,$ErrMsg);
					$Lastreq_id=DB_Last_Insert_ID($db,'rejection_tbl',$ErrMsg);

                    $issue_transit=$s->getField('transit_table','ST_issue',$_REQUEST['issue_slip_no'],'id');
         //***********************************   Transfer Receipt Details *********************************//

         
        $countval= count($_REQUEST['itemid']);
		for($i=0;$i<$countval;$i++)
		{
 				if($_REQUEST['itemid'][$i]!='' )
				{			
				     					  
					
						$data_arr['parentid']=$Lastreq_id;
						$data_arr['issue_qty']=$_POST['issueqty'][$i];
						$data_arr['receiveqty']=$_POST['receiveqty'][$i];
						$data_arr['itemid']= $_POST['itemid'][$i];
						$data_arr['shortqty']= $_REQUEST['shortqty'][$i];
						$data_arr['returnqty']= $_REQUEST['returnqty'][$i];
						$data_arr['balance_qty']= $_POST['balance_qty'][$i];
						$insertResult=$s->insertRecord('rejection_tbl_details',$data_arr);
						$row2=DB_query($insertResult,$db);
					
     					//*********************  rate ******************//
						
						$ssummary=DB_query("select * from stock_summary 
						                        where site='".$_REQUEST['receive_branch']."'
		                                          and store='".$_REQUEST['receive_store']."'
												  and item='".$_REQUEST['itemid'][$i]."' 
												  and  date='".date('Y-m-d')."'",$db);
		                                         $rdata=DB_fetch_array($ssummary);
		          if($rdata['closing_rate']=='')
		            {
		                   $getclosing="Select * from stock_summary where site='".$_REQUEST['receive_branch']."' 
						    and store='".$_REQUEST['receive_store']."'  and item='".$_REQUEST['itemid'][$i]."' and 
							date <'".date('Y-m-d')."' ORDER BY DATE DESC LIMIT 0,1";
						 $preresult=DB_query($getclosing,$db);
						$prrow=DB_fetch_array($preresult);
			               $rate=$prrow['closing_rate'];
									
		
		            }
		          else
		            {
		           $rate=$rdata['closing_rate'];
		           }
		         if($rate=='')
		          $rate=0;
				  
             //*************************** Maintain Transit Table Details  *****************************************//
			 
				                           if($_POST['balance_qty'][$i]==0)
										   {
										   
										   
											$update=DB_query("update transit_table_details set status=1
											         , bal_qty='".$_REQUEST['balance_qty'][$i]."'
													 where itemid='".$_REQUEST['itemid'][$i]."' 
													 and parentid='".$issue_transit."'",$db);				  
										   }
										   else 
										   {
										   $update=DB_query("update transit_table_details set status=0
											         , bal_qty='".$_REQUEST['balance_qty'][$i]."'
											         where itemid='".$_REQUEST['itemid'][$i]."' 
													 and parentid='".$issue_transit."'",$db);
										   								   
										   
										   }						   
										   
										   
				  
												
//*****************************************	********Stock Summary ******************* *****************************************//	
					

             			
					 
					 
					 $data_date1=explode('/',$_REQUEST['date']);
					     $date51=$data_date1[2].'-'.$data_date1[1].'-'.$data_date1[0];
					      $date21= strtotime(date("Y-m-d", strtotime($date51)) . " +0 day");
                            $date= date("Y-m-d",$date21);		 
						 
					$check_current_opening="Select * from stock_summary where date='".$date."'
					                        and site='".$_REQUEST['issuesite']."' and store='".$_REQUEST['issue_store']."' 
					                        and item='".$_REQUEST['itemid'][$i]."'";					
					$check_current_op_result=DB_query($check_current_opening,$db); 	 
					 
					if(DB_num_rows($check_current_op_result)=='0')
					{
						  $get_previous_closing="Select * from stock_summary where site='".$_REQUEST['issuesite']."'
						                       and store='".$_REQUEST['issue_store']."' and item='".$_REQUEST['itemid'][$i]."' 
											   and date<'".$date."' ORDER BY DATE DESC LIMIT 0,1";	
											 				 
						$previous_closing_result=DB_query($get_previous_closing,$db);
						$previous_closing_row=DB_fetch_array($previous_closing_result);						
						
						if(DB_num_rows($previous_closing_result)=='0')
						{
							$check_opening_entry="Select * from opening_stockitemwise 
													where site='".$_REQUEST['issuesite']."' 
													and store='".$_REQUEST['issue_store']."' 
													and itemcode='".$_REQUEST['itemid'][$i]."'
													and reveive_date<'".$date."'  ";
													
							$check_opening_entry_result=DB_query($check_opening_entry,$db);
							$check_opening_entry_row=DB_fetch_array($check_opening_entry_result);							
							if(DB_num_rows($check_opening_entry_result)>0){
							$opening_qty=$check_opening_entry_row['stock'];
							$opening_rate=$check_opening_entry_row['rate'];
							$opening_value=$check_opening_entry_row['amount'];}else
							{$opening_qty='';
							$opening_rate='';
							$opening_value='';}
						}
						else
						{
							$opening_qty=$previous_closing_row['closing_qty'];
							$opening_rate=$previous_closing_row['closing_rate'];
							$opening_value=$previous_closing_row['closing_value'];						
						}
							$inwards_qty=$_REQUEST['returnqty'][$i]; 
							$inwards_value=($rate*$_REQUEST['returnqty'][$i]);
							$inwards_rate=$inwards_value/$inwards_qty;
							
							$closing_qty=$opening_qty+$inwards_qty;
							$closing_value=$opening_value+$inwards_value;
							$closing_rate=round($closing_value/$closing_qty);
							
							$insert_first_summary="Insert into stock_summary set 
													site='".$_REQUEST['issuesite']."',     
													store='".$_REQUEST['issue_store']."',    
													item='".$_REQUEST['itemid'][$i]."',
													opening_qty='".$opening_qty."',  
													opening_rate='".$opening_rate."',    
													opening_value='".$opening_value."',    
													inwards_qty='".$inwards_qty."',    
													inwards_rate='".round($inwards_rate,2)."',    
													inwards_value='".$inwards_value."',    
													closing_qty='".$closing_qty."',    
													closing_rate='".$closing_rate."',    
													closing_value='".$closing_value."',    
													date='".$date."'";
							$result1=DB_query($insert_first_summary,$db);
						
					}
					else
					{
					$check_current_op_row=DB_fetch_array($check_current_op_result);
					$id=$check_current_op_row['id'];
					$inwards_qty=$check_current_op_row['inwards_qty']+$_REQUEST['returnqty'][$i]; 
					$inwards_value=$check_current_op_row['inwards_value']+($rate*$_REQUEST['returnqty'][$i]); 
					$inwards_rate=$inwards_value/ $inwards_qty;
					
										
					$closing_qty=$check_current_op_row['opening_qty']+$inwards_qty
					            -$check_current_op_row['outwards_qty'];
					$closing_value=$check_current_op_row['opening_value']+$inwards_value-$check_current_op_row['outwards_value'];
					$closing_rate= round($closing_value/$closing_qty);
						
						$update_summary="Update  stock_summary set 
													inwards_qty='".$inwards_qty."',    
													inwards_rate='".round($inwards_rate,2)."',    
													inwards_value='".$inwards_value."',    
													closing_qty='".$closing_qty."',    
													closing_rate='".$closing_rate."',    
													closing_value='".$closing_value."' where id=$id ";
						$result2=DB_query($update_summary,$db);
						
						
					}
					
 //******************************************************************************************************************************//	
							
						$data_date=explode('/',$_REQUEST['date']);
					     $date5=$data_date[2].'-'.$data_date[1].'-'.$data_date[0];
					      $date2= strtotime(date("Y-m-d", strtotime($date5)) . " +1 day");
                            $date1= date("Y-m-d",$date2);	
							
												
					         
							   if($date1<=date('Y-m-d'))
							   {
 get($_REQUEST['issuesite'],$_REQUEST['issue_store'],$date1,$_REQUEST['itemid'][$i],$closing_qty,$closing_rate,$closing_value);
								}			
							
				  
					
					
					
					}
		  }
		  
		   //************************* Maintain Status  For Site Transfer Issue *************************//
		 
		 $sql_row=DB_query("select * from transit_table_details where 
		                    parentid=".$issue_transit."",$db);
		 $numrow=DB_num_rows($sql_row); $count1=0;
		 while($row1=DB_fetch_array($sql_row))
		 {
			if($row1['status']==1)
			{ $count1=$count1+1; }}
			 if($count1==$numrow)
			   {
			   $update_issue=DB_query("update transfer_issue set status='receive' where id='".$_REQUEST['issue_slip_no']."'",$db); 
			  $update1=Db_query("delete from  transit_table  where id='".$issue_transit."'",$db);
			   $update2=Db_query("delete from  transit_table_details  where parentid='".$issue_transit."'",$db);
			    }
				else
				{
				 $update1=Db_query("update  transit_table set ST_reject='".$Lastreq_id."'  where id='".$issue_transit."'",$db);
				}
			   
			  
			  
			  //*******************************************************************************************//








header('location:index.php?route=Inventory/transation/return_rejection_index&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');

}

?>
     







	
<h2 align="center"><strong>Return/Rejection</strong></h2>
<form name="form1" action="" method="post" >

  <div style="overflow:auto; width:810px; height:auto">
  
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr>
<th style="width:150px;"><strong>Voucher No.</strong></th>
<td style="width:250px;"><input type="text" id="voucherno" name="voucherno"  style="width:100%" value="Auto Generated" readonly/></td>
    <th style="width:150px;">Date</th>
    <td style="width:250px;"><input type="text" id="date" name="date" class="datepicker"  style="width:100%" /></td>
</tr>


<tr>
    <th><strong> Return/Reject To </strong></th>
    <td>
	<select name="issuesite" onchange="showreceiveside(this.value)" id="issuesite" style="width:100%">
     <option value="000">---Select---</option>
        <?php 		  
		$sql = "SELECT loccode, locationname FROM locations ";     
	   $resultStkLocs = DB_query($sql,$db);
	   while ($myrow=DB_fetch_array($resultStkLocs))
	   { ?>
        <option value="<?php echo $myrow['loccode']; ?>" <?php echo ($sitecode==$myrow['loccode']?'selected':'')?>><?php echo $myrow['locationname'];?></option>
        <?php  }	?>
      </select>
	</td>
    <th> Store(To)</th>
    <td><div id="issuestore"></div>
	</td>
</tr>
<tr>
    <th><strong>Return/Reject From </strong></th>
    <td><div id="receivebranch">
	  <select name="receive_branch"  id="receive_branch" style="width:100%">
        <option value="000">---Select---</option>
        <?php 
	
		$sql = "SELECT loccode, locationname FROM locations ";

	$resultStkLocs = DB_query($sql,$db);
	  while ($myrow=DB_fetch_array($resultStkLocs))
	  {
 		?>
        <option value="<?php echo $myrow['loccode']; ?>" <?php echo ($ReceivedFrom==$myrow['loccode']?'selected':'')?>><?php echo $myrow['locationname'];?></option>
        <?php  }	?>
      </select>
	</div>
	
	</td>
    <th> Store(From)</th>
    <td><div id="receive_store"></div>	</td>
</tr>

<tr>
    <th><strong>Issue Slip No.</strong></th>
    <td><div id="issue_slip_no"></div>	
	</td>
    <th>Return/Rejection Type</th>
    <td>
    <select name="return_type" style="width:100%" id="sitecode" >
	 <option value="">Select</option>
	 <option value="Rejection">Rejection</option>
      <option value="Return">Return</option>
    </select></td>
</tr>

<tr>
    <th><strong>Gate Pass No.</strong></th>
    <td>
	<input type="text" name="pass_no" id="pass_no" style="width:100%"  />
	</td>
    <td colspan="2">&nbsp;</td>
</tr>
</table> 

</div>

	

  <div style="overflow:auto; width:810px; height:auto">
     <div id="itemdata">      
</div>
  </div>
  


<p align="center">
  <input type="submit" name="Submit" value="Save"  />
   <input type="submit" value="Cancel" name="Back" />
  
  
  
</p>
</form>
<script>
function getitemdata(str)
{

if (str.length==0)
  {
  document.getElementById("itemdata").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("itemdata").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_rejection_items.php?q="+str,true);
xmlhttp.send();
}
function showreceiveside(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("issuestore").innerHTML=data[0];
	document.getElementById("receivebranch").innerHTML=data[1];
	document.getElementById("issue_slip_no").innerHTML=data[2];
	

	
    }
  }
  xmlhttp.open("GET","get_sitenamedata_rejection.php?site="+str,true);
	xmlhttp.send();

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

	
}

function showreceivestore(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("receive_store").innerHTML=data[0];
	
	  }
  }
  xmlhttp.open("GET","get_sitenamedata_rejection2.php?site="+str,true);
	xmlhttp.send();

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

	
}

function calculate_qty(i)
{
if(document.getElementById("returnqty_"+i).value!='')
{
var shortqty=parseFloat(document.getElementById("balqty_"+i).value);
var returnqty=parseFloat(document.getElementById("returnqty_"+i).value);
if(returnqty>shortqty)
{
alert("Return qty should not be greater then short qty");
document.getElementById("returnqty_"+i).value='';
document.getElementById("balance_qty_"+i).value='';
return false;
}

var qty=shortqty-returnqty;
document.getElementById("balance_qty_"+i).value=qty;
return true;
}




}



</script>
