<?
session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");

	if($savecount==0)
	{

		$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
		$kar=mysqli_fetch_array($ker);
		if(strlen($brcode)<=0)
		{
 			print "<script> alert('Session timed out'); parent.document.location='index.html'; </script>";
 			return;
		}

 		$i=0;
		foreach($slno as $a)
		{
			 $slno[$i]=$a; 
 			 $i++;
		}

 		$i=0;
		foreach($partcode as $a)
		{
			 $partcode[$i]=$a; 
 			 $i++;
		}

		$i=0;
		foreach($partname as $a)
		{
			 $partname[$i]=$a; 
 			 $i++;
		}

		$i=0;
		foreach($retn_qty as $a)
		{
 			$retn_qty[$i]=$a; 
 			$i++;
		}

		$i=0;
		foreach($pr_tot as $a)
		{
 			$pr_tot[$i]=$a; 
 			$i++;
		}



		

              $c=0;
		$dt=date('Y-m-d');

              $chk_res=mysqli_query($db,"select *  from  purchretn where brcode='$brcode' and pur_invno='$invno' ");
		
		if(mysqli_num_rows($chk_res)>0){

			$chk_row=mysqli_fetch_row($chk_res);

              	mysqli_query($db,"delete from purchretn where brcode='$brcode' and pur_invno='$invno'");

              	mysqli_query($db,"delete from journal2  where brcode='$brcode' and jvno='$chk_row[jvno]'");
		}


             $invres=mysqli_query($db,"select max(pr_no) from purchretn where brcode='$brcode' ");
		$invrow=mysqli_fetch_row($invres);
		if($invrow[0]>0)
 			$pr_no=($invrow[0]+1);
		else
 			$pr_no=1;


		$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$jvrow=mysqli_fetch_row($jvres);
		if($jvrow[0]>0)
 			$jvno=($jvrow[0]+1);
		else
			$jvno=1;


		for($j=0;$j<$i;$j++)
		{

			if($retn_qty[$j]>0)
			{

				 $sql="insert into purchretn values('','$pr_no','$invno','$slno[$j]','$partcode[$j]','$partname[$j]', '$retn_qty[$j]', '$pr_tot[$j]','$dt', '$brcode', '$jvno' )";

   				 mysqli_query($db,$sql) or die("Err 1".mysqli_error($db));

                            $c++;
			}
                  
		}

             if($c>0)
	      {
		$taxdrres=mysqli_query($db,"select sum(pr_tot) from purchretn where pr_no='$pr_no' and  brcode='$brcode'");
  		$taxdrrow=mysqli_fetch_array($taxdrres);

  
		$ledger='Spare Purchase Return';  
  	       $sql="insert into journal2 values('$dt','$jvno','Dr','$cname','$taxdrrow[0]','0','Spare pur_return no: $pr_no,PBill:$pbillno','$brcode','Spare Pur','Pur return Order','$pr_no','0')";
       	   mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  
 	       $sql="insert into journal2 values('$dt','$jvno','Cr','$ledger','0','$net_total','Spare pur_return: $pr_no,PBill:$pbillno','$brcode','Spare Pur  return','Pur  return Order','$pr_no','0')";
    		   mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));


		$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
		$jvdifrow=mysqli_fetch_row($jvdifres);
		$dif=$jvdifrow[0];

		if($dif<0)
			{
		       $sql="insert into journal2 values('$dt','$jvno','Dr','Round Off','".abs($dif)."', '0','Round of adjusted JC No: $jcno','$brcode','JC Spare Pur return Invoice','jcno','$jcno','$pr_no')";
			  mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
			}
		else if($dif>0)
			{
 			 $sql="insert into journal2 values('$dt','$jvno','Cr','Round Off','0','".abs($dif)."','Round of adjusted JC No: $jcno','$brcode','JC Spare Pur return Invoice','jcno','$jcno','$pr_no')";
			   mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
			}

		$jdres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
		$jdiff=mysqli_fetch_row($jdres);
		if($jdiff[0]!=$jdiff[1]||$jdiff[0]==0)
		{
 			print "<sctipt> alert('Error in posting. Please check the data now');</script>";
		}

  
		
  		echo "<script> document.getElementById('savecount').value=1;</script>";
           
              if($mode=='edit_purchase_return'){
		 
                echo "Data Updated ";
		  echo "<script> document.location='edit_purchase_return.php';</script>";	
            
              }
              
              if($mode=='purchase_return'){
			
                echo "Data saved ";
		  echo "<script> document.location='purchret.php';</script>";	
            
              } 


           }
            
           else{
          
             if($mode=='purchase_return')
                echo 'Invalid Purchase Return'; 
              
             if($mode=='edit_purchase_return')
                echo 'data updated';
            
             exit;
           }
       }

	elseif($savecount==1)
	{
		echo "Data already saved";
 		 if($mode=='purchase_return'){
			
		  echo "<script> document.location=''purchret.php';</script>";	
            
              }
              
              if($mode=='edit_purchase_return'){
			
		  echo "<script> document.location='edit_purchase_return.php';</script>";	
            
              } 

	}

?>

