<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
if($savecount==0)
{
//$tbl2="stocksummary1".$brcode.$user;
include ("opendb.php");
//include ("workshop_calstock.php");
  if(($cus=='Customer name')||($reg=='Register number'))
    {
    echo "Data could not saved <br> Invalid customer name/register number";
    }


   	elseif($pmode=='')
    	{
    	echo "Data could not saved <br> Invalid Payment Mode";
    	}

 	elseif($model=='')
    	{
    	echo "Data could not saved <br> Invalid Vehicle Model";
    	}
  else
  {
 

 
   

$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 
 $i++;
}
$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}
$i=0;
foreach($rtnqty as $a)
{
 $rtnqty[$i]=$a; 
 $i++;
}
$i=0;
foreach($unitprice as $a)
{
 $unitprice[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}
$i=0;
foreach($typ as $a)
{
 $typ[$i]=$a; 
 $i++;
}
$i=0;
foreach($dis as $a)
{
 $dis[$i]=$a; 
 $i++;
}
$i=0;
foreach($sperson as $a)
{
 $sperson[$i]=$a; 
 $i++;
}


$flg=0;
$spersonflg=0;
for($j=0;$j<$i;$j++)
{
 $k=$j+1;
	
 //$qry=mysqli_query($db,"select * from $tbl2 where partcode='$partcode[$j]' and type='$typ[$j]' and brcode='$brcode'");
// if(mysqli_num_rows($qry)<=0)
 //{
  // echo "select * from $tbl2 where partname='$partname[$j]' and type='$typ[$j]' and brcode='$brcode'"."<br>";
 // print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  //$flg++;
 //}




  if(($rtnqty[$j]=='')||($rtnqty[$j]=='0'))
    {
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
    $flg++;
    }

   if($sperson[$j]=='')
    {
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
    $spersonflg++;
    }


}
	if($flg>0)
 	{
  		echo "<small>Check the partname/Qty of Red marked row(s)</small>";
 	}

	elseif($spersonflg>0)
 	{
  	echo "<small>Select the SalesPerson of Red marked row(s)</small>";
 	}
	else
 	{

	
	$find_jv=mysqli_query($db,"select * from workshop_slsretn where slsrtn_invno='$invno' and brcode='$brcode'");
  	$f_j=mysqli_fetch_row($find_jv);
  	$j_no=$f_j[23];

  	mysqli_query($db,"delete from workshop_slsretn where slsrtn_invno='$invno' and brcode='$brcode'");
	 if($j_no >0){
		 mysqli_query($db,"delete from journal2 where jvno='$j_no' and brcode='$brcode'");

  		     }
 
	$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;

 

      $flg1=0;
	for($m=0;$m<$i;$m++)
       {
   	$qry1=mysqli_query($db,"select * from partmasterparts where partname='$partname[$m]' ");
       $row1=mysqli_fetch_array($qry1);
     	$qry2=mysqli_query($db,"select * from subgroupmasterparts where subname='$row1[2]' ");
     	$row2=mysqli_fetch_array($qry2);
	mysqli_query($db,"insert into workshop_slsretn(invno,invdate,customer,vehno,item,item_code,qty,unitprice,dis,total,flag,group1,brcode,salesman,group2,pmode,chasisno,model,coup_no,jvno,slsrtn_invno,slsrtn_invdt) values('$sldinv_no','$sldinv_dt','$cus','$reg','$partname[$m]','$partcode[$m]','$rtnqty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$typ[$m]','$row1[2]','$brcode','$sperson[$m]','$row2[1]','$pmode','$chno','$model','$coup_no','$jvno','$inv','$invdt')");

    //mysqli_query($db,"insert into workshop_slsretn(invno,invdate,customer,vehno,item,item_code,qty,unitprice,dis,total,flag,group1,brcode,salesman,group2,pmode,chasisno,model,coup_no,jvno) values('$invno','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','$qty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$typ[$m]','$row1[2]','$brcode','$sperson[$m]','$row2[1]','$pmode','$chno','$model','$coup_no','$jvno')");
 	$flg1++;
 
      }  
 	if($flg1==$i) 
 	{
	$taxdrres=mysqli_query($db,"select sum(total) from workshop_slsretn where invno='$invno' and  brcode='$brcode'");

  	$taxdrrow=mysqli_fetch_array($taxdrres);
  
    //$ledger='Spare Purchase';  
  	$sql="insert into journal2 values('$invdt','$jvno','Dr','$cus','$taxdrrow[0]','0','Sales Returnno: $invno,PBill:$coup_no','$brcode','sales return credit',' ','','0')";
     	mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  
  	$sql="insert into journal2 values('$invdt','$jvno','Cr','Sales Return Credit','0','$taxdrrow[0]','Sales Returnno: $invno,PBill:$coup_no','$brcode','sales return credit',' ','','0')";
     	mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
	print "<script> window.open('workshop_printslsrtn.php?inno=$invno'); </script>";
  	 echo "Data Updated";
  	 echo "<script> document.getElementById('savecount').value=1;document.location='workshop_salesret.php';</script>";
 	}  
  }

}
}
elseif($savecount==1)
{
echo "Data already Updated";
  echo "<script> document.location='workshop_salesret.php';</script>";
}

?>
