<?php 

require 'connection.php';

for($count = 0; $count<count($_POST['hidden_itemid']); $count++)
{
	
 	$srno= $_POST['hidden_srno'][$count];
 	$itemid= $_POST['hidden_itemid'][$count];
 	$qty= $_POST['hidden_qty'][$count];
 	$rate= $_POST['hidden_rate'][$count];
 	$accid= $_POST['hidden_accid'][$count];
 	$dated= $_POST['hidden_dated'][$count];
 	$vno= $_POST['hidden_vno'][$count];
 	$vno3= $_POST['hidden_vno'][$count];

   $qry = "SELECT posted FROM saleret where slrid = $vno and posted='posted'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $rowposted = mysqli_fetch_array($run);

// if($rowposted['posted']=='posted' or $qty=="" or $rate==""){
  if ($run->num_rows > 0) {
	
$msg= "Invoice already saved ! operation failed ! or some field empty !";
die($msg);
}
else {

$amount=$rate * $qty;


// ************************************SALES TABLE INSERT*****************************************

// Check if Sales no exist or not
$chkprid= $vno;
  $qry = "SELECT vno FROM saleret  where slrid = '$chkprid'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $count1 = mysqli_num_rows($run);
		if($count1==1){

			// Total amount create
	$qry2 = "SELECT netamt FROM saleret  where slrid = '$chkprid'";
   $run2 = mysqli_query($connect, $qry2) or die(mysqli_error($connect));
   $row2 = mysqli_fetch_array($run2);
   $prtotalamt= $row2['netamt'];

   $totalaount= ($prtotalamt)+($amount);
	$qry1 = "UPDATE `saleret` SET dated='$dated',slrid='$vno',accid='$accid' ,netamt='$totalaount' where vno=$vno";
	$run = mysqli_query($connect, $qry1) or die(mysqli_error($connect));

 	}
 	else{
// Insert into Sales table
 $query="INSERT INTO `saleret`(`slrid`, `dated`, `billno`, `accid`, `netamt`) VALUES('$vno','$dated','$vno','$accid','$amount')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));
 	}


// *****************SRTART STOCK LEDGER TABLE INSERT***********************
	// Insert into stoctLedger table
	$pramount= $qty* $rate;
	$types= 'Med-SLR';
	$query1="INSERT INTO `stockledger`(`srno`, `def`, `dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockin`, `ratein`, `costin`) VALUES('$srno', '6', '$dated','$accid','$vno','$vno','$types','$itemid','$qty','$rate','$pramount')";
	$result1=mysqli_query($connect,$query1) or die(mysqli_error($connect));

// *****************************END STOCK LEDGER TABLE INSERT*************************************

// *****************************START LEDGER DETAIL TABLE INSERT*************************************

	// Insert into ledger
	$desc= 'Sales Return# '. $vno;
$insrtledger="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `vno`, `refno`, `entrytype`, `description`, `itemid`, `qty`, `rate`) VALUES('6', '$dated','$accid','$vno','$vno','$types','$desc','$itemid','$qty','$rate')";
	$resultledger=mysqli_query($connect,$insrtledger) or die(mysqli_error($connect));
// **********************END LEDGER DETAIL TABLE INSERT***************************
}

}
if($resultledger){
  echo "Item added !";
}
 ?>